[m-rev.] diff: fix a bug in uin64.cast_from_uint/1

Julien Fischer jfischer at opturion.com
Sat Jul 10 04:42:50 AEST 2021


Fix a bug in uint64.cast_from_uint/1.

Add a test of uint -> uint64 conversion.

library/uint64.m:
     Fix a typo that broke uint64.cast_from_uint/1.

tests/hard_coded/Mmakefile:
tests/hard_coded/from_uint_uint64.{m,exp,exp2}:
     Add a test of uint -> uint64 conversion.

tests/hard_coded/from_int_int*.m:
tests/hard_coded/from_uint_uint*.m:
     Fix errors in comments.

Julien.

diff --git a/library/uint64.m b/library/uint64.m
index 50e4bd2..0f81082 100644
--- a/library/uint64.m
+++ b/library/uint64.m
@@ -520,7 +520,7 @@ det_from_int(I) = U64 :-
      cast_from_uint(U::in) = (U64::out),
      [will_not_call_mercury, promise_pure, thread_safe],
  "
-    U64 = (long) U ^ 0xffffffffL;
+    U64 = (long) U & 0xffffffffL;
  ").

  %---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 78d9e98..5c28003 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -164,6 +164,7 @@ ORDINARY_PROGS = \
  	from_int_uint8 \
  	from_uint_uint16 \
  	from_uint_uint32 \
+	from_uint_uint64 \
  	from_uint_uint8 \
  	func_and_pred \
  	func_ctor_ambig \
diff --git a/tests/hard_coded/from_int_int16.m b/tests/hard_coded/from_int_int16.m
index c2c6d66..4d72dde 100644
--- a/tests/hard_coded/from_int_int16.m
+++ b/tests/hard_coded/from_int_int16.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test converson of Mercury ints to signed 16-bit integers.
+% Test conversion of ints to signed 16-bit integers.

  :- module from_int_int16.
  :- interface.
diff --git a/tests/hard_coded/from_int_int32.m b/tests/hard_coded/from_int_int32.m
index 3895fc7..8bebac9 100644
--- a/tests/hard_coded/from_int_int32.m
+++ b/tests/hard_coded/from_int_int32.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test converson of Mercury ints to signed 32-bit integers.
+% Test conversion of ints to signed 32-bit integers.

  :- module from_int_int32.
  :- interface.
diff --git a/tests/hard_coded/from_int_int64.m b/tests/hard_coded/from_int_int64.m
index 813e7b2..cf73b42 100644
--- a/tests/hard_coded/from_int_int64.m
+++ b/tests/hard_coded/from_int_int64.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test conversion of Mercury ints to signed 64-bit integers.
+% Test conversion of ints to signed 64-bit integers.
  % The .exp file is for systems where int is 64-bit.
  % The .exp2 file is for systems where int is 32-bit.

diff --git a/tests/hard_coded/from_int_int8.m b/tests/hard_coded/from_int_int8.m
index 050f0f1..aa1e2f3 100644
--- a/tests/hard_coded/from_int_int8.m
+++ b/tests/hard_coded/from_int_int8.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test converson of Mercury ints to signed 8-bit integers.
+% Test conversion of ints to signed 8-bit integers.

  :- module from_int_int8.
  :- interface.
diff --git a/tests/hard_coded/from_int_uint16.m b/tests/hard_coded/from_int_uint16.m
index 9497638..0aa1e1e 100644
--- a/tests/hard_coded/from_int_uint16.m
+++ b/tests/hard_coded/from_int_uint16.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test conversion of Mercury ints to unsigned 16-bit integers.
+% Test conversion of uints to unsigned 16-bit integers.

  :- module from_int_uint16.
  :- interface.
diff --git a/tests/hard_coded/from_int_uint32.m b/tests/hard_coded/from_int_uint32.m
index 11f48de..33ba082 100644
--- a/tests/hard_coded/from_int_uint32.m
+++ b/tests/hard_coded/from_int_uint32.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%
  %
-% Test conversion of Mercury ints to unsigned 32-bit integers.
+% Test conversion of ints to unsigned 32-bit integers.
  %
  % The .exp file is for when int is 64-bit.
  % The .exp2 file is for when int is 32-bit.
