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.core;
31
32 import java.util.Comparator;
33 import java.util.Iterator;
34 import java.util.NoSuchElementException;
35
36 import junit.framework.TestCase;
37
38 import org.sat4j.specs.IVec;
39
40
41
42
43
44
45
46
47
48
49 public class VecTest extends TestCase {
50
51
52
53
54
55
56 public VecTest(String arg0) {
57 super(arg0);
58 }
59
60
61
62
63 @Override
64 protected void setUp() throws Exception {
65 super.setUp();
66 this.myvec = new Vec<Integer>();
67 }
68
69
70
71
72 @Override
73 protected void tearDown() throws Exception {
74 super.tearDown();
75 }
76
77
78
79
80 public void testVec() {
81 IVec<Integer> vec = new Vec<Integer>();
82 assertEquals(0, vec.size());
83 }
84
85
86
87
88 public void testVecint() {
89 IVec<Integer> vec = new Vec<Integer>(10, new Integer(0));
90 assertEquals(new Integer(0), vec.last());
91 assertEquals(10, vec.size());
92 }
93
94
95
96
97 public void testVecintObject() {
98 Integer pad = new Integer(10);
99 IVec<Integer> vec = new Vec<Integer>(10, pad);
100 assertEquals(pad, vec.last());
101 assertEquals(10, vec.size());
102
103 }
104
105 public void testSize() {
106 assertEquals(0, this.myvec.size());
107 this.myvec.push(null);
108 assertEquals(1, this.myvec.size());
109 this.myvec.push(null);
110 assertEquals(2, this.myvec.size());
111 this.myvec.pop();
112 assertEquals(1, this.myvec.size());
113 this.myvec.pop();
114 assertEquals(0, this.myvec.size());
115 }
116
117 public void testShrink() {
118 for (int i = 0; i < 15; i++) {
119 this.myvec.push(new Integer(i));
120 }
121 assertEquals(15, this.myvec.size());
122 this.myvec.shrink(10);
123 assertEquals(5, this.myvec.size());
124 assertEquals(new Integer(4), this.myvec.last());
125 this.myvec.shrink(0);
126 assertEquals(5, this.myvec.size());
127 assertEquals(new Integer(4), this.myvec.last());
128 }
129
130 public void testShrinkTo() {
131 for (int i = 0; i < 15; i++) {
132 this.myvec.push(new Integer(i));
133 }
134 assertEquals(15, this.myvec.size());
135 this.myvec.shrinkTo(10);
136 assertEquals(10, this.myvec.size());
137 assertEquals(new Integer(9), this.myvec.last());
138 this.myvec.shrinkTo(10);
139 assertEquals(10, this.myvec.size());
140 assertEquals(new Integer(9), this.myvec.last());
141
142 }
143
144 public void testPop() {
145 for (int i = 0; i < 15; i++) {
146 this.myvec.push(new Integer(i));
147 }
148 assertEquals(15, this.myvec.size());
149 this.myvec.pop();
150 assertEquals(14, this.myvec.size());
151 assertEquals(new Integer(13), this.myvec.last());
152 }
153
154
155
156
157 public void testGrowToint() {
158 assertEquals(0, this.myvec.size());
159 this.myvec.growTo(12, null);
160 assertEquals(12, this.myvec.size());
161 assertNull(this.myvec.last());
162 this.myvec.growTo(20, null);
163 assertEquals(20, this.myvec.size());
164 assertNull(this.myvec.last());
165 }
166
167
168
169
170 public void testGrowTointObject() {
171 assertEquals(0, this.myvec.size());
172 Integer douze = new Integer(12);
173 this.myvec.growTo(12, douze);
174 assertEquals(12, this.myvec.size());
175 assertEquals(douze, this.myvec.last());
176 Integer treize = new Integer(13);
177 this.myvec.growTo(20, treize);
178 assertEquals(20, this.myvec.size());
179 assertEquals(treize, this.myvec.last());
180 }
181
182
183
184
185 public void testPush() {
186 assertEquals(0, this.myvec.size());
187 for (int i = 0; i < 10; i++) {
188 this.myvec.push(new Integer(0));
189 }
190 assertEquals(10, this.myvec.size());
191 assertEquals(new Integer(0), this.myvec.last());
192 }
193
194
195
196
197 public void testPushObject() {
198 Integer deux = new Integer(2);
199 assertEquals(0, this.myvec.size());
200 for (int i = 0; i < 10; i++) {
201 this.myvec.push(deux);
202 }
203 assertEquals(10, this.myvec.size());
204 assertEquals(deux, this.myvec.last());
205 }
206
207 public void testClear() {
208 this.myvec.push(null);
209 this.myvec.push(null);
210 this.myvec.clear();
211 assertEquals(0, this.myvec.size());
212 }
213
214 public void testLast() {
215 for (int i = 0; i < 10; i++) {
216 Integer myint = new Integer(i);
217 this.myvec.push(myint);
218 assertEquals(myint, this.myvec.last());
219 }
220 }
221
222 public void testGet() {
223 for (int i = 0; i < 10; i++) {
224 Integer myint = new Integer(i);
225 this.myvec.push(myint);
226 assertEquals(myint, this.myvec.get(i));
227 }
228 }
229
230 public void testCopyTo() {
231 Vec<Integer> nvec = new Vec<Integer>();
232 this.myvec.growTo(15, new Integer(15));
233 this.myvec.copyTo(nvec);
234 assertEquals(15, nvec.size());
235 assertEquals(15, this.myvec.size());
236 assertEquals(this.myvec.last(), nvec.last());
237
238 }
239
240 public void testMoveTo() {
241 Vec<Integer> nvec = new Vec<Integer>();
242 this.myvec.growTo(15, new Integer(15));
243 this.myvec.moveTo(nvec);
244 assertEquals(15, nvec.size());
245 assertEquals(0, this.myvec.size());
246 assertEquals(new Integer(15), nvec.last());
247 }
248
249 public void testSelectionSort() {
250 Vec<Integer> nvec = new Vec<Integer>();
251 for (int i = 30; i > 0; i--) {
252 nvec.push(new Integer(i));
253 }
254 Comparator<Integer> comp = new DefaultComparator<Integer>();
255 nvec.selectionSort(0, 30, comp);
256 for (int i = 1; i <= 30; i++) {
257 assertEquals(i, nvec.get(i - 1).intValue());
258 }
259 }
260
261 public void testSort() {
262 IVec<Integer> nvec = new Vec<Integer>();
263 for (int i = 101; i > 0; i--) {
264 nvec.push(new Integer(i));
265 }
266 nvec.push(new Integer(30));
267 nvec.push(new Integer(40));
268 Comparator<Integer> comp = new DefaultComparator<Integer>();
269 nvec.sort(comp);
270 for (int i = 1; i <= 30; i++) {
271 assertEquals(i, nvec.get(i - 1).intValue());
272 }
273 }
274
275 public void testSortEmpty() {
276 IVec<Integer> nvec = new Vec<Integer>();
277 Comparator<Integer> comp = new DefaultComparator<Integer>();
278 nvec.sort(comp);
279 }
280
281 public void testSortUnique() {
282 IVec<Integer> nvec = new Vec<Integer>();
283 for (int i = 101; i > 0; i--) {
284 nvec.push(new Integer(i));
285 }
286 nvec.push(new Integer(30));
287 nvec.push(new Integer(40));
288 nvec.push(new Integer(50));
289 nvec.push(new Integer(55));
290 nvec.push(new Integer(60));
291 Comparator<Integer> comp = new DefaultComparator<Integer>();
292 nvec.sortUnique(comp);
293 for (int i = 1; i <= 101; i++) {
294 assertEquals(i, nvec.get(i - 1).intValue());
295 }
296 }
297
298 public void testDelete() {
299 IVec<Integer> nvec = new Vec<Integer>();
300 for (int i = 0; i < 100; i++) {
301 nvec.push(new Integer(i));
302 }
303 assertEquals(new Integer(10), nvec.delete(10));
304 assertEquals(new Integer(99), nvec.get(10));
305 nvec.clear();
306 nvec.push(new Integer(1));
307 assertEquals(new Integer(1), nvec.delete(0));
308 }
309
310 public void testRemove() {
311 IVec<Integer> nvec = new Vec<Integer>();
312 for (int i = 0; i < 100; i++) {
313 nvec.push(new Integer(i));
314 }
315 Integer toRemove = nvec.get(10);
316 nvec.remove(toRemove);
317 assertEquals(99, nvec.size());
318 assertEquals(new Integer(11), nvec.get(10));
319 nvec.clear();
320 toRemove = new Integer(1);
321 nvec.push(toRemove);
322 assertEquals(1, nvec.size());
323 nvec.remove(toRemove);
324 assertEquals(0, nvec.size());
325 }
326
327 public void testEquals() {
328 IVec<Integer> nvec = new Vec<Integer>(3, new Integer(2));
329 IVec<Integer> vect = new Vec<Integer>(3, new Integer(2));
330 IVec<Integer> vecf = new Vec<Integer>(4, new Integer(2));
331 IVec<Integer> vecf2 = new Vec<Integer>(2, new Integer(2));
332 IVec<Integer> vecf3 = new Vec<Integer>(3, new Integer(3));
333 assertEquals(nvec, vect);
334 assertFalse(nvec.equals(vecf));
335 assertFalse(nvec.equals(vecf2));
336 assertFalse(nvec.equals(vecf3));
337
338 }
339
340 public void testIterator() {
341 Vec<String> str = new Vec<String>();
342 str.push("titi");
343 str.push("toto");
344 str.push("tata");
345 Iterator<String> it = str.iterator();
346 assertTrue(it.hasNext());
347 assertEquals("titi", it.next());
348 assertTrue(it.hasNext());
349 assertEquals("toto", it.next());
350 assertTrue(it.hasNext());
351 assertEquals("tata", it.next());
352 assertFalse(it.hasNext());
353 }
354
355 public void testNoSuchElementException() {
356 Vec<String> str = new Vec<String>();
357 Iterator<String> it = str.iterator();
358 assertFalse(it.hasNext());
359 try {
360 it.next();
361 fail();
362 } catch (NoSuchElementException e) {
363
364 }
365 }
366
367 private IVec<Integer> myvec;
368
369 }