ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TreeMapTest.java
Revision: 1.8
Committed: Mon Nov 16 05:30:08 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +4 -4 lines
Log Message:
whitespace

File Contents

# Content
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
5 */
6
7 import junit.framework.*;
8 import java.util.*;
9 import java.util.concurrent.*;
10 import java.io.*;
11
12 public class TreeMapTest extends JSR166TestCase {
13 public static void main(String[] args) {
14 junit.textui.TestRunner.run (suite());
15 }
16 public static Test suite() {
17 return new TestSuite(TreeMapTest.class);
18 }
19
20 /**
21 * Create a map from Integers 1-5 to Strings "A"-"E".
22 */
23 private static TreeMap map5() {
24 TreeMap map = new TreeMap();
25 assertTrue(map.isEmpty());
26 map.put(one, "A");
27 map.put(five, "E");
28 map.put(three, "C");
29 map.put(two, "B");
30 map.put(four, "D");
31 assertFalse(map.isEmpty());
32 assertEquals(5, map.size());
33 return map;
34 }
35
36 /**
37 * clear removes all pairs
38 */
39 public void testClear() {
40 TreeMap map = map5();
41 map.clear();
42 assertEquals(map.size(), 0);
43 }
44
45 /**
46 *
47 */
48 public void testConstructFromSorted() {
49 TreeMap map = map5();
50 TreeMap map2 = new TreeMap(map);
51 assertEquals(map, map2);
52 }
53
54 /**
55 * Maps with same contents are equal
56 */
57 public void testEquals() {
58 TreeMap map1 = map5();
59 TreeMap map2 = map5();
60 assertEquals(map1, map2);
61 assertEquals(map2, map1);
62 map1.clear();
63 assertFalse(map1.equals(map2));
64 assertFalse(map2.equals(map1));
65 }
66
67 /**
68 * containsKey returns true for contained key
69 */
70 public void testContainsKey() {
71 TreeMap map = map5();
72 assertTrue(map.containsKey(one));
73 assertFalse(map.containsKey(zero));
74 }
75
76 /**
77 * containsValue returns true for held values
78 */
79 public void testContainsValue() {
80 TreeMap map = map5();
81 assertTrue(map.containsValue("A"));
82 assertFalse(map.containsValue("Z"));
83 }
84
85 /**
86 * get returns the correct element at the given key,
87 * or null if not present
88 */
89 public void testGet() {
90 TreeMap map = map5();
91 assertEquals("A", (String)map.get(one));
92 TreeMap empty = new TreeMap();
93 assertNull(empty.get(one));
94 }
95
96 /**
97 * isEmpty is true of empty map and false for non-empty
98 */
99 public void testIsEmpty() {
100 TreeMap empty = new TreeMap();
101 TreeMap map = map5();
102 assertTrue(empty.isEmpty());
103 assertFalse(map.isEmpty());
104 }
105
106 /**
107 * firstKey returns first key
108 */
109 public void testFirstKey() {
110 TreeMap map = map5();
111 assertEquals(one, map.firstKey());
112 }
113
114 /**
115 * lastKey returns last key
116 */
117 public void testLastKey() {
118 TreeMap map = map5();
119 assertEquals(five, map.lastKey());
120 }
121
122
123 /**
124 * keySet.toArray returns contains all keys
125 */
126 public void testKeySetToArray() {
127 TreeMap map = map5();
128 Set s = map.keySet();
129 Object[] ar = s.toArray();
130 assertTrue(s.containsAll(Arrays.asList(ar)));
131 assertEquals(5, ar.length);
132 ar[0] = m10;
133 assertFalse(s.containsAll(Arrays.asList(ar)));
134 }
135
136 /**
137 * descendingkeySet.toArray returns contains all keys
138 */
139 public void testDescendingKeySetToArray() {
140 TreeMap map = map5();
141 Set s = map.descendingKeySet();
142 Object[] ar = s.toArray();
143 assertEquals(5, ar.length);
144 assertTrue(s.containsAll(Arrays.asList(ar)));
145 ar[0] = m10;
146 assertFalse(s.containsAll(Arrays.asList(ar)));
147 }
148
149 /**
150 * keySet returns a Set containing all the keys
151 */
152 public void testKeySet() {
153 TreeMap map = map5();
154 Set s = map.keySet();
155 assertEquals(5, s.size());
156 assertTrue(s.contains(one));
157 assertTrue(s.contains(two));
158 assertTrue(s.contains(three));
159 assertTrue(s.contains(four));
160 assertTrue(s.contains(five));
161 }
162
163 /**
164 * keySet is ordered
165 */
166 public void testKeySetOrder() {
167 TreeMap map = map5();
168 Set s = map.keySet();
169 Iterator i = s.iterator();
170 Integer last = (Integer)i.next();
171 assertEquals(last, one);
172 int count = 1;
173 while (i.hasNext()) {
174 Integer k = (Integer)i.next();
175 assertTrue(last.compareTo(k) < 0);
176 last = k;
177 ++count;
178 }
179 assertEquals(count ,5);
180 }
181
182 /**
183 * descending iterator of key set is inverse ordered
184 */
185 public void testKeySetDescendingIteratorOrder() {
186 TreeMap map = map5();
187 NavigableSet s = map.navigableKeySet();
188 Iterator i = s.descendingIterator();
189 Integer last = (Integer)i.next();
190 assertEquals(last, five);
191 int count = 1;
192 while (i.hasNext()) {
193 Integer k = (Integer)i.next();
194 assertTrue(last.compareTo(k) > 0);
195 last = k;
196 ++count;
197 }
198 assertEquals(count ,5);
199 }
200
201 /**
202 * descendingKeySet is ordered
203 */
204 public void testDescendingKeySetOrder() {
205 TreeMap map = map5();
206 Set s = map.descendingKeySet();
207 Iterator i = s.iterator();
208 Integer last = (Integer)i.next();
209 assertEquals(last, five);
210 int count = 1;
211 while (i.hasNext()) {
212 Integer k = (Integer)i.next();
213 assertTrue(last.compareTo(k) > 0);
214 last = k;
215 ++count;
216 }
217 assertEquals(count, 5);
218 }
219
220 /**
221 * descending iterator of descendingKeySet is ordered
222 */
223 public void testDescendingKeySetDescendingIteratorOrder() {
224 TreeMap map = map5();
225 NavigableSet s = map.descendingKeySet();
226 Iterator i = s.descendingIterator();
227 Integer last = (Integer)i.next();
228 assertEquals(last, one);
229 int count = 1;
230 while (i.hasNext()) {
231 Integer k = (Integer)i.next();
232 assertTrue(last.compareTo(k) < 0);
233 last = k;
234 ++count;
235 }
236 assertEquals(count, 5);
237 }
238
239 /**
240 * values collection contains all values
241 */
242 public void testValues() {
243 TreeMap map = map5();
244 Collection s = map.values();
245 assertEquals(5, s.size());
246 assertTrue(s.contains("A"));
247 assertTrue(s.contains("B"));
248 assertTrue(s.contains("C"));
249 assertTrue(s.contains("D"));
250 assertTrue(s.contains("E"));
251 }
252
253 /**
254 * entrySet contains all pairs
255 */
256 public void testEntrySet() {
257 TreeMap map = map5();
258 Set s = map.entrySet();
259 assertEquals(5, s.size());
260 Iterator it = s.iterator();
261 while (it.hasNext()) {
262 Map.Entry e = (Map.Entry) it.next();
263 assertTrue(
264 (e.getKey().equals(one) && e.getValue().equals("A")) ||
265 (e.getKey().equals(two) && e.getValue().equals("B")) ||
266 (e.getKey().equals(three) && e.getValue().equals("C")) ||
267 (e.getKey().equals(four) && e.getValue().equals("D")) ||
268 (e.getKey().equals(five) && e.getValue().equals("E")));
269 }
270 }
271
272 /**
273 * descendingEntrySet contains all pairs
274 */
275 public void testDescendingEntrySet() {
276 TreeMap map = map5();
277 Set s = map.descendingMap().entrySet();
278 assertEquals(5, s.size());
279 Iterator it = s.iterator();
280 while (it.hasNext()) {
281 Map.Entry e = (Map.Entry) it.next();
282 assertTrue(
283 (e.getKey().equals(one) && e.getValue().equals("A")) ||
284 (e.getKey().equals(two) && e.getValue().equals("B")) ||
285 (e.getKey().equals(three) && e.getValue().equals("C")) ||
286 (e.getKey().equals(four) && e.getValue().equals("D")) ||
287 (e.getKey().equals(five) && e.getValue().equals("E")));
288 }
289 }
290
291 /**
292 * entrySet.toArray contains all entries
293 */
294 public void testEntrySetToArray() {
295 TreeMap map = map5();
296 Set s = map.entrySet();
297 Object[] ar = s.toArray();
298 assertEquals(5, ar.length);
299 for (int i = 0; i < 5; ++i) {
300 assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
301 assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
302 }
303 }
304
305 /**
306 * descendingEntrySet.toArray contains all entries
307 */
308 public void testDescendingEntrySetToArray() {
309 TreeMap map = map5();
310 Set s = map.descendingMap().entrySet();
311 Object[] ar = s.toArray();
312 assertEquals(5, ar.length);
313 for (int i = 0; i < 5; ++i) {
314 assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
315 assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
316 }
317 }
318
319 /**
320 * putAll adds all key-value pairs from the given map
321 */
322 public void testPutAll() {
323 TreeMap empty = new TreeMap();
324 TreeMap map = map5();
325 empty.putAll(map);
326 assertEquals(5, empty.size());
327 assertTrue(empty.containsKey(one));
328 assertTrue(empty.containsKey(two));
329 assertTrue(empty.containsKey(three));
330 assertTrue(empty.containsKey(four));
331 assertTrue(empty.containsKey(five));
332 }
333
334 /**
335 * remove removes the correct key-value pair from the map
336 */
337 public void testRemove() {
338 TreeMap map = map5();
339 map.remove(five);
340 assertEquals(4, map.size());
341 assertFalse(map.containsKey(five));
342 }
343
344 /**
345 * lowerEntry returns preceding entry.
346 */
347 public void testLowerEntry() {
348 TreeMap map = map5();
349 Map.Entry e1 = map.lowerEntry(three);
350 assertEquals(two, e1.getKey());
351
352 Map.Entry e2 = map.lowerEntry(six);
353 assertEquals(five, e2.getKey());
354
355 Map.Entry e3 = map.lowerEntry(one);
356 assertNull(e3);
357
358 Map.Entry e4 = map.lowerEntry(zero);
359 assertNull(e4);
360
361 }
362
363 /**
364 * higherEntry returns next entry.
365 */
366 public void testHigherEntry() {
367 TreeMap map = map5();
368 Map.Entry e1 = map.higherEntry(three);
369 assertEquals(four, e1.getKey());
370
371 Map.Entry e2 = map.higherEntry(zero);
372 assertEquals(one, e2.getKey());
373
374 Map.Entry e3 = map.higherEntry(five);
375 assertNull(e3);
376
377 Map.Entry e4 = map.higherEntry(six);
378 assertNull(e4);
379
380 }
381
382 /**
383 * floorEntry returns preceding entry.
384 */
385 public void testFloorEntry() {
386 TreeMap map = map5();
387 Map.Entry e1 = map.floorEntry(three);
388 assertEquals(three, e1.getKey());
389
390 Map.Entry e2 = map.floorEntry(six);
391 assertEquals(five, e2.getKey());
392
393 Map.Entry e3 = map.floorEntry(one);
394 assertEquals(one, e3.getKey());
395
396 Map.Entry e4 = map.floorEntry(zero);
397 assertNull(e4);
398
399 }
400
401 /**
402 * ceilingEntry returns next entry.
403 */
404 public void testCeilingEntry() {
405 TreeMap map = map5();
406 Map.Entry e1 = map.ceilingEntry(three);
407 assertEquals(three, e1.getKey());
408
409 Map.Entry e2 = map.ceilingEntry(zero);
410 assertEquals(one, e2.getKey());
411
412 Map.Entry e3 = map.ceilingEntry(five);
413 assertEquals(five, e3.getKey());
414
415 Map.Entry e4 = map.ceilingEntry(six);
416 assertNull(e4);
417
418 }
419
420
421 /**
422 * lowerKey returns preceding element
423 */
424 public void testLowerKey() {
425 TreeMap q = map5();
426 Object e1 = q.lowerKey(three);
427 assertEquals(two, e1);
428
429 Object e2 = q.lowerKey(six);
430 assertEquals(five, e2);
431
432 Object e3 = q.lowerKey(one);
433 assertNull(e3);
434
435 Object e4 = q.lowerKey(zero);
436 assertNull(e4);
437
438 }
439
440 /**
441 * higherKey returns next element
442 */
443 public void testHigherKey() {
444 TreeMap q = map5();
445 Object e1 = q.higherKey(three);
446 assertEquals(four, e1);
447
448 Object e2 = q.higherKey(zero);
449 assertEquals(one, e2);
450
451 Object e3 = q.higherKey(five);
452 assertNull(e3);
453
454 Object e4 = q.higherKey(six);
455 assertNull(e4);
456
457 }
458
459 /**
460 * floorKey returns preceding element
461 */
462 public void testFloorKey() {
463 TreeMap q = map5();
464 Object e1 = q.floorKey(three);
465 assertEquals(three, e1);
466
467 Object e2 = q.floorKey(six);
468 assertEquals(five, e2);
469
470 Object e3 = q.floorKey(one);
471 assertEquals(one, e3);
472
473 Object e4 = q.floorKey(zero);
474 assertNull(e4);
475
476 }
477
478 /**
479 * ceilingKey returns next element
480 */
481 public void testCeilingKey() {
482 TreeMap q = map5();
483 Object e1 = q.ceilingKey(three);
484 assertEquals(three, e1);
485
486 Object e2 = q.ceilingKey(zero);
487 assertEquals(one, e2);
488
489 Object e3 = q.ceilingKey(five);
490 assertEquals(five, e3);
491
492 Object e4 = q.ceilingKey(six);
493 assertNull(e4);
494
495 }
496
497 /**
498 * pollFirstEntry returns entries in order
499 */
500 public void testPollFirstEntry() {
501 TreeMap map = map5();
502 Map.Entry e = map.pollFirstEntry();
503 assertEquals(one, e.getKey());
504 assertEquals("A", e.getValue());
505 e = map.pollFirstEntry();
506 assertEquals(two, e.getKey());
507 map.put(one, "A");
508 e = map.pollFirstEntry();
509 assertEquals(one, e.getKey());
510 assertEquals("A", e.getValue());
511 e = map.pollFirstEntry();
512 assertEquals(three, e.getKey());
513 map.remove(four);
514 e = map.pollFirstEntry();
515 assertEquals(five, e.getKey());
516 try {
517 e.setValue("A");
518 shouldThrow();
519 } catch (Exception ok) {
520 }
521 e = map.pollFirstEntry();
522 assertNull(e);
523 }
524
525 /**
526 * pollLastEntry returns entries in order
527 */
528 public void testPollLastEntry() {
529 TreeMap map = map5();
530 Map.Entry e = map.pollLastEntry();
531 assertEquals(five, e.getKey());
532 assertEquals("E", e.getValue());
533 e = map.pollLastEntry();
534 assertEquals(four, e.getKey());
535 map.put(five, "E");
536 e = map.pollLastEntry();
537 assertEquals(five, e.getKey());
538 assertEquals("E", e.getValue());
539 e = map.pollLastEntry();
540 assertEquals(three, e.getKey());
541 map.remove(two);
542 e = map.pollLastEntry();
543 assertEquals(one, e.getKey());
544 try {
545 e.setValue("E");
546 shouldThrow();
547 } catch (Exception ok) {
548 }
549 e = map.pollLastEntry();
550 assertNull(e);
551 }
552
553 /**
554 * size returns the correct values
555 */
556 public void testSize() {
557 TreeMap map = map5();
558 TreeMap empty = new TreeMap();
559 assertEquals(0, empty.size());
560 assertEquals(5, map.size());
561 }
562
563 /**
564 * toString contains toString of elements
565 */
566 public void testToString() {
567 TreeMap map = map5();
568 String s = map.toString();
569 for (int i = 1; i <= 5; ++i) {
570 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
571 }
572 }
573
574 // Exception tests
575
576 /**
577 * get(null) of nonempty map throws NPE
578 */
579 public void testGet_NullPointerException() {
580 try {
581 TreeMap c = map5();
582 c.get(null);
583 shouldThrow();
584 } catch (NullPointerException e) {}
585 }
586
587 /**
588 * containsKey(null) of nonempty map throws NPE
589 */
590 public void testContainsKey_NullPointerException() {
591 try {
592 TreeMap c = map5();
593 c.containsKey(null);
594 shouldThrow();
595 } catch (NullPointerException e) {}
596 }
597
598 /**
599 * remove(null) throws NPE for nonempty map
600 */
601 public void testRemove1_NullPointerException() {
602 try {
603 TreeMap c = new TreeMap();
604 c.put("sadsdf", "asdads");
605 c.remove(null);
606 shouldThrow();
607 } catch (NullPointerException e) {}
608 }
609
610 /**
611 * A deserialized map equals original
612 */
613 public void testSerialization() {
614 TreeMap q = map5();
615
616 try {
617 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
618 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
619 out.writeObject(q);
620 out.close();
621
622 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
623 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
624 TreeMap r = (TreeMap)in.readObject();
625 assertEquals(q.size(), r.size());
626 assertTrue(q.equals(r));
627 assertTrue(r.equals(q));
628 } catch (Exception e) {
629 e.printStackTrace();
630 unexpectedException();
631 }
632 }
633
634 /**
635 * subMap returns map with keys in requested range
636 */
637 public void testSubMapContents() {
638 TreeMap map = map5();
639 NavigableMap sm = map.subMap(two, true, four, false);
640 assertEquals(two, sm.firstKey());
641 assertEquals(three, sm.lastKey());
642 assertEquals(2, sm.size());
643 assertFalse(sm.containsKey(one));
644 assertTrue(sm.containsKey(two));
645 assertTrue(sm.containsKey(three));
646 assertFalse(sm.containsKey(four));
647 assertFalse(sm.containsKey(five));
648 Iterator i = sm.keySet().iterator();
649 Object k;
650 k = (Integer)(i.next());
651 assertEquals(two, k);
652 k = (Integer)(i.next());
653 assertEquals(three, k);
654 assertFalse(i.hasNext());
655 Iterator r = sm.descendingKeySet().iterator();
656 k = (Integer)(r.next());
657 assertEquals(three, k);
658 k = (Integer)(r.next());
659 assertEquals(two, k);
660 assertFalse(r.hasNext());
661
662 Iterator j = sm.keySet().iterator();
663 j.next();
664 j.remove();
665 assertFalse(map.containsKey(two));
666 assertEquals(4, map.size());
667 assertEquals(1, sm.size());
668 assertEquals(three, sm.firstKey());
669 assertEquals(three, sm.lastKey());
670 assertTrue(sm.remove(three) != null);
671 assertTrue(sm.isEmpty());
672 assertEquals(3, map.size());
673 }
674
675 public void testSubMapContents2() {
676 TreeMap map = map5();
677 NavigableMap sm = map.subMap(two, true, three, false);
678 assertEquals(1, sm.size());
679 assertEquals(two, sm.firstKey());
680 assertEquals(two, sm.lastKey());
681 assertFalse(sm.containsKey(one));
682 assertTrue(sm.containsKey(two));
683 assertFalse(sm.containsKey(three));
684 assertFalse(sm.containsKey(four));
685 assertFalse(sm.containsKey(five));
686 Iterator i = sm.keySet().iterator();
687 Object k;
688 k = (Integer)(i.next());
689 assertEquals(two, k);
690 assertFalse(i.hasNext());
691 Iterator r = sm.descendingKeySet().iterator();
692 k = (Integer)(r.next());
693 assertEquals(two, k);
694 assertFalse(r.hasNext());
695
696 Iterator j = sm.keySet().iterator();
697 j.next();
698 j.remove();
699 assertFalse(map.containsKey(two));
700 assertEquals(4, map.size());
701 assertEquals(0, sm.size());
702 assertTrue(sm.isEmpty());
703 assertTrue(sm.remove(three) == null);
704 assertEquals(4, map.size());
705 }
706
707 /**
708 * headMap returns map with keys in requested range
709 */
710 public void testHeadMapContents() {
711 TreeMap map = map5();
712 NavigableMap sm = map.headMap(four, false);
713 assertTrue(sm.containsKey(one));
714 assertTrue(sm.containsKey(two));
715 assertTrue(sm.containsKey(three));
716 assertFalse(sm.containsKey(four));
717 assertFalse(sm.containsKey(five));
718 Iterator i = sm.keySet().iterator();
719 Object k;
720 k = (Integer)(i.next());
721 assertEquals(one, k);
722 k = (Integer)(i.next());
723 assertEquals(two, k);
724 k = (Integer)(i.next());
725 assertEquals(three, k);
726 assertFalse(i.hasNext());
727 sm.clear();
728 assertTrue(sm.isEmpty());
729 assertEquals(2, map.size());
730 assertEquals(four, map.firstKey());
731 }
732
733 /**
734 * headMap returns map with keys in requested range
735 */
736 public void testTailMapContents() {
737 TreeMap map = map5();
738 NavigableMap sm = map.tailMap(two, true);
739 assertFalse(sm.containsKey(one));
740 assertTrue(sm.containsKey(two));
741 assertTrue(sm.containsKey(three));
742 assertTrue(sm.containsKey(four));
743 assertTrue(sm.containsKey(five));
744 Iterator i = sm.keySet().iterator();
745 Object k;
746 k = (Integer)(i.next());
747 assertEquals(two, k);
748 k = (Integer)(i.next());
749 assertEquals(three, k);
750 k = (Integer)(i.next());
751 assertEquals(four, k);
752 k = (Integer)(i.next());
753 assertEquals(five, k);
754 assertFalse(i.hasNext());
755 Iterator r = sm.descendingKeySet().iterator();
756 k = (Integer)(r.next());
757 assertEquals(five, k);
758 k = (Integer)(r.next());
759 assertEquals(four, k);
760 k = (Integer)(r.next());
761 assertEquals(three, k);
762 k = (Integer)(r.next());
763 assertEquals(two, k);
764 assertFalse(r.hasNext());
765
766 Iterator ei = sm.entrySet().iterator();
767 Map.Entry e;
768 e = (Map.Entry)(ei.next());
769 assertEquals(two, e.getKey());
770 assertEquals("B", e.getValue());
771 e = (Map.Entry)(ei.next());
772 assertEquals(three, e.getKey());
773 assertEquals("C", e.getValue());
774 e = (Map.Entry)(ei.next());
775 assertEquals(four, e.getKey());
776 assertEquals("D", e.getValue());
777 e = (Map.Entry)(ei.next());
778 assertEquals(five, e.getKey());
779 assertEquals("E", e.getValue());
780 assertFalse(i.hasNext());
781
782 NavigableMap ssm = sm.tailMap(four, true);
783 assertEquals(four, ssm.firstKey());
784 assertEquals(five, ssm.lastKey());
785 assertTrue(ssm.remove(four) != null);
786 assertEquals(1, ssm.size());
787 assertEquals(3, sm.size());
788 assertEquals(4, map.size());
789 }
790
791 Random rnd = new Random(666);
792 BitSet bs;
793
794 /**
795 * Submaps of submaps subdivide correctly
796 */
797 public void testRecursiveSubMaps() {
798 int mapSize = 1000;
799 Class cl = TreeMap.class;
800 NavigableMap<Integer, Integer> map = newMap(cl);
801 bs = new BitSet(mapSize);
802
803 populate(map, mapSize);
804 check(map, 0, mapSize - 1, true);
805 check(map.descendingMap(), 0, mapSize - 1, false);
806
807 mutateMap(map, 0, mapSize - 1);
808 check(map, 0, mapSize - 1, true);
809 check(map.descendingMap(), 0, mapSize - 1, false);
810
811 bashSubMap(map.subMap(0, true, mapSize, false),
812 0, mapSize - 1, true);
813 }
814
815 static NavigableMap<Integer, Integer> newMap(Class cl) {
816 NavigableMap<Integer, Integer> result = null;
817 try {
818 result = (NavigableMap<Integer, Integer>) cl.newInstance();
819 } catch (Exception e) {
820 fail();
821 }
822 assertEquals(result.size(), 0);
823 assertFalse(result.keySet().iterator().hasNext());
824 return result;
825 }
826
827 void populate(NavigableMap<Integer, Integer> map, int limit) {
828 for (int i = 0, n = 2 * limit / 3; i < n; i++) {
829 int key = rnd.nextInt(limit);
830 put(map, key);
831 }
832 }
833
834 void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) {
835 int size = map.size();
836 int rangeSize = max - min + 1;
837
838 // Remove a bunch of entries directly
839 for (int i = 0, n = rangeSize / 2; i < n; i++) {
840 remove(map, min - 5 + rnd.nextInt(rangeSize + 10));
841 }
842
843 // Remove a bunch of entries with iterator
844 for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
845 if (rnd.nextBoolean()) {
846 bs.clear(it.next());
847 it.remove();
848 }
849 }
850
851 // Add entries till we're back to original size
852 while (map.size() < size) {
853 int key = min + rnd.nextInt(rangeSize);
854 assertTrue(key >= min && key<= max);
855 put(map, key);
856 }
857 }
858
859 void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) {
860 int size = map.size();
861 int rangeSize = max - min + 1;
862
863 // Remove a bunch of entries directly
864 for (int i = 0, n = rangeSize / 2; i < n; i++) {
865 remove(map, min - 5 + rnd.nextInt(rangeSize + 10));
866 }
867
868 // Remove a bunch of entries with iterator
869 for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
870 if (rnd.nextBoolean()) {
871 bs.clear(it.next());
872 it.remove();
873 }
874 }
875
876 // Add entries till we're back to original size
877 while (map.size() < size) {
878 int key = min - 5 + rnd.nextInt(rangeSize + 10);
879 if (key >= min && key<= max) {
880 put(map, key);
881 } else {
882 try {
883 map.put(key, 2 * key);
884 fail();
885 } catch (IllegalArgumentException e) {
886 // expected
887 }
888 }
889 }
890 }
891
892 void put(NavigableMap<Integer, Integer> map, int key) {
893 if (map.put(key, 2 * key) == null)
894 bs.set(key);
895 }
896
897 void remove(NavigableMap<Integer, Integer> map, int key) {
898 if (map.remove(key) != null)
899 bs.clear(key);
900 }
901
902 void bashSubMap(NavigableMap<Integer, Integer> map,
903 int min, int max, boolean ascending) {
904 check(map, min, max, ascending);
905 check(map.descendingMap(), min, max, !ascending);
906
907 mutateSubMap(map, min, max);
908 check(map, min, max, ascending);
909 check(map.descendingMap(), min, max, !ascending);
910
911 // Recurse
912 if (max - min < 2)
913 return;
914 int midPoint = (min + max) / 2;
915
916 // headMap - pick direction and endpoint inclusion randomly
917 boolean incl = rnd.nextBoolean();
918 NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl);
919 if (ascending) {
920 if (rnd.nextBoolean())
921 bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true);
922 else
923 bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1),
924 false);
925 } else {
926 if (rnd.nextBoolean())
927 bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false);
928 else
929 bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max,
930 true);
931 }
932
933 // tailMap - pick direction and endpoint inclusion randomly
934 incl = rnd.nextBoolean();
935 NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl);
936 if (ascending) {
937 if (rnd.nextBoolean())
938 bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true);
939 else
940 bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max,
941 false);
942 } else {
943 if (rnd.nextBoolean()) {
944 bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false);
945 } else {
946 bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1),
947 true);
948 }
949 }
950
951 // subMap - pick direction and endpoint inclusion randomly
952 int rangeSize = max - min + 1;
953 int[] endpoints = new int[2];
954 endpoints[0] = min + rnd.nextInt(rangeSize);
955 endpoints[1] = min + rnd.nextInt(rangeSize);
956 Arrays.sort(endpoints);
957 boolean lowIncl = rnd.nextBoolean();
958 boolean highIncl = rnd.nextBoolean();
959 if (ascending) {
960 NavigableMap<Integer,Integer> sm = map.subMap(
961 endpoints[0], lowIncl, endpoints[1], highIncl);
962 if (rnd.nextBoolean())
963 bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
964 endpoints[1] - (highIncl ? 0 : 1), true);
965 else
966 bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
967 endpoints[1] - (highIncl ? 0 : 1), false);
968 } else {
969 NavigableMap<Integer,Integer> sm = map.subMap(
970 endpoints[1], highIncl, endpoints[0], lowIncl);
971 if (rnd.nextBoolean())
972 bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
973 endpoints[1] - (highIncl ? 0 : 1), false);
974 else
975 bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
976 endpoints[1] - (highIncl ? 0 : 1), true);
977 }
978 }
979
980 /**
981 * min and max are both inclusive. If max < min, interval is empty.
982 */
983 void check(NavigableMap<Integer, Integer> map,
984 final int min, final int max, final boolean ascending) {
985 class ReferenceSet {
986 int lower(int key) {
987 return ascending ? lowerAscending(key) : higherAscending(key);
988 }
989 int floor(int key) {
990 return ascending ? floorAscending(key) : ceilingAscending(key);
991 }
992 int ceiling(int key) {
993 return ascending ? ceilingAscending(key) : floorAscending(key);
994 }
995 int higher(int key) {
996 return ascending ? higherAscending(key) : lowerAscending(key);
997 }
998 int first() {
999 return ascending ? firstAscending() : lastAscending();
1000 }
1001 int last() {
1002 return ascending ? lastAscending() : firstAscending();
1003 }
1004 int lowerAscending(int key) {
1005 return floorAscending(key - 1);
1006 }
1007 int floorAscending(int key) {
1008 if (key < min)
1009 return -1;
1010 else if (key > max)
1011 key = max;
1012
1013 // BitSet should support this! Test would run much faster
1014 while (key >= min) {
1015 if (bs.get(key))
1016 return(key);
1017 key--;
1018 }
1019 return -1;
1020 }
1021 int ceilingAscending(int key) {
1022 if (key < min)
1023 key = min;
1024 else if (key > max)
1025 return -1;
1026 int result = bs.nextSetBit(key);
1027 return result > max ? -1 : result;
1028 }
1029 int higherAscending(int key) {
1030 return ceilingAscending(key + 1);
1031 }
1032 private int firstAscending() {
1033 int result = ceilingAscending(min);
1034 return result > max ? -1 : result;
1035 }
1036 private int lastAscending() {
1037 int result = floorAscending(max);
1038 return result < min ? -1 : result;
1039 }
1040 }
1041 ReferenceSet rs = new ReferenceSet();
1042
1043 // Test contents using containsKey
1044 int size = 0;
1045 for (int i = min; i <= max; i++) {
1046 boolean bsContainsI = bs.get(i);
1047 assertEquals(bsContainsI, map.containsKey(i));
1048 if (bsContainsI)
1049 size++;
1050 }
1051 assertEquals(map.size(), size);
1052
1053 // Test contents using contains keySet iterator
1054 int size2 = 0;
1055 int previousKey = -1;
1056 for (int key : map.keySet()) {
1057 assertTrue(bs.get(key));
1058 size2++;
1059 assertTrue(previousKey < 0 ||
1060 (ascending ? key - previousKey > 0 : key - previousKey < 0));
1061 previousKey = key;
1062 }
1063 assertEquals(size2, size);
1064
1065 // Test navigation ops
1066 for (int key = min - 1; key <= max + 1; key++) {
1067 assertEq(map.lowerKey(key), rs.lower(key));
1068 assertEq(map.floorKey(key), rs.floor(key));
1069 assertEq(map.higherKey(key), rs.higher(key));
1070 assertEq(map.ceilingKey(key), rs.ceiling(key));
1071 }
1072
1073 // Test extrema
1074 if (map.size() != 0) {
1075 assertEq(map.firstKey(), rs.first());
1076 assertEq(map.lastKey(), rs.last());
1077 } else {
1078 assertEq(rs.first(), -1);
1079 assertEq(rs.last(), -1);
1080 try {
1081 map.firstKey();
1082 fail();
1083 } catch (NoSuchElementException e) {
1084 // expected
1085 }
1086 try {
1087 map.lastKey();
1088 fail();
1089 } catch (NoSuchElementException e) {
1090 // expected
1091 }
1092 }
1093 }
1094
1095 static void assertEq(Integer i, int j) {
1096 if (i == null)
1097 assertEquals(j, -1);
1098 else
1099 assertEquals((int) i, j);
1100 }
1101
1102 static boolean eq(Integer i, int j) {
1103 return i == null ? j == -1 : i == j;
1104 }
1105
1106 }