1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.sat4j.minisat;
31
32 import java.io.IOException;
33
34 import junit.framework.TestCase;
35
36 import org.sat4j.core.VecInt;
37 import org.sat4j.minisat.core.DataStructureFactory;
38 import org.sat4j.minisat.core.Solver;
39 import org.sat4j.reader.InstanceReader;
40 import org.sat4j.reader.ParseFormatException;
41 import org.sat4j.specs.ContradictionException;
42 import org.sat4j.specs.IConstr;
43 import org.sat4j.specs.IVecInt;
44 import org.sat4j.specs.TimeoutException;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class TestsFonctionnels extends TestCase {
59
60 private static final String PREFIX = System.getProperty("test.prefix");
61
62
63
64
65
66
67 public TestsFonctionnels(String arg0) {
68 super(arg0);
69 }
70
71 public void testSat() {
72 try {
73 this.reader.parseInstance(PREFIX + "aim-50-yes-ok.cnf");
74 assertTrue(this.solver.isSatisfiable());
75 } catch (TimeoutException e) {
76 fail();
77 } catch (Exception e) {
78 fail();
79 }
80 }
81
82 public void testUnsat() throws TimeoutException {
83 try {
84 this.reader.parseInstance(PREFIX + "aim-50-no-ok.cnf");
85 assertFalse(this.solver.isSatisfiable());
86 } catch (IOException e) {
87 fail();
88 } catch (ParseFormatException e) {
89 fail();
90 } catch (ContradictionException e) {
91
92 }
93 }
94
95 public void testTrivialUnsat() {
96 this.solver.newVar(1);
97 IVecInt vec = new VecInt();
98 vec.push(1);
99 try {
100 this.solver.addClause(vec);
101 } catch (ContradictionException e) {
102 fail();
103 }
104 vec.clear();
105 vec.push(-1);
106 try {
107 this.solver.addClause(vec);
108 fail();
109 } catch (ContradictionException e1) {
110 }
111 }
112
113 public void testTrivialSat() throws TimeoutException {
114 this.solver.reset();
115 this.solver.newVar(2);
116 try {
117 IVecInt vec = new VecInt();
118 vec.push(1);
119 this.solver.addClause(vec);
120 vec.clear();
121 vec.push(-2);
122 this.solver.addClause(vec);
123 assertTrue(this.solver.isSatisfiable());
124 } catch (ContradictionException e) {
125 fail();
126 }
127 }
128
129 @Deprecated
130 public void testTrivialSatNewVar() throws TimeoutException {
131 try {
132 this.solver.newVar(0);
133 this.solver.newVar();
134 IVecInt vec = new VecInt();
135 vec.push(1);
136 this.solver.addClause(vec);
137 vec.clear();
138 this.solver.newVar();
139 vec.push(-2);
140 this.solver.addClause(vec);
141 assertTrue(this.solver.isSatisfiable());
142 } catch (ContradictionException e) {
143 fail();
144 }
145 }
146
147 public void testBug001() throws TimeoutException {
148 this.solver.reset();
149 try {
150 this.reader.parseInstance(PREFIX + "bug001.cnf");
151 } catch (Exception e) {
152 e.printStackTrace();
153 fail();
154 }
155 assertTrue(this.solver.isSatisfiable());
156 }
157
158 public void testTrivialInconsistentFormula() {
159 this.solver.reset();
160 try {
161 this.reader.parseInstance(PREFIX + "test3.dimacs");
162 assertFalse(this.solver.isSatisfiable());
163 } catch (ContradictionException e) {
164
165 } catch (Exception e) {
166 e.printStackTrace();
167 fail();
168 }
169 }
170
171 public void testCommentsInInstance() {
172 this.solver.reset();
173 try {
174 this.reader.parseInstance("EZCNF:" + PREFIX + "testcomments.cnf");
175 assertFalse(this.solver.isSatisfiable());
176 } catch (ContradictionException e) {
177
178 } catch (Exception e) {
179 fail(e.getMessage());
180 }
181 }
182
183 public void testRemoveConstraints() throws TimeoutException {
184 try {
185 this.solver.newVar(3);
186 assertEquals(0, this.solver.nConstraints());
187 IVecInt vec = new VecInt();
188 vec.push(1).push(-2);
189 IConstr c = this.solver.addClause(vec);
190 assertNotNull(c);
191 assertEquals(1, this.solver.nConstraints());
192 vec.clear();
193 vec.push(-1).push(-2);
194 c = this.solver.addClause(vec);
195 assertNotNull(c);
196 assertEquals(2, this.solver.nConstraints());
197 vec.clear();
198 vec.push(-1).push(2);
199 this.solver.addClause(vec);
200
201 assertEquals(3, this.solver.nConstraints());
202 vec.clear();
203 vec.push(1).push(2);
204 this.solver.addClause(vec);
205 assertEquals(4, this.solver.nConstraints());
206
207 assertFalse(this.solver.isSatisfiable());
208 this.solver.removeConstr(c);
209 assertEquals(3, this.solver.nConstraints());
210 assertTrue(this.solver.isSatisfiable());
211 assertEquals(1, this.solver.model()[0]);
212 assertEquals(2, this.solver.model()[1]);
213 vec.clear();
214 vec.push(-1).push(-2);
215 try {
216 c = this.solver.addClause(vec);
217 assertNotNull(c);
218 assertEquals(4, this.solver.nConstraints());
219 assertFalse(this.solver.isSatisfiable());
220 } catch (ContradictionException ce) {
221
222 }
223 } catch (ContradictionException e) {
224 fail();
225 }
226 }
227
228 public void testRemoveAtLeast() {
229 this.solver.newVar(3);
230 IVecInt c1 = new VecInt().push(1).push(2).push(3);
231 try {
232 this.solver.addClause(c1);
233 assertEquals(1, this.solver.nConstraints());
234 assertEquals(3, c1.size());
235 IConstr atLeast = this.solver.addAtLeast(c1, 2);
236 assertEquals(2, this.solver.nConstraints());
237 this.solver.removeConstr(atLeast);
238 assertEquals(1, this.solver.nConstraints());
239 } catch (ContradictionException e) {
240 fail();
241 }
242 }
243
244 public void testIsImplied() {
245 this.solver.newVar(3);
246 IVecInt c1 = new VecInt().push(1);
247 try {
248 this.solver.addClause(c1);
249 assertTrue("isImplied(1) ", this.solver.getVocabulary()
250 .isImplied(2));
251 assertFalse("isImplied(2) :", this.solver.getVocabulary()
252 .isImplied(4));
253 this.solver.propagate();
254 assertTrue("isImplied(1) ", this.solver.getVocabulary()
255 .isImplied(2));
256 assertFalse("isImplied(2) :", this.solver.getVocabulary()
257 .isImplied(4));
258 } catch (ContradictionException e) {
259 fail();
260 }
261 }
262
263 public void testIsImplied3() {
264 this.solver.newVar(1);
265 IVecInt c1 = new VecInt().push(-1);
266 try {
267 this.solver.addClause(c1);
268 this.solver.propagate();
269 } catch (ContradictionException e) {
270 fail();
271 }
272 assertTrue("isImplied(1) ", this.solver.getVocabulary().isImplied(2));
273 assertFalse("isSatisfiedl(1)",
274 this.solver.getVocabulary().isSatisfied(2));
275 assertTrue("isFalsified(1)", this.solver.getVocabulary().isFalsified(2));
276 }
277
278 public void testWhenNewVarNotCalled() {
279 IVecInt c1 = new VecInt().push(-1);
280 try {
281 this.solver.addClause(c1);
282 this.solver.propagate();
283 } catch (ContradictionException e) {
284 fail();
285 }
286 }
287
288
289
290
291 @Override
292 protected void setUp() throws Exception {
293 this.solver = SolverFactory.newMiniSATHeap();
294 this.reader = new InstanceReader(this.solver);
295 }
296
297 private Solver<DataStructureFactory> solver;
298
299 private InstanceReader reader;
300 }