Files
mercury/tests/general/array_test.m
Zoltan Somogyi a19bcaaed5 Updates to programming style in the library.
library/bt_array.m:
    Reorder the arguments of the resize and shrink predicates
    to make them easier to use with state variables.

tests/general/array_test.m:
    Update the test calls to these two predicates.

library/version_array2d.m:
    Rewrite part of this module using a programming style that includes
    variable names that are longer than one character :-(. In the absence
    of comprehensive, or even basic, test cases, leave the rest using
    the old style.

    Add a non-field-syntax lookup operation.

    Mark the site of a probable bug.

library/version_bitmap.m:
    Add non-field-syntax versions of the get_bit and set_bit operations.

NEWS.md:
    Announce the reordering and the new predicates above.

library/getopt.m:
library/getopt_io.m:
    Apply to getopt_io.m an update that was previously applied to getopt.m,
    even though getopt.m is now computed from getopt_io.m.

library/array2d.m:
library/bit_buffer.m:
library/bit_buffer.read.m:
library/bit_buffer.write.m:
library/bitmap.m:
library/cord.m:
library/edit_distance.m:
library/edit_seq.m:
library/fat_sparse_bitset.m:
library/fatter_sparse_bitset.m:
library/list.m:
library/one_or_more.m:
library/pair.m:
library/ra_list.m:
library/rbtree.m:
library/set_bbbtree.m:
library/set_ctree234.m:
library/set_ordlist.m:
library/set_tree234.m:
library/set_unordlist.m:
library/solutions.m:
library/sparse_bitset.m:
library/test_bitset.m:
library/thread.barrier.m:
library/thread.m:
library/thread.semaphore.m:
library/time.m:
library/tree_bitset.m:
library/version_array.m:
library/version_hash_table.m:
library/version_store.m:
    General updates to style, the main ones being the following.

    Using standard names for type variables:

    - K and V for key and value types in map-like structures
    - A to F for types of accumulators
    - T for most everything else, possibly with a numeric suffix
      to distinguish two separate types that nevertheless play
      similar roles.

    (I left descriptive type var names such as Stream and Store unchanged.)

    Adding or expanding descriptions of exported predicates and functions.

    Declaring and defining field syntax getters and setters without using ^,
    while keeping the use of field syntax, including ^, in the operations'
    descriptions.

    Defining field syntax operations operations in terms of the
    non-field-syntax versions, not vice versa.

    Defining function versions of operations in terms of the predicate
    versions, not vice versa.
2024-12-29 20:09:09 +11:00

170 lines
4.9 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
% Some checks of array implementation.
:- module array_test.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module array.
:- import_module bt_array.
:- import_module int.
:- import_module list.
:- import_module maybe.
:- import_module string.
main(!IO) :-
test([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], !IO).
:- pred test(list(int)::in, io::di, io::uo) is det.
test(Xs, !IO) :-
CmpFunc =
( func(X, Y) = Res :-
compare(Res, X, Y)
),
CmpPred =
( pred(X :: in, Y :: in, Res :: out) is det :-
compare(Res, X, Y)
),
array.from_list(Xs, A0),
array.to_list(A0, As0),
write_message_int_list("A0: ", As0, !IO),
array.max(A0, AMax0),
write_message_int("AMax0: ", AMax0, !IO),
array.min(A0, AMin0),
write_message_int("AMin0: ", AMin0, !IO),
array.size(A0, ASize),
write_message_int("ASize: ", ASize, !IO),
array.bounds(A0, AMin1, AMax1),
write_message_int("AMin1: ", AMin1, !IO),
write_message_int("AMax1: ", AMax1, !IO),
( if array.binary_search(CmpFunc, A0, 4, ABsearch) then
MaybeABsearch = yes(ABsearch)
else
MaybeABsearch = no
),
write_message_maybe_int("ABsearch: ", MaybeABsearch, !IO),
array.set(8, 100, A0, A1),
array.to_list(A1, As1),
write_message_int_list("A1: ", As1, !IO),
array.resize(15, 1000, A1, A2),
array.to_list(A2, As2),
write_message_int_list("A2: ", As2, !IO),
array.shrink(10, A2, A3),
array.to_list(A3, As3),
write_message_int_list("A3: ", As3, !IO),
A4 = array.sort(array(1 `..` 10)),
array.to_list(A4, As4),
write_message_int_list("A4: ", As4, !IO),
A5 = array.sort(array(list.reverse(1 `..` 10))),
array.to_list(A5, As5),
write_message_int_list("A5: ", As5, !IO),
A6 = array.from_reverse_list(Xs),
As6 = array.to_list(A6),
write_message_int_list("A6: ", As6, !IO),
bt_array.from_list(0, Xs, B0),
bt_array.to_list(B0, Bs0),
write_message_int_list("B0: ", Bs0, !IO),
bt_array.max(B0, BMax0),
write_message_int("BMax0: ", BMax0, !IO),
bt_array.min(B0, BMin0),
write_message_int("BMin0: ", BMin0, !IO),
bt_array.size(B0, BSize),
write_message_int("BSize: ", BSize, !IO),
bt_array.bounds(B0, BMin1, BMax1),
write_message_int("BMin1: ", BMin1, !IO),
write_message_int("BMax1: ", BMax1, !IO),
( if bt_array.bsearch(B0, 4, CmpPred, BBsearch) then
MaybeBBsearch = yes(BBsearch)
else
MaybeBBsearch = no
),
write_message_maybe_int("BBsearch: ", MaybeBBsearch, !IO),
bt_array.set(B0, 8, 100, B1),
bt_array.to_list(B1, Bs1),
write_message_int_list("B1: ", Bs1, !IO),
bt_array.resize(0, 14, 1000, B1, B2),
bt_array.to_list(B2, Bs2),
write_message_int_list("B2: ", Bs2, !IO),
bt_array.shrink(0, 9, B2, B3),
bt_array.to_list(B3, Bs3),
write_message_int_list("B3: ", Bs3, !IO),
% Finally, just in case, compare the two implementations.
( if
As0 = Bs0,
AMax0 = BMax1,
AMin0 = BMin1,
ASize = BSize,
AMin1 = BMin1,
AMax1 = BMax1,
AMax0 = AMax1, % Sanity check
AMin0 = AMin1, % Sanity check
BMax0 = BMax1, % Sanity check
BMin0 = BMin1, % Sanity check
MaybeABsearch = MaybeBBsearch,
As1 = Bs1,
As2 = Bs2,
As3 = Bs3,
As1 = As3, % Sanity check
Bs1 = Bs3 % Sanity check
then
io.write_string("Results all match\n", !IO)
else
io.write_string("Results don't match\n", !IO)
).
:- pred write_message_int(string::in, int::in, io::di, io::uo) is det.
write_message_int(Msg, O, !IO) :-
io.write_string(Msg, !IO),
io.write_int(O, !IO),
io.nl(!IO).
:- pred write_message_maybe_int(string::in, maybe(int)::in,
io::di, io::uo) is det.
write_message_maybe_int(Msg, no, !IO) :-
io.write_string(Msg, !IO),
io.write_string("no", !IO),
io.nl(!IO).
write_message_maybe_int(Msg, yes(I), !IO) :-
io.write_string(Msg, !IO),
io.write_string("yes(", !IO),
io.write_int(I, !IO),
io.write_string(")", !IO),
io.nl(!IO).
:- pred write_message_int_list(string::in, list(int)::in,
io::di, io::uo) is det.
write_message_int_list(Msg, List, !IO) :-
io.write_string(Msg, !IO),
(
List = [],
io.write_string("[]", !IO)
;
List = [I | Is],
io.write_char('[', !IO),
io.write_int(I, !IO),
write_int_list_rest(Is, !IO),
io.write_char(']', !IO)
),
io.nl(!IO).
:- pred write_int_list_rest(list(int)::in, io::di, io::uo) is det.
write_int_list_rest([], !IO).
write_int_list_rest([I | Is], !IO) :-
io.write_string(", ", !IO),
io.write_int(I, !IO),
write_int_list_rest(Is, !IO).