Files
mercury/tests/hard_coded/division_test.m
Zoltan Somogyi 33eb3028f5 Clean up the tests in half the test directories.
tests/accumulator/*.m:
tests/analysis_*/*.m:
tests/benchmarks*/*.m:
tests/debugger*/*.{m,exp,inp}:
tests/declarative_debugger*/*.{m,exp,inp}:
tests/dppd*/*.m:
tests/exceptions*/*.m:
tests/general*/*.m:
tests/grade_subdirs*/*.m:
tests/hard_coded*/*.m:
    Make these tests use four-space indentation, and ensure that
    each module is imported on its own line. (I intend to use the latter
    to figure out which subdirectories' tests can be executed in parallel.)

    These changes usually move code to different lines. For the debugger tests,
    specify the new line numbers in .inp files and expect them in .exp files.
2015-02-14 20:14:03 +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__state::di, io__state::uo) is det.
:- implementation.
:- import_module int.
main -->
(
{ 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
->
io__write_string("`//' test succeeded\n")
;
io__write_string("`//' test failed\n")
),
(
{ 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
->
io__write_string("rem test succeeded\n")
;
io__write_string("rem test failed\n")
),
(
{ 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
->
io__write_string("div test succeeded\n")
;
io__write_string("div test failed\n")
),
(
{ 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
->
io__write_string("mod test succeeded\n")
;
io__write_string("mod test failed\n")
).
:- 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).