
% ------------------------------------------------------------------------------------------------------
% Test update_fact
% This test is based on prova-examples/ex054.prova 
% ------------------------------------------------------------------------------------------------------

testcase("updateFactTestCase.prova").

%-----------------------------------------------------------------------
% Imported libraries
%-----------------------------------------------------------------------
:-eval(consult('ContractLog/list_math.prova')).
:-eval(consult('./ContractLog/math.prova')).
:-eval(consult('./ContractLog/list.prova')).
:-eval(consult('./ContractLog/datetime.prova')).
:-eval(consult('./ContractLog/testcase.prova')).
:-eval(consult('./ContractLog/utils.prova')).
:-eval(consult('./ContractLog/update.prova')).

% ------------------------------------------------------------------------------------------------------
% (Meta) test rules
% ------------------------------------------------------------------------------------------------------

:-eval(add("updateFactTestCase","ex054_1(X,Y) :- assert( f(2,'b') ),	update_fact( f(2,_) ), f(X,Y).")).
:-eval(add("updateFactTestCase","ex054_2(X,Y) :- update_fact( f(2,'b') ), f(X,Y).")).
:-eval(add("updateFactTestCase","ex054_3(X,Y) :- assert( f(2,'b') ), update_fact( f(2,'b') ), f(X,Y).")).
:-eval(add("updateFactTestCase","ex054_4(X,Y) :- assert( f(2,'a') ),	update_fact( f(2,_), f(2,'b') ), f(X,Y).")).

% ------------------------------------------------------------------------------------------------------
% Test facts
% ------------------------------------------------------------------------------------------------------

% No facts used in this test

% ------------------------------------------------------------------------------------------------------
% Tests
% ------------------------------------------------------------------------------------------------------

testSuccess("update_fact Test 1","Test 1 succeeded: a general update_fact subsumes an existing specific fact") :-
	testcase("updateFactTestCase.prova"),
	testResultsOrder(ex054_1(X,Y),Y,[Y]). 
testFailure("update_fact Test 1","Incorrect results in ex054_1(X,Y)") :-
	not(testSuccess("update_fact Test 1",Message)).

testSuccess("update_fact Test 2","Test 2 succeeded: a specific update_fact is added as is") :-
	testcase("updateFactTestCase.prova"),
	testResultsOrder(ex054_2(X,Y),Y,[Y,"b"]). 
testFailure("update_fact Test 2","Incorrect results in ex054_2(X,Y)") :-
	not(testSuccess("update_fact Test 2",Message)).

testSuccess("update_fact Test 3","Test 3 succeeded: an update_fact subsumes ALL previous matching facts") :-
	testcase("updateFactTestCase.prova"),
	testResultsOrder(ex054_3(X,Y),Y,[Y,"b"]). 
testFailure("update_fact Test 3","Incorrect results in ex054_3(X,Y)") :-
	not(testSuccess("update_fact Test 3",Message)).

testSuccess("update_fact Test 4","Test 4 succeeded: an update_fact with extra argument for subsumed facts removes all the existing facts that match") :-
	testcase("updateFactTestCase.prova"),
	testResultsOrder(ex054_4(X,Y),Y,["b"]). 
testFailure("update_fact Test 4","Incorrect results in ex054_4(X,Y)") :-
	not(testSuccess("update_fact Test 4",Message)).

runTest("updateFactTestCase.prova") :-
	testSuccess("update_fact Test 1",Message),
	testSuccess("update_fact Test 2",Message),
	testSuccess("update_fact Test 3",Message),
	testSuccess("update_fact Test 4",Message).

% ------------------------------------------------------------------------------------------------------
% Test Coverage
% ------------------------------------------------------------------------------------------------------

testCoverage(LP,CoveredClauses,NotCoveredClauses,OverAll,NotCovered,Covered,CoverageLevel):-
 	partial(metaLP(LP),"updateFactTestCase"), 
  	coverage(
  		[a(1,1),a(2,2)],
  		LP,	 						
  		CoveredClauses,
  		NotCoveredClauses,	
  		CoverageLevel),
  		size(LP,OverAll),
  		size(NotCoveredClauses,NotCovered),
  		size(CoveredClauses,Covered).
