1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8%
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License.
13%
14% The Original Code is  The ECLiPSe Constraint Logic Programming System.
15% The Initial Developer of the Original Code is  Cisco Systems, Inc.
16% Portions created by the Initial Developer are
17% Copyright (C) 1995-2006 Cisco Systems, Inc.  All Rights Reserved.
18%
19% Contributor(s):
20%
21% END LICENSE BLOCK
22% ----------------------------------------------------------------------
23% System:	ECLiPSe Constraint Logic Programming System
24% Version:	$Id: repairtest.pl,v 1.1 2006/09/23 01:53:53 snovello Exp $
25% ----------------------------------------------------------------------
26
27:- use_module(repair).
28:- lib(fd).
29
30
31repair :-
32	( conflict_vars([C|_onflict]) ->
33		indomain(C),
34		repair
35	; conflict_constraints([C|_onflictConstraints]) ->
36		term_variables(C, Vars),
37		deleteffc(Var,Vars, _),
38		Var tent_get Val,
39		(Var = Val ; Var ## Val), % choice
40		repair
41	;
42		true
43	).
44
45test :-
46	test(X),write(X),fail.
47
48test(a) :-
49    [X]::0..1,
50    [S]::0..10,
51    S #>= X + 1 r,
52    X tent_set 1,
53    S tent_set 5,
54    write_expect(a1,[],[]),
55    S #<= 7,
56    write_expect(a2,[],[]),
57    S#<= 4,
58    write_expect(a3,[S #>= X + 1],[S]).
59
60test(b) :-
61	[X,Y]::1..10,
62	X #> Y r,
63	X tent_set 1,
64	Y tent_set 2,
65	write_expect(b1,[X #> Y],[X]),
66	X tent_set 3,
67	write_expect(b2,[],[]),
68	X tent_set 33,
69	write_expect(b3,[X #> Y],[X]).
70
71test(c) :-
72	[X,Y]::1..10,
73	tent_set(X,5),
74	tent_set(Y,3),
75	r(X #> Y),
76	write_expect(c1,[],[]),
77	Y = 8,
78	write_expect(c2,[X #> Y],[X]),
79	repair,
80	write_expect(c3,[],[]).
81
82test(d) :-
83	[X,Y,Z]::1..10,
84	tent_set(X,5),
85	tent_set(Y,3),
86	tent_set(Z,7),
87	r(Z#>X),r(Z#>Y),
88	write_expect(d1,[],[]),
89	Y=7,
90	write_expect(d2,[Z #> Y,Z #> X],[Z]),
91	repair,
92	write_expect(d3,[],[]).
93
94test(e) :-
95	 X::0..1, X tent_set 0,X#>0 r,
96	 ( delayed_goals([]) -> true
97	 ; writeln('e1 failed')
98	 ).
99test(g) :-
100	[X,Y]::0..2,
101	tent_set(X,4),
102	tent_set(Y,4),
103	r(Y #>= X),
104	write_expect(g1,[Y #>= X],[X,Y]),
105	X=1,
106	write_expect(g2,[Y #>= X],[Y]),
107	Y=1,
108	write_expect(g3,[],[]).
109
110test(h) :-
111	[X,Y,A,B]::0..8,
112	[X,Y,A,B] tent_set [3,5,3,5],
113	8 #= X+Y r,
114	A #<= X r,
115	B #<= Y r,
116	write_expect(h1,[],[]),
117	A #> 3,
118	write_expect(h2,[B #<= Y,A #<=X,8 #= X+Y],[X,A,Y,B]),
119	A=8,
120	write_expect(h3,[],[]).
121
122test(i) :-
123	X = 1 r,
124	write_expect(i1,[],[]),
125	X = 1,
126	write_expect(i2,[],[]).
127
128test(j) :-
129	[X,Y]::1..10,
130	X #> Y r_no_prop,
131	X tent_set 2,
132	Y tent_set 1,
133	write_expect(j1,[],[]),
134	X=Y,
135	write_expect(j2,[X #> X],[]).
136
137test(k) :-
138	X::1..10,
139	X tent_set 5,
140	suspend(X tent_set 3,3,X->ga_chg),
141	X #< 5,
142	write_expect(k1,[],[]).
143
144test(l) :-
145	#?(1 #> X,B) r_conflict a-b(X,B),
146	X :: 1..10,
147	X tent_set 0,
148	expect_ccs(l1,a,[b(X,B)]),
149	X tent_set 1,
150	expect_ccs(l2,a,[]).
151test(' -- Repair test done').
152
153:- tool((#?)/2,(#?)/3).
154#?(Goal,B,M) :-
155	call(Goal,M) -> B=1 ; B=0.
156
157expect_ccs(Id,Key,Ds) :-
158	conflict_constraints(Key,Is),
159	sort(Ds,SDs),
160	sort(Is,SIs),
161	( SIs ==  SDs ->
162	    true
163	;
164	    writeln(Id:expected_constraints(SIs=SDs))
165	).
166
167expect_ccs(Id,Ds) :-
168	conflict_constraints(Is),
169	sort(Ds,SDs),
170	sort(Is,SIs),
171	( SIs ==  SDs ->
172	    true
173	;
174	    writeln(Id:expected_constraints(SIs=SDs))
175	).
176
177expect_cvs(Id,Ds) :-
178	conflict_vars(Is),
179	sort(Ds,SDs),
180	sort(Is,SIs),
181	( SIs ==  SDs ->
182	    true
183	;
184	    writeln(Id:expected_vars(SIs=SDs))
185	).
186
187write_expect(Id,CCe,CVe) :-
188	expect_ccs(Id,CCe),
189	expect_cvs(Id,CVe).
190
191% :- test.
192