Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / util / ArrayIntSetTestBase.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.util;
19
20 import java.util.NoSuchElementException;
21
22 import gov.nasa.jpf.util.test.TestJPF;
23 import org.junit.Test;
24
25 public abstract class ArrayIntSetTestBase extends TestJPF {
26
27   protected abstract ArrayIntSet createArrayIntSet();
28   protected abstract ArrayIntSet createArrayIntSet(int n);
29   
30   @Test
31   public void testInsert(){
32     ArrayIntSet s = createArrayIntSet();
33     s.add(42);
34     s.add(43);
35     s.add(41);
36     s.add(42);
37     s.add(0);
38     
39     System.out.println(s);
40     
41     assertTrue(s.size() == 4);
42     assertTrue(s.contains(0));
43     assertTrue(s.contains(41));
44     assertTrue(s.contains(42));
45     assertTrue(s.contains(43));
46   }
47   
48   @Test
49   public void testRemove(){
50     ArrayIntSet s = createArrayIntSet();
51     s.add(42);
52     assertTrue(s.size() == 1);
53     assertTrue(s.contains(42));
54     
55     s.remove(42);
56     assertFalse(s.contains(42));
57     assertTrue(s.size() == 0);
58     
59     s.add(42);
60     s.add(42000);
61     s.add(0);
62     assertTrue(s.size() == 3);
63     s.remove(42000);
64     assertTrue(s.size() == 2);
65     assertFalse(s.contains(42000));
66     s.remove(0);
67     assertFalse(s.contains(0));
68     assertTrue(s.size() == 1);
69   }
70   
71   @Test
72   public void testRemoveLast(){
73     ArrayIntSet s = createArrayIntSet(2);
74     s.add(1);
75     s.add(2);
76     
77     s.remove(2);
78     assertTrue( s.size() == 1);
79     assertTrue( s.contains(1));
80     
81     s.remove(1);
82     assertTrue( s.isEmpty());
83   }
84   
85   @Test
86   public void testRemoveFirst(){
87     ArrayIntSet s = createArrayIntSet(2);
88     s.add(1);
89     s.add(2);
90     
91     s.remove(1);
92     assertTrue( s.size() == 1);
93     assertTrue( s.contains(2));
94     
95     s.remove(2);
96     assertTrue( s.isEmpty());
97   }
98   
99   
100   @Test
101   public void testIterator(){
102     ArrayIntSet s = createArrayIntSet();
103     s.add(1);
104     s.add(2);
105     s.add(3);
106     
107     int i=0;
108     IntIterator it = s.intIterator();
109     while (it.hasNext()){
110       System.out.print(it.next());
111       i++;
112     }
113     System.out.println();
114     assertTrue(i == 3);
115     
116     assertTrue( !it.hasNext());
117     try {
118       it.next();
119       fail("iterator failed to throw NoSuchElementException");
120     } catch (NoSuchElementException nsex){
121       // that's expected
122     }
123     
124     it = s.intIterator(); // fresh one
125     while (it.hasNext()){
126       if (it.next() == 2){
127         it.remove();
128         assertTrue( s.size() == 2);
129         break;
130       }
131     }
132     i = it.next();
133     assertTrue(i == 3);
134     it.remove();
135     assertTrue(s.size() == 1);
136     assertTrue( !it.hasNext());
137     
138     s.add(42);
139     it = s.intIterator();
140     assertTrue(it.next() == 1);
141     it.remove();
142     assertTrue( it.next() == 42);
143     it.remove();
144     assertTrue( s.isEmpty());
145   }
146   
147   @Test
148   public void testComparison(){
149     int[][] a = {
150         {42, 0, 41},
151         {42, 41, 0},
152         {0, 42, 41},
153         {0, 41, 42},
154         {41, 0, 42},
155         {41, 42, 0}
156     };
157     
158     ArrayIntSet[] set = new ArrayIntSet[a.length];
159     
160     for (int i=0; i< a.length; i++) {
161       set[i]= createArrayIntSet();
162       int[] v = a[i];
163       for (int j=0; j<v.length; j++) {
164         set[i].add(v[j]);
165       }
166     }
167     
168     ArrayIntSet s1 = null, s2 = null;
169     for (int i=1; i< a.length; i++) {
170       s1 = set[i-1];
171       s2 = set[i];
172       System.out.println("comparing " + s1 + " with " + s2);
173       
174       assertTrue(s1.hashCode() == s2.hashCode());
175       assertTrue(s1.equals(s2));
176     }
177         
178     // inverse test
179     s2.remove(41);
180     assertFalse( s1.hashCode() == s2.hashCode());
181     assertFalse( s1.equals(s2));
182   }
183 }