[m-rev.] diff: add tests of basic arithmetic for 16-bit integers
Julien Fischer
jfischer at opturion.com
Mon Aug 21 22:01:43 AEST 2017
Add tests of basic arithmetic for 16-bit integers.
tests/hard_coded/arith_int16.{m,exp}:
tests/hard_coded/arith_uint16.{m,exp}:
tests/hard_coded/Mmakefile:
As above.
Julien.
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 20e12cfd4..b68057c48 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -618,7 +618,9 @@ ifeq "$(findstring profdeep,$(GRADE))" ""
NON_PROFDEEP_PROGS = \
$(NON_PROFDEEP_PROGS_2) \
allow_stubs \
+ arith_int16 \
arith_int8 \
+ arith_uint16 \
arith_uint8 \
bitmap_empty \
bitmap_test \
diff --git a/tests/hard_coded/arith_int16.exp b/tests/hard_coded/arith_int16.exp
index e69de29bb..aaf6d0947 100644
--- a/tests/hard_coded/arith_int16.exp
+++ b/tests/hard_coded/arith_int16.exp
@@ -0,0 +1,647 @@
+*** Test unary operation '+' ***
+
++ -32768 = -32768
++ -128 = -128
++ 0 = 0
++ 1 = 1
++ 2 = 2
++ 8 = 8
++ 10 = 10
++ 16 = 16
++ 127 = 127
++ 255 = 255
++ 32767 = 32767
+
+*** Test unary operation '-' ***
+
+- -32768 = -32768
+- -128 = 128
+- 0 = 0
+- 1 = -1
+- 2 = -2
+- 8 = -8
+- 10 = -10
+- 16 = -16
+- 127 = -127
+- 255 = -255
+- 32767 = -32767
+
+*** Test binary operation '+' ***
+
+-32768 + -32768 = 0
+-32768 + -128 = 32640
+-32768 + 0 = -32768
+-32768 + 1 = -32767
+-32768 + 2 = -32766
+-32768 + 8 = -32760
+-32768 + 10 = -32758
+-32768 + 16 = -32752
+-32768 + 127 = -32641
+-32768 + 255 = -32513
+-32768 + 32767 = -1
+-128 + -32768 = 32640
+-128 + -128 = -256
+-128 + 0 = -128
+-128 + 1 = -127
+-128 + 2 = -126
+-128 + 8 = -120
+-128 + 10 = -118
+-128 + 16 = -112
+-128 + 127 = -1
+-128 + 255 = 127
+-128 + 32767 = 32639
+0 + -32768 = -32768
+0 + -128 = -128
+0 + 0 = 0
+0 + 1 = 1
+0 + 2 = 2
+0 + 8 = 8
+0 + 10 = 10
+0 + 16 = 16
+0 + 127 = 127
+0 + 255 = 255
+0 + 32767 = 32767
+1 + -32768 = -32767
+1 + -128 = -127
+1 + 0 = 1
+1 + 1 = 2
+1 + 2 = 3
+1 + 8 = 9
+1 + 10 = 11
+1 + 16 = 17
+1 + 127 = 128
+1 + 255 = 256
+1 + 32767 = -32768
+2 + -32768 = -32766
+2 + -128 = -126
+2 + 0 = 2
+2 + 1 = 3
+2 + 2 = 4
+2 + 8 = 10
+2 + 10 = 12
+2 + 16 = 18
+2 + 127 = 129
+2 + 255 = 257
+2 + 32767 = -32767
+8 + -32768 = -32760
+8 + -128 = -120
+8 + 0 = 8
+8 + 1 = 9
+8 + 2 = 10
+8 + 8 = 16
+8 + 10 = 18
+8 + 16 = 24
+8 + 127 = 135
+8 + 255 = 263
+8 + 32767 = -32761
+10 + -32768 = -32758
+10 + -128 = -118
+10 + 0 = 10
+10 + 1 = 11
+10 + 2 = 12
+10 + 8 = 18
+10 + 10 = 20
+10 + 16 = 26
+10 + 127 = 137
+10 + 255 = 265
+10 + 32767 = -32759
+16 + -32768 = -32752
+16 + -128 = -112
+16 + 0 = 16
+16 + 1 = 17
+16 + 2 = 18
+16 + 8 = 24
+16 + 10 = 26
+16 + 16 = 32
+16 + 127 = 143
+16 + 255 = 271
+16 + 32767 = -32753
+127 + -32768 = -32641
+127 + -128 = -1
+127 + 0 = 127
+127 + 1 = 128
+127 + 2 = 129
+127 + 8 = 135
+127 + 10 = 137
+127 + 16 = 143
+127 + 127 = 254
+127 + 255 = 382
+127 + 32767 = -32642
+255 + -32768 = -32513
+255 + -128 = 127
+255 + 0 = 255
+255 + 1 = 256
+255 + 2 = 257
+255 + 8 = 263
+255 + 10 = 265
+255 + 16 = 271
+255 + 127 = 382
+255 + 255 = 510
+255 + 32767 = -32514
+32767 + -32768 = -1
+32767 + -128 = 32639
+32767 + 0 = 32767
+32767 + 1 = -32768
+32767 + 2 = -32767
+32767 + 8 = -32761
+32767 + 10 = -32759
+32767 + 16 = -32753
+32767 + 127 = -32642
+32767 + 255 = -32514
+32767 + 32767 = -2
+
+*** Test binary operation '-' ***
+
+-32768 - -32768 = 0
+-32768 - -128 = -32640
+-32768 - 0 = -32768
+-32768 - 1 = 32767
+-32768 - 2 = 32766
+-32768 - 8 = 32760
+-32768 - 10 = 32758
+-32768 - 16 = 32752
+-32768 - 127 = 32641
+-32768 - 255 = 32513
+-32768 - 32767 = 1
+-128 - -32768 = 32640
+-128 - -128 = 0
+-128 - 0 = -128
+-128 - 1 = -129
+-128 - 2 = -130
+-128 - 8 = -136
+-128 - 10 = -138
+-128 - 16 = -144
+-128 - 127 = -255
+-128 - 255 = -383
+-128 - 32767 = 32641
+0 - -32768 = -32768
+0 - -128 = 128
+0 - 0 = 0
+0 - 1 = -1
+0 - 2 = -2
+0 - 8 = -8
+0 - 10 = -10
+0 - 16 = -16
+0 - 127 = -127
+0 - 255 = -255
+0 - 32767 = -32767
+1 - -32768 = -32767
+1 - -128 = 129
+1 - 0 = 1
+1 - 1 = 0
+1 - 2 = -1
+1 - 8 = -7
+1 - 10 = -9
+1 - 16 = -15
+1 - 127 = -126
+1 - 255 = -254
+1 - 32767 = -32766
+2 - -32768 = -32766
+2 - -128 = 130
+2 - 0 = 2
+2 - 1 = 1
+2 - 2 = 0
+2 - 8 = -6
+2 - 10 = -8
+2 - 16 = -14
+2 - 127 = -125
+2 - 255 = -253
+2 - 32767 = -32765
+8 - -32768 = -32760
+8 - -128 = 136
+8 - 0 = 8
+8 - 1 = 7
+8 - 2 = 6
+8 - 8 = 0
+8 - 10 = -2
+8 - 16 = -8
+8 - 127 = -119
+8 - 255 = -247
+8 - 32767 = -32759
+10 - -32768 = -32758
+10 - -128 = 138
+10 - 0 = 10
+10 - 1 = 9
+10 - 2 = 8
+10 - 8 = 2
+10 - 10 = 0
+10 - 16 = -6
+10 - 127 = -117
+10 - 255 = -245
+10 - 32767 = -32757
+16 - -32768 = -32752
+16 - -128 = 144
+16 - 0 = 16
+16 - 1 = 15
+16 - 2 = 14
+16 - 8 = 8
+16 - 10 = 6
+16 - 16 = 0
+16 - 127 = -111
+16 - 255 = -239
+16 - 32767 = -32751
+127 - -32768 = -32641
+127 - -128 = 255
+127 - 0 = 127
+127 - 1 = 126
+127 - 2 = 125
+127 - 8 = 119
+127 - 10 = 117
+127 - 16 = 111
+127 - 127 = 0
+127 - 255 = -128
+127 - 32767 = -32640
+255 - -32768 = -32513
+255 - -128 = 383
+255 - 0 = 255
+255 - 1 = 254
+255 - 2 = 253
+255 - 8 = 247
+255 - 10 = 245
+255 - 16 = 239
+255 - 127 = 128
+255 - 255 = 0
+255 - 32767 = -32512
+32767 - -32768 = -1
+32767 - -128 = -32641
+32767 - 0 = 32767
+32767 - 1 = 32766
+32767 - 2 = 32765
+32767 - 8 = 32759
+32767 - 10 = 32757
+32767 - 16 = 32751
+32767 - 127 = 32640
+32767 - 255 = 32512
+32767 - 32767 = 0
+
+*** Test binary operation '*' ***
+
+-32768 * -32768 = 0
+-32768 * -128 = 0
+-32768 * 0 = 0
+-32768 * 1 = -32768
+-32768 * 2 = 0
+-32768 * 8 = 0
+-32768 * 10 = 0
+-32768 * 16 = 0
+-32768 * 127 = -32768
+-32768 * 255 = -32768
+-32768 * 32767 = -32768
+-128 * -32768 = 0
+-128 * -128 = 16384
+-128 * 0 = 0
+-128 * 1 = -128
+-128 * 2 = -256
+-128 * 8 = -1024
+-128 * 10 = -1280
+-128 * 16 = -2048
+-128 * 127 = -16256
+-128 * 255 = -32640
+-128 * 32767 = 128
+0 * -32768 = 0
+0 * -128 = 0
+0 * 0 = 0
+0 * 1 = 0
+0 * 2 = 0
+0 * 8 = 0
+0 * 10 = 0
+0 * 16 = 0
+0 * 127 = 0
+0 * 255 = 0
+0 * 32767 = 0
+1 * -32768 = -32768
+1 * -128 = -128
+1 * 0 = 0
+1 * 1 = 1
+1 * 2 = 2
+1 * 8 = 8
+1 * 10 = 10
+1 * 16 = 16
+1 * 127 = 127
+1 * 255 = 255
+1 * 32767 = 32767
+2 * -32768 = 0
+2 * -128 = -256
+2 * 0 = 0
+2 * 1 = 2
+2 * 2 = 4
+2 * 8 = 16
+2 * 10 = 20
+2 * 16 = 32
+2 * 127 = 254
+2 * 255 = 510
+2 * 32767 = -2
+8 * -32768 = 0
+8 * -128 = -1024
+8 * 0 = 0
+8 * 1 = 8
+8 * 2 = 16
+8 * 8 = 64
+8 * 10 = 80
+8 * 16 = 128
+8 * 127 = 1016
+8 * 255 = 2040
+8 * 32767 = -8
+10 * -32768 = 0
+10 * -128 = -1280
+10 * 0 = 0
+10 * 1 = 10
+10 * 2 = 20
+10 * 8 = 80
+10 * 10 = 100
+10 * 16 = 160
+10 * 127 = 1270
+10 * 255 = 2550
+10 * 32767 = -10
+16 * -32768 = 0
+16 * -128 = -2048
+16 * 0 = 0
+16 * 1 = 16
+16 * 2 = 32
+16 * 8 = 128
+16 * 10 = 160
+16 * 16 = 256
+16 * 127 = 2032
+16 * 255 = 4080
+16 * 32767 = -16
+127 * -32768 = -32768
+127 * -128 = -16256
+127 * 0 = 0
+127 * 1 = 127
+127 * 2 = 254
+127 * 8 = 1016
+127 * 10 = 1270
+127 * 16 = 2032
+127 * 127 = 16129
+127 * 255 = 32385
+127 * 32767 = 32641
+255 * -32768 = -32768
+255 * -128 = -32640
+255 * 0 = 0
+255 * 1 = 255
+255 * 2 = 510
+255 * 8 = 2040
+255 * 10 = 2550
+255 * 16 = 4080
+255 * 127 = 32385
+255 * 255 = -511
+255 * 32767 = 32513
+32767 * -32768 = -32768
+32767 * -128 = 128
+32767 * 0 = 0
+32767 * 1 = 32767
+32767 * 2 = -2
+32767 * 8 = -8
+32767 * 10 = -10
+32767 * 16 = -16
+32767 * 127 = 32641
+32767 * 255 = 32513
+32767 * 32767 = 1
+
+*** Test binary operation '/' ***
+
+-32768 / -32768 = 1
+-32768 / -128 = 256
+-32768 / 0 = <<exception>>
+-32768 / 1 = -32768
+-32768 / 2 = -16384
+-32768 / 8 = -4096
+-32768 / 10 = -3276
+-32768 / 16 = -2048
+-32768 / 127 = -258
+-32768 / 255 = -128
+-32768 / 32767 = -1
+-128 / -32768 = 0
+-128 / -128 = 1
+-128 / 0 = <<exception>>
+-128 / 1 = -128
+-128 / 2 = -64
+-128 / 8 = -16
+-128 / 10 = -12
+-128 / 16 = -8
+-128 / 127 = -1
+-128 / 255 = 0
+-128 / 32767 = 0
+0 / -32768 = 0
+0 / -128 = 0
+0 / 0 = <<exception>>
+0 / 1 = 0
+0 / 2 = 0
+0 / 8 = 0
+0 / 10 = 0
+0 / 16 = 0
+0 / 127 = 0
+0 / 255 = 0
+0 / 32767 = 0
+1 / -32768 = 0
+1 / -128 = 0
+1 / 0 = <<exception>>
+1 / 1 = 1
+1 / 2 = 0
+1 / 8 = 0
+1 / 10 = 0
+1 / 16 = 0
+1 / 127 = 0
+1 / 255 = 0
+1 / 32767 = 0
+2 / -32768 = 0
+2 / -128 = 0
+2 / 0 = <<exception>>
+2 / 1 = 2
+2 / 2 = 1
+2 / 8 = 0
+2 / 10 = 0
+2 / 16 = 0
+2 / 127 = 0
+2 / 255 = 0
+2 / 32767 = 0
+8 / -32768 = 0
+8 / -128 = 0
+8 / 0 = <<exception>>
+8 / 1 = 8
+8 / 2 = 4
+8 / 8 = 1
+8 / 10 = 0
+8 / 16 = 0
+8 / 127 = 0
+8 / 255 = 0
+8 / 32767 = 0
+10 / -32768 = 0
+10 / -128 = 0
+10 / 0 = <<exception>>
+10 / 1 = 10
+10 / 2 = 5
+10 / 8 = 1
+10 / 10 = 1
+10 / 16 = 0
+10 / 127 = 0
+10 / 255 = 0
+10 / 32767 = 0
+16 / -32768 = 0
+16 / -128 = 0
+16 / 0 = <<exception>>
+16 / 1 = 16
+16 / 2 = 8
+16 / 8 = 2
+16 / 10 = 1
+16 / 16 = 1
+16 / 127 = 0
+16 / 255 = 0
+16 / 32767 = 0
+127 / -32768 = 0
+127 / -128 = 0
+127 / 0 = <<exception>>
+127 / 1 = 127
+127 / 2 = 63
+127 / 8 = 15
+127 / 10 = 12
+127 / 16 = 7
+127 / 127 = 1
+127 / 255 = 0
+127 / 32767 = 0
+255 / -32768 = 0
+255 / -128 = -1
+255 / 0 = <<exception>>
+255 / 1 = 255
+255 / 2 = 127
+255 / 8 = 31
+255 / 10 = 25
+255 / 16 = 15
+255 / 127 = 2
+255 / 255 = 1
+255 / 32767 = 0
+32767 / -32768 = 0
+32767 / -128 = -255
+32767 / 0 = <<exception>>
+32767 / 1 = 32767
+32767 / 2 = 16383
+32767 / 8 = 4095
+32767 / 10 = 3276
+32767 / 16 = 2047
+32767 / 127 = 258
+32767 / 255 = 128
+32767 / 32767 = 1
+
+*** Test binary operation 'rem' ***
+
+-32768 rem -32768 = 0
+-32768 rem -128 = 0
+-32768 rem 0 = <<exception>>
+-32768 rem 1 = 0
+-32768 rem 2 = 0
+-32768 rem 8 = 0
+-32768 rem 10 = -8
+-32768 rem 16 = 0
+-32768 rem 127 = -2
+-32768 rem 255 = -128
+-32768 rem 32767 = -1
+-128 rem -32768 = -128
+-128 rem -128 = 0
+-128 rem 0 = <<exception>>
+-128 rem 1 = 0
+-128 rem 2 = 0
+-128 rem 8 = 0
+-128 rem 10 = -8
+-128 rem 16 = 0
+-128 rem 127 = -1
+-128 rem 255 = -128
+-128 rem 32767 = -128
+0 rem -32768 = 0
+0 rem -128 = 0
+0 rem 0 = <<exception>>
+0 rem 1 = 0
+0 rem 2 = 0
+0 rem 8 = 0
+0 rem 10 = 0
+0 rem 16 = 0
+0 rem 127 = 0
+0 rem 255 = 0
+0 rem 32767 = 0
+1 rem -32768 = 1
+1 rem -128 = 1
+1 rem 0 = <<exception>>
+1 rem 1 = 0
+1 rem 2 = 1
+1 rem 8 = 1
+1 rem 10 = 1
+1 rem 16 = 1
+1 rem 127 = 1
+1 rem 255 = 1
+1 rem 32767 = 1
+2 rem -32768 = 2
+2 rem -128 = 2
+2 rem 0 = <<exception>>
+2 rem 1 = 0
+2 rem 2 = 0
+2 rem 8 = 2
+2 rem 10 = 2
+2 rem 16 = 2
+2 rem 127 = 2
+2 rem 255 = 2
+2 rem 32767 = 2
+8 rem -32768 = 8
+8 rem -128 = 8
+8 rem 0 = <<exception>>
+8 rem 1 = 0
+8 rem 2 = 0
+8 rem 8 = 0
+8 rem 10 = 8
+8 rem 16 = 8
+8 rem 127 = 8
+8 rem 255 = 8
+8 rem 32767 = 8
+10 rem -32768 = 10
+10 rem -128 = 10
+10 rem 0 = <<exception>>
+10 rem 1 = 0
+10 rem 2 = 0
+10 rem 8 = 2
+10 rem 10 = 0
+10 rem 16 = 10
+10 rem 127 = 10
+10 rem 255 = 10
+10 rem 32767 = 10
+16 rem -32768 = 16
+16 rem -128 = 16
+16 rem 0 = <<exception>>
+16 rem 1 = 0
+16 rem 2 = 0
+16 rem 8 = 0
+16 rem 10 = 6
+16 rem 16 = 0
+16 rem 127 = 16
+16 rem 255 = 16
+16 rem 32767 = 16
+127 rem -32768 = 127
+127 rem -128 = 127
+127 rem 0 = <<exception>>
+127 rem 1 = 0
+127 rem 2 = 1
+127 rem 8 = 7
+127 rem 10 = 7
+127 rem 16 = 15
+127 rem 127 = 0
+127 rem 255 = 127
+127 rem 32767 = 127
+255 rem -32768 = 255
+255 rem -128 = 127
+255 rem 0 = <<exception>>
+255 rem 1 = 0
+255 rem 2 = 1
+255 rem 8 = 7
+255 rem 10 = 5
+255 rem 16 = 15
+255 rem 127 = 1
+255 rem 255 = 0
+255 rem 32767 = 255
+32767 rem -32768 = 32767
+32767 rem -128 = 127
+32767 rem 0 = <<exception>>
+32767 rem 1 = 0
+32767 rem 2 = 1
+32767 rem 8 = 7
+32767 rem 10 = 7
+32767 rem 16 = 15
+32767 rem 127 = 1
+32767 rem 255 = 127
+32767 rem 32767 = 0
diff --git a/tests/hard_coded/arith_int16.m b/tests/hard_coded/arith_int16.m
index e69de29bb..5f997aeb9 100644
--- a/tests/hard_coded/arith_int16.m
+++ b/tests/hard_coded/arith_int16.m
@@ -0,0 +1,117 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+
+% Test arithmetic operations for signed 16-bit integers.
+
+:- module arith_int16.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module int16.
+
+:- import_module exception.
+:- import_module list.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+ run_unop_test(int16.(+), "+", !IO),
+ io.nl(!IO),
+ run_unop_test(int16.(-), "-", !IO),
+ io.nl(!IO),
+ %run_unop_test(int16.abs, "abs", !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((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 rem Y), "rem", !IO).
+
+%---------------------------------------------------------------------------%
+
+:- pred run_unop_test((func(int16) = int16)::in, string::in,
+ io::di, io::uo) is cc_multi.
+
+run_unop_test(UnOpFunc, Desc, !IO) :-
+ io.format("*** Test unary operation '%s' ***\n\n", [s(Desc)], !IO),
+ As = numbers,
+ list.foldl(run_unop_test_2(UnOpFunc, Desc), As, !IO).
+
+:- pred run_unop_test_2((func(int16) = int16)::in, string::in, int16::in,
+ io::di, io::uo) is cc_multi.
+
+run_unop_test_2(UnOpFunc, Desc, A, !IO) :-
+ ( try []
+ Result0 = UnOpFunc(A)
+ then
+ ResultStr = int16_to_string(Result0)
+ catch_any _ ->
+ ResultStr = "<<exception>>"
+ ),
+ io.format("%s %s = %s\n",
+ [s(Desc), s(int16_to_string(A)), s(ResultStr)], !IO).
+
+%---------------------------------------------------------------------------%
+
+:- pred run_binop_test((func(int16, int16) = int16)::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(int16, int16) = int16)::in, string::in,
+ list(int16)::in, int16::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(int16, int16) = int16)::in, string::in,
+ int16::in, int16::in, io::di, io::uo) is cc_multi.
+
+run_binop_test_3(BinOpFunc, Desc, A, B, !IO) :-
+ ( try []
+ Result0 = BinOpFunc(A, B)
+ then
+ ResultStr = int16_to_string(Result0)
+ catch_any _ ->
+ ResultStr = "<<exception>>"
+ ),
+ io.format("%s %s %s = %s\n",
+ [s(int16_to_string(A)), s(Desc), s(int16_to_string(B)), s(ResultStr)], !IO).
+
+:- func numbers = list(int16).
+
+numbers = [
+ -32_768_i16,
+ -128_i16,
+ 0_i16,
+ 1_i16,
+ 2_i16,
+ 8_i16,
+ 10_i16,
+ 16_i16,
+ 127_i16,
+ 255_i16,
+ 32_767_i16
+].
+
+%---------------------------------------------------------------------------%
+:- end_module arith_int16.
+%---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/arith_uint16.exp b/tests/hard_coded/arith_uint16.exp
index e69de29bb..3906dd344 100644
--- a/tests/hard_coded/arith_uint16.exp
+++ b/tests/hard_coded/arith_uint16.exp
@@ -0,0 +1,334 @@
+*** Test binary operation '+' ***
+
+0 + 0 = 0
+0 + 1 = 1
+0 + 2 = 2
+0 + 8 = 8
+0 + 10 = 10
+0 + 16 = 16
+0 + 255 = 255
+0 + 65535 = 65535
+1 + 0 = 1
+1 + 1 = 2
+1 + 2 = 3
+1 + 8 = 9
+1 + 10 = 11
+1 + 16 = 17
+1 + 255 = 256
+1 + 65535 = 0
+2 + 0 = 2
+2 + 1 = 3
+2 + 2 = 4
+2 + 8 = 10
+2 + 10 = 12
+2 + 16 = 18
+2 + 255 = 257
+2 + 65535 = 1
+8 + 0 = 8
+8 + 1 = 9
+8 + 2 = 10
+8 + 8 = 16
+8 + 10 = 18
+8 + 16 = 24
+8 + 255 = 263
+8 + 65535 = 7
+10 + 0 = 10
+10 + 1 = 11
+10 + 2 = 12
+10 + 8 = 18
+10 + 10 = 20
+10 + 16 = 26
+10 + 255 = 265
+10 + 65535 = 9
+16 + 0 = 16
+16 + 1 = 17
+16 + 2 = 18
+16 + 8 = 24
+16 + 10 = 26
+16 + 16 = 32
+16 + 255 = 271
+16 + 65535 = 15
+255 + 0 = 255
+255 + 1 = 256
+255 + 2 = 257
+255 + 8 = 263
+255 + 10 = 265
+255 + 16 = 271
+255 + 255 = 510
+255 + 65535 = 254
+65535 + 0 = 65535
+65535 + 1 = 0
+65535 + 2 = 1
+65535 + 8 = 7
+65535 + 10 = 9
+65535 + 16 = 15
+65535 + 255 = 254
+65535 + 65535 = 65534
+
+*** Test binary operation '-' ***
+
+0 - 0 = 0
+0 - 1 = 65535
+0 - 2 = 65534
+0 - 8 = 65528
+0 - 10 = 65526
+0 - 16 = 65520
+0 - 255 = 65281
+0 - 65535 = 1
+1 - 0 = 1
+1 - 1 = 0
+1 - 2 = 65535
+1 - 8 = 65529
+1 - 10 = 65527
+1 - 16 = 65521
+1 - 255 = 65282
+1 - 65535 = 2
+2 - 0 = 2
+2 - 1 = 1
+2 - 2 = 0
+2 - 8 = 65530
+2 - 10 = 65528
+2 - 16 = 65522
+2 - 255 = 65283
+2 - 65535 = 3
+8 - 0 = 8
+8 - 1 = 7
+8 - 2 = 6
+8 - 8 = 0
+8 - 10 = 65534
+8 - 16 = 65528
+8 - 255 = 65289
+8 - 65535 = 9
+10 - 0 = 10
+10 - 1 = 9
+10 - 2 = 8
+10 - 8 = 2
+10 - 10 = 0
+10 - 16 = 65530
+10 - 255 = 65291
+10 - 65535 = 11
+16 - 0 = 16
+16 - 1 = 15
+16 - 2 = 14
+16 - 8 = 8
+16 - 10 = 6
+16 - 16 = 0
+16 - 255 = 65297
+16 - 65535 = 17
+255 - 0 = 255
+255 - 1 = 254
+255 - 2 = 253
+255 - 8 = 247
+255 - 10 = 245
+255 - 16 = 239
+255 - 255 = 0
+255 - 65535 = 256
+65535 - 0 = 65535
+65535 - 1 = 65534
+65535 - 2 = 65533
+65535 - 8 = 65527
+65535 - 10 = 65525
+65535 - 16 = 65519
+65535 - 255 = 65280
+65535 - 65535 = 0
+
+*** Test binary operation '*' ***
+
+0 * 0 = 0
+0 * 1 = 0
+0 * 2 = 0
+0 * 8 = 0
+0 * 10 = 0
+0 * 16 = 0
+0 * 255 = 0
+0 * 65535 = 0
+1 * 0 = 0
+1 * 1 = 1
+1 * 2 = 2
+1 * 8 = 8
+1 * 10 = 10
+1 * 16 = 16
+1 * 255 = 255
+1 * 65535 = 65535
+2 * 0 = 0
+2 * 1 = 2
+2 * 2 = 4
+2 * 8 = 16
+2 * 10 = 20
+2 * 16 = 32
+2 * 255 = 510
+2 * 65535 = 65534
+8 * 0 = 0
+8 * 1 = 8
+8 * 2 = 16
+8 * 8 = 64
+8 * 10 = 80
+8 * 16 = 128
+8 * 255 = 2040
+8 * 65535 = 65528
+10 * 0 = 0
+10 * 1 = 10
+10 * 2 = 20
+10 * 8 = 80
+10 * 10 = 100
+10 * 16 = 160
+10 * 255 = 2550
+10 * 65535 = 65526
+16 * 0 = 0
+16 * 1 = 16
+16 * 2 = 32
+16 * 8 = 128
+16 * 10 = 160
+16 * 16 = 256
+16 * 255 = 4080
+16 * 65535 = 65520
+255 * 0 = 0
+255 * 1 = 255
+255 * 2 = 510
+255 * 8 = 2040
+255 * 10 = 2550
+255 * 16 = 4080
+255 * 255 = 65025
+255 * 65535 = 65281
+65535 * 0 = 0
+65535 * 1 = 65535
+65535 * 2 = 65534
+65535 * 8 = 65528
+65535 * 10 = 65526
+65535 * 16 = 65520
+65535 * 255 = 65281
+65535 * 65535 = 1
+
+*** Test binary operation '/' ***
+
+0 / 0 = <<exception>>
+0 / 1 = 0
+0 / 2 = 0
+0 / 8 = 0
+0 / 10 = 0
+0 / 16 = 0
+0 / 255 = 0
+0 / 65535 = 0
+1 / 0 = <<exception>>
+1 / 1 = 1
+1 / 2 = 0
+1 / 8 = 0
+1 / 10 = 0
+1 / 16 = 0
+1 / 255 = 0
+1 / 65535 = 0
+2 / 0 = <<exception>>
+2 / 1 = 2
+2 / 2 = 1
+2 / 8 = 0
+2 / 10 = 0
+2 / 16 = 0
+2 / 255 = 0
+2 / 65535 = 0
+8 / 0 = <<exception>>
+8 / 1 = 8
+8 / 2 = 4
+8 / 8 = 1
+8 / 10 = 0
+8 / 16 = 0
+8 / 255 = 0
+8 / 65535 = 0
+10 / 0 = <<exception>>
+10 / 1 = 10
+10 / 2 = 5
+10 / 8 = 1
+10 / 10 = 1
+10 / 16 = 0
+10 / 255 = 0
+10 / 65535 = 0
+16 / 0 = <<exception>>
+16 / 1 = 16
+16 / 2 = 8
+16 / 8 = 2
+16 / 10 = 1
+16 / 16 = 1
+16 / 255 = 0
+16 / 65535 = 0
+255 / 0 = <<exception>>
+255 / 1 = 255
+255 / 2 = 127
+255 / 8 = 31
+255 / 10 = 25
+255 / 16 = 15
+255 / 255 = 1
+255 / 65535 = 0
+65535 / 0 = <<exception>>
+65535 / 1 = 65535
+65535 / 2 = 32767
+65535 / 8 = 8191
+65535 / 10 = 6553
+65535 / 16 = 4095
+65535 / 255 = 257
+65535 / 65535 = 1
+
+*** Test binary operation 'rem' ***
+
+0 rem 0 = <<exception>>
+0 rem 1 = 0
+0 rem 2 = 0
+0 rem 8 = 0
+0 rem 10 = 0
+0 rem 16 = 0
+0 rem 255 = 0
+0 rem 65535 = 0
+1 rem 0 = <<exception>>
+1 rem 1 = 0
+1 rem 2 = 1
+1 rem 8 = 1
+1 rem 10 = 1
+1 rem 16 = 1
+1 rem 255 = 1
+1 rem 65535 = 1
+2 rem 0 = <<exception>>
+2 rem 1 = 0
+2 rem 2 = 0
+2 rem 8 = 2
+2 rem 10 = 2
+2 rem 16 = 2
+2 rem 255 = 2
+2 rem 65535 = 2
+8 rem 0 = <<exception>>
+8 rem 1 = 0
+8 rem 2 = 0
+8 rem 8 = 0
+8 rem 10 = 8
+8 rem 16 = 8
+8 rem 255 = 8
+8 rem 65535 = 8
+10 rem 0 = <<exception>>
+10 rem 1 = 0
+10 rem 2 = 0
+10 rem 8 = 2
+10 rem 10 = 0
+10 rem 16 = 10
+10 rem 255 = 10
+10 rem 65535 = 10
+16 rem 0 = <<exception>>
+16 rem 1 = 0
+16 rem 2 = 0
+16 rem 8 = 0
+16 rem 10 = 6
+16 rem 16 = 0
+16 rem 255 = 16
+16 rem 65535 = 16
+255 rem 0 = <<exception>>
+255 rem 1 = 0
+255 rem 2 = 1
+255 rem 8 = 7
+255 rem 10 = 5
+255 rem 16 = 15
+255 rem 255 = 0
+255 rem 65535 = 255
+65535 rem 0 = <<exception>>
+65535 rem 1 = 0
+65535 rem 2 = 1
+65535 rem 8 = 7
+65535 rem 10 = 5
+65535 rem 16 = 15
+65535 rem 255 = 0
+65535 rem 65535 = 0
diff --git a/tests/hard_coded/arith_uint16.m b/tests/hard_coded/arith_uint16.m
index e69de29bb..eaa709880 100644
--- a/tests/hard_coded/arith_uint16.m
+++ b/tests/hard_coded/arith_uint16.m
@@ -0,0 +1,82 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+
+% Test arithmetic operations for unsigned 16-bit integers.
+
+:- module arith_uint16.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module uint16.
+
+:- import_module exception.
+:- import_module list.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+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((func(X, Y) = X / Y), "/", !IO),
+ io.nl(!IO),
+ run_binop_test((func(X, Y) = X rem Y), "rem", !IO).
+
+:- pred run_binop_test((func(uint16, uint16) = uint16)::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(uint16, uint16) = uint16)::in, string::in,
+ list(uint16)::in, uint16::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(uint16, uint16) = uint16)::in, string::in,
+ uint16::in, uint16::in, io::di, io::uo) is cc_multi.
+
+run_binop_test_3(BinOpFunc, Desc, A, B, !IO) :-
+ ( try []
+ Result0 = BinOpFunc(A, B)
+ then
+ ResultStr = uint16_to_string(Result0)
+ catch_any _ ->
+ ResultStr = "<<exception>>"
+ ),
+ io.format("%s %s %s = %s\n",
+ [s(uint16_to_string(A)), s(Desc), s(uint16_to_string(B)), s(ResultStr)], !IO).
+
+:- func numbers = list(uint16).
+
+numbers = [
+ 0_u16,
+ 1_u16,
+ 2_u16,
+ 8_u16,
+ 10_u16,
+ 16_u16,
+ 255_u16,
+ 65_535_u16
+].
+
+%---------------------------------------------------------------------------%
+:- end_module arith_uint16.
+%---------------------------------------------------------------------------%
More information about the reviews
mailing list