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

Comparing jsr166/src/test/tck/ConcurrentSkipListSetTest.java (file contents):
Revision 1.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.52 by jsr166, Wed Jan 27 01:57:24 2021 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
10 < import java.io.*;
7 > import java.util.Arrays;
8 > import java.util.BitSet;
9 > import java.util.Collection;
10 > import java.util.Comparator;
11 > import java.util.Iterator;
12 > import java.util.NavigableSet;
13 > import java.util.NoSuchElementException;
14 > import java.util.Random;
15 > import java.util.Set;
16 > import java.util.SortedSet;
17 > import java.util.concurrent.ConcurrentSkipListSet;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class ConcurrentSkipListSetTest extends JSR166TestCase {
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run (suite());  
24 >        main(suite(), args);
25      }
26      public static Test suite() {
27 <        return new TestSuite(ConcurrentSkipListSetTest.class);
27 >        return new TestSuite(ConcurrentSkipListSetTest.class);
28      }
29  
30 <    static class MyReverseComparator implements Comparator {
30 >    static class MyReverseComparator implements Comparator {
31 >        @SuppressWarnings("unchecked")
32          public int compare(Object x, Object y) {
33 <            int i = ((Integer)x).intValue();
23 <            int j = ((Integer)y).intValue();
24 <            if (i < j) return 1;
25 <            if (i > j) return -1;
26 <            return 0;
33 >            return ((Comparable)y).compareTo(x);
34          }
35      }
36  
37      /**
38 <     * Create a set of given size containing consecutive
39 <     * Integers 0 ... n.
38 >     * Returns a new set of given size containing consecutive
39 >     * Items 0 ... n - 1.
40       */
41 <    private ConcurrentSkipListSet populatedSet(int n) {
42 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
41 >    private static ConcurrentSkipListSet<Item> populatedSet(int n) {
42 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
43          assertTrue(q.isEmpty());
44 <        for(int i = n-1; i >= 0; i-=2)
45 <            assertTrue(q.add(new Integer(i)));
46 <        for(int i = (n & 1); i < n; i+=2)
47 <            assertTrue(q.add(new Integer(i)));
44 >        for (int i = n - 1; i >= 0; i -= 2)
45 >            mustAdd(q, i);
46 >        for (int i = (n & 1); i < n; i += 2)
47 >            mustAdd(q, i);
48          assertFalse(q.isEmpty());
49 <        assertEquals(n, q.size());
49 >        mustEqual(n, q.size());
50          return q;
51      }
52  
53      /**
54 <     * Create set of first 5 ints
54 >     * Returns a new set of first 5 ints.
55       */
56 <    private ConcurrentSkipListSet set5() {
57 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
56 >    private static ConcurrentSkipListSet<Item> set5() {
57 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
58          assertTrue(q.isEmpty());
59          q.add(one);
60          q.add(two);
61          q.add(three);
62          q.add(four);
63          q.add(five);
64 <        assertEquals(5, q.size());
64 >        mustEqual(5, q.size());
65          return q;
66      }
67 <
67 >
68      /**
69       * A new set has unbounded capacity
70       */
71      public void testConstructor1() {
72 <        assertEquals(0, new ConcurrentSkipListSet().size());
72 >        mustEqual(0, new ConcurrentSkipListSet<Item>().size());
73      }
74  
75      /**
# Line 70 | Line 77 | public class ConcurrentSkipListSetTest e
77       */
78      public void testConstructor3() {
79          try {
80 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
80 >            new ConcurrentSkipListSet<Item>((Collection<Item>)null);
81              shouldThrow();
82 <        }
76 <        catch (NullPointerException success) {}
82 >        } catch (NullPointerException success) {}
83      }
84  
85      /**
# Line 81 | Line 87 | public class ConcurrentSkipListSetTest e
87       */
88      public void testConstructor4() {
89          try {
90 <            Integer[] ints = new Integer[SIZE];
85 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
90 >            new ConcurrentSkipListSet<Item>(Arrays.asList(new Item[SIZE]));
91              shouldThrow();
92 <        }
88 <        catch (NullPointerException success) {}
92 >        } catch (NullPointerException success) {}
93      }
94  
95      /**
96       * Initializing from Collection with some null elements throws NPE
97       */
98      public void testConstructor5() {
99 +        Item[] items = new Item[2];
100 +        items[0] = zero;
101          try {
102 <            Integer[] ints = new Integer[SIZE];
97 <            for (int i = 0; i < SIZE-1; ++i)
98 <                ints[i] = new Integer(i);
99 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
102 >            new ConcurrentSkipListSet<Item>(Arrays.asList(items));
103              shouldThrow();
104 <        }
102 <        catch (NullPointerException success) {}
104 >        } catch (NullPointerException success) {}
105      }
106  
107      /**
108       * Set contains all elements of collection used to initialize
109       */
110      public void testConstructor6() {
111 <        try {
112 <            Integer[] ints = new Integer[SIZE];
113 <            for (int i = 0; i < SIZE; ++i)
114 <                ints[i] = new Integer(i);
113 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
114 <            for (int i = 0; i < SIZE; ++i)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
111 >        Item[] items = defaultItems;
112 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>(Arrays.asList(items));
113 >        for (int i = 0; i < SIZE; ++i)
114 >            mustEqual(items[i], q.pollFirst());
115      }
116  
117      /**
118       * The comparator used in constructor is used
119       */
120      public void testConstructor7() {
121 <        try {
122 <            MyReverseComparator cmp = new MyReverseComparator();
123 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
124 <            assertEquals(cmp, q.comparator());
125 <            Integer[] ints = new Integer[SIZE];
126 <            for (int i = 0; i < SIZE; ++i)
127 <                ints[i] = new Integer(i);
128 <            q.addAll(Arrays.asList(ints));
132 <            for (int i = SIZE-1; i >= 0; --i)
133 <                assertEquals(ints[i], q.pollFirst());
134 <        }
135 <        finally {}
121 >        MyReverseComparator cmp = new MyReverseComparator();
122 >        @SuppressWarnings("unchecked")
123 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>(cmp);
124 >        mustEqual(cmp, q.comparator());
125 >        Item[] items = defaultItems;
126 >        q.addAll(Arrays.asList(items));
127 >        for (int i = SIZE - 1; i >= 0; --i)
128 >            mustEqual(items[i], q.pollFirst());
129      }
130  
131      /**
132       * isEmpty is true before add, false after
133       */
134      public void testEmpty() {
135 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
135 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
136          assertTrue(q.isEmpty());
137 <        q.add(new Integer(1));
137 >        mustAdd(q, one);
138          assertFalse(q.isEmpty());
139 <        q.add(new Integer(2));
139 >        mustAdd(q, two);
140          q.pollFirst();
141          q.pollFirst();
142          assertTrue(q.isEmpty());
# Line 153 | Line 146 | public class ConcurrentSkipListSetTest e
146       * size changes when elements added and removed
147       */
148      public void testSize() {
149 <        ConcurrentSkipListSet q = populatedSet(SIZE);
149 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
150          for (int i = 0; i < SIZE; ++i) {
151 <            assertEquals(SIZE-i, q.size());
151 >            mustEqual(SIZE - i, q.size());
152              q.pollFirst();
153          }
154          for (int i = 0; i < SIZE; ++i) {
155 <            assertEquals(i, q.size());
156 <            q.add(new Integer(i));
155 >            mustEqual(i, q.size());
156 >            mustAdd(q, i);
157          }
158      }
159  
# Line 168 | Line 161 | public class ConcurrentSkipListSetTest e
161       * add(null) throws NPE
162       */
163      public void testAddNull() {
164 <        try {
165 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
164 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
165 >        try {
166              q.add(null);
167              shouldThrow();
168 <        } catch (NullPointerException success) { }  
168 >        } catch (NullPointerException success) {}
169      }
170  
171      /**
172       * Add of comparable element succeeds
173       */
174      public void testAdd() {
175 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
175 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
176          assertTrue(q.add(zero));
177          assertTrue(q.add(one));
178      }
# Line 188 | Line 181 | public class ConcurrentSkipListSetTest e
181       * Add of duplicate element fails
182       */
183      public void testAddDup() {
184 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
184 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
185          assertTrue(q.add(zero));
186          assertFalse(q.add(zero));
187      }
# Line 197 | Line 190 | public class ConcurrentSkipListSetTest e
190       * Add of non-Comparable throws CCE
191       */
192      public void testAddNonComparable() {
193 +        ConcurrentSkipListSet<Object> q = new ConcurrentSkipListSet<>();
194          try {
201            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
202            q.add(new Object());
195              q.add(new Object());
196              q.add(new Object());
197              shouldThrow();
198 +        } catch (ClassCastException success) {
199 +            assertTrue(q.size() < 2);
200 +            for (int i = 0, size = q.size(); i < size; i++)
201 +                assertSame(Object.class, q.pollFirst().getClass());
202 +            assertNull(q.pollFirst());
203 +            assertTrue(q.isEmpty());
204 +            mustEqual(0, q.size());
205          }
207        catch(ClassCastException success) {}
206      }
207  
208      /**
209       * addAll(null) throws NPE
210       */
211      public void testAddAll1() {
212 +        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
213          try {
215            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
214              q.addAll(null);
215              shouldThrow();
216 <        }
219 <        catch (NullPointerException success) {}
216 >        } catch (NullPointerException success) {}
217      }
218 +
219      /**
220       * addAll of a collection with null elements throws NPE
221       */
222      public void testAddAll2() {
223 +        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
224 +        Item[] items = new Item[SIZE];
225          try {
226 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
227 <            Integer[] ints = new Integer[SIZE];
228 <            q.addAll(Arrays.asList(ints));
226 >            q.addAll(Arrays.asList(items));
227              shouldThrow();
228 <        }
231 <        catch (NullPointerException success) {}
228 >        } catch (NullPointerException success) {}
229      }
230 +
231      /**
232       * addAll of a collection with any null elements throws NPE after
233       * possibly adding some elements
234       */
235      public void testAddAll3() {
236 +        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
237 +        Item[] items = new Item[2]; items[0] = zero;
238          try {
239 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
240 <            Integer[] ints = new Integer[SIZE];
241 <            for (int i = 0; i < SIZE-1; ++i)
242 <                ints[i] = new Integer(i);
243 <            q.addAll(Arrays.asList(ints));
239 >            q.addAll(Arrays.asList(items));
240              shouldThrow();
241 <        }
246 <        catch (NullPointerException success) {}
241 >        } catch (NullPointerException success) {}
242      }
243  
244      /**
245       * Set contains all elements of successful addAll
246       */
247      public void testAddAll5() {
248 <        try {
249 <            Integer[] empty = new Integer[0];
250 <            Integer[] ints = new Integer[SIZE];
251 <            for (int i = 0; i < SIZE; ++i)
252 <                ints[i] = new Integer(SIZE-1-i);
253 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
254 <            assertFalse(q.addAll(Arrays.asList(empty)));
260 <            assertTrue(q.addAll(Arrays.asList(ints)));
261 <            for (int i = 0; i < SIZE; ++i)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
248 >        Item[] empty = new Item[0];
249 >        Item[] items = defaultItems;
250 >        ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
251 >        assertFalse(q.addAll(Arrays.asList(empty)));
252 >        assertTrue(q.addAll(Arrays.asList(items)));
253 >        for (int i = 0; i < SIZE; ++i)
254 >            mustEqual(i, q.pollFirst());
255      }
256  
257      /**
258       * pollFirst succeeds unless empty
259       */
260      public void testPollFirst() {
261 <        ConcurrentSkipListSet q = populatedSet(SIZE);
261 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
262          for (int i = 0; i < SIZE; ++i) {
263 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
263 >            mustEqual(i, q.pollFirst());
264          }
265 <        assertNull(q.pollFirst());
265 >        assertNull(q.pollFirst());
266      }
267  
268      /**
269       * pollLast succeeds unless empty
270       */
271      public void testPollLast() {
272 <        ConcurrentSkipListSet q = populatedSet(SIZE);
273 <        for (int i = SIZE-1; i >= 0; --i) {
274 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
272 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
273 >        for (int i = SIZE - 1; i >= 0; --i) {
274 >            mustEqual(i, q.pollLast());
275          }
276 <        assertNull(q.pollFirst());
276 >        assertNull(q.pollFirst());
277      }
278  
289
279      /**
280       * remove(x) removes x and returns true if present
281       */
282      public void testRemoveElement() {
283 <        ConcurrentSkipListSet q = populatedSet(SIZE);
284 <        for (int i = 1; i < SIZE; i+=2) {
285 <            assertTrue(q.remove(new Integer(i)));
286 <        }
287 <        for (int i = 0; i < SIZE; i+=2) {
288 <            assertTrue(q.remove(new Integer(i)));
289 <            assertFalse(q.remove(new Integer(i+1)));
283 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
284 >        for (int i = 1; i < SIZE; i += 2) {
285 >            mustContain(q, i);
286 >            mustRemove(q, i);
287 >            mustNotContain(q, i);
288 >            mustContain(q, i - 1);
289 >        }
290 >        for (int i = 0; i < SIZE; i += 2) {
291 >            mustContain(q, i);
292 >            mustRemove(q, i);
293 >            mustNotContain(q, i);
294 >            mustNotRemove(q, i + 1);
295 >            mustNotContain(q, i + 1);
296          }
297          assertTrue(q.isEmpty());
298      }
299 <        
299 >
300      /**
301       * contains(x) reports true when elements added but not yet removed
302       */
303      public void testContains() {
304 <        ConcurrentSkipListSet q = populatedSet(SIZE);
304 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
305          for (int i = 0; i < SIZE; ++i) {
306 <            assertTrue(q.contains(new Integer(i)));
306 >            mustContain(q, i);
307              q.pollFirst();
308 <            assertFalse(q.contains(new Integer(i)));
308 >            mustNotContain(q, i);
309          }
310      }
311  
# Line 318 | Line 313 | public class ConcurrentSkipListSetTest e
313       * clear removes all elements
314       */
315      public void testClear() {
316 <        ConcurrentSkipListSet q = populatedSet(SIZE);
316 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
317          q.clear();
318          assertTrue(q.isEmpty());
319 <        assertEquals(0, q.size());
320 <        q.add(new Integer(1));
319 >        mustEqual(0, q.size());
320 >        mustAdd(q, one);
321          assertFalse(q.isEmpty());
322          q.clear();
323          assertTrue(q.isEmpty());
# Line 332 | Line 327 | public class ConcurrentSkipListSetTest e
327       * containsAll(c) is true when c contains a subset of elements
328       */
329      public void testContainsAll() {
330 <        ConcurrentSkipListSet q = populatedSet(SIZE);
331 <        ConcurrentSkipListSet p = new ConcurrentSkipListSet();
330 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
331 >        ConcurrentSkipListSet<Item> p = new ConcurrentSkipListSet<>();
332          for (int i = 0; i < SIZE; ++i) {
333              assertTrue(q.containsAll(p));
334              assertFalse(p.containsAll(q));
335 <            p.add(new Integer(i));
335 >            mustAdd(p, i);
336          }
337          assertTrue(p.containsAll(q));
338      }
# Line 346 | Line 341 | public class ConcurrentSkipListSetTest e
341       * retainAll(c) retains only those elements of c and reports true if changed
342       */
343      public void testRetainAll() {
344 <        ConcurrentSkipListSet q = populatedSet(SIZE);
345 <        ConcurrentSkipListSet p = populatedSet(SIZE);
344 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
345 >        ConcurrentSkipListSet<Item> p = populatedSet(SIZE);
346          for (int i = 0; i < SIZE; ++i) {
347              boolean changed = q.retainAll(p);
348              if (i == 0)
# Line 356 | Line 351 | public class ConcurrentSkipListSetTest e
351                  assertTrue(changed);
352  
353              assertTrue(q.containsAll(p));
354 <            assertEquals(SIZE-i, q.size());
354 >            mustEqual(SIZE - i, q.size());
355              p.pollFirst();
356          }
357      }
# Line 366 | Line 361 | public class ConcurrentSkipListSetTest e
361       */
362      public void testRemoveAll() {
363          for (int i = 1; i < SIZE; ++i) {
364 <            ConcurrentSkipListSet q = populatedSet(SIZE);
365 <            ConcurrentSkipListSet p = populatedSet(i);
364 >            ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
365 >            ConcurrentSkipListSet<Item> p = populatedSet(i);
366              assertTrue(q.removeAll(p));
367 <            assertEquals(SIZE-i, q.size());
367 >            mustEqual(SIZE - i, q.size());
368              for (int j = 0; j < i; ++j) {
369 <                Integer I = (Integer)(p.pollFirst());
375 <                assertFalse(q.contains(I));
369 >                mustNotContain(q, p.pollFirst());
370              }
371          }
372      }
373  
380    
381
374      /**
375       * lower returns preceding element
376       */
377      public void testLower() {
378 <        ConcurrentSkipListSet q = set5();
378 >        ConcurrentSkipListSet<Item> q = set5();
379          Object e1 = q.lower(three);
380 <        assertEquals(two, e1);
380 >        mustEqual(two, e1);
381  
382          Object e2 = q.lower(six);
383 <        assertEquals(five, e2);
383 >        mustEqual(five, e2);
384  
385          Object e3 = q.lower(one);
386          assertNull(e3);
387  
388          Object e4 = q.lower(zero);
389          assertNull(e4);
398
390      }
391  
392      /**
393       * higher returns next element
394       */
395      public void testHigher() {
396 <        ConcurrentSkipListSet q = set5();
396 >        ConcurrentSkipListSet<Item> q = set5();
397          Object e1 = q.higher(three);
398 <        assertEquals(four, e1);
398 >        mustEqual(four, e1);
399  
400          Object e2 = q.higher(zero);
401 <        assertEquals(one, e2);
401 >        mustEqual(one, e2);
402  
403          Object e3 = q.higher(five);
404          assertNull(e3);
405  
406          Object e4 = q.higher(six);
407          assertNull(e4);
417
408      }
409  
410      /**
411       * floor returns preceding element
412       */
413      public void testFloor() {
414 <        ConcurrentSkipListSet q = set5();
414 >        ConcurrentSkipListSet<Item> q = set5();
415          Object e1 = q.floor(three);
416 <        assertEquals(three, e1);
416 >        mustEqual(three, e1);
417  
418          Object e2 = q.floor(six);
419 <        assertEquals(five, e2);
419 >        mustEqual(five, e2);
420  
421          Object e3 = q.floor(one);
422 <        assertEquals(one, e3);
422 >        mustEqual(one, e3);
423  
424          Object e4 = q.floor(zero);
425          assertNull(e4);
436
426      }
427  
428      /**
429       * ceiling returns next element
430       */
431      public void testCeiling() {
432 <        ConcurrentSkipListSet q = set5();
432 >        ConcurrentSkipListSet<Item> q = set5();
433          Object e1 = q.ceiling(three);
434 <        assertEquals(three, e1);
434 >        mustEqual(three, e1);
435  
436          Object e2 = q.ceiling(zero);
437 <        assertEquals(one, e2);
437 >        mustEqual(one, e2);
438  
439          Object e3 = q.ceiling(five);
440 <        assertEquals(five, e3);
440 >        mustEqual(five, e3);
441  
442          Object e4 = q.ceiling(six);
443          assertNull(e4);
455
444      }
445  
446      /**
447 <     * toArray contains all elements
447 >     * toArray contains all elements in sorted order
448       */
449      public void testToArray() {
450 <        ConcurrentSkipListSet q = populatedSet(SIZE);
451 <        Object[] o = q.toArray();
452 <        Arrays.sort(o);
453 <        for(int i = 0; i < o.length; i++)
454 <            assertEquals(o[i], q.pollFirst());
450 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
451 >        Object[] a = q.toArray();
452 >        assertSame(Object[].class, a.getClass());
453 >        for (Object o : a)
454 >            assertSame(o, q.pollFirst());
455 >        assertTrue(q.isEmpty());
456      }
457  
458      /**
459 <     * toArray(a) contains all elements
459 >     * toArray(a) contains all elements in sorted order
460       */
461      public void testToArray2() {
462 <        ConcurrentSkipListSet q = populatedSet(SIZE);
463 <        Integer[] ints = new Integer[SIZE];
464 <        ints = (Integer[])q.toArray(ints);
465 <        Arrays.sort(ints);
466 <        for(int i = 0; i < ints.length; i++)
467 <            assertEquals(ints[i], q.pollFirst());
462 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
463 >        Item[] items = new Item[SIZE];
464 >        assertSame(items, q.toArray(items));
465 >        for (Item o : items)
466 >            assertSame(o, q.pollFirst());
467 >        assertTrue(q.isEmpty());
468      }
469 <    
469 >
470      /**
471       * iterator iterates through all elements
472       */
473      public void testIterator() {
474 <        ConcurrentSkipListSet q = populatedSet(SIZE);
475 <        int i = 0;
476 <        Iterator it = q.iterator();
477 <        while(it.hasNext()) {
478 <            assertTrue(q.contains(it.next()));
479 <            ++i;
480 <        }
492 <        assertEquals(i, SIZE);
474 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
475 >        Iterator<? extends Item> it = q.iterator();
476 >        int i;
477 >        for (i = 0; it.hasNext(); i++)
478 >            mustContain(q, it.next());
479 >        mustEqual(i, SIZE);
480 >        assertIteratorExhausted(it);
481      }
482  
483      /**
484       * iterator of empty set has no elements
485       */
486      public void testEmptyIterator() {
487 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
488 <        int i = 0;
489 <        Iterator it = q.iterator();
502 <        while(it.hasNext()) {
503 <            assertTrue(q.contains(it.next()));
504 <            ++i;
505 <        }
506 <        assertEquals(i, 0);
487 >        NavigableSet<Item> s = new ConcurrentSkipListSet<>();
488 >        assertIteratorExhausted(s.iterator());
489 >        assertIteratorExhausted(s.descendingSet().iterator());
490      }
491  
492      /**
493       * iterator.remove removes current element
494       */
495 <    public void testIteratorRemove () {
496 <        final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
497 <        q.add(new Integer(2));
498 <        q.add(new Integer(1));
499 <        q.add(new Integer(3));
495 >    public void testIteratorRemove() {
496 >        final ConcurrentSkipListSet<Item> q = new ConcurrentSkipListSet<>();
497 >        q.add(two);
498 >        q.add(one);
499 >        q.add(three);
500  
501 <        Iterator it = q.iterator();
501 >        Iterator<? extends Item> it = q.iterator();
502          it.next();
503          it.remove();
504  
505          it = q.iterator();
506 <        assertEquals(it.next(), new Integer(2));
507 <        assertEquals(it.next(), new Integer(3));
506 >        mustEqual(it.next(), two);
507 >        mustEqual(it.next(), three);
508          assertFalse(it.hasNext());
509      }
510  
528
511      /**
512       * toString contains toStrings of elements
513       */
514      public void testToString() {
515 <        ConcurrentSkipListSet q = populatedSet(SIZE);
515 >        ConcurrentSkipListSet<Item> q = populatedSet(SIZE);
516          String s = q.toString();
517          for (int i = 0; i < SIZE; ++i) {
518 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
518 >            assertTrue(s.contains(String.valueOf(i)));
519          }
520 <    }        
520 >    }
521  
522      /**
523 <     * A deserialized serialized set has same elements
523 >     * A cloned set equals original
524       */
525 <    public void testSerialization() {
526 <        ConcurrentSkipListSet q = populatedSet(SIZE);
527 <        try {
528 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
529 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
530 <            out.writeObject(q);
531 <            out.close();
532 <
533 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
534 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
535 <            ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
554 <            assertEquals(q.size(), r.size());
555 <            while (!q.isEmpty())
556 <                assertEquals(q.pollFirst(), r.pollFirst());
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
525 >    public void testClone() {
526 >        ConcurrentSkipListSet<Item> x = populatedSet(SIZE);
527 >        ConcurrentSkipListSet<Item> y = x.clone();
528 >
529 >        assertNotSame(x, y);
530 >        mustEqual(x.size(), y.size());
531 >        mustEqual(x, y);
532 >        mustEqual(y, x);
533 >        while (!x.isEmpty()) {
534 >            assertFalse(y.isEmpty());
535 >            mustEqual(x.pollFirst(), y.pollFirst());
536          }
537 +        assertTrue(y.isEmpty());
538 +    }
539 +
540 +    /**
541 +     * A deserialized/reserialized set equals original
542 +     */
543 +    public void testSerialization() throws Exception {
544 +        NavigableSet<Item> x = populatedSet(SIZE);
545 +        NavigableSet<Item> y = serialClone(x);
546 +
547 +        assertNotSame(x, y);
548 +        mustEqual(x.size(), y.size());
549 +        mustEqual(x, y);
550 +        mustEqual(y, x);
551 +        while (!x.isEmpty()) {
552 +            assertFalse(y.isEmpty());
553 +            mustEqual(x.pollFirst(), y.pollFirst());
554 +        }
555 +        assertTrue(y.isEmpty());
556      }
557  
558      /**
559       * subSet returns set with keys in requested range
560       */
561      public void testSubSetContents() {
562 <        ConcurrentSkipListSet set = set5();
563 <        SortedSet sm = set.subSet(two, four);
564 <        assertEquals(two, sm.first());
565 <        assertEquals(three, sm.last());
566 <        assertEquals(2, sm.size());
567 <        assertFalse(sm.contains(one));
568 <        assertTrue(sm.contains(two));
569 <        assertTrue(sm.contains(three));
570 <        assertFalse(sm.contains(four));
571 <        assertFalse(sm.contains(five));
572 <        Iterator i = sm.iterator();
573 <        Object k;
574 <        k = (Integer)(i.next());
575 <        assertEquals(two, k);
576 <        k = (Integer)(i.next());
582 <        assertEquals(three, k);
562 >        ConcurrentSkipListSet<Item> set = set5();
563 >        SortedSet<Item> sm = set.subSet(two, four);
564 >        mustEqual(two, sm.first());
565 >        mustEqual(three, sm.last());
566 >        mustEqual(2, sm.size());
567 >        mustNotContain(sm, one);
568 >        mustContain(sm, two);
569 >        mustContain(sm, three);
570 >        mustNotContain(sm, four);
571 >        mustNotContain(sm, five);
572 >        Iterator<? extends Item> i = sm.iterator();
573 >        Item k = i.next();
574 >        mustEqual(two, k);
575 >        k = i.next();
576 >        mustEqual(three, k);
577          assertFalse(i.hasNext());
578 <        Iterator j = sm.iterator();
578 >        Iterator<? extends Item> j = sm.iterator();
579          j.next();
580          j.remove();
581 <        assertFalse(set.contains(two));
582 <        assertEquals(4, set.size());
583 <        assertEquals(1, sm.size());
584 <        assertEquals(three, sm.first());
585 <        assertEquals(three, sm.last());
586 <        assertTrue(sm.remove(three));
581 >        mustNotContain(set, two);
582 >        mustEqual(4, set.size());
583 >        mustEqual(1, sm.size());
584 >        mustEqual(three, sm.first());
585 >        mustEqual(three, sm.last());
586 >        mustRemove(sm, three);
587          assertTrue(sm.isEmpty());
588 <        assertEquals(3, set.size());
588 >        mustEqual(3, set.size());
589      }
590  
591      public void testSubSetContents2() {
592 <        ConcurrentSkipListSet set = set5();
593 <        SortedSet sm = set.subSet(two, three);
594 <        assertEquals(1, sm.size());
595 <        assertEquals(two, sm.first());
596 <        assertEquals(two, sm.last());
597 <        assertFalse(sm.contains(one));
598 <        assertTrue(sm.contains(two));
599 <        assertFalse(sm.contains(three));
600 <        assertFalse(sm.contains(four));
601 <        assertFalse(sm.contains(five));
602 <        Iterator i = sm.iterator();
603 <        Object k;
604 <        k = (Integer)(i.next());
611 <        assertEquals(two, k);
592 >        ConcurrentSkipListSet<Item> set = set5();
593 >        SortedSet<Item> sm = set.subSet(two, three);
594 >        mustEqual(1, sm.size());
595 >        mustEqual(two, sm.first());
596 >        mustEqual(two, sm.last());
597 >        mustNotContain(sm, one);
598 >        mustContain(sm, two);
599 >        mustNotContain(sm, three);
600 >        mustNotContain(sm, four);
601 >        mustNotContain(sm, five);
602 >        Iterator<? extends Item> i = sm.iterator();
603 >        Item k = i.next();
604 >        mustEqual(two, k);
605          assertFalse(i.hasNext());
606 <        Iterator j = sm.iterator();
606 >        Iterator<? extends Item> j = sm.iterator();
607          j.next();
608          j.remove();
609 <        assertFalse(set.contains(two));
610 <        assertEquals(4, set.size());
611 <        assertEquals(0, sm.size());
609 >        mustNotContain(set, two);
610 >        mustEqual(4, set.size());
611 >        mustEqual(0, sm.size());
612          assertTrue(sm.isEmpty());
613 <        assertFalse(sm.remove(three));
614 <        assertEquals(4, set.size());
613 >        mustNotRemove(sm, three);
614 >        mustEqual(4, set.size());
615      }
616  
617      /**
618       * headSet returns set with keys in requested range
619       */
620      public void testHeadSetContents() {
621 <        ConcurrentSkipListSet set = set5();
622 <        SortedSet sm = set.headSet(four);
623 <        assertTrue(sm.contains(one));
624 <        assertTrue(sm.contains(two));
625 <        assertTrue(sm.contains(three));
626 <        assertFalse(sm.contains(four));
627 <        assertFalse(sm.contains(five));
628 <        Iterator i = sm.iterator();
629 <        Object k;
630 <        k = (Integer)(i.next());
631 <        assertEquals(one, k);
632 <        k = (Integer)(i.next());
633 <        assertEquals(two, k);
634 <        k = (Integer)(i.next());
642 <        assertEquals(three, k);
621 >        ConcurrentSkipListSet<Item> set = set5();
622 >        SortedSet<Item> sm = set.headSet(four);
623 >        mustContain(sm, one);
624 >        mustContain(sm, two);
625 >        mustContain(sm, three);
626 >        mustNotContain(sm, four);
627 >        mustNotContain(sm, five);
628 >        Iterator<? extends Item> i = sm.iterator();
629 >        Item k = i.next();
630 >        mustEqual(one, k);
631 >        k = i.next();
632 >        mustEqual(two, k);
633 >        k = i.next();
634 >        mustEqual(three, k);
635          assertFalse(i.hasNext());
636          sm.clear();
637          assertTrue(sm.isEmpty());
638 <        assertEquals(2, set.size());
639 <        assertEquals(four, set.first());
638 >        mustEqual(2, set.size());
639 >        mustEqual(four, set.first());
640      }
641  
642      /**
643       * tailSet returns set with keys in requested range
644       */
645      public void testTailSetContents() {
646 <        ConcurrentSkipListSet set = set5();
647 <        SortedSet sm = set.tailSet(two);
648 <        assertFalse(sm.contains(one));
649 <        assertTrue(sm.contains(two));
650 <        assertTrue(sm.contains(three));
651 <        assertTrue(sm.contains(four));
652 <        assertTrue(sm.contains(five));
653 <        Iterator i = sm.iterator();
654 <        Object k;
655 <        k = (Integer)(i.next());
656 <        assertEquals(two, k);
657 <        k = (Integer)(i.next());
658 <        assertEquals(three, k);
659 <        k = (Integer)(i.next());
660 <        assertEquals(four, k);
661 <        k = (Integer)(i.next());
662 <        assertEquals(five, k);
646 >        ConcurrentSkipListSet<Item> set = set5();
647 >        SortedSet<Item> sm = set.tailSet(two);
648 >        mustNotContain(sm, one);
649 >        mustContain(sm, two);
650 >        mustContain(sm, three);
651 >        mustContain(sm, four);
652 >        mustContain(sm, five);
653 >        mustContain(sm, two);
654 >        Iterator<? extends Item> i = sm.iterator();
655 >        Item k = i.next();
656 >        mustEqual(two, k);
657 >        k = i.next();
658 >        mustEqual(three, k);
659 >        k = i.next();
660 >        mustEqual(four, k);
661 >        k = i.next();
662 >        mustEqual(five, k);
663          assertFalse(i.hasNext());
664  
665 <        SortedSet ssm = sm.tailSet(four);
666 <        assertEquals(four, ssm.first());
667 <        assertEquals(five, ssm.last());
668 <        assertTrue(ssm.remove(four));
669 <        assertEquals(1, ssm.size());
670 <        assertEquals(3, sm.size());
671 <        assertEquals(4, set.size());
665 >        SortedSet<Item> ssm = sm.tailSet(four);
666 >        mustEqual(four, ssm.first());
667 >        mustEqual(five, ssm.last());
668 >        mustRemove(ssm, four);
669 >        mustEqual(1, ssm.size());
670 >        mustEqual(3, sm.size());
671 >        mustEqual(4, set.size());
672 >    }
673 >
674 >    Random rnd = new Random(666);
675 >
676 >    /**
677 >     * Subsets of subsets subdivide correctly
678 >     */
679 >    public void testRecursiveSubSets() throws Exception {
680 >        int setSize = expensiveTests ? 1000 : 100;
681 >        Class<?> cl = ConcurrentSkipListSet.class;
682 >
683 >        NavigableSet<Item> set = newSet(cl);
684 >        BitSet bs = new BitSet(setSize);
685 >
686 >        populate(set, setSize, bs);
687 >        check(set,                 0, setSize - 1, true, bs);
688 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
689 >
690 >        mutateSet(set, 0, setSize - 1, bs);
691 >        check(set,                 0, setSize - 1, true, bs);
692 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
693 >
694 >        bashSubSet(set.subSet(zero, true, itemFor(setSize), false),
695 >                   0, setSize - 1, true, bs);
696 >    }
697 >
698 >    /**
699 >     * addAll is idempotent
700 >     */
701 >    public void testAddAll_idempotent() throws Exception {
702 >        Set<Item> x = populatedSet(SIZE);
703 >        Set<Item> y = new ConcurrentSkipListSet<>(x);
704 >        y.addAll(x);
705 >        mustEqual(x, y);
706 >        mustEqual(y, x);
707 >    }
708 >
709 >    static NavigableSet<Item> newSet(Class<?> cl) throws Exception {
710 >        @SuppressWarnings("unchecked")
711 >        NavigableSet<Item> result =
712 >            (NavigableSet<Item>) cl.getConstructor().newInstance();
713 >        mustEqual(0, result.size());
714 >        assertFalse(result.iterator().hasNext());
715 >        return result;
716 >    }
717 >
718 >    void populate(NavigableSet<Item> set, int limit, BitSet bs) {
719 >        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
720 >            int element = rnd.nextInt(limit);
721 >            put(set, element, bs);
722 >        }
723 >    }
724 >
725 >    void mutateSet(NavigableSet<Item> set, int min, int max, BitSet bs) {
726 >        int size = set.size();
727 >        int rangeSize = max - min + 1;
728 >
729 >        // Remove a bunch of entries directly
730 >        for (int i = 0, n = rangeSize / 2; i < n; i++) {
731 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
732 >        }
733 >
734 >        // Remove a bunch of entries with iterator
735 >        for (Iterator<Item> it = set.iterator(); it.hasNext(); ) {
736 >            if (rnd.nextBoolean()) {
737 >                bs.clear(it.next().value);
738 >                it.remove();
739 >            }
740 >        }
741 >
742 >        // Add entries till we're back to original size
743 >        while (set.size() < size) {
744 >            int element = min + rnd.nextInt(rangeSize);
745 >            assertTrue(element >= min && element <= max);
746 >            put(set, element, bs);
747 >        }
748 >    }
749 >
750 >    void mutateSubSet(NavigableSet<Item> set, int min, int max,
751 >                      BitSet bs) {
752 >        int size = set.size();
753 >        int rangeSize = max - min + 1;
754 >
755 >        // Remove a bunch of entries directly
756 >        for (int i = 0, n = rangeSize / 2; i < n; i++) {
757 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
758 >        }
759 >
760 >        // Remove a bunch of entries with iterator
761 >        for (Iterator<Item> it = set.iterator(); it.hasNext(); ) {
762 >            if (rnd.nextBoolean()) {
763 >                bs.clear(it.next().value);
764 >                it.remove();
765 >            }
766 >        }
767 >
768 >        // Add entries till we're back to original size
769 >        while (set.size() < size) {
770 >            int element = min - 5 + rnd.nextInt(rangeSize + 10);
771 >            if (element >= min && element <= max) {
772 >                put(set, element, bs);
773 >            } else {
774 >                try {
775 >                    set.add(itemFor(element));
776 >                    shouldThrow();
777 >                } catch (IllegalArgumentException success) {}
778 >            }
779 >        }
780 >    }
781 >
782 >    void put(NavigableSet<Item> set, int element, BitSet bs) {
783 >        if (set.add(itemFor(element)))
784 >            bs.set(element);
785 >    }
786 >
787 >    void remove(NavigableSet<Item> set, int element, BitSet bs) {
788 >        if (set.remove(itemFor(element)))
789 >            bs.clear(element);
790 >    }
791 >
792 >    void bashSubSet(NavigableSet<Item> set,
793 >                    int min, int max, boolean ascending,
794 >                    BitSet bs) {
795 >        check(set, min, max, ascending, bs);
796 >        check(set.descendingSet(), min, max, !ascending, bs);
797 >
798 >        mutateSubSet(set, min, max, bs);
799 >        check(set, min, max, ascending, bs);
800 >        check(set.descendingSet(), min, max, !ascending, bs);
801 >
802 >        // Recurse
803 >        if (max - min < 2)
804 >            return;
805 >        int midPoint = (min + max) / 2;
806 >
807 >        // headSet - pick direction and endpoint inclusion randomly
808 >        boolean incl = rnd.nextBoolean();
809 >        NavigableSet<Item> hm = set.headSet(itemFor(midPoint), incl);
810 >        if (ascending) {
811 >            if (rnd.nextBoolean())
812 >                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
813 >            else
814 >                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
815 >                           false, bs);
816 >        } else {
817 >            if (rnd.nextBoolean())
818 >                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
819 >            else
820 >                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
821 >                           true, bs);
822 >        }
823 >
824 >        // tailSet - pick direction and endpoint inclusion randomly
825 >        incl = rnd.nextBoolean();
826 >        NavigableSet<Item> tm = set.tailSet(itemFor(midPoint),incl);
827 >        if (ascending) {
828 >            if (rnd.nextBoolean())
829 >                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
830 >            else
831 >                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
832 >                           false, bs);
833 >        } else {
834 >            if (rnd.nextBoolean()) {
835 >                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
836 >            } else {
837 >                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
838 >                           true, bs);
839 >            }
840 >        }
841 >
842 >        // subSet - pick direction and endpoint inclusion randomly
843 >        int rangeSize = max - min + 1;
844 >        int[] endpoints = new int[2];
845 >        endpoints[0] = min + rnd.nextInt(rangeSize);
846 >        endpoints[1] = min + rnd.nextInt(rangeSize);
847 >        Arrays.sort(endpoints);
848 >        boolean lowIncl = rnd.nextBoolean();
849 >        boolean highIncl = rnd.nextBoolean();
850 >        if (ascending) {
851 >            NavigableSet<Item> sm = set.subSet(
852 >                itemFor(endpoints[0]), lowIncl, itemFor(endpoints[1]), highIncl);
853 >            if (rnd.nextBoolean())
854 >                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
855 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
856 >            else
857 >                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
858 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
859 >        } else {
860 >            NavigableSet<Item> sm = set.subSet(
861 >                itemFor(endpoints[1]), highIncl, itemFor(endpoints[0]), lowIncl);
862 >            if (rnd.nextBoolean())
863 >                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
864 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
865 >            else
866 >                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
867 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
868 >        }
869 >    }
870 >
871 >    /**
872 >     * min and max are both inclusive.  If max < min, interval is empty.
873 >     */
874 >    void check(NavigableSet<Item> set,
875 >               final int min, final int max, final boolean ascending,
876 >               final BitSet bs) {
877 >        class ReferenceSet {
878 >            int lower(int element) {
879 >                return ascending ?
880 >                    lowerAscending(element) : higherAscending(element);
881 >            }
882 >            int floor(int element) {
883 >                return ascending ?
884 >                    floorAscending(element) : ceilingAscending(element);
885 >            }
886 >            int ceiling(int element) {
887 >                return ascending ?
888 >                    ceilingAscending(element) : floorAscending(element);
889 >            }
890 >            int higher(int element) {
891 >                return ascending ?
892 >                    higherAscending(element) : lowerAscending(element);
893 >            }
894 >            int first() {
895 >                return ascending ? firstAscending() : lastAscending();
896 >            }
897 >            int last() {
898 >                return ascending ? lastAscending() : firstAscending();
899 >            }
900 >            int lowerAscending(int element) {
901 >                return floorAscending(element - 1);
902 >            }
903 >            int floorAscending(int element) {
904 >                if (element < min)
905 >                    return -1;
906 >                else if (element > max)
907 >                    element = max;
908 >
909 >                // BitSet should support this! Test would run much faster
910 >                while (element >= min) {
911 >                    if (bs.get(element))
912 >                        return element;
913 >                    element--;
914 >                }
915 >                return -1;
916 >            }
917 >            int ceilingAscending(int element) {
918 >                if (element < min)
919 >                    element = min;
920 >                else if (element > max)
921 >                    return -1;
922 >                int result = bs.nextSetBit(element);
923 >                return result > max ? -1 : result;
924 >            }
925 >            int higherAscending(int element) {
926 >                return ceilingAscending(element + 1);
927 >            }
928 >            private int firstAscending() {
929 >                int result = ceilingAscending(min);
930 >                return result > max ? -1 : result;
931 >            }
932 >            private int lastAscending() {
933 >                int result = floorAscending(max);
934 >                return result < min ? -1 : result;
935 >            }
936 >        }
937 >        ReferenceSet rs = new ReferenceSet();
938 >
939 >        // Test contents using containsElement
940 >        int size = 0;
941 >        for (int i = min; i <= max; i++) {
942 >            boolean bsContainsI = bs.get(i);
943 >            mustEqual(bsContainsI, set.contains(itemFor(i)));
944 >            if (bsContainsI)
945 >                size++;
946 >        }
947 >        mustEqual(size, set.size());
948 >
949 >        // Test contents using contains elementSet iterator
950 >        int size2 = 0;
951 >        int previousElement = -1;
952 >        for (Item element : set) {
953 >            assertTrue(bs.get(element.value));
954 >            size2++;
955 >            assertTrue(previousElement < 0 || (ascending ?
956 >                element.value - previousElement > 0 : element.value - previousElement < 0));
957 >            previousElement = element.value;
958 >        }
959 >        mustEqual(size2, size);
960 >
961 >        // Test navigation ops
962 >        for (int element = min - 1; element <= max + 1; element++) {
963 >            Item e = itemFor(element);
964 >            assertEq(set.lower(e), rs.lower(element));
965 >            assertEq(set.floor(e), rs.floor(element));
966 >            assertEq(set.higher(e), rs.higher(element));
967 >            assertEq(set.ceiling(e), rs.ceiling(element));
968 >        }
969 >
970 >        // Test extrema
971 >        if (set.size() != 0) {
972 >            assertEq(set.first(), rs.first());
973 >            assertEq(set.last(), rs.last());
974 >        } else {
975 >            mustEqual(rs.first(), -1);
976 >            mustEqual(rs.last(),  -1);
977 >            try {
978 >                set.first();
979 >                shouldThrow();
980 >            } catch (NoSuchElementException success) {}
981 >            try {
982 >                set.last();
983 >                shouldThrow();
984 >            } catch (NoSuchElementException success) {}
985 >        }
986 >    }
987 >
988 >    static void assertEq(Item i, int j) {
989 >        if (i == null)
990 >            mustEqual(j, -1);
991 >        else
992 >            mustEqual(i, j);
993 >    }
994 >
995 >    static boolean eq(Item i, int j) {
996 >        return (i == null) ? j == -1 : i.value == j;
997      }
998  
999   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines