View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
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   * Created on 16 oct. 2003
42   *
43   */
44  
45  /**
46   * @author leberre
47   * 
48   */
49  public class VecTest extends TestCase {
50  
51      /**
52       * Constructor for VecTest.
53       * 
54       * @param arg0
55       */
56      public VecTest(String arg0) {
57          super(arg0);
58      }
59  
60      /*
61       * @see TestCase#setUp()
62       */
63      @Override
64      protected void setUp() throws Exception {
65          super.setUp();
66          this.myvec = new Vec<Integer>();
67      }
68  
69      /*
70       * @see TestCase#tearDown()
71       */
72      @Override
73      protected void tearDown() throws Exception {
74          super.tearDown();
75      }
76  
77      /*
78       * Test pour void Vec()
79       */
80      public void testVec() {
81          IVec<Integer> vec = new Vec<Integer>();
82          assertEquals(0, vec.size());
83      }
84  
85      /*
86       * Test pour void Vec(int)
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       * Test pour void Vec(int, Object)
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      * Test pour void growTo(int)
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      * Test pour void growTo(int, Object)
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      * Test pour void push()
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      * Test pour void push(Object)
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             // ok
364         }
365     }
366 
367     private IVec<Integer> myvec;
368 
369 }