1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    2% Tests for arithmetic predicates and operators
    3
    4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    5
    6
    7%
    8% List of test suites
    9%
   10
   11test_suites([test_is, test_arith, test_plus, test_minus, test_times,
   12             test_division, test_mod, test_floor, test_round, test_ceiling,
   13             test_truncate, test_floatarith, test_abs, test_sqrt,
   14             test_power]).
   15
   16
   17%
   18% is/2 test
   19% - ISO -
   20%
   21
   22test_is_1(Result) :- 'is'(Result, 3 + 11.0).
   23test_is_2(X, Y) :- X = 1 + 2, Y is X * 3.
   24test_is_3 :- X = foo, 'is'(X, 77).
   25test_is_4(N) :- 'is'(77, N).
   26
   27throws_exception(test_is_4).
   28
   29
   30%
   31% =:=/2, =\=/2, >/2, </2, >=/2 and =</2 tests
   32% - ISO -
   33%
   34
   35test_arith_1 :- '=:='(0, 1).
   36test_arith_2 :- '=\\='(0, 1).
   37test_arith_3 :- '<'(0, 1).
   38test_arith_4 :- '>'(0, 1).
   39test_arith_5 :- '>='(0, 1).
   40test_arith_6 :- '=<'(0, 1).
   41test_arith_7 :- '=:='(1.0, 1).
   42test_arith_8 :- '=\\='(1.0, 1).
   43test_arith_9 :- '<'(1.0, 1).
   44test_arith_10 :- '>'(1.0, 1).
   45test_arith_11 :- '>='(1.0, 1).
   46test_arith_12 :- '=<'(1.0, 1).
   47test_arith_13 :- '=:='(3 * 2, 7 - 1).
   48test_arith_14 :- '=\\='(3 * 2, 7 - 1).
   49test_arith_15 :- '<'(3 * 2, 7 - 1).
   50test_arith_16 :- '>'(3 * 2, 7 - 1).
   51test_arith_17 :- '>='(3 * 2, 7 - 1).
   52test_arith_18 :- '=<'(3 * 2, 7 - 1).
   53test_arith_19(X) :- '=:='(X, 5).
   54test_arith_20(X) :- '=\\='(X, 5).
   55test_arith_21(X) :- '<'(X, 5).
   56test_arith_22(X) :- '>'(X, 5).
   57test_arith_23(X) :- '>='(X, 5).
   58test_arith_24(X) :- '=<'(X, 5).
   59
   60throws_exception(test_arith_19).
   61throws_exception(test_arith_20).
   62throws_exception(test_arith_21).
   63throws_exception(test_arith_22).
   64throws_exception(test_arith_23).
   65throws_exception(test_arith_24).
   66
   67test_arith_1b :- 0 =:= 1.
   68test_arith_2b :- 0 =\= 1.
   69test_arith_3b :- 0 < 1.
   70test_arith_4b :- 0 > 1.
   71test_arith_5b :- 0 >= 1.
   72test_arith_6b :- 0 =< 1.
   73test_arith_7b :- 1.0 =:= 1.
   74test_arith_8b :- 1.0 =\= 1.
   75test_arith_9b :- 1.0 < 1.
   76test_arith_10b :- 1.0 > 1.
   77test_arith_11b :- 1.0 >= 1.
   78test_arith_12b :- 1.0 =< 1.
   79test_arith_13b :- 3 * 2 =:= 7 - 1.
   80test_arith_14b :- 3 * 2 =\= 7 - 1.
   81test_arith_15b :- 3 * 2 < 7 - 1.
   82test_arith_16b :- 3 * 2 > 7 - 1.
   83test_arith_17b :- 3 * 2 >= 7 - 1.
   84test_arith_18b :- 3 * 2 =< 7 - 1.
   85test_arith_19b(X) :- X =:= 5.
   86test_arith_20b(X) :- X =\= 5.
   87test_arith_21b(X) :- X < 5.
   88test_arith_22b(X) :- X > 5.
   89test_arith_23b(X) :- X >= 5.
   90test_arith_24b(X) :- X =< 5.
   91
   92throws_exception(test_arith_19b).
   93throws_exception(test_arith_20b).
   94throws_exception(test_arith_21b).
   95throws_exception(test_arith_22b).
   96throws_exception(test_arith_23b).
   97throws_exception(test_arith_24b).
   98
   99
  100%
  101% +/2 test
  102% - ISO -
  103%
  104
  105test_plus_1(X) :- X is '+'(7, 35).
  106test_plus_2(X) :- X is '+'(0, 3 + 11).
  107test_plus_3(X) :- X is '+'(0, 3.2 + 11).
  108test_plus_4(X, N) :- X is '+'(77, N).
  109test_plus_5(X) :- Y = foo, X is '+'(Y, 77).
  110
  111throws_exception(test_plus_4).
  112throws_exception(test_plus_5).
  113
  114test_plus_1b(X) :- X is 7 + 35.
  115test_plus_2b(X) :- X is 0 + (3 + 11).
  116test_plus_3b(X) :- X is 0 + (3.2 + 11).
  117test_plus_4b(X, N) :- X is 77 + N.
  118test_plus_5b(X) :- Y = foo, X is Y + 77.
  119
  120throws_exception(test_plus_4b).
  121throws_exception(test_plus_5b).
  122
  123% The following tests aren't included because the 'max_integer' flag isn't
  124% available in all versions of SWI-Prolog, so the tests may fail or throw
  125% an exception depending on the installed version of SWI-Prolog
  126%
  127%test_plus_X1(MI, X) :- current_prolog_flag(max_integer, MI), X is '+'(MI, 1).
  128%test_plus_X1b(MI, X) :- current_prolog_flag(max_integer, MI), X is MI + 1.
  129
  130
  131%
  132% -/2 test
  133% - ISO -
  134%
  135
  136test_minus_1(X) :- X is '-'(7).
  137test_minus_2(X) :- X is '-'(3 - 11).
  138test_minus_3(X) :- X is '-'(3.2 - 11).
  139test_minus_4(X, N) :- X is '-'(N).
  140test_minus_5(X) :- Y = foo, X is '-'(Y).
  141test_minus_6(X) :- X is '-'(7, 35).
  142test_minus_7(X) :- X is '-'(20, 3 + 11).
  143test_minus_8(X) :- X is '-'(0, 3.2 + 11).
  144test_minus_9(X, N) :- X is '-'(77, N).
  145test_minus_10(X) :- Y = foo, X is '-'(Y, 77).
  146
  147throws_exception(test_minus_4).
  148throws_exception(test_minus_5).
  149throws_exception(test_minus_9).
  150throws_exception(test_minus_10).
  151
  152test_minus_1b(X) :- X is -7.
  153test_minus_2b(X) :- X is -(3 - 11).
  154test_minus_3b(X) :- X is -(3.2 - 11).
  155test_minus_4b(X, N) :- X is -N.
  156test_minus_5b(X) :- Y = foo, X is -Y.
  157test_minus_6b(X) :- X is 7 - 35.
  158test_minus_7b(X) :- X is 20 - (3 + 11).
  159test_minus_8b(X) :- X is 0 - (3.2 + 11).
  160test_minus_9b(X, N) :- X is 77 - N.
  161test_minus_10b(X) :- Y = foo, X is Y - 77.
  162
  163throws_exception(test_minus_4b).
  164throws_exception(test_minus_5b).
  165throws_exception(test_minus_9b).
  166throws_exception(test_minus_10b).
  167
  168% The following tests aren't included because the 'max_integer' flag isn't
  169% available in all versions of SWI-Prolog, so the tests may fail or throw
  170% an exception depending on the installed version of SWI-Prolog
  171%
  172%test_minus_X1(MI, X) :- current_prolog_flag(max_integer, MI), X is '-'(-1, MI).
  173%test_minus_X1b(MI, X) :- current_prolog_flag(max_integer, MI), X is -1 - MI.
  174
  175
  176%
  177% */2 test
  178% - ISO -
  179%
  180
  181test_times_1(X) :- X is '*'(7, 35).
  182test_times_2(X) :- X is '*'(0, 3 + 11).
  183test_times_3(X) :- X is '*'(1.5, 3.2 + 11).
  184test_times_4(X, N) :- X is '*'(77, N).
  185test_times_5(X) :- Y = foo, X is '*'(Y, 77).
  186
  187throws_exception(test_times_4).
  188throws_exception(test_times_5).
  189
  190test_times_1b(X) :- X is 7 * 35.
  191test_times_2b(X) :- X is 0 * (3 + 11).
  192test_times_3b(X) :- X is 1.5 * (3.2 + 11).
  193test_times_4b(X, N) :- X is 77 * N.
  194test_times_5b(X) :- Y = foo, X is Y * 77.
  195
  196throws_exception(test_times_4b).
  197throws_exception(test_times_5b).
  198
  199% The following tests aren't included because the 'max_integer' flag isn't
  200% available in all versions of SWI-Prolog, so the tests may fail or throw
  201% an exception depending on the installed version of SWI-Prolog
  202%
  203%test_times_X1(MI, X) :- current_prolog_flag(max_integer, MI), X is '*'(MI, 2).
  204%test_times_6b(MI, X) :- current_prolog_flag(max_integer, MI), X is MI * 2.
  205
  206
  207%
  208% '/'/2 test
  209% - ISO -
  210%
  211
  212test_division_1(X) :- X is '/'(7, 35).
  213test_division_2(X) :- X is '/'(7.0, 35).
  214test_division_3(X) :- X is '/'(140, 3 + 11).
  215test_division_4(X) :- X is '/'(20.1644, 3.2 + 11).
  216test_division_5(X) :- X is '/'(7, -3). % Implementation dependent
  217test_division_6(X) :- X is '/'(-7, 3). % Implementation dependent
  218test_division_7(X, N) :- X is '/'(77, N).
  219test_division_8(X) :- Y = foo, X is '/'(Y, 77).
  220test_division_9(X) :- X is '/'(3, 0).
  221
  222throws_exception(test_division_7).
  223throws_exception(test_division_8).
  224throws_exception(test_division_9).
  225
  226test_division_1b(X) :- X is 7 / 35.
  227test_division_2b(X) :- X is 7.0 / 35.
  228test_division_3b(X) :- X is 140 / (3 + 11).
  229test_division_4b(X) :- X is 20.1644 / (3.2 + 11).
  230test_division_5b(X) :- X is 7 / -3. % Implementation dependent
  231test_division_6b(X) :- X is -7 / 3. % Implementation dependent
  232test_division_7b(X, N) :- X is 77 / N.
  233test_division_8b(X) :- Y = foo, X is Y / 77.
  234test_division_9b(X) :- X is 3 / 0.
  235
  236throws_exception(test_division_7b).
  237throws_exception(test_division_8b).
  238throws_exception(test_division_9b).
  239
  240
  241%
  242% mod/2 test
  243% - ISO -
  244%
  245
  246test_mod_1(X) :- X is mod(7, 3).
  247test_mod_2(X) :- X is mod(0, 3 + 11).
  248test_mod_3(X, N) :- X is mod(77, N).
  249test_mod_4(X) :- Y = foo, X is mod(Y, 77).
  250test_mod_5(X) :- X is mod(7.5, 2).
  251test_mod_6(X) :- X is mod(7, 0).
  252test_mod_7(X) :- X is mod(7, -2).
  253
  254throws_exception(test_mod_3).
  255throws_exception(test_mod_4).
  256throws_exception(test_mod_5).
  257throws_exception(test_mod_6).
  258%throws_exception(test_mod_7). % Should throw an exception according to
  259                               % 2nd draft of ISO Prolog Standard
  260
  261
  262%
  263% floor/1 test
  264% - ISO -
  265%
  266
  267test_floor_1(X) :- X is floor(7.4).
  268test_floor_2(X) :- X is floor(-0.4).
  269
  270
  271%
  272% round/1 test
  273% - ISO -
  274%
  275
  276test_round_1(X) :- X is round(7.5).
  277test_round_2(X) :- X is round(7.6).
  278test_round_3(X) :- X is round(-0.6).
  279test_round_4(X, N) :- X is round(N).
  280
  281throws_exception(test_round_4).
  282
  283
  284%
  285% ceiling/1 test
  286% - ISO -
  287%
  288
  289test_ceiling_1(X) :- X is ceiling(-0.5).
  290
  291
  292%
  293% truncate/1 test
  294% - ISO -
  295%
  296
  297test_truncate_1(X) :- X is truncate(-0.5).
  298test_truncate_2(X) :- Y = foo, X is truncate(Y).
  299
  300throws_exception(test_truncate_2).
  301
  302
  303%
  304% float/1 test
  305% - ISO -
  306%
  307
  308test_floatarith_1(X) :- X is float(7).
  309test_floatarith_2(X) :- X is float(7.3).
  310test_floatarith_3(X) :- X is float(5 / 3).
  311test_floatarith_4(X, N) :- X is float(N).
  312test_floatarith_5(X) :- Y = foo, X is float(Y).
  313
  314throws_exception(test_floatarith_4).
  315throws_exception(test_floatarith_5).
  316
  317% The following test isn't included because the 'max_integer' flag isn't
  318% available in all versions of SWI-Prolog, so the test may fail or throw
  319% an exception depending on the installed version of SWI-Prolog
  320%
  321%test_floatarith_X1(MI, R, X) :- current_prolog_flag(max_integer, MI),
  322%                                R is float(MI) * 2, X is floor(R).
  323
  324
  325%
  326% abs/1 test
  327% - ISO -
  328%
  329
  330test_abs_1(X) :- X is abs(7).
  331test_abs_2(X) :- X is abs(3 - 11).
  332test_abs_3(X) :- X is abs(3.2 - 11.0).
  333test_abs_4(X, N) :- X is abs(N).
  334test_abs_5(X) :- Y = foo, X is abs(Y).
  335
  336throws_exception(test_abs_4).
  337throws_exception(test_abs_5).
  338
  339
  340%
  341% sqrt/1 test
  342% - ISO -
  343%
  344
  345test_sqrt_1(X) :- X is sqrt(0.0).
  346test_sqrt_2(X) :- X is sqrt(4.0).
  347test_sqrt_3(X) :- X is sqrt(0).
  348test_sqrt_4(X) :- X is sqrt(1.0).
  349test_sqrt_5(X, N) :- X is sqrt(N).
  350test_sqrt_6(X) :- Y = foo, X is sqrt(Y).
  351test_sqrt_7(X) :- X is sqrt(-1.0).
  352
  353throws_exception(test_sqrt_5).
  354throws_exception(test_sqrt_6).
  355throws_exception(test_sqrt_7).
  356
  357
  358%
  359% **/2 test
  360% - ISO -
  361%
  362
  363test_power_1(X) :- X is '**'(5, 3).
  364test_power_2(X) :- X is '**'(-5.0, 3).
  365test_power_3(X) :- X is '**'(5, -1).
  366test_power_4(X, N) :- X is '**'(77, N).
  367test_power_5(X) :- Y = foo, X is '**'(Y, 2).
  368test_power_6(X) :- X is '**'(5, 3.0).
  369test_power_7(X) :- X is '**'(0.0, 0).
  370
  371throws_exception(test_power_4).
  372throws_exception(test_power_5).
  373
  374test_power_1b(X) :- X is 5 ** 3.
  375test_power_2b(X) :- X is -5.0 ** 3.
  376test_power_3b(X) :- X is 5 ** -1.
  377test_power_4b(X, N) :- X is 77 ** N.
  378test_power_5b(X) :- Y = foo, X is Y ** 2.
  379test_power_6b(X) :- X is 5 ** 3.0.
  380test_power_7b(X) :- X is 0.0 ** 0.
  381
  382throws_exception(test_power_4b).
  383throws_exception(test_power_5b)