% $Id: test_helpers.mod,v 1.1 2007/05/09 19:46:38 leavens Exp $
% Helpers for testing for lambda Prolog programs.
module test_helpers.

  local checkall_help int -> int -> int -> o.
  local check_series_help int -> int -> int -> int -> int -> int -> o.
  local checkall_endmsg int -> o.

  %% ``checkall'' checks all the tests that exist.
  checkall :- checkall_help 1 0 0.

  %% ``checkall_help'' checks all the tests I,J starting at test I,1.
  checkall_help I Total Failed :-
       check_series I Total' Failed',
       Total' > 0,
       !,
       Iplus1 is I + 1,
       Total'' is Total + Total',
       Failed'' is Failed + Failed',
       checkall_help Iplus1 Total'' Failed''.
  checkall_help I Total Failed :- checkall_end Total Failed.

  %% ``checkall_series I T F'' checks all the tests with coordinates I,J,
  %% for some J, starting at test I,1, and reports the total run
  %% and total failures in T and F.
  check_series I Total Failed :- check_series_help I 1 0 0 Total Failed.

  %% ``check_series_help I J Total Failed Total1 Failed1'' checks all the tests
  %% with first coordinate I, starting at test I,J, and reports the total run
  %% and total failures in the accumulators Total1 and Failed1.
  check_series_help I J Total Failed Total1 Failed1 :-
       check_exists I J,
       !,
       do_test (check I J) I J F,
       Jplus1 is J + 1,
       Total' is Total + 1,
       Failed' is Failed + F,
       check_series_help I Jplus1 Total' Failed' Total1 Failed1.
  check_series_help I J Total Failed Total Failed.

  %% Run (test) P, note failures, and succeed so can keep going...
  do_test P I J 0 :-
       print ".",
       P,
       !.
  do_test P I J 1 :-
       print "\nTest ",
       printterm std_out I,
       print " ",
       printterm std_out J,
       print " failed\n".

  %% Print a suitable ending message for testing
  checkall_end Total 0 :-
       print "\n",
       print "successfully ran ",
       checkall_endmsg Total.
  checkall_end Total Failed :-
       print "\n",
       printterm std_out Failed,
       print " failure(s) in ",
       checkall_endmsg Total.

  %% This really is the last thing printed.
  checkall_endmsg Total :-
       printterm std_out Total,
       print " tests.\n".
