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

Comparing jsr166/src/test/tck/TreeSubMapTest.java (file contents):
Revision 1.9 by jsr166, Sat Nov 21 17:38:06 2009 UTC vs.
Revision 1.23 by jsr166, Sat Apr 25 04:55:31 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.ArrayList;
8 > import java.util.Arrays;
9 > import java.util.Collection;
10 > import java.util.Iterator;
11 > import java.util.Map;
12 > import java.util.NavigableMap;
13 > import java.util.Set;
14 > import java.util.SortedMap;
15 > import java.util.TreeMap;
16 >
17 > import junit.framework.Test;
18 > import junit.framework.TestSuite;
19  
20   public class TreeSubMapTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());
22 >        main(suite(), args);
23      }
24      public static Test suite() {
25          return new TestSuite(TreeSubMapTest.class);
26      }
27  
28      /**
29 <     * Create a map from Integers 1-5 to Strings "A"-"E".
29 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
30       */
31      private static NavigableMap map5() {
32          TreeMap map = new TreeMap();
# Line 42 | Line 50 | public class TreeSubMapTest extends JSR1
50      }
51  
52      /**
53 <     * Create a map from Integers -5 to -1 to Strings "A"-"E".
53 >     * Returns a new map from Integers -5 to -1 to Strings "A"-"E".
54       */
55      private static NavigableMap dmap5() {
56          TreeMap map = new TreeMap();
# Line 64 | Line 72 | public class TreeSubMapTest extends JSR1
72      }
73  
74      /**
75 <     *  clear removes all pairs
75 >     * clear removes all pairs
76       */
77      public void testClear() {
78          NavigableMap map = map5();
79          map.clear();
80 <        assertEquals(map.size(), 0);
80 >        assertEquals(0, map.size());
81      }
82  
75
83      /**
84 <     *  Maps with same contents are equal
84 >     * Maps with same contents are equal
85       */
86      public void testEquals() {
87          NavigableMap map1 = map5();
# Line 87 | Line 94 | public class TreeSubMapTest extends JSR1
94      }
95  
96      /**
97 <     *  containsKey returns true for contained key
97 >     * containsKey returns true for contained key
98       */
99      public void testContainsKey() {
100          NavigableMap map = map5();
# Line 96 | Line 103 | public class TreeSubMapTest extends JSR1
103      }
104  
105      /**
106 <     *  containsValue returns true for held values
106 >     * containsValue returns true for held values
107       */
108      public void testContainsValue() {
109          NavigableMap map = map5();
# Line 105 | Line 112 | public class TreeSubMapTest extends JSR1
112      }
113  
114      /**
115 <     *  get returns the correct element at the given key,
116 <     *  or null if not present
115 >     * get returns the correct element at the given key,
116 >     * or null if not present
117       */
118      public void testGet() {
119          NavigableMap map = map5();
# Line 116 | Line 123 | public class TreeSubMapTest extends JSR1
123      }
124  
125      /**
126 <     *  isEmpty is true of empty map and false for non-empty
126 >     * isEmpty is true of empty map and false for non-empty
127       */
128      public void testIsEmpty() {
129          NavigableMap empty = map0();
# Line 126 | Line 133 | public class TreeSubMapTest extends JSR1
133      }
134  
135      /**
136 <     *   firstKey returns first key
136 >     * firstKey returns first key
137       */
138      public void testFirstKey() {
139          NavigableMap map = map5();
# Line 134 | Line 141 | public class TreeSubMapTest extends JSR1
141      }
142  
143      /**
144 <     *   lastKey returns last key
144 >     * lastKey returns last key
145       */
146      public void testLastKey() {
147          NavigableMap map = map5();
148          assertEquals(five, map.lastKey());
149      }
150  
144
151      /**
152 <     *   keySet returns a Set containing all the keys
152 >     * keySet returns a Set containing all the keys
153       */
154      public void testKeySet() {
155          NavigableMap map = map5();
# Line 157 | Line 163 | public class TreeSubMapTest extends JSR1
163      }
164  
165      /**
166 <     *   keySet is ordered
166 >     * keySet is ordered
167       */
168      public void testKeySetOrder() {
169          NavigableMap map = map5();
# Line 206 | Line 212 | public class TreeSubMapTest extends JSR1
212      }
213  
214      /**
215 <     *   putAll  adds all key-value pairs from the given map
215 >     * putAll adds all key-value pairs from the given map
216       */
217      public void testPutAll() {
218          NavigableMap empty = map0();
# Line 221 | Line 227 | public class TreeSubMapTest extends JSR1
227      }
228  
229      /**
230 <     *   remove removes the correct key-value pair from the map
230 >     * remove removes the correct key-value pair from the map
231       */
232      public void testRemove() {
233          NavigableMap map = map5();
# Line 246 | Line 252 | public class TreeSubMapTest extends JSR1
252  
253          Map.Entry e4 = map.lowerEntry(zero);
254          assertNull(e4);
249
255      }
256  
257      /**
# Line 265 | Line 270 | public class TreeSubMapTest extends JSR1
270  
271          Map.Entry e4 = map.higherEntry(six);
272          assertNull(e4);
268
273      }
274  
275      /**
# Line 284 | Line 288 | public class TreeSubMapTest extends JSR1
288  
289          Map.Entry e4 = map.floorEntry(zero);
290          assertNull(e4);
287
291      }
292  
293      /**
# Line 303 | Line 306 | public class TreeSubMapTest extends JSR1
306  
307          Map.Entry e4 = map.ceilingEntry(six);
308          assertNull(e4);
306
309      }
310  
311      /**
# Line 364 | Line 366 | public class TreeSubMapTest extends JSR1
366      }
367  
368      /**
369 <     *   size returns the correct values
369 >     * size returns the correct values
370       */
371      public void testSize() {
372          NavigableMap map = map5();
# Line 380 | Line 382 | public class TreeSubMapTest extends JSR1
382          NavigableMap map = map5();
383          String s = map.toString();
384          for (int i = 1; i <= 5; ++i) {
385 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
385 >            assertTrue(s.contains(String.valueOf(i)));
386          }
387      }
388  
# Line 390 | Line 392 | public class TreeSubMapTest extends JSR1
392       * get(null) of nonempty map throws NPE
393       */
394      public void testGet_NullPointerException() {
395 +        NavigableMap c = map5();
396          try {
394            NavigableMap c = map5();
397              c.get(null);
398              shouldThrow();
399          } catch (NullPointerException success) {}
# Line 401 | Line 403 | public class TreeSubMapTest extends JSR1
403       * containsKey(null) of nonempty map throws NPE
404       */
405      public void testContainsKey_NullPointerException() {
406 +        NavigableMap c = map5();
407          try {
405            NavigableMap c = map5();
408              c.containsKey(null);
409              shouldThrow();
410          } catch (NullPointerException success) {}
# Line 412 | Line 414 | public class TreeSubMapTest extends JSR1
414       * put(null,x) throws NPE
415       */
416      public void testPut1_NullPointerException() {
417 +        NavigableMap c = map5();
418          try {
416            NavigableMap c = map5();
419              c.put(null, "whatever");
420              shouldThrow();
421          } catch (NullPointerException success) {}
# Line 423 | Line 425 | public class TreeSubMapTest extends JSR1
425       * remove(null) throws NPE
426       */
427      public void testRemove1_NullPointerException() {
428 +        NavigableMap c = map5();
429          try {
427            NavigableMap c = map5();
430              c.remove(null);
431              shouldThrow();
432          } catch (NullPointerException success) {}
# Line 434 | Line 436 | public class TreeSubMapTest extends JSR1
436       * A deserialized map equals original
437       */
438      public void testSerialization() throws Exception {
439 <        NavigableMap q = map5();
439 >        NavigableMap x = map5();
440 >        NavigableMap y = serialClone(x);
441  
442 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
443 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
444 <        out.writeObject(q);
445 <        out.close();
446 <
444 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
445 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
446 <        NavigableMap r = (NavigableMap)in.readObject();
447 <        assertFalse(r.isEmpty());
448 <        assertEquals(q.size(), r.size());
449 <        assertTrue(q.equals(r));
450 <        assertTrue(r.equals(q));
442 >        assertNotSame(x, y);
443 >        assertEquals(x.size(), y.size());
444 >        assertEquals(x.toString(), y.toString());
445 >        assertEquals(x, y);
446 >        assertEquals(y, x);
447      }
448  
453
454
449      /**
450       * subMap returns map with keys in requested range
451       */
# Line 481 | Line 475 | public class TreeSubMapTest extends JSR1
475          assertEquals(1, sm.size());
476          assertEquals(three, sm.firstKey());
477          assertEquals(three, sm.lastKey());
478 <        assertTrue(sm.remove(three) != null);
478 >        assertEquals("C", sm.remove(three));
479          assertTrue(sm.isEmpty());
480          assertEquals(3, map.size());
481      }
# Line 509 | Line 503 | public class TreeSubMapTest extends JSR1
503          assertEquals(4, map.size());
504          assertEquals(0, sm.size());
505          assertTrue(sm.isEmpty());
506 <        assertTrue(sm.remove(three) == null);
506 >        assertSame(sm.remove(three), null);
507          assertEquals(4, map.size());
508      }
509  
# Line 581 | Line 575 | public class TreeSubMapTest extends JSR1
575          SortedMap ssm = sm.tailMap(four);
576          assertEquals(four, ssm.firstKey());
577          assertEquals(five, ssm.lastKey());
578 <        assertTrue(ssm.remove(four) != null);
578 >        assertEquals("D", ssm.remove(four));
579          assertEquals(1, ssm.size());
580          assertEquals(3, sm.size());
581          assertEquals(4, map.size());
582      }
583  
584      /**
585 <     *  clear removes all pairs
585 >     * clear removes all pairs
586       */
587      public void testDescendingClear() {
588          NavigableMap map = dmap5();
589          map.clear();
590 <        assertEquals(map.size(), 0);
590 >        assertEquals(0, map.size());
591      }
592  
599
593      /**
594 <     *  Maps with same contents are equal
594 >     * Maps with same contents are equal
595       */
596      public void testDescendingEquals() {
597          NavigableMap map1 = dmap5();
# Line 611 | Line 604 | public class TreeSubMapTest extends JSR1
604      }
605  
606      /**
607 <     *  containsKey returns true for contained key
607 >     * containsKey returns true for contained key
608       */
609      public void testDescendingContainsKey() {
610          NavigableMap map = dmap5();
# Line 620 | Line 613 | public class TreeSubMapTest extends JSR1
613      }
614  
615      /**
616 <     *  containsValue returns true for held values
616 >     * containsValue returns true for held values
617       */
618      public void testDescendingContainsValue() {
619          NavigableMap map = dmap5();
# Line 629 | Line 622 | public class TreeSubMapTest extends JSR1
622      }
623  
624      /**
625 <     *  get returns the correct element at the given key,
626 <     *  or null if not present
625 >     * get returns the correct element at the given key,
626 >     * or null if not present
627       */
628      public void testDescendingGet() {
629          NavigableMap map = dmap5();
# Line 640 | Line 633 | public class TreeSubMapTest extends JSR1
633      }
634  
635      /**
636 <     *  isEmpty is true of empty map and false for non-empty
636 >     * isEmpty is true of empty map and false for non-empty
637       */
638      public void testDescendingIsEmpty() {
639          NavigableMap empty = dmap0();
# Line 650 | Line 643 | public class TreeSubMapTest extends JSR1
643      }
644  
645      /**
646 <     *   firstKey returns first key
646 >     * firstKey returns first key
647       */
648      public void testDescendingFirstKey() {
649          NavigableMap map = dmap5();
# Line 658 | Line 651 | public class TreeSubMapTest extends JSR1
651      }
652  
653      /**
654 <     *   lastKey returns last key
654 >     * lastKey returns last key
655       */
656      public void testDescendingLastKey() {
657          NavigableMap map = dmap5();
658          assertEquals(m5, map.lastKey());
659      }
660  
668
661      /**
662 <     *   keySet returns a Set containing all the keys
662 >     * keySet returns a Set containing all the keys
663       */
664      public void testDescendingKeySet() {
665          NavigableMap map = dmap5();
# Line 681 | Line 673 | public class TreeSubMapTest extends JSR1
673      }
674  
675      /**
676 <     *   keySet is ordered
676 >     * keySet is ordered
677       */
678      public void testDescendingKeySetOrder() {
679          NavigableMap map = dmap5();
# Line 711 | Line 703 | public class TreeSubMapTest extends JSR1
703      }
704  
705      /**
706 <     *  keySet.toArray returns contains all keys
706 >     * keySet.toArray returns contains all keys
707       */
708      public void testDescendingAscendingKeySetToArray() {
709          NavigableMap map = dmap5();
# Line 724 | Line 716 | public class TreeSubMapTest extends JSR1
716      }
717  
718      /**
719 <     *  descendingkeySet.toArray returns contains all keys
719 >     * descendingkeySet.toArray returns contains all keys
720       */
721      public void testDescendingDescendingKeySetToArray() {
722          NavigableMap map = dmap5();
# Line 737 | Line 729 | public class TreeSubMapTest extends JSR1
729      }
730  
731      /**
732 <     *  Values.toArray contains all values
732 >     * Values.toArray contains all values
733       */
734      public void testDescendingValuesToArray() {
735          NavigableMap map = dmap5();
# Line 752 | Line 744 | public class TreeSubMapTest extends JSR1
744          assertTrue(s.contains("E"));
745      }
746  
755
747      /**
748       * entrySet contains all pairs
749       */
# Line 773 | Line 764 | public class TreeSubMapTest extends JSR1
764      }
765  
766      /**
767 <     *   putAll  adds all key-value pairs from the given map
767 >     * putAll adds all key-value pairs from the given map
768       */
769      public void testDescendingPutAll() {
770          NavigableMap empty = dmap0();
# Line 787 | Line 778 | public class TreeSubMapTest extends JSR1
778          assertTrue(empty.containsKey(m5));
779      }
780  
790
781      /**
782 <     *   remove removes the correct key-value pair from the map
782 >     * remove removes the correct key-value pair from the map
783       */
784      public void testDescendingRemove() {
785          NavigableMap map = dmap5();
# Line 814 | Line 804 | public class TreeSubMapTest extends JSR1
804  
805          Map.Entry e4 = map.lowerEntry(zero);
806          assertNull(e4);
817
807      }
808  
809      /**
# Line 833 | Line 822 | public class TreeSubMapTest extends JSR1
822  
823          Map.Entry e4 = map.higherEntry(m6);
824          assertNull(e4);
836
825      }
826  
827      /**
# Line 852 | Line 840 | public class TreeSubMapTest extends JSR1
840  
841          Map.Entry e4 = map.floorEntry(zero);
842          assertNull(e4);
855
843      }
844  
845      /**
# Line 871 | Line 858 | public class TreeSubMapTest extends JSR1
858  
859          Map.Entry e4 = map.ceilingEntry(m6);
860          assertNull(e4);
874
861      }
862  
863      /**
# Line 929 | Line 915 | public class TreeSubMapTest extends JSR1
915      }
916  
917      /**
918 <     *   size returns the correct values
918 >     * size returns the correct values
919       */
920      public void testDescendingSize() {
921          NavigableMap map = dmap5();
# Line 945 | Line 931 | public class TreeSubMapTest extends JSR1
931          NavigableMap map = dmap5();
932          String s = map.toString();
933          for (int i = 1; i <= 5; ++i) {
934 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
934 >            assertTrue(s.contains(String.valueOf(i)));
935          }
936      }
937  
# Line 955 | Line 941 | public class TreeSubMapTest extends JSR1
941       * get(null) of nonempty map throws NPE
942       */
943      public void testDescendingGet_NullPointerException() {
944 +        NavigableMap c = dmap5();
945          try {
959            NavigableMap c = dmap5();
946              c.get(null);
947              shouldThrow();
948          } catch (NullPointerException success) {}
# Line 966 | Line 952 | public class TreeSubMapTest extends JSR1
952       * put(null,x) throws NPE
953       */
954      public void testDescendingPut1_NullPointerException() {
955 +        NavigableMap c = dmap5();
956          try {
970            NavigableMap c = dmap5();
957              c.put(null, "whatever");
958              shouldThrow();
959          } catch (NullPointerException success) {}
# Line 977 | Line 963 | public class TreeSubMapTest extends JSR1
963       * A deserialized map equals original
964       */
965      public void testDescendingSerialization() throws Exception {
966 <        NavigableMap q = dmap5();
966 >        NavigableMap x = dmap5();
967 >        NavigableMap y = serialClone(x);
968  
969 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
970 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
971 <        out.writeObject(q);
972 <        out.close();
973 <
987 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
988 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
989 <        NavigableMap r = (NavigableMap)in.readObject();
990 <        assertEquals(q.size(), r.size());
991 <        assertTrue(q.equals(r));
992 <        assertTrue(r.equals(q));
969 >        assertNotSame(x, y);
970 >        assertEquals(x.size(), y.size());
971 >        assertEquals(x.toString(), y.toString());
972 >        assertEquals(x, y);
973 >        assertEquals(y, x);
974      }
975  
995
976      /**
977       * subMap returns map with keys in requested range
978       */
# Line 1022 | Line 1002 | public class TreeSubMapTest extends JSR1
1002          assertEquals(1, sm.size());
1003          assertEquals(m3, sm.firstKey());
1004          assertEquals(m3, sm.lastKey());
1005 <        assertTrue(sm.remove(m3) != null);
1005 >        assertEquals("C", sm.remove(m3));
1006          assertTrue(sm.isEmpty());
1007          assertEquals(3, map.size());
1008      }
# Line 1050 | Line 1030 | public class TreeSubMapTest extends JSR1
1030          assertEquals(4, map.size());
1031          assertEquals(0, sm.size());
1032          assertTrue(sm.isEmpty());
1033 <        assertTrue(sm.remove(m3) == null);
1033 >        assertSame(sm.remove(m3), null);
1034          assertEquals(4, map.size());
1035      }
1036  
# Line 1122 | Line 1102 | public class TreeSubMapTest extends JSR1
1102          SortedMap ssm = sm.tailMap(m4);
1103          assertEquals(m4, ssm.firstKey());
1104          assertEquals(m5, ssm.lastKey());
1105 <        assertTrue(ssm.remove(m4) != null);
1105 >        assertEquals("D", ssm.remove(m4));
1106          assertEquals(1, ssm.size());
1107          assertEquals(3, sm.size());
1108          assertEquals(4, map.size());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines