mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-11 03:45:33 +00:00
87 lines
2.5 KiB
Mathematica
87 lines
2.5 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
|
|
%---------------------------------------------------------------------------%
|
|
|
|
% Test arithmetic operations for unsigned 64-bit integers.
|
|
|
|
:- module arith_uint64.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is cc_multi.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module uint64.
|
|
|
|
:- 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(uint64, uint64) = uint64)::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(uint64, uint64) = uint64)::in, string::in,
|
|
list(uint64)::in, uint64::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(uint64, uint64) = uint64)::in, string::in,
|
|
uint64::in, uint64::in, io::di, io::uo) is cc_multi.
|
|
|
|
run_binop_test_3(BinOpFunc, Desc, A, B, !IO) :-
|
|
( try []
|
|
Result0 = BinOpFunc(A, B)
|
|
then
|
|
ResultStr = uint64_to_string(Result0)
|
|
catch_any _ ->
|
|
ResultStr = "<<exception>>"
|
|
),
|
|
io.format("%s %s %s = %s\n",
|
|
[s(uint64_to_string(A)), s(Desc), s(uint64_to_string(B)),
|
|
s(ResultStr)], !IO).
|
|
|
|
:- func numbers = list(uint64).
|
|
|
|
numbers = [
|
|
0_u64,
|
|
1_u64,
|
|
2_u64,
|
|
8_u64,
|
|
10_u64,
|
|
16_u64,
|
|
255_u64,
|
|
65_535_u64,
|
|
2_147_483_647_u64,
|
|
4_294_967_295_u64,
|
|
18_446_744_073_709_551_615_u64
|
|
].
|
|
|
|
%---------------------------------------------------------------------------%
|
|
:- end_module arith_uint64.
|
|
%---------------------------------------------------------------------------%
|