[m-rev.] diff: add a test of basic arithmetic on uints
Julien Fischer
jfischer at opturion.com
Wed Apr 26 16:21:32 AEST 2017
Add a test of basic arithmetic on uints.
tests/hard_coded/Mmakefile:
tests/hard_coded/uint_arith.m:
tests/hard_coded/uint_arith.exp*:
As above. (The .exp file is for 64-bit machines; the .exp2 for
32-bit.)
Julien.
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index a51681e..e611975 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -636,6 +636,7 @@ ifeq "$(findstring profdeep,$(GRADE))" ""
try_syntax_5 \
try_syntax_6 \
try_syntax_7 \
+ uint_arith \
user_defined_equality \
version_array_test \
version_hash_table_delete \
diff --git a/tests/hard_coded/uint_arith.exp b/tests/hard_coded/uint_arith.exp
index e69de29..c10e78c 100644
--- a/tests/hard_coded/uint_arith.exp
+++ b/tests/hard_coded/uint_arith.exp
@@ -0,0 +1,201 @@
+*** Test binary operation '+' ***
+
+0u + 0u = 0u
+0u + 1u = 1u
+0u + 2u = 2u
+0u + 8u = 8u
+0u + 10u = 10u
+0u + 16u = 16u
+0u + 32u = 32u
+0u + 18446744073709551615u = 18446744073709551615u
+1u + 0u = 1u
+1u + 1u = 2u
+1u + 2u = 3u
+1u + 8u = 9u
+1u + 10u = 11u
+1u + 16u = 17u
+1u + 32u = 33u
+1u + 18446744073709551615u = 0u
+2u + 0u = 2u
+2u + 1u = 3u
+2u + 2u = 4u
+2u + 8u = 10u
+2u + 10u = 12u
+2u + 16u = 18u
+2u + 32u = 34u
+2u + 18446744073709551615u = 1u
+8u + 0u = 8u
+8u + 1u = 9u
+8u + 2u = 10u
+8u + 8u = 16u
+8u + 10u = 18u
+8u + 16u = 24u
+8u + 32u = 40u
+8u + 18446744073709551615u = 7u
+10u + 0u = 10u
+10u + 1u = 11u
+10u + 2u = 12u
+10u + 8u = 18u
+10u + 10u = 20u
+10u + 16u = 26u
+10u + 32u = 42u
+10u + 18446744073709551615u = 9u
+16u + 0u = 16u
+16u + 1u = 17u
+16u + 2u = 18u
+16u + 8u = 24u
+16u + 10u = 26u
+16u + 16u = 32u
+16u + 32u = 48u
+16u + 18446744073709551615u = 15u
+32u + 0u = 32u
+32u + 1u = 33u
+32u + 2u = 34u
+32u + 8u = 40u
+32u + 10u = 42u
+32u + 16u = 48u
+32u + 32u = 64u
+32u + 18446744073709551615u = 31u
+18446744073709551615u + 0u = 18446744073709551615u
+18446744073709551615u + 1u = 0u
+18446744073709551615u + 2u = 1u
+18446744073709551615u + 8u = 7u
+18446744073709551615u + 10u = 9u
+18446744073709551615u + 16u = 15u
+18446744073709551615u + 32u = 31u
+18446744073709551615u + 18446744073709551615u = 18446744073709551614u
+
+*** Test binary operation '-' ***
+
+0u - 0u = 0u
+0u - 1u = 18446744073709551615u
+0u - 2u = 18446744073709551614u
+0u - 8u = 18446744073709551608u
+0u - 10u = 18446744073709551606u
+0u - 16u = 18446744073709551600u
+0u - 32u = 18446744073709551584u
+0u - 18446744073709551615u = 1u
+1u - 0u = 1u
+1u - 1u = 0u
+1u - 2u = 18446744073709551615u
+1u - 8u = 18446744073709551609u
+1u - 10u = 18446744073709551607u
+1u - 16u = 18446744073709551601u
+1u - 32u = 18446744073709551585u
+1u - 18446744073709551615u = 2u
+2u - 0u = 2u
+2u - 1u = 1u
+2u - 2u = 0u
+2u - 8u = 18446744073709551610u
+2u - 10u = 18446744073709551608u
+2u - 16u = 18446744073709551602u
+2u - 32u = 18446744073709551586u
+2u - 18446744073709551615u = 3u
+8u - 0u = 8u
+8u - 1u = 7u
+8u - 2u = 6u
+8u - 8u = 0u
+8u - 10u = 18446744073709551614u
+8u - 16u = 18446744073709551608u
+8u - 32u = 18446744073709551592u
+8u - 18446744073709551615u = 9u
+10u - 0u = 10u
+10u - 1u = 9u
+10u - 2u = 8u
+10u - 8u = 2u
+10u - 10u = 0u
+10u - 16u = 18446744073709551610u
+10u - 32u = 18446744073709551594u
+10u - 18446744073709551615u = 11u
+16u - 0u = 16u
+16u - 1u = 15u
+16u - 2u = 14u
+16u - 8u = 8u
+16u - 10u = 6u
+16u - 16u = 0u
+16u - 32u = 18446744073709551600u
+16u - 18446744073709551615u = 17u
+32u - 0u = 32u
+32u - 1u = 31u
+32u - 2u = 30u
+32u - 8u = 24u
+32u - 10u = 22u
+32u - 16u = 16u
+32u - 32u = 0u
+32u - 18446744073709551615u = 33u
+18446744073709551615u - 0u = 18446744073709551615u
+18446744073709551615u - 1u = 18446744073709551614u
+18446744073709551615u - 2u = 18446744073709551613u
+18446744073709551615u - 8u = 18446744073709551607u
+18446744073709551615u - 10u = 18446744073709551605u
+18446744073709551615u - 16u = 18446744073709551599u
+18446744073709551615u - 32u = 18446744073709551583u
+18446744073709551615u - 18446744073709551615u = 0u
+
+*** Test binary operation '*' ***
+
+0u * 0u = 0u
+0u * 1u = 0u
+0u * 2u = 0u
+0u * 8u = 0u
+0u * 10u = 0u
+0u * 16u = 0u
+0u * 32u = 0u
+0u * 18446744073709551615u = 0u
+1u * 0u = 0u
+1u * 1u = 1u
+1u * 2u = 2u
+1u * 8u = 8u
+1u * 10u = 10u
+1u * 16u = 16u
+1u * 32u = 32u
+1u * 18446744073709551615u = 18446744073709551615u
+2u * 0u = 0u
+2u * 1u = 2u
+2u * 2u = 4u
+2u * 8u = 16u
+2u * 10u = 20u
+2u * 16u = 32u
+2u * 32u = 64u
+2u * 18446744073709551615u = 18446744073709551614u
+8u * 0u = 0u
+8u * 1u = 8u
+8u * 2u = 16u
+8u * 8u = 64u
+8u * 10u = 80u
+8u * 16u = 128u
+8u * 32u = 256u
+8u * 18446744073709551615u = 18446744073709551608u
+10u * 0u = 0u
+10u * 1u = 10u
+10u * 2u = 20u
+10u * 8u = 80u
+10u * 10u = 100u
+10u * 16u = 160u
+10u * 32u = 320u
+10u * 18446744073709551615u = 18446744073709551606u
+16u * 0u = 0u
+16u * 1u = 16u
+16u * 2u = 32u
+16u * 8u = 128u
+16u * 10u = 160u
+16u * 16u = 256u
+16u * 32u = 512u
+16u * 18446744073709551615u = 18446744073709551600u
+32u * 0u = 0u
+32u * 1u = 32u
+32u * 2u = 64u
+32u * 8u = 256u
+32u * 10u = 320u
+32u * 16u = 512u
+32u * 32u = 1024u
+32u * 18446744073709551615u = 18446744073709551584u
+18446744073709551615u * 0u = 0u
+18446744073709551615u * 1u = 18446744073709551615u
+18446744073709551615u * 2u = 18446744073709551614u
+18446744073709551615u * 8u = 18446744073709551608u
+18446744073709551615u * 10u = 18446744073709551606u
+18446744073709551615u * 16u = 18446744073709551600u
+18446744073709551615u * 32u = 18446744073709551584u
+18446744073709551615u * 18446744073709551615u = 1u
+
diff --git a/tests/hard_coded/uint_arith.exp2 b/tests/hard_coded/uint_arith.exp2
index e69de29..b17991f 100644
--- a/tests/hard_coded/uint_arith.exp2
+++ b/tests/hard_coded/uint_arith.exp2
@@ -0,0 +1,201 @@
+*** Test binary operation '+' ***
+
+0u + 0u = 0u
+0u + 1u = 1u
+0u + 2u = 2u
+0u + 8u = 8u
+0u + 10u = 10u
+0u + 16u = 16u
+0u + 32u = 32u
+0u + 4294967295u = 4294967295u
+1u + 0u = 1u
+1u + 1u = 2u
+1u + 2u = 3u
+1u + 8u = 9u
+1u + 10u = 11u
+1u + 16u = 17u
+1u + 32u = 33u
+1u + 4294967295u = 0u
+2u + 0u = 2u
+2u + 1u = 3u
+2u + 2u = 4u
+2u + 8u = 10u
+2u + 10u = 12u
+2u + 16u = 18u
+2u + 32u = 34u
+2u + 4294967295u = 1u
+8u + 0u = 8u
+8u + 1u = 9u
+8u + 2u = 10u
+8u + 8u = 16u
+8u + 10u = 18u
+8u + 16u = 24u
+8u + 32u = 40u
+8u + 4294967295u = 7u
+10u + 0u = 10u
+10u + 1u = 11u
+10u + 2u = 12u
+10u + 8u = 18u
+10u + 10u = 20u
+10u + 16u = 26u
+10u + 32u = 42u
+10u + 4294967295u = 9u
+16u + 0u = 16u
+16u + 1u = 17u
+16u + 2u = 18u
+16u + 8u = 24u
+16u + 10u = 26u
+16u + 16u = 32u
+16u + 32u = 48u
+16u + 4294967295u = 15u
+32u + 0u = 32u
+32u + 1u = 33u
+32u + 2u = 34u
+32u + 8u = 40u
+32u + 10u = 42u
+32u + 16u = 48u
+32u + 32u = 64u
+32u + 4294967295u = 31u
+4294967295u + 0u = 4294967295u
+4294967295u + 1u = 0u
+4294967295u + 2u = 1u
+4294967295u + 8u = 7u
+4294967295u + 10u = 9u
+4294967295u + 16u = 15u
+4294967295u + 32u = 31u
+4294967295u + 4294967295u = 4294967294u
+
+*** Test binary operation '-' ***
+
+0u - 0u = 0u
+0u - 1u = 4294967295u
+0u - 2u = 4294967294u
+0u - 8u = 4294967288u
+0u - 10u = 4294967286u
+0u - 16u = 4294967280u
+0u - 32u = 4294967264u
+0u - 4294967295u = 1u
+1u - 0u = 1u
+1u - 1u = 0u
+1u - 2u = 4294967295u
+1u - 8u = 4294967289u
+1u - 10u = 4294967287u
+1u - 16u = 4294967281u
+1u - 32u = 4294967265u
+1u - 4294967295u = 2u
+2u - 0u = 2u
+2u - 1u = 1u
+2u - 2u = 0u
+2u - 8u = 4294967290u
+2u - 10u = 4294967288u
+2u - 16u = 4294967282u
+2u - 32u = 4294967266u
+2u - 4294967295u = 3u
+8u - 0u = 8u
+8u - 1u = 7u
+8u - 2u = 6u
+8u - 8u = 0u
+8u - 10u = 4294967294u
+8u - 16u = 4294967288u
+8u - 32u = 4294967272u
+8u - 4294967295u = 9u
+10u - 0u = 10u
+10u - 1u = 9u
+10u - 2u = 8u
+10u - 8u = 2u
+10u - 10u = 0u
+10u - 16u = 4294967290u
+10u - 32u = 4294967274u
+10u - 4294967295u = 11u
+16u - 0u = 16u
+16u - 1u = 15u
+16u - 2u = 14u
+16u - 8u = 8u
+16u - 10u = 6u
+16u - 16u = 0u
+16u - 32u = 4294967280u
+16u - 4294967295u = 17u
+32u - 0u = 32u
+32u - 1u = 31u
+32u - 2u = 30u
+32u - 8u = 24u
+32u - 10u = 22u
+32u - 16u = 16u
+32u - 32u = 0u
+32u - 4294967295u = 33u
+4294967295u - 0u = 4294967295u
+4294967295u - 1u = 4294967294u
+4294967295u - 2u = 4294967293u
+4294967295u - 8u = 4294967287u
+4294967295u - 10u = 4294967285u
+4294967295u - 16u = 4294967279u
+4294967295u - 32u = 4294967263u
+4294967295u - 4294967295u = 0u
+
+*** Test binary operation '*' ***
+
+0u * 0u = 0u
+0u * 1u = 0u
+0u * 2u = 0u
+0u * 8u = 0u
+0u * 10u = 0u
+0u * 16u = 0u
+0u * 32u = 0u
+0u * 4294967295u = 0u
+1u * 0u = 0u
+1u * 1u = 1u
+1u * 2u = 2u
+1u * 8u = 8u
+1u * 10u = 10u
+1u * 16u = 16u
+1u * 32u = 32u
+1u * 4294967295u = 4294967295u
+2u * 0u = 0u
+2u * 1u = 2u
+2u * 2u = 4u
+2u * 8u = 16u
+2u * 10u = 20u
+2u * 16u = 32u
+2u * 32u = 64u
+2u * 4294967295u = 4294967294u
+8u * 0u = 0u
+8u * 1u = 8u
+8u * 2u = 16u
+8u * 8u = 64u
+8u * 10u = 80u
+8u * 16u = 128u
+8u * 32u = 256u
+8u * 4294967295u = 4294967288u
+10u * 0u = 0u
+10u * 1u = 10u
+10u * 2u = 20u
+10u * 8u = 80u
+10u * 10u = 100u
+10u * 16u = 160u
+10u * 32u = 320u
+10u * 4294967295u = 4294967286u
+16u * 0u = 0u
+16u * 1u = 16u
+16u * 2u = 32u
+16u * 8u = 128u
+16u * 10u = 160u
+16u * 16u = 256u
+16u * 32u = 512u
+16u * 4294967295u = 4294967280u
+32u * 0u = 0u
+32u * 1u = 32u
+32u * 2u = 64u
+32u * 8u = 256u
+32u * 10u = 320u
+32u * 16u = 512u
+32u * 32u = 1024u
+32u * 4294967295u = 4294967264u
+4294967295u * 0u = 0u
+4294967295u * 1u = 4294967295u
+4294967295u * 2u = 4294967294u
+4294967295u * 8u = 4294967288u
+4294967295u * 10u = 4294967286u
+4294967295u * 16u = 4294967280u
+4294967295u * 32u = 4294967264u
+4294967295u * 4294967295u = 1u
+
diff --git a/tests/hard_coded/uint_arith.m b/tests/hard_coded/uint_arith.m
index e69de29..785c159 100644
--- a/tests/hard_coded/uint_arith.m
+++ b/tests/hard_coded/uint_arith.m
@@ -0,0 +1,83 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+
+% Test basic arithmetic operations for unsigned unsigned integers.
+
+:- module uint_arith.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module exception.
+:- import_module list.
+:- import_module string.
+:- import_module uint.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+ run_binop_test((func(X, Y) = X + Y), "+", !IO),
+ io.nl(!IO),
+ run_binop_test((func(X, Y) = X - Y), "-", !IO),
+ io.nl(!IO),
+ run_binop_test((func(X, Y) = X * Y), "*", !IO),
+ io.nl(!IO).
+ %run_binop_test(uint.(/), "/", !IO). % NYI.
+
+:- pred run_binop_test((func(uint, uint) = uint)::in, string::in,
+ io::di, io::uo) is cc_multi.
+
+run_binop_test(BinOpFunc, Desc, !IO) :-
+ io.format("*** Test binary operation '%s' ***\n\n", [s(Desc)], !IO),
+ As = numbers,
+ Bs = numbers,
+ list.foldl(run_binop_test_2(BinOpFunc, Desc, Bs), As, !IO).
+
+:- pred run_binop_test_2((func(uint, uint) = uint)::in, string::in,
+ list(uint)::in, uint::in, io::di, io::uo) is cc_multi.
+
+run_binop_test_2(BinOpFunc, Desc, Bs, A, !IO) :-
+ list.foldl(run_binop_test_3(BinOpFunc, Desc, A), Bs, !IO).
+
+:- pred run_binop_test_3((func(uint, uint) = uint)::in, string::in,
+ uint::in, uint::in, io::di, io::uo) is cc_multi.
+
+run_binop_test_3(BinOpFunc, Desc, A, B, !IO) :-
+ ( try []
+ Result0 = BinOpFunc(A, B)
+ then
+ ResultStr = to_string(Result0)
+ catch_any _ ->
+ ResultStr = "<<exception>>"
+ ),
+ io.format("%s %s %s = %s\n",
+ [s(to_string(A)), s(Desc), s(to_string(B)), s(ResultStr)], !IO).
+
+:- func numbers = list(uint).
+
+numbers = [
+ 0u,
+ 1u,
+ 2u,
+ 8u,
+ 10u,
+ 16u,
+ 32u,
+ uint.max_uint
+].
+
+:- func to_string(uint) = string.
+
+to_string(U) = uint_to_string(U) ++ "u".
+
+%---------------------------------------------------------------------------%
+:- end_module uint_arith.
+%---------------------------------------------------------------------------%
More information about the reviews
mailing list