ITEC 380 - Testing Prolog Programs

It is useful to have a way to test prolog programs, and here is one way to do it. Create a group of predicates that test your your prolog program, and put the predicates in a file. Here is an example file, called p1Test.P:

 
o :- 
p2, p3, p4,
q2, q3, q4,
r2, r3, r4,
s2, s3, s4.

p2 :- checkLast([b, a], a).
p3 :- checkLast([a], a).
p4 :- not checkLast([], a).

q2 :- checkNextToLast([b, a], b).
q3 :- not checkNextToLast([a], a).
q4 :- not checkNextToLast([], a).

r2 :- checkLastTwoReversed([b, a], [a,b]).
r3 :- not checkLastTwoReversed([a], [a,b]).
r4 :- not checkLastTwoReversed([], [a,b]).

s2 :- checkReverseLastTwo([b, a], [a,b]).
s3 :- not checkReverseLastTwo([a], [a,b]).
s4 :- not checkReverseLastTwo([], [a,b]).


:- consult(p1).

checkLast(X, Y) :- write(X), write(Y), last(X, Y).
checkNextToLast(X, Y) :- write(X), write(Y), nextToLast(X, Y).
checkLastTwoReversed(X, Y) :- write(X), write(Y), lastTwoReversed(X, Y).
checkReverseLastTwo(X, Y) :- write(X), write(Y), reverseLastTwo(X, Y).
To cause p1Test.P to be loaded you can do the following:
  /software/unix/bin/xsb  <  loadP1Test   
where loadP1Test is a file that contains the following:
[p1Test].
o.
p2.
p3.
p4.
q2.
q3.
q4.
r2.
r3.
r4.
s2.
s3.
s4.
Since this comes from standard input it loads file p1Test, then tries predicate o, then tries all of the other predicates.

If all of the tests work, then this will be seen by the success of o and you do not need to look at the results of the other tests. If o fails, then you can look at the other tests to see which one failed.


[LaTeX -> HTML by ltoh]
Edward G. Okie
Last modified: Aug 26 2002