:- op(test, fx, 1200).

test_mode.

test( ( G  :- Sols ) :-
      reset_sols,
      init_io( Streams ),
      catch( do_test(G, Sols), Done,  handler( Done ) ).
      close_io( Streams ).

do_test(G0, Sols)
      copy_term(G0, G),
      catch( ( G, answer(G, Sol) ) , Error, test_error(Error, Sol) ),
      next_solution( I, Sol, G0, Sol ),
      !.
do_test(G, Sols) :-
    counter(I),
    failure(G, Sols, I).

next_solution( I, Sol , G0, Sols ) :-
    inc(I),
    fetch(I, Sols, Pattern, Next),
    (
     Sol =@= Pattern
    ->
     success(I, G0)
    ;
     error(I, G0, Sol )
    ),
    ( var(Next) -> throw( done ) ).