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.core;
31
32 import junit.framework.TestCase;
33
34 import org.sat4j.core.VecInt;
35 import org.sat4j.minisat.SolverFactory;
36 import org.sat4j.specs.ContradictionException;
37 import org.sat4j.specs.ISolver;
38 import org.sat4j.specs.IVecInt;
39 import org.sat4j.specs.TimeoutException;
40
41 public class TestAtMost extends TestCase {
42
43 private ISolver solver;
44
45 public TestAtMost(String s) {
46 super(s);
47 }
48
49 @Override
50 protected void setUp() {
51 this.solver = SolverFactory.newMiniSATHeap();
52 }
53
54 public void testUnEssaiSat() throws TimeoutException {
55 try {
56 this.solver.reset();
57
58 this.solver.newVar(3);
59
60
61 IVecInt vec = new VecInt();
62 vec.push(1);
63 vec.push(2);
64 vec.push(3);
65 this.solver.addAtLeast(vec, 2);
66
67
68
69 vec = new VecInt();
70 vec.push(1);
71 vec.push(-2);
72 this.solver.addAtLeast(vec, 2);
73
74 boolean isSat = this.solver.isSatisfiable();
75
76 assertTrue(isSat);
77 } catch (ContradictionException e) {
78 assertTrue(false);
79 }
80 }
81
82 public void testUnEssaiUnsat() throws TimeoutException {
83 try {
84 this.solver.reset();
85
86 this.solver.newVar(2);
87
88
89 IVecInt vecLit = new VecInt();
90 vecLit.push(1);
91 vecLit.push(-2);
92 this.solver.addAtLeast(vecLit, 1);
93
94
95
96 vecLit = new VecInt();
97 vecLit.push(-1);
98 this.solver.addAtLeast(vecLit, 1);
99
100
101
102 vecLit = new VecInt();
103 vecLit.push(2);
104 this.solver.addAtLeast(vecLit, 1);
105
106 boolean isSat = this.solver.isSatisfiable();
107
108 assertFalse(isSat);
109 } catch (ContradictionException e) {
110 assertTrue(false);
111 }
112
113 }
114
115 public void test2Sat() throws TimeoutException {
116 try {
117 this.solver.reset();
118
119 this.solver.newVar(2);
120
121
122 IVecInt vecLit = new VecInt();
123 vecLit.push(1);
124 vecLit.push(-2);
125 this.solver.addAtMost(vecLit, 3);
126
127 boolean isSat = this.solver.isSatisfiable();
128
129 assertTrue(isSat);
130 } catch (ContradictionException e) {
131 assertTrue(false);
132 }
133 }
134
135 public void test4Unsat() throws TimeoutException {
136 try {
137 this.solver.reset();
138
139 this.solver.newVar(2);
140
141
142 IVecInt vecLit = new VecInt();
143 vecLit.push(1);
144 vecLit.push(-2);
145
146 this.solver.addAtLeast(vecLit, 3);
147
148 this.solver.isSatisfiable();
149
150 fail();
151 } catch (ContradictionException e) {
152 }
153 }
154
155 public void test3Unsat() throws TimeoutException {
156 try {
157 this.solver.reset();
158
159 this.solver.newVar(2);
160
161
162 IVecInt vecLit = new VecInt();
163 vecLit.push(1);
164 this.solver.addAtLeast(vecLit, 1);
165
166
167 vecLit = new VecInt();
168 vecLit.push(2);
169 this.solver.addAtLeast(vecLit, 1);
170
171
172 vecLit = new VecInt();
173 vecLit.push(1);
174 vecLit.push(2);
175 this.solver.addAtMost(vecLit, 1);
176
177 this.solver.isSatisfiable();
178
179 fail();
180 } catch (ContradictionException e) {
181 }
182
183 }
184
185 public void test5Sat() throws TimeoutException {
186 try {
187 this.solver.reset();
188
189 this.solver.newVar(2);
190
191
192 IVecInt vecLit = new VecInt();
193 vecLit.push(1);
194 vecLit.push(-2);
195
196 this.solver.addAtMost(vecLit, 0);
197
198 boolean isSat = this.solver.isSatisfiable();
199
200 assertTrue(isSat);
201 } catch (ContradictionException e) {
202 assertTrue(false);
203 }
204 }
205 }