%% The following routines can be used to check your code using the sample testcases in 
%% this directory. The following runs all tests and reports errors found. 

%% Note that assignements are expected to be lists of entries of the form
%%   val(Variable,Truthvalue)
%% and that the variables are expected to appear in this list in the same order they appear 
%% in the "variables" declaration in the program. 

%%% call runall to run all the tests. If any test fails an error message 
%%  will describe the problem. If nothing is reported all tests were passed. 
%%  You can also run individaul tests using the predicate "test" below. 

runall :- sample(S), test(S,_), fail. 


sample(sample1). 
sample(sample2). 
sample(sample3). 
sample(sample4). 
sample(sample5). 
sample(sample6). 
sample(sample7). 
sample(sample8). 
sample(sample9). 
sample(sample10). 
sample(sample11). 
sample(sample12). 
sample(sample13). 
sample(sample14). 
sample(sample15). 
sample(sample16). 
sample(sample17). 
sample(sample18). 
sample(sample19). 
sample(sample20). 


%% call e.g. test(sample1,1) to test that your program 'check' accepts all the expected answers. 

test(S,1) :- expectedanswer(S,I,O), not(check(S,I,O)), 
            write('On '), write(S), write(' the program does not accept answer '), nl, 
            write('I = '), write(I), nl, 
            write('O = '), write(O), nl, nl.

%% call e.g. test(sample1,2) to test that your program produces only expected answers

test(S,2) :-  check(S,I,O), not(expectedanswer(S,I,O)), 
            write('On '), write(S), write(' your program produces unexpected answer '), nl, 
            write('I = '), write(I), nl ,
            write('O = '), write(O), nl, nl.

%% call e.g. test3(sample1), test4(sample1) to test your program in the mode where 
%% S and I are given and O is a variable 

test(S,3) :- expectedanswer(S,I,O), not(produces(S,I,O)) ,
            write('On '), write(S), write(' the program does not produce answer '), nl, 
            write('O = '), write(O), nl,
            write(' when given input '), nl, 
            write('I = '), write(I), nl, nl. 
          

%%% in the following, we should really use O == O2, since we do not want to 
%%  unify O and O2. However, there appears to be a bug in iprolog, so we hack it to = here. 

produces(S,I,O) :- check(S,I,O2), O = O2, !. 

% test 4 is not complete for checking unexpected answers, it only checks 
% for I that are part of an expected answer, instead of all I. 

test(S,4) :- expectedanswer(S,I,O), check(S,I,O2), not(expectedanswer(S,I,O2)), 
            write('On '), write(S), write(' and input'), nl, 
            write('I = '), write(I), nl, 
            write(' your program produces unexpected answer '), nl, 
            write('O = '), write(O2), nl, nl.



%%% The following facts describe the expected answers. There is no fact for 
%%% a sample if the program is syntactically correct and satisfies its specification. 

expectedanswer(sample1,[val(a,false),val(b,true),val(c,true),val(d,true)],
                        [val(a,true),val(b,true),val(c,true),val(d,true)]).



%% sample2-sample6 have no expected answers 
 

expectedanswer(sample7,syntaxerror,syntaxerror). 
expectedanswer(sample8,syntaxerror,syntaxerror). 
expectedanswer(sample9,syntaxerror,syntaxerror). 
expectedanswer(sample10,syntaxerror,syntaxerror). 
expectedanswer(sample11,syntaxerror,syntaxerror). 


expectedanswer(sample12,[val(a,true)],[val(a,true)]). 

expectedanswer(sample13,[val(a,false),val(b,false),val(c,false)], [val(a,false),val(b,false),val(c,false)]). 


expectedanswer(sample14,[val(a,false),val(b,false),val(c,false)], [val(a,false),val(b,false),val(c,false)]). 
expectedanswer(sample14,[val(a,false),val(b,false),val(c,true)], [val(a,false),val(b,false),val(c,true)]). 
expectedanswer(sample14,[val(a,false),val(b,true),val(c,false)], [val(a,false),val(b,true),val(c,false)]). 
expectedanswer(sample14,[val(a,false),val(b,true),val(c,true)], [val(a,false),val(b,true),val(c,true)]). 
expectedanswer(sample14,[val(a,true),val(b,false),val(c,false)], [val(a,true),val(b,false),val(c,false)]). 
expectedanswer(sample14,[val(a,true),val(b,false),val(c,true)], [val(a,true),val(b,false),val(c,true)]). 
expectedanswer(sample14,[val(a,true),val(b,true),val(c,false)], [val(a,true),val(b,true),val(c,false)]). 
expectedanswer(sample14,[val(a,true),val(b,true),val(c,true)], [val(a,true),val(b,true),val(c,true)]). 

%% sample15 satisfies its specification. 

expectedanswer(sample16,syntaxerror,syntaxerror). 

expectedanswer(sample17,[val(a,true)],nonterminating). 

%% sample18  satisfies its specification 

expectedanswer(sample19,[val(p,false),val(q,false),val(r,false),val(s,false), val(t,false),val(change,false)], 
                         [val(p,true),val(q,true),val(r,true),val(s,false), val(t,false),val(change,false)]).

expectedanswer(sample20,[val(q,true)],nonterminating). 
expectedanswer(sample20,[val(q,false)],[val(q,false)]).




