ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArraySetTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11 + import java.io.*;
12  
13 < public class CopyOnWriteArraySetTest extends TestCase{
13 > public class CopyOnWriteArraySetTest extends JSR166TestCase {
14      
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 19 | Line 20 | public class CopyOnWriteArraySetTest ext
20          return new TestSuite(CopyOnWriteArraySetTest.class);
21      }
22  
23 <    static CopyOnWriteArraySet fullSet(int n){
23 >    static CopyOnWriteArraySet populatedSet(int n){
24          CopyOnWriteArraySet a = new CopyOnWriteArraySet();
25          assertTrue(a.isEmpty());
26          for (int i = 0; i < n; ++i)
# Line 30 | Line 31 | public class CopyOnWriteArraySetTest ext
31      }
32  
33      /**
34 <     *  Test to verify addAll correctly adds each element from the given collection
34 >     * Default-constructed set is empty
35       */
36 <    public void testAddAll(){
37 <        CopyOnWriteArraySet full = fullSet(3);
36 >    public void testConstructor() {
37 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
38 >        assertTrue(a.isEmpty());
39 >    }
40 >
41 >    /**
42 >     * Collection-constructed list holds all of its elements
43 >     */
44 >    public void testConstructor3() {
45 >        Integer[] ints = new Integer[SIZE];
46 >        for (int i = 0; i < SIZE-1; ++i)
47 >            ints[i] = new Integer(i);
48 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
49 >        for (int i = 0; i < SIZE; ++i)
50 >            assertTrue(a.contains(ints[i]));
51 >    }
52 >        
53 >
54 >    /**
55 >     *   addAll correctly adds each element from the given collection
56 >     */
57 >    public void testAddAll() {
58 >        CopyOnWriteArraySet full = populatedSet(3);
59          Vector v = new Vector();
60 <        v.add(new Integer(3));
61 <        v.add(new Integer(4));
62 <        v.add(new Integer(5));
60 >        v.add(three);
61 >        v.add(four);
62 >        v.add(five);
63          full.addAll(v);
64          assertEquals(6, full.size());
65      }
66  
67      /**
68 <     *  Test to verify addAllAbsent adds each element from the given collection that did not
68 >     *   addAllAbsent adds each element from the given collection that did not
69       *  already exist in the List
70       */
71 <    public void testAddAll2(){
72 <        CopyOnWriteArraySet full = fullSet(3);
71 >    public void testAddAll2() {
72 >        CopyOnWriteArraySet full = populatedSet(3);
73          Vector v = new Vector();
74 <        v.add(new Integer(3));
75 <        v.add(new Integer(4));
76 <        v.add(new Integer(1)); // will not add this element
74 >        v.add(three);
75 >        v.add(four);
76 >        v.add(one); // will not add this element
77          full.addAll(v);
78          assertEquals(5, full.size());
79      }
80  
81      /**
82 <     *  Test to verify addIfAbsent will not add the element if it already exists in the list
82 >     *   addIfAbsent will not add the element if it already exists in the list
83       */
84 <    public void testAdd2(){
85 <        CopyOnWriteArraySet full = fullSet(3);
86 <        full.add(new Integer(1));
84 >    public void testAdd2() {
85 >        CopyOnWriteArraySet full = populatedSet(3);
86 >        full.add(one);
87          assertEquals(3, full.size());
88      }
89  
90      /**
91 <     *  test to verify addIfAbsent correctly adds the element when it does not exist in the list
91 >     *   addIfAbsent correctly adds the element when it does not exist
92 >     *   in the list
93       */
94 <    public void testAdd3(){
95 <        CopyOnWriteArraySet full = fullSet(3);
96 <        full.add(new Integer(3));
97 <        assertTrue(full.contains(new Integer(3)));
94 >    public void testAdd3() {
95 >        CopyOnWriteArraySet full = populatedSet(3);
96 >        full.add(three);
97 >        assertTrue(full.contains(three));
98      }
99  
100      /**
101 <     *  Test to verify clear correctly removes all elements from the list
101 >     *   clear correctly removes all elements from the list
102       */
103 <    public void testClear(){
104 <        CopyOnWriteArraySet full = fullSet(3);
103 >    public void testClear() {
104 >        CopyOnWriteArraySet full = populatedSet(3);
105          full.clear();
106          assertEquals(0, full.size());
107      }
108  
109      /**
110 <     *  Test to verify contains returns the correct values
110 >     *   contains returns the correct values
111       */
112 <    public void testContains(){
113 <        CopyOnWriteArraySet full = fullSet(3);
114 <        assertTrue(full.contains(new Integer(1)));
115 <        assertFalse(full.contains(new Integer(5)));
112 >    public void testContains() {
113 >        CopyOnWriteArraySet full = populatedSet(3);
114 >        assertTrue(full.contains(one));
115 >        assertFalse(full.contains(five));
116      }
117  
118 +    /**
119 +     * Sets with equal elements are equal
120 +     */
121      public void testEquals() {
122 <        CopyOnWriteArraySet a = fullSet(3);
123 <        CopyOnWriteArraySet b = fullSet(3);
122 >        CopyOnWriteArraySet a = populatedSet(3);
123 >        CopyOnWriteArraySet b = populatedSet(3);
124          assertTrue(a.equals(b));
125          assertTrue(b.equals(a));
126          assertEquals(a.hashCode(), b.hashCode());
127 <        a.add(new Integer(-1));
127 >        a.add(m1);
128          assertFalse(a.equals(b));
129          assertFalse(b.equals(a));
130 <        b.add(new Integer(-1));
130 >        b.add(m1);
131          assertTrue(a.equals(b));
132          assertTrue(b.equals(a));
133          assertEquals(a.hashCode(), b.hashCode());
# Line 109 | Line 135 | public class CopyOnWriteArraySetTest ext
135  
136      
137      /**
138 <     *  Test to verify containsAll returns the correct values
138 >     *   containsAll returns the correct values
139       */
140 <    public void testContainsAll(){
141 <        CopyOnWriteArraySet full = fullSet(3);
140 >    public void testContainsAll() {
141 >        CopyOnWriteArraySet full = populatedSet(3);
142          Vector v = new Vector();
143 <        v.add(new Integer(1));
144 <        v.add(new Integer(2));
143 >        v.add(one);
144 >        v.add(two);
145          assertTrue(full.containsAll(v));
146 <        v.add(new Integer(6));
146 >        v.add(six);
147          assertFalse(full.containsAll(v));
148      }
149  
150      /**
151 <     *  Test to verify isEmpty returns the correct values
151 >     *   isEmpty returns the correct values
152       */
153 <    public void testIsEmpty(){
153 >    public void testIsEmpty() {
154          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
155 <        CopyOnWriteArraySet full = fullSet(3);
155 >        CopyOnWriteArraySet full = populatedSet(3);
156          assertTrue(empty.isEmpty());
157          assertFalse(full.isEmpty());
158      }
159  
160      /**
161 <     *  Test to verify iterator() returns an iterator containing the elements of the list
161 >     *   iterator() returns an iterator containing the elements of the list
162       */
163 <    public void testIterator(){
164 <        CopyOnWriteArraySet full = fullSet(3);
163 >    public void testIterator() {
164 >        CopyOnWriteArraySet full = populatedSet(3);
165          Iterator i = full.iterator();
166          int j;
167          for(j = 0; i.hasNext(); j++)
# Line 143 | Line 169 | public class CopyOnWriteArraySetTest ext
169          assertEquals(3, j);
170      }
171  
172 +    /**
173 +     * iterator remove is unsupported
174 +     */
175      public void testIteratorRemove () {
176 <        CopyOnWriteArraySet full = fullSet(3);
176 >        CopyOnWriteArraySet full = populatedSet(3);
177          Iterator it = full.iterator();
178          it.next();
179          try {
180              it.remove();
181 <            fail("should throw");
181 >            shouldThrow();
182          }
183          catch (UnsupportedOperationException success) {}
184      }
185  
186 <    public void testToString(){
187 <        CopyOnWriteArraySet full = fullSet(3);
186 >    /**
187 >     * toString holds toString of elements
188 >     */
189 >    public void testToString() {
190 >        CopyOnWriteArraySet full = populatedSet(3);
191          String s = full.toString();
192          for (int i = 0; i < 3; ++i) {
193              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
# Line 164 | Line 196 | public class CopyOnWriteArraySetTest ext
196  
197  
198      /**
199 <     *  Test to verify removeAll correctly removes all elements from the given collection
199 >     *   removeAll correctly removes all elements from the given collection
200       */
201 <    public void testRemoveAll(){
202 <        CopyOnWriteArraySet full = fullSet(3);
201 >    public void testRemoveAll() {
202 >        CopyOnWriteArraySet full = populatedSet(3);
203          Vector v = new Vector();
204 <        v.add(new Integer(1));
205 <        v.add(new Integer(2));
204 >        v.add(one);
205 >        v.add(two);
206          full.removeAll(v);
207          assertEquals(1, full.size());
208      }
209  
210  
211 <    public void testRemove(){
212 <        CopyOnWriteArraySet full = fullSet(3);
213 <        full.remove(new Integer(1));
214 <        assertFalse(full.contains(new Integer(1)));
211 >    /**
212 >     * remove removes an element
213 >     */
214 >    public void testRemove() {
215 >        CopyOnWriteArraySet full = populatedSet(3);
216 >        full.remove(one);
217 >        assertFalse(full.contains(one));
218          assertEquals(2, full.size());
219      }
220  
221      /**
222 <     *  Test to verify size returns the correct values
222 >     *   size returns the correct values
223       */
224 <    public void testSize(){
224 >    public void testSize() {
225          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
226 <        CopyOnWriteArraySet full = fullSet(3);
226 >        CopyOnWriteArraySet full = populatedSet(3);
227          assertEquals(3, full.size());
228          assertEquals(0, empty.size());
229      }
230  
231      /**
232 <     *  Test to verify toArray returns an Object array containing all elements from the list
232 >     *   toArray returns an Object array containing all elements from the list
233       */
234 <    public void testToArray(){
235 <        CopyOnWriteArraySet full = fullSet(3);
234 >    public void testToArray() {
235 >        CopyOnWriteArraySet full = populatedSet(3);
236          Object[] o = full.toArray();
237          assertEquals(3, o.length);
238          assertEquals(0, ((Integer)o[0]).intValue());
# Line 206 | Line 241 | public class CopyOnWriteArraySetTest ext
241      }
242  
243      /**
244 <     *  test to verify toArray returns an Integer array containing all elements from the list
244 >     *   toArray returns an Integer array containing all elements from
245 >     *   the list
246       */
247 <    public void testToArray2(){
248 <        CopyOnWriteArraySet full = fullSet(3);
247 >    public void testToArray2() {
248 >        CopyOnWriteArraySet full = populatedSet(3);
249          Integer[] i = new Integer[3];
250          i = (Integer[])full.toArray(i);
251          assertEquals(3, i.length);
# Line 219 | Line 255 | public class CopyOnWriteArraySetTest ext
255      }
256  
257  
222
223    // Exception tests
224
258      /**
259 <     *  Test to verify toArray throws an ArrayStoreException when the given array
260 <     *  can not store the objects inside the list
259 >     *  toArray throws an ArrayStoreException when the given array can
260 >     *  not store the objects inside the list
261       */
262 <    public void testToArray_ArrayStoreException(){
263 <        try{
262 >    public void testToArray_ArrayStoreException() {
263 >        try {
264              CopyOnWriteArraySet c = new CopyOnWriteArraySet();
265              c.add("zfasdfsdf");
266              c.add("asdadasd");
267              c.toArray(new Long[5]);
268 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
269 <        }catch(ArrayStoreException e){}
268 >            shouldThrow();
269 >        } catch(ArrayStoreException e){}
270 >    }
271 >
272 >    /**
273 >     *
274 >     */
275 >    public void testSerialization() {
276 >        CopyOnWriteArraySet q = populatedSet(SIZE);
277 >
278 >        try {
279 >            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
280 >            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
281 >            out.writeObject(q);
282 >            out.close();
283 >
284 >            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
285 >            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
286 >            CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
287 >            assertEquals(q.size(), r.size());
288 >            assertTrue(q.equals(r));
289 >            assertTrue(r.equals(q));
290 >        } catch(Exception e){
291 >            unexpectedException();
292 >        }
293      }
294  
295   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines