Files
mercury/tests/hard_coded/division_test.m
Julien Fischer 1f6d83692a Update programming style in tests/hard_coded.
tests/hard_coded/*.m:
    Update programming style, unless doing so would change
    the meaning of the test, in particular:

    - use '.' as a module qualifier in place of '__'
    - use {write,print}_line where appropriate
    - use if-then-else in place of C -> T ; E
    - use state variables in place of DCGs

tests/hard_coded/dir_test.m:
    Document what the expected outputs correspond to.

    Use a uniform module qualifier in the output.

tests/hard_coded/dir_test.exp*:
    Conform to the above change.
2021-01-07 13:58:12 +11:00

87 lines
2.7 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% test the handling of `//', rem, div, and mod.
% the first pair should truncate towards zero.
% the second pair should truncate towards negative infinity.
:- module division_test.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
main(!IO) :-
( if
quot_test(3, 8, 0, 3), % 3 / 8 = 0 + 3/8
quot_test(5, 8, 0, 5), % 5 / 8 = 0 + 5/8
quot_test(7, 2, 3, 1), % 7 / 2 = 3 + 1/2
quot_test(100, 13, 7, 9) % 100 / 13 = 7 + 9/13
then
io.write_string("`//' test succeeded\n", !IO)
else
io.write_string("`//' test failed\n", !IO)
),
( if
rem_test(3, 8, 0, 3), % 3 / 8 = 0 + 3/8
rem_test(5, 8, 0, 5), % 5 / 8 = 0 + 5/8
rem_test(7, 2, 3, 1), % 7 / 2 = 3 + 1/2
rem_test(100, 13, 7, 9) % 100 / 13 = 7 + 9/13
then
io.write_string("rem test succeeded\n", !IO)
else
io.write_string("rem test failed\n", !IO)
),
( if
div_test(3, 8, 0, 3), % 3 / 8 = 0 + 3/8
div_test(5, 8, 0, 5), % 5 / 8 = 0 + 5/8
div_test(7, 2, 3, 1), % 7 / 2 = 3 + 1/2
div_test(100, 13, 7, 9) % 100 / 13 = 7 + 9/13
then
io.write_string("div test succeeded\n", !IO)
else
io.write_string("div test failed\n", !IO)
),
( if
mod_test(3, 8, 0, 3), % 3 / 8 = 0 + 3/8
mod_test(5, 8, 0, 5), % 5 / 8 = 0 + 5/8
mod_test(7, 2, 3, 1), % 7 / 2 = 3 + 1/2
mod_test(100, 13, 7, 9) % 100 / 13 = 7 + 9/13
then
io.write_string("mod test succeeded\n", !IO)
else
io.write_string("mod test failed\n", !IO)
).
:- pred quot_test(int::in, int::in, int::in, int::in) is semidet.
quot_test(Num, Div, Quot, _Rem) :-
Num // Div = Quot,
(-Num) // Div = -Quot,
(-Num) // (-Div) = Quot,
Num // (-Div) = -Quot.
:- pred rem_test(int::in, int::in, int::in, int::in) is semidet.
rem_test(Num, Div, _Quot, Rem) :-
Num rem Div = Rem,
(-Num) rem Div = -Rem,
(-Num) rem (-Div) = -Rem,
Num rem (-Div) = Rem.
:- pred div_test(int::in, int::in, int::in, int::in) is semidet.
div_test(Num, Div, Quot, _Rem) :-
Num div Div = Quot,
(-Num) div Div = -Quot - 1,
(-Num) div (-Div) = Quot,
Num div (-Div) = -Quot - 1.
:- pred mod_test(int::in, int::in, int::in, int::in) is semidet.
mod_test(Num, Div, _Quot, Rem) :-
Num mod Div = Rem,
(-Num) mod Div = Div - Rem,
(-Num) mod (-Div) = -Rem,
Num mod (-Div) = -(Div - Rem).