Simple arithmetic functors

The basic arithmetic functors are defined as part of the evaluable functors which shall be implemented by a standard-conforming Prolog processor. Each evaluable functor corresponds to one or more operations according to the types of the values which are obtained by evaluating the argument(s) of the functor.

The following list identifies the simple arithmetic functors defined as part of the standard:

Note that the symbols between parentheses are infix predefined operators.

The following list identifies the functors actually exercised by this test:

Example tests

Let's start with some simple tests verifying the value of single expressions.

First of all, let's start an appropriate fixture.

fit.ActionFixture
start jTrolog.engine.EvaluationFixture

Then, ask the engine to evaluate an expression and verify the result.

jTrolog.engine.PrologActionFixture
enter evaluable '+'(7, 35).
check value 42
enter evaluable '+'(0, 3+11).
check value 14
enter evaluable '+'(0, 3.2+11).
check value 14.2
enter evaluable '+'(77, N).
check exception instantiation_error
enter evaluable '+'(foo, 77).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable '-'(7).
check value -7
enter evaluable '-'(3-11).
check value 8
enter evaluable '-'(3.2-11).
check value 7.8
enter evaluable '-'(N).
check exception instantiation_error
enter evaluable '-'(foo).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable '-'(7, 35).
check value -28
enter evaluable '-'(20, 3+11).
check value 6
enter evaluable '-'(0, 3.2+11).
check value -14.2
enter evaluable '-'(77, N).
check exception instantiation_error
enter evaluable '-'(foo, 77).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable '*'(7, 35).
check value 245
enter evaluable '*'(0, 3+11).
check value 0
enter evaluable '*'(1.5, 3.2+11).
check value 21.3 expected
21.299999999999997 actual
enter evaluable '*'(77, N).
check exception instantiation_error
enter evaluable '*'(foo, 77).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable '/'(7, 35).
check value 0
enter evaluable '/'(7.0, 35).
check value 0.2
enter evaluable '/'(140, 3+11).
check value 10
enter evaluable '/'(20.164, 3.2+11).
check value 1.42
enter evaluable '/'(7, -3).
check value -2
enter evaluable '/'(-7, 3).
check value -2
enter evaluable '/'(77, N).
check exception instantiation_error
enter evaluable '/'(foo, 77).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable '/'(3, 0).
check exception evaluation_error(zero_divisor) expected
no exception actual
enter evaluable mod(7, 3).
check value 1
enter evaluable mod(0, 3+11).
check value 0
enter evaluable mod(7, -2).
check value -1 expected
1 actual
enter evaluable mod(77, N).
check exception instantiation_error
enter evaluable mod(foo, 77).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable mod(7.5, 2).
check exception type_error(integer, 7.5) expected
no exception actual
enter evaluable mod(7, 0).
check exception evaluation_error(zero_division) expected
/ by zero actual
enter evaluable floor(7.4).
check value 7
enter evaluable floor(-0.4).
check value -1
enter evaluable round(7.5).
check value 8
enter evaluable round(7.6).
check value 8
enter evaluable round(-0.6).
check value -1
enter evaluable round(N).
check exception instantiation_error
enter evaluable ceiling(-0.5).
check value 0
enter evaluable truncate(-0.5).
check value 0
enter evaluable truncate(foo).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable float(7).
check value 7.0
enter evaluable float(7.3).
check value 7.3
enter evaluable float(5 / 3).
check value 1.0
enter evaluable float(N).
check exception instantiation_error
enter evaluable float(foo).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual
enter evaluable abs(7).
check value 7
enter evaluable abs(3-11).
check value 8
enter evaluable abs(3.2-11.0).
check value 7.8
enter evaluable abs(N).
check exception instantiation_error
enter evaluable abs(foo).
check exception type_error(number, foo) expected
type_error(Evaluable, foo/0) actual

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start jTrolog.engine.EngineFixture

Then, ask the engine to solve a query, and check for the exception thrown.

jTrolog.engine.PrologActionFixture
enter query current_prolog_flag(max_integer, MI), X is '+'(MI, 1).
check hasSolution false expected
true actual
check exception evaluation_error(int_overflow) expected
no exception actual
enter query current_prolog_flag(max_integer, MI), X is '-'('+'(MI, 1), 1).
check hasSolution false expected
true actual
check exception evaluation_error(int_overflow) expected
no exception actual
enter query current_prolog_flag(max_integer, MI), X is '*'(MI, 2).
check hasSolution false expected
true actual
check exception evaluation_error(int_overflow) expected
no exception actual
enter query current_prolog_flag(max_integer, MI), R is float(MI) * 2, X is floor(R).
check hasSolution false expected
true actual
check exception evaluation_error(int_overflow) expected
no exception actual

The results of the tests for Simple arithmetic functors are as follows:

fit.Summary
counts 39 right, 22 wrong, 0 ignored, 0 exceptions
run date Tue Jan 15 12:14:16 CET 2008
run elapsed time 0:01.26