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;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertTrue;
35
36 import org.junit.Test;
37 import org.sat4j.core.ConstrGroup;
38 import org.sat4j.core.VecInt;
39 import org.sat4j.minisat.SolverFactory;
40 import org.sat4j.specs.ContradictionException;
41 import org.sat4j.specs.ISolver;
42 import org.sat4j.specs.IVecInt;
43 import org.sat4j.specs.TimeoutException;
44
45 public class TestConstrGroup {
46
47 @Test
48 public void testDeleteGroup() throws ContradictionException {
49 ISolver solver = SolverFactory.newDefault();
50 ConstrGroup g1 = new ConstrGroup();
51 IVecInt clause = new VecInt(new int[] { 1, 2, -3 });
52 solver.addClause(clause);
53
54 clause.clear();
55 clause.push(2).push(-3).push(-5);
56 g1.add(solver.addClause(clause));
57 clause.clear();
58 clause.push(-3).push(-2).push(-4);
59 g1.add(solver.addClause(clause));
60 assertEquals(3, solver.nConstraints());
61 g1.removeFrom(solver);
62 assertEquals(1, solver.nConstraints());
63 }
64
65 @Test
66 public void canPutAUnitClauseInAGroup() throws ContradictionException {
67 ISolver solver = SolverFactory.newDefault();
68 ConstrGroup g1 = new ConstrGroup();
69
70 IVecInt clause = new VecInt(new int[] { 1 });
71 g1.add(solver.addClause(clause));
72 assertEquals(1, solver.nConstraints());
73 }
74
75 @Test
76 public void checkBugReportedByThomas() throws ContradictionException {
77 ISolver solver = SolverFactory.newDefault();
78 ConstrGroup g1 = new ConstrGroup();
79
80 IVecInt clause = new VecInt(new int[] { 1 });
81 solver.addClause(clause);
82
83
84 clause.clear();
85 clause.push(2).push(-3).push(-5);
86 g1.add(solver.addClause(clause));
87
88 clause.clear();
89 clause.push(-3).push(-2).push(-4);
90 g1.add(solver.addClause(clause));
91 assertEquals(3, solver.nConstraints());
92
93 g1.removeFrom(solver);
94 assertEquals(1, solver.nConstraints());
95 }
96
97 @Test
98 public void checkItWorksAfterRunningTheSolver()
99 throws ContradictionException, TimeoutException {
100 ISolver solver = SolverFactory.newDefault();
101 ConstrGroup g1 = new ConstrGroup();
102
103 IVecInt clause = new VecInt(new int[] { 1 });
104 solver.addClause(clause);
105
106
107 clause.clear();
108 clause.push(-1).push(-2).push(-3);
109 g1.add(solver.addClause(clause));
110
111 clause.clear();
112 clause.push(-1).push(2).push(-3);
113 g1.add(solver.addClause(clause));
114 assertEquals(3, solver.nConstraints());
115 assertTrue(solver.isSatisfiable());
116 assertTrue(solver.model(1));
117 assertFalse(solver.model(3));
118 g1.removeFrom(solver);
119 assertEquals(1, solver.nConstraints());
120 }
121
122 @Test
123 public void checkGroupDoesWorkWhenClausesAreReducedByUnitPropgation()
124 throws ContradictionException {
125 ISolver solver = SolverFactory.newDefault();
126 ConstrGroup g1 = new ConstrGroup();
127
128 IVecInt clause = new VecInt(new int[] { 1 });
129 solver.addClause(clause);
130
131
132 clause.clear();
133 clause.push(-1).push(-2);
134 g1.add(solver.addClause(clause));
135 assertEquals(1, g1.size());
136 }
137
138 @Test
139 public void checkTheExpectedWayToDealWithUnitClausesToRemove()
140 throws ContradictionException, TimeoutException {
141 ISolver solver = SolverFactory.newDefault();
142 ConstrGroup g1 = new ConstrGroup();
143
144 IVecInt clause = new VecInt(new int[] { 1 });
145 solver.addClause(clause);
146
147
148 clause.clear();
149 clause.push(2).push(-3);
150 g1.add(solver.addClause(clause));
151
152 clause.clear();
153 clause.push(-2).push(4);
154 g1.add(solver.addClause(clause));
155
156 IVecInt unitClauses = new VecInt(new int[] { 3, -4 });
157
158 assertFalse(solver.isSatisfiable(unitClauses));
159
160 g1.removeFrom(solver);
161 assertTrue(solver.isSatisfiable(unitClauses));
162 }
163 }