Files
mercury/tests/hard_coded/from_int_int32.m
Julien Fischer 2f39008fbf Add tests of int -> {int32,uint32} conversion.
Fix a bug in int -> uint32 conversion for the C grades.

tests/Mmakefile:
tests/hard_coded/from_int_int32.{m,exp}:
tests/hard_coded/from_int_uint32.m:
     Add the above tests.

tests/hard_coded/from_int_uint32.exp:
     Expected output for machines with a 64-bit int type.

tests/hard_coded/from_int_uint32.exp2:
     Expected output for machines with a 32-bit int type.

library/uint32.m:
     Fix a bug in the definition of from_int/2.  On machines where Mercury has
     a 64-bit int type the conversion should succeed for inputs up to (and
     including) UINT32_MAX.
2017-09-13 00:53:17 -04:00

66 lines
1.5 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
% Test converson of Mercury ints to signed 32-bit integers.
:- module from_int_int32.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int32.
:- 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(IntStr, !IO) :-
io.format("from_int(%s) = ", [s(IntStr)], !IO),
( if
string.to_int(IntStr, Int),
int32.from_int(Int, Int32)
then
io.format("%s\n", [s(int32_to_string(Int32))], !IO)
else
io.write_string("<<out-of-range>>\n", !IO)
).
:- func numbers = list(string).
numbers = [
"-9223372036854775808",
"-2147483649",
"-2147483648",
"-32768",
"-128",
"0",
"1",
"2",
"8",
"10",
"16",
"127",
"32767",
"2147483647",
"2147483648",
"9223372036854775807"
].
%---------------------------------------------------------------------------%
:- end_module from_int_int32.
%---------------------------------------------------------------------------%