Files
mercury/tests/hard_coded/array_resize.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

111 lines
4.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
%
% Test array.resize/3.
%
%---------------------------------------------------------------------------%
:- module array_resize.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module array.
:- import_module exception.
:- import_module list.
:- import_module uint.
:- type fruit
---> orange
; lemon
; apple
; pear
; pineapple.
:- type color
---> color(uint8, uint8, uint8).
main(!IO) :-
test_resize([1, 2, 3, 4], -1, 42, !IO), % Should raise exception.
test_resize([1, 2, 3, 4], 0, 42, !IO),
test_resize([1, 2, 3, 4], 3, 42, !IO),
test_resize([1, 2, 3, 4], 4, 42, !IO),
test_resize([1, 2, 3, 4], 5, 42, !IO),
test_resize([1i8, 2i8, 3i8, 4i8], 3, 42i8, !IO),
test_resize([1i8, 2i8, 3i8, 4i8], 6, 42i8, !IO),
test_resize([1u8, 2u8, 3u8, 4u8], 3, 42u8, !IO),
test_resize([1u8, 2u8, 3u8, 4u8], 6, 42u8, !IO),
test_resize([1i16, 2i16, 3i16, 4i16], 3, 42i16, !IO),
test_resize([1i16, 2i16, 3i16, 4i16], 6, 42i16, !IO),
test_resize([1u16, 2u16, 3u16, 4u16], 3, 42u16, !IO),
test_resize([1u16, 2u16, 3u16, 4u16], 6, 42u16, !IO),
test_resize([1i32, 2i32, 3i32, 4i32], 3, 42i32, !IO),
test_resize([1i32, 2i32, 3i32, 4i32], 6, 42i32, !IO),
test_resize([1u32, 2u32, 3u32, 4u32], 3, 42u32, !IO),
test_resize([1u32, 2u32, 3u32, 4u32], 6, 42u32, !IO),
test_resize([1i64, 2i64, 3i64, 4i64], 3, 42i64, !IO),
test_resize([1i64, 2i64, 3i64, 4i64], 6, 42i64, !IO),
test_resize([1u64, 2u64, 3u64, 4u64], 3, 42u64, !IO),
test_resize([1u64, 2u64, 3u64, 4u64], 6, 42u64, !IO),
test_resize([1.0, 2.0, 3.0, 4.0], 3, 42.0, !IO),
test_resize([1.0, 2.0, 3.0, 4.0], 36, 42.0, !IO),
test_resize(["one", "two", "three", "four"], 3, "forty-two", !IO),
test_resize([1.0, 2.0, 3.0, 4.0], 6, 42.0, !IO),
test_resize(['a', 'b', 'c', 'd'], 3, 'X', !IO),
test_resize(['a', 'b', 'c', 'd'], 6, 'X', !IO),
test_resize([orange, lemon, apple, pear], 3, pineapple, !IO),
test_resize([orange, lemon, apple, pear], 6, pineapple, !IO),
test_resize([color(1u8, 1u8, 1u8), color(2u8, 2u8, 2u8),
color(3u8, 3u8, 3u8), color(4u8, 4u8, 4u8)], 3, color(0u8, 0u8, 0u8),
!IO),
test_resize([color(1u8, 1u8, 1u8), color(2u8, 2u8, 2u8),
color(3u8, 3u8, 3u8), color(4u8, 4u8, 4u8)], 6, color(0u8, 0u8, 0u8),
!IO),
test_resize([[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]], 3, [42], !IO),
test_resize([[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]], 6, [42], !IO).
:- pred test_resize(list(T)::in, int::in, T::in, io::di, io::uo) is cc_multi.
test_resize(Elems, Size, Init, !IO) :-
io.write_string("================\n", !IO),
array.from_list(Elems, Array0),
io.write_string("Array0 = ", !IO),
io.write_line(Array0, !IO),
io.write_string("Size = ", !IO),
io.write_line(Size, !IO),
io.write_string("Init = ", !IO),
io.write_line(Init, !IO),
( try [] (
array.resize(Size, Init, Array0, Array)
) then
io.write_string("Array = ", !IO),
io.write_line(Array, !IO),
array.from_list(Elems, ArrayU0),
( try [] (
array.uresize(uint.cast_from_int(Size), Init, ArrayU0, ArrayU)
) then
array.to_list(Array, ArrayList),
array.to_list(ArrayU, ArrayListU),
( if ArrayList = ArrayListU then
true
else
io.write_string("resize and uresize results differ\n", !IO)
)
catch software_error(S) ->
io.write_string("URESIZE EXCEPTION: ", !IO),
io.write_line(S, !IO)
)
catch software_error(S) ->
io.write_string("EXCEPTION: ", !IO),
io.write_line(S, !IO)
).