Files
mercury/tests/hard_coded/array_test_2.m
Zoltan Somogyi 94fadf1797 Add uint versions of array operations.
library/array.m:
    Add uint versions of most of this module's operations.

NEWS.md:
    Mention the new operations.

library/edit_distance.m:
    Use the new array operations to eliminate some casts.

library/edit_seq.m:
    Minor style fix.

tests/hard_coded/array_gen.{m,exp}:
tests/hard_coded/array_test_2.{m,exp}:
    Extend the tests of array.m's operations to the corresponding uint
    versions. Expect the output for the new versions.

tests/hard_coded/array_resize.m:
tests/hard_coded/array_shrink.m:
    Extend the tests of array.m's operations to the corresponding uint
    versions, but generate output only if their results differ from the
    original int versions. (They don't differ.)

tests/hard_coded/array_primitives.m:
    Style fixes.
2026-01-08 03:47:09 +11:00

120 lines
3.6 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module array_test_2.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module array.
:- import_module bool.
:- import_module int.
:- import_module list.
%---------------------------------------------------------------------------%
main(!IO) :-
% Exercise each of the native types for the Java backend.
test([10, 11, 12, 13, 14], !IO),
test([10.0, 11.1, 12.2, 13.3, 14.4], !IO),
test(['p', 'o', 'k', 'e', 'y'], !IO),
test([yes, no, yes, yes, no], !IO),
test(["ten", "eleven", "twelve", "thirteen", "fourteen"], !IO).
:- pred test(list(T)::in, io::di, io::uo) is det.
test(List, !IO) :-
io.write_string("----\n", !IO),
% Calls array.init, array.unsafe_insert_items.
array.from_list(List, Array),
% Calls array.bounds, array.fetch_items, array.foldr, array.elem,
% array.lookup.
array.to_list(Array, Elements),
io.write_string("size: ", !IO),
io.write_int(array.size(Array), !IO),
io.nl(!IO),
io.write_string("elements: ", !IO),
io.write(Elements, !IO),
io.nl(!IO),
( if semidet_lookup(Array, -1, _) then
io.write_string("error: out of bounds lookup suceeded\n", !IO)
else
io.write_string("good: out of bounds lookup averted\n", !IO)
),
( if semidet_lookup(Array, list.length(List), _) then
io.write_string("error: out of bounds lookup suceeded\n", !IO)
else
io.write_string("good: out of bounds lookup averted\n", !IO)
),
( if semidet_ulookup(Array, list.ulength(List), _) then
io.write_string("error: out of bounds ulookup suceeded\n", !IO)
else
io.write_string("good: out of bounds ulookup averted\n", !IO)
),
Elem = list.det_head(List),
array.copy(Array, ArrayB),
array.copy(Array, ArrayC),
( if semidet_set(-1, Elem, Array, _) then
io.write_string("error: out of bounds set succeeded\n", !IO)
else
io.write_string("good: out of bounds set averted\n", !IO)
),
( if semidet_set(5, Elem, ArrayB, _) then
io.write_string("error: out of bounds set succeeded\n", !IO)
else
io.write_string("good: out of bounds set averted\n", !IO)
),
( if semidet_uset(5u, Elem, ArrayC, _) then
io.write_string("error: out of bounds uset succeeded\n", !IO)
else
io.write_string("good: out of bounds uset averted\n", !IO)
),
some [!A] (
array.from_list(List, !:A),
array.resize(list.length(List), Elem, !A),
io.write_string("resize without resizing: ", !IO),
io.write(!.A, !IO),
io.nl(!IO),
array.resize(1 + list.length(List)//2, Elem, !A),
io.write_string("shrink: ", !IO),
io.write(!.A, !IO),
io.nl(!IO),
array.resize(list.length(List), Elem, !A),
io.write_string("enlarge: ", !IO), % embiggen
io.write(!.A, !IO),
io.nl(!IO),
array.resize(0, Elem, !A),
io.write_string("empty: ", !IO),
io.write(!.A, !IO),
io.nl(!IO),
array.resize(0, Elem, !A),
io.write_string("still empty: ", !IO),
io.write(!.A, !IO),
io.nl(!IO),
array.resize(3, Elem, !A),
io.write_string("nonempty from empty: ", !IO),
io.write(!.A, !IO),
io.nl(!IO)
).