@@ -60,7 +60,6 @@ numbers = [
      "16",
      "127",
      "32767",
-     % The next two will only work on machines where 'int' is 64-bit.
      "2147483647",  % INT32_MAX
      "2147483648",  % INT32_MAX + 1
      "4294967295",  % UINT32_MAX
diff --git a/tests/hard_coded/from_int_uint8.m b/tests/hard_coded/from_int_uint8.m
index a57fc95..5599d9d 100644
--- a/tests/hard_coded/from_int_uint8.m
+++ b/tests/hard_coded/from_int_uint8.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test conversion of Mercury ints to unsigned 8-bit integers.
+% Test conversion of ints to unsigned 8-bit integers.

  :- module from_int_uint8.
  :- interface.
diff --git a/tests/hard_coded/from_uint_uint16.m b/tests/hard_coded/from_uint_uint16.m
index cb3bb9b..ebd5d7f 100644
--- a/tests/hard_coded/from_uint_uint16.m
+++ b/tests/hard_coded/from_uint_uint16.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test conversion of Mercury ints to unsigned 16-bit integers.
+% Test conversion of uints to unsigned 16-bit integers.

  :- module from_uint_uint16.
  :- interface.
diff --git a/tests/hard_coded/from_uint_uint32.m b/tests/hard_coded/from_uint_uint32.m
index f5dd575..8a14e2d 100644
--- a/tests/hard_coded/from_uint_uint32.m
+++ b/tests/hard_coded/from_uint_uint32.m
@@ -2,10 +2,10 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%
  %
-% Test conversion of Mercury ints to unsigned 32-bit integers.
+% Test conversion of uints to unsigned 32-bit integers.
  %
-% The .exp file is for when int is 64-bit.
-% The .exp2 file is for when int is 32-bit.
+% The .exp file is for when uint is 64-bit.
+% The .exp2 file is for when uint is 32-bit.
  %
  %---------------------------------------------------------------------------%

@@ -55,8 +55,6 @@ numbers = [
      "16",
      "127",
      "32767",
-     % The next two will only work on machines where 'int' is 64-bit.
-     % XXX two?
      "2147483647",  % INT32_MAX
      "2147483648",  % INT32_MAX + 1
      "4294967295",  % UINT32_MAX
diff --git a/tests/hard_coded/from_uint_uint64.exp b/tests/hard_coded/from_uint_uint64.exp
index e69de29..3770eeb 100644
--- a/tests/hard_coded/from_uint_uint64.exp
+++ b/tests/hard_coded/from_uint_uint64.exp
@@ -0,0 +1,15 @@
+from_uint(0) = 0
+from_uint(1) = 1
+from_uint(2) = 2
+from_uint(8) = 8
+from_uint(10) = 10
+from_uint(16) = 16
+from_uint(127) = 127
+from_uint(32767) = 32767
+from_uint(2147483647) = 2147483647
+from_uint(2147483648) = 2147483648
+from_uint(4294967295) = 4294967295
+from_uint(4294967296) = 4294967296
+from_uint(9223372036854775807) = 9223372036854775807
+from_uint(9223372036854775808) = 9223372036854775808
+from_uint(18446744073709551615) = 18446744073709551615
diff --git a/tests/hard_coded/from_uint_uint64.exp2 b/tests/hard_coded/from_uint_uint64.exp2
index e69de29..b6c43b9 100644
--- a/tests/hard_coded/from_uint_uint64.exp2
+++ b/tests/hard_coded/from_uint_uint64.exp2
@@ -0,0 +1,15 @@
+from_uint(0) = 0
+from_uint(1) = 1
+from_uint(2) = 2
+from_uint(8) = 8
+from_uint(10) = 10
+from_uint(16) = 16
+from_uint(127) = 127
+from_uint(32767) = 32767
+from_uint(2147483647) = 2147483647
+from_uint(2147483648) = 2147483648
+from_uint(4294967295) = 4294967295
+from_uint(4294967296) = <<out-of-range>>
+from_uint(9223372036854775807) = <<out-of-range>>
+from_uint(9223372036854775808) = <<out-of-range>>
+from_uint(18446744073709551615) = <<out-of-range>>
diff --git a/tests/hard_coded/from_uint_uint64.m b/tests/hard_coded/from_uint_uint64.m
index e69de29..e40ca0a 100644
--- a/tests/hard_coded/from_uint_uint64.m
+++ b/tests/hard_coded/from_uint_uint64.m
@@ -0,0 +1,69 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+%
+% Test conversion of uints to unsigned 64-bit integers.
+%
+% The .exp file is for when int is 64-bit.
+% The .exp2 file is for when int is 32-bit.
+%
+%---------------------------------------------------------------------------%
+
+:- module from_uint_uint64.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module uint64.
+
+:- import_module list.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+    list.foldl(do_test, numbers, !IO).
+
+:- pred do_test(string::in, io::di, io::uo) is det.
+
+do_test(UIntStr, !IO) :-
+    io.format("from_uint(%s) = ", [s(UIntStr)], !IO),
+    ( if
+        string.to_uint(UIntStr, UInt),
+        UInt64 = uint64.cast_from_uint(UInt)
+    then
+        io.format("%u\n", [u64(UInt64)], !IO)
+    else
+        io.write_string("<<out-of-range>>\n", !IO)
+    ).
+
+:- func numbers = list(string).
+
+numbers = [
+    "0",
+    "1",
+    "2",
+    "8",
+    "10",
+    "16",
+    "127",
+    "32767",
+    "2147483647",          % INT32_MAX
+    "2147483648",          % INT32_MAX + 1
+    "4294967295",          % UINT32_MAX
+    "4294967296",          % UINT32_MAX + 1
+    "9223372036854775807", % INT64_MAX
+    "9223372036854775808", % INT64_MAX + 1
+    "18446744073709551615" % UINT64_MAX
+].
+
+%---------------------------------------------------------------------------%
+:- end_module from_uint_uint64.
+%---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/from_uint_uint8.m b/tests/hard_coded/from_uint_uint8.m
index 27a6bf2..cabc9c3 100644
--- a/tests/hard_coded/from_uint_uint8.m
+++ b/tests/hard_coded/from_uint_uint8.m
@@ -2,7 +2,7 @@
  % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
  %---------------------------------------------------------------------------%

-% Test conversion of Mercury ints to unsigned 8-bit integers.
+% Test conversion of uints to unsigned 8-bit integers.

  :- module from_uint_uint8.
  :- interface.


More information about the reviews mailing list