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

Comparing jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java (file contents):
Revision 1.12 by jsr166, Tue Dec 1 09:48:13 2009 UTC vs.
Revision 1.29 by jsr166, Sat Jan 17 22:55:06 2015 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.Comparator;
9 > import java.util.Iterator;
10 > import java.util.NavigableSet;
11 > import java.util.SortedSet;
12 > import java.util.concurrent.ConcurrentSkipListSet;
13 >
14 > import junit.framework.Test;
15 > import junit.framework.TestSuite;
16  
17   public class ConcurrentSkipListSubSetTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(ConcurrentSkipListSubSetTest.class);
# Line 24 | Line 29 | public class ConcurrentSkipListSubSetTes
29      }
30  
31      /**
32 <     * Create a set of given size containing consecutive
32 >     * Returns a new set of given size containing consecutive
33       * Integers 0 ... n.
34       */
35 <    private NavigableSet populatedSet(int n) {
36 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
35 >    private NavigableSet<Integer> populatedSet(int n) {
36 >        ConcurrentSkipListSet<Integer> q =
37 >            new ConcurrentSkipListSet<Integer>();
38          assertTrue(q.isEmpty());
39  
40 <        for (int i = n-1; i >= 0; i-=2)
40 >        for (int i = n-1; i >= 0; i -= 2)
41              assertTrue(q.add(new Integer(i)));
42 <        for (int i = (n & 1); i < n; i+=2)
42 >        for (int i = (n & 1); i < n; i += 2)
43              assertTrue(q.add(new Integer(i)));
44          assertTrue(q.add(new Integer(-n)));
45          assertTrue(q.add(new Integer(n)));
# Line 44 | Line 50 | public class ConcurrentSkipListSubSetTes
50      }
51  
52      /**
53 <     * Create set of first 5 ints
53 >     * Returns a new set of first 5 ints.
54       */
55      private NavigableSet set5() {
56          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
# Line 62 | Line 68 | public class ConcurrentSkipListSubSetTes
68      }
69  
70      /**
71 <     * Create set of first 5 negative ints
71 >     * Returns a new set of first 5 negative ints.
72       */
73      private NavigableSet dset5() {
74          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
# Line 96 | Line 102 | public class ConcurrentSkipListSubSetTes
102          assertEquals(0, set0().size());
103      }
104  
99
105      /**
106       * isEmpty is true before add, false after
107       */
# Line 167 | Line 172 | public class ConcurrentSkipListSubSetTes
172          } catch (ClassCastException success) {}
173      }
174  
170
175      /**
176       * addAll(null) throws NPE
177       */
# Line 178 | Line 182 | public class ConcurrentSkipListSubSetTes
182              shouldThrow();
183          } catch (NullPointerException success) {}
184      }
185 +
186      /**
187       * addAll of a collection with null elements throws NPE
188       */
# Line 189 | Line 194 | public class ConcurrentSkipListSubSetTes
194              shouldThrow();
195          } catch (NullPointerException success) {}
196      }
197 +
198      /**
199       * addAll of a collection with any null elements throws NPE after
200       * possibly adding some elements
# Line 235 | Line 241 | public class ConcurrentSkipListSubSetTes
241       */
242      public void testRemoveElement() {
243          NavigableSet q = populatedSet(SIZE);
244 <        for (int i = 1; i < SIZE; i+=2) {
245 <            assertTrue(q.remove(new Integer(i)));
246 <        }
247 <        for (int i = 0; i < SIZE; i+=2) {
248 <            assertTrue(q.remove(new Integer(i)));
249 <            assertFalse(q.remove(new Integer(i+1)));
244 >        for (int i = 1; i < SIZE; i += 2) {
245 >            assertTrue(q.contains(i));
246 >            assertTrue(q.remove(i));
247 >            assertFalse(q.contains(i));
248 >            assertTrue(q.contains(i-1));
249 >        }
250 >        for (int i = 0; i < SIZE; i += 2) {
251 >            assertTrue(q.contains(i));
252 >            assertTrue(q.remove(i));
253 >            assertFalse(q.contains(i));
254 >            assertFalse(q.remove(i+1));
255 >            assertFalse(q.contains(i+1));
256          }
257          assertTrue(q.isEmpty());
258      }
# Line 314 | Line 326 | public class ConcurrentSkipListSubSetTes
326              assertTrue(q.removeAll(p));
327              assertEquals(SIZE-i, q.size());
328              for (int j = 0; j < i; ++j) {
329 <                Integer I = (Integer)(p.pollFirst());
330 <                assertFalse(q.contains(I));
329 >                Integer x = (Integer)(p.pollFirst());
330 >                assertFalse(q.contains(x));
331              }
332          }
333      }
334  
323
324
335      /**
336       * lower returns preceding element
337       */
# Line 395 | Line 405 | public class ConcurrentSkipListSubSetTes
405      }
406  
407      /**
408 <     * toArray contains all elements
408 >     * toArray contains all elements in sorted order
409       */
410      public void testToArray() {
411          NavigableSet q = populatedSet(SIZE);
412          Object[] o = q.toArray();
403        Arrays.sort(o);
413          for (int i = 0; i < o.length; i++)
414 <            assertEquals(o[i], q.pollFirst());
414 >            assertSame(o[i], q.pollFirst());
415      }
416  
417      /**
418 <     * toArray(a) contains all elements
418 >     * toArray(a) contains all elements in sorted order
419       */
420      public void testToArray2() {
421 <        NavigableSet q = populatedSet(SIZE);
421 >        NavigableSet<Integer> q = populatedSet(SIZE);
422          Integer[] ints = new Integer[SIZE];
423 <        ints = (Integer[])q.toArray(ints);
424 <        Arrays.sort(ints);
423 >        Integer[] array = q.toArray(ints);
424 >        assertSame(ints, array);
425          for (int i = 0; i < ints.length; i++)
426 <            assertEquals(ints[i], q.pollFirst());
426 >            assertSame(ints[i], q.pollFirst());
427      }
428  
429      /**
# Line 422 | Line 431 | public class ConcurrentSkipListSubSetTes
431       */
432      public void testIterator() {
433          NavigableSet q = populatedSet(SIZE);
425        int i = 0;
434          Iterator it = q.iterator();
435 <        while (it.hasNext()) {
435 >        int i;
436 >        for (i = 0; it.hasNext(); i++)
437              assertTrue(q.contains(it.next()));
429            ++i;
430        }
438          assertEquals(i, SIZE);
439 +        assertIteratorExhausted(it);
440      }
441  
442      /**
443       * iterator of empty set has no elements
444       */
445      public void testEmptyIterator() {
446 <        NavigableSet q = set0();
439 <        int i = 0;
440 <        Iterator it = q.iterator();
441 <        while (it.hasNext()) {
442 <            assertTrue(q.contains(it.next()));
443 <            ++i;
444 <        }
445 <        assertEquals(i, 0);
446 >        assertIteratorExhausted(set0().iterator());
447      }
448  
449      /**
450       * iterator.remove removes current element
451       */
452 <    public void testIteratorRemove () {
452 >    public void testIteratorRemove() {
453          final NavigableSet q = set0();
454          q.add(new Integer(2));
455          q.add(new Integer(1));
# Line 464 | Line 465 | public class ConcurrentSkipListSubSetTes
465          assertFalse(it.hasNext());
466      }
467  
467
468      /**
469       * toString contains toStrings of elements
470       */
# Line 472 | Line 472 | public class ConcurrentSkipListSubSetTes
472          NavigableSet q = populatedSet(SIZE);
473          String s = q.toString();
474          for (int i = 0; i < SIZE; ++i) {
475 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
475 >            assertTrue(s.contains(String.valueOf(i)));
476          }
477      }
478  
# Line 480 | Line 480 | public class ConcurrentSkipListSubSetTes
480       * A deserialized serialized set has same elements
481       */
482      public void testSerialization() throws Exception {
483 <        NavigableSet q = populatedSet(SIZE);
484 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
485 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
486 <        out.writeObject(q);
487 <        out.close();
488 <
489 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
490 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
491 <        NavigableSet r = (NavigableSet)in.readObject();
492 <        assertEquals(q.size(), r.size());
493 <        while (!q.isEmpty())
494 <            assertEquals(q.pollFirst(), r.pollFirst());
483 >        NavigableSet x = populatedSet(SIZE);
484 >        NavigableSet y = serialClone(x);
485 >
486 >        assertNotSame(y, x);
487 >        assertEquals(x.size(), y.size());
488 >        assertEquals(x, y);
489 >        assertEquals(y, x);
490 >        while (!x.isEmpty()) {
491 >            assertFalse(y.isEmpty());
492 >            assertEquals(x.pollFirst(), y.pollFirst());
493 >        }
494 >        assertTrue(y.isEmpty());
495      }
496  
497      /**
# Line 669 | Line 669 | public class ConcurrentSkipListSubSetTes
669          } catch (ClassCastException success) {}
670      }
671  
672
672      /**
673       * addAll(null) throws NPE
674       */
# Line 680 | Line 679 | public class ConcurrentSkipListSubSetTes
679              shouldThrow();
680          } catch (NullPointerException success) {}
681      }
682 +
683      /**
684       * addAll of a collection with null elements throws NPE
685       */
# Line 691 | Line 691 | public class ConcurrentSkipListSubSetTes
691              shouldThrow();
692          } catch (NullPointerException success) {}
693      }
694 +
695      /**
696       * addAll of a collection with any null elements throws NPE after
697       * possibly adding some elements
# Line 737 | Line 738 | public class ConcurrentSkipListSubSetTes
738       */
739      public void testDescendingRemoveElement() {
740          NavigableSet q = populatedSet(SIZE);
741 <        for (int i = 1; i < SIZE; i+=2) {
741 >        for (int i = 1; i < SIZE; i += 2) {
742              assertTrue(q.remove(new Integer(i)));
743          }
744 <        for (int i = 0; i < SIZE; i+=2) {
744 >        for (int i = 0; i < SIZE; i += 2 ) {
745              assertTrue(q.remove(new Integer(i)));
746              assertFalse(q.remove(new Integer(i+1)));
747          }
# Line 816 | Line 817 | public class ConcurrentSkipListSubSetTes
817              assertTrue(q.removeAll(p));
818              assertEquals(SIZE-i, q.size());
819              for (int j = 0; j < i; ++j) {
820 <                Integer I = (Integer)(p.pollFirst());
821 <                assertFalse(q.contains(I));
820 >                Integer x = (Integer)(p.pollFirst());
821 >                assertFalse(q.contains(x));
822              }
823          }
824      }
825  
825
826
826      /**
827       * lower returns preceding element
828       */
# Line 913 | Line 912 | public class ConcurrentSkipListSubSetTes
912      public void testDescendingToArray2() {
913          NavigableSet q = populatedSet(SIZE);
914          Integer[] ints = new Integer[SIZE];
915 <        ints = (Integer[])q.toArray(ints);
915 >        assertSame(ints, q.toArray(ints));
916          Arrays.sort(ints);
917          for (int i = 0; i < ints.length; i++)
918              assertEquals(ints[i], q.pollFirst());
# Line 944 | Line 943 | public class ConcurrentSkipListSubSetTes
943              assertTrue(q.contains(it.next()));
944              ++i;
945          }
946 <        assertEquals(i, 0);
946 >        assertEquals(0, i);
947      }
948  
949      /**
950       * iterator.remove removes current element
951       */
952 <    public void testDescendingIteratorRemove () {
952 >    public void testDescendingIteratorRemove() {
953          final NavigableSet q = dset0();
954          q.add(new Integer(2));
955          q.add(new Integer(1));
# Line 966 | Line 965 | public class ConcurrentSkipListSubSetTes
965          assertFalse(it.hasNext());
966      }
967  
969
968      /**
969       * toString contains toStrings of elements
970       */
# Line 974 | Line 972 | public class ConcurrentSkipListSubSetTes
972          NavigableSet q = populatedSet(SIZE);
973          String s = q.toString();
974          for (int i = 0; i < SIZE; ++i) {
975 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
975 >            assertTrue(s.contains(String.valueOf(i)));
976          }
977      }
978  
# Line 982 | Line 980 | public class ConcurrentSkipListSubSetTes
980       * A deserialized serialized set has same elements
981       */
982      public void testDescendingSerialization() throws Exception {
983 <        NavigableSet q = populatedSet(SIZE);
984 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
985 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
986 <        out.writeObject(q);
987 <        out.close();
988 <
989 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
990 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
991 <        NavigableSet r = (NavigableSet)in.readObject();
992 <        assertEquals(q.size(), r.size());
993 <        while (!q.isEmpty())
994 <            assertEquals(q.pollFirst(), r.pollFirst());
983 >        NavigableSet x = dset5();
984 >        NavigableSet y = serialClone(x);
985 >
986 >        assertNotSame(y, x);
987 >        assertEquals(x.size(), y.size());
988 >        assertEquals(x, y);
989 >        assertEquals(y, x);
990 >        while (!x.isEmpty()) {
991 >            assertFalse(y.isEmpty());
992 >            assertEquals(x.pollFirst(), y.pollFirst());
993 >        }
994 >        assertTrue(y.isEmpty());
995      }
996  
997      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines