Repository navigation
Expand file tree
/
Copy pathBTreeMapTest4.java
More file actions
1873 lines (1701 loc) · 67.5 KB
/
Copy pathBTreeMapTest4.java
File metadata and controls
1873 lines (1701 loc) · 67.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapdb;
import java.io.Serializable;
import java.text.CollationKey;
import java.text.Collator;
import java.util.*;
import java.util.Map.Entry;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class BTreeMapTest4 extends junit.framework.TestCase {
protected <K,V> BTreeMap<K,V> newBTreeMap(Map map) {
BTreeMap ret = DBMaker.newMemoryDB()
.cacheDisable()
.transactionDisable().make()
.createTreeMap("test").nodeSize(6).make();
ret.putAll(map);
return ret;
}
protected <K,V> BTreeMap<K,V> newBTreeMap(Comparator comp) {
return DBMaker.newMemoryDB()
.cacheDisable()
.transactionDisable().make()
.createTreeMap("test").nodeSize(6).comparator(comp).make();
}
protected static <K,V> BTreeMap<K,V> newBTreeMap() {
return DBMaker.newMemoryDB()
.cacheDisable()
.transactionDisable().make()
.getTreeMap("test");
}
public static class ReversedComparator implements Comparator,Serializable {
private static final long serialVersionUID = -6582440135976043229L;
public int compare(Object o1, Object o2) {
return -(((Comparable) o1).compareTo(o2));
}
public boolean equals(Object o1, Object o2) {
return (((Comparable) o1).compareTo(o2)) == 0;
}
}
// Regression for Harmony-1026
public static class MockComparator<T extends Comparable<T>> implements
Comparator<T>, Serializable {
private static final long serialVersionUID = 5203668427652057645L;
public int compare(T o1, T o2) {
if (o1 == o2) {
return 0;
}
if (null == o1 || null == o2) {
return -1;
}
T c1 = o1;
T c2 = o2;
return c1.compareTo(c2);
}
}
BTreeMap tm;
Object objArray[] = new Object[1000];
/**
* @tests java.util.TreeMap#TreeMap(java.util.Comparator)
*/
public void test_ConstructorLjava_util_Comparator() {
// Test for method java.util.TreeMap(java.util.Comparator)
Comparator comp = new ReversedComparator();
BTreeMap reversedTreeMap = newBTreeMap(comp);
assertEquals("TreeMap answered incorrect comparator", reversedTreeMap
.comparator().getClass().toString(),comp.getClass().toString());
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
reversedTreeMap.firstKey().equals(new Integer(2).toString()));
assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
reversedTreeMap.lastKey().equals(new Integer(1).toString()));
}
/**
* @tests java.util.TreeMap#clear()
*/
public void test_clear() {
// Test for method void java.util.TreeMap.clear()
tm.clear();
assertEquals("Cleared map returned non-zero size", 0, tm.size());
}
/**
* @tests java.util.TreeMap#comparator()
*/
public void test_comparator() {
// Test for method java.util.Comparator java.util.TreeMap.comparator()\
Comparator comp = new ReversedComparator();
BTreeMap reversedTreeMap = newBTreeMap(comp);
assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
.comparator() == comp);
reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
reversedTreeMap.firstKey().equals(new Integer(2).toString()));
assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
reversedTreeMap.lastKey().equals(new Integer(1).toString()));
}
/**
* @tests java.util.TreeMap#containsKey(java.lang.Object)
*/
public void test_containsKeyLjava_lang_Object() {
// Test for method boolean
// java.util.TreeMap.containsKey(java.lang.Object)
assertTrue("Returned false for valid key", tm.containsKey("95"));
assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
}
/**
* @tests java.util.TreeMap#containsValue(java.lang.Object)
*/
public void test_containsValueLjava_lang_Object() {
// Test for method boolean
// java.util.TreeMap.containsValue(java.lang.Object)
assertTrue("Returned false for valid value", tm
.containsValue(objArray[986]));
assertTrue("Returned true for invalid value", !tm
.containsValue(new Object()));
}
/**
* @tests java.util.TreeMap#entrySet()
*/
public void test_entrySet() {
// Test for method java.util.Set java.util.TreeMap.entrySet()
Set anEntrySet = tm.entrySet();
Iterator entrySetIterator = anEntrySet.iterator();
assertTrue("EntrySet is incorrect size",
anEntrySet.size() == objArray.length);
Map.Entry entry;
while (entrySetIterator.hasNext()) {
entry = (Map.Entry) entrySetIterator.next();
assertEquals("EntrySet does not contain correct mappings", tm
.get(entry.getKey()), entry.getValue());
}
}
/**
* @tests java.util.TreeMap#firstKey()
*/
public void test_firstKey() {
// Test for method java.lang.Object java.util.TreeMap.firstKey()
assertEquals("Returned incorrect first key", "0", tm.firstKey());
}
/**
* @tests java.util.TreeMap#get(java.lang.Object)
*/
public void test_getLjava_lang_Object() {
// Test for method java.lang.Object
// java.util.TreeMap.get(java.lang.Object)
Object o = Long.MIN_VALUE;
tm.put("Hello", o);
assertEquals("Failed to get mapping", tm.get("Hello"), o);
// Test for the same key & same value
tm = newBTreeMap();
Object o2 = Long.MAX_VALUE;
Integer key1 = 1;
Integer key2 = 2;
assertNull(tm.put(key1, o));
assertNull(tm.put(key2, o));
assertEquals(2, tm.values().size());
assertEquals(2, tm.keySet().size());
assertEquals(tm.get(key1), tm.get(key2));
assertEquals(o, tm.put(key1, o2));
assertEquals(o2, tm.get(key1));
}
// Regression for ill-behaved collator
static class IllBehavedCollator extends Collator implements Serializable {
private static final long serialVersionUID = 3009434843065697796L;
@Override
public int compare(String o1, String o2) {
if (o1 == null) {
return 0;
}
return o1.compareTo(o2);
}
@Override
public CollationKey getCollationKey(String string) {
return null;
}
@Override
public int hashCode() {
return 0;
}
}
/**
* @tests java.util.TreeMap#headMap(java.lang.Object)
*/
public void test_headMapLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.headMap(java.lang.Object)
Map head = tm.headMap("100");
assertEquals("Returned map of incorrect size", 3, head.size());
assertTrue("Returned incorrect elements", head.containsKey("0")
&& head.containsValue(new Integer("1"))
&& head.containsKey("10"));
// Regression for Harmony-1026
BTreeMap<Integer, Double> map = newBTreeMap(
new MockComparator());
map.put(1, 2.1);
map.put(2, 3.1);
map.put(3, 4.5);
map.put(7, 21.3);
SortedMap<Integer, Double> smap = map.headMap(-1);
assertEquals(0, smap.size());
Set<Integer> keySet = smap.keySet();
assertEquals(0, keySet.size());
Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
assertEquals(0, entrySet.size());
Collection<Double> valueCollection = smap.values();
assertEquals(0, valueCollection.size());
// // Regression for Harmony-1066
// assertTrue(head instanceof Serializable);
BTreeMap<String, String> treemap = newBTreeMap(new IllBehavedCollator());
// assertEquals(0, treemap.headMap(null).size());
treemap = newBTreeMap();
SortedMap<String, String> headMap = treemap.headMap("100");
headMap.headMap("100");
SortedMap<Integer,Integer> intMap,sub;
int size = 16;
intMap = newBTreeMap();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
sub = intMap.headMap(-1);
assertEquals("size should be zero",sub.size(),0);
assertTrue("submap should be empty",sub.isEmpty());
try{
sub.firstKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
BTreeMap t = newBTreeMap();
try {
SortedMap th = t.headMap(null);
fail("Should throw a NullPointerException");
} catch( NullPointerException npe) {
// expected
}
try{
sub.lastKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
size = 256;
intMap = newBTreeMap();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
sub = intMap.headMap(-1);
assertEquals("size should be zero",sub.size(),0);
assertTrue("submap should be empty",sub.isEmpty());
try{
sub.firstKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
try{
sub.lastKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
}
/**
* @tests java.util.TreeMap#keySet()
*/
public void test_keySet() {
// Test for method java.util.Set java.util.TreeMap.keySet()
Set ks = tm.keySet();
assertTrue("Returned set of incorrect size",
ks.size() == objArray.length);
for (int i = 0; i < tm.size(); i++) {
assertTrue("Returned set is missing keys", ks.contains(new Integer(
i).toString()));
}
}
/**
* @tests java.util.TreeMap#lastKey()
*/
public void test_lastKey() {
// Test for method java.lang.Object java.util.TreeMap.lastKey()
assertTrue("Returned incorrect last key", tm.lastKey().equals(
objArray[objArray.length - 1].toString()));
assertNotSame(objArray[objArray.length - 1].toString(), tm.lastKey());
assertEquals(objArray[objArray.length - 2].toString(), tm
.headMap("999").lastKey());
assertEquals(objArray[objArray.length - 1].toString(), tm
.tailMap("123").lastKey());
assertEquals(objArray[objArray.length - 2].toString(), tm.subMap("99",
"999").lastKey());
}
public void test_lastKey_after_subMap() {
BTreeMap<String, String> tm = newBTreeMap();
tm.put("001", "VAL001");
tm.put("003", "VAL003");
tm.put("002", "VAL002");
SortedMap<String, String> sm = tm;
String firstKey = sm.firstKey();
String lastKey="";
for (int i = 1; i <= tm.size(); i++) {
try{
lastKey = sm.lastKey();
}
catch(NoSuchElementException excep){
fail("NoSuchElementException thrown when there are elements in the map");
}
sm = sm.subMap(firstKey, lastKey);
}
}
/**
* @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
*/
public void test_putLjava_lang_ObjectLjava_lang_Object() {
// Test for method java.lang.Object
// java.util.TreeMap.put(java.lang.Object, java.lang.Object)
Object o = Long.MIN_VALUE;
tm.put("Hello", o);
assertEquals("Failed to put mapping", tm.get("Hello") , o);
// regression for Harmony-780
tm = newBTreeMap();
assertNull(tm.put(new Object(), new Object()));
try {
tm.put(new Integer(1), new Object());
fail("should throw ClassCastException");
} catch (ClassCastException e) {
// expected
}
tm = newBTreeMap();
assertNull(tm.put(new Integer(1), new Object()));
try {
tm.put(new Object(), new Object());
fail("Should throw a ClassCastException");
} catch (ClassCastException e) {
// expected
}
// // regression for Harmony-2474
// // but RI6 changes its behavior
// // so the test changes too
// tm = newBTreeMap();
// try {
// tm.remove(o);
// fail("should throw ClassCastException");
// } catch (ClassCastException e) {
// //expected
// }
}
/**
* @tests java.util.TreeMap#putAll(java.util.Map)
*/
public void test_putAllLjava_util_Map() {
// Test for method void java.util.TreeMap.putAll(java.util.Map)
BTreeMap x = newBTreeMap();
x.putAll(tm);
assertTrue("Map incorrect size after put", x.size() == tm.size());
for (Object element : objArray) {
assertTrue("Failed to put all elements", x.get(element.toString())
.equals(element));
}
}
/**
* @tests java.util.TreeMap#remove(java.lang.Object)
*/
public void test_removeLjava_lang_Object() {
// Test for method java.lang.Object
// java.util.TreeMap.remove(java.lang.Object)
tm.remove("990");
assertTrue("Failed to remove mapping", !tm.containsKey("990"));
}
/**
* @tests java.util.TreeMap#size()
*/
public void test_size() {
// Test for method int java.util.TreeMap.size()
assertEquals("Returned incorrect size", 1000, tm.size());
assertEquals("Returned incorrect size", 447, tm.headMap("500").size());
assertEquals("Returned incorrect size", 1000, tm.headMap("null").size());
assertEquals("Returned incorrect size", 0, tm.headMap("").size());
assertEquals("Returned incorrect size", 448, tm.headMap("500a").size());
assertEquals("Returned incorrect size", 553, tm.tailMap("500").size());
assertEquals("Returned incorrect size", 0, tm.tailMap("null").size());
assertEquals("Returned incorrect size", 1000, tm.tailMap("").size());
assertEquals("Returned incorrect size", 552, tm.tailMap("500a").size());
assertEquals("Returned incorrect size", 111, tm.subMap("500", "600")
.size());
try {
tm.subMap("null", "600");
fail("Should throw an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
assertEquals("Returned incorrect size", 1000, tm.subMap("", "null")
.size());
}
/**
* @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
*/
public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
.toString());
assertEquals("subMap is of incorrect size", 9, subMap.size());
for (int counter = 100; counter < 109; counter++) {
assertTrue("SubMap contains incorrect elements", subMap.get(
objArray[counter].toString()).equals(objArray[counter]));
}
try {
tm.subMap(objArray[9].toString(), objArray[1].toString());
fail("end key less than start key should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
// Regression test for typo in lastKey method
SortedMap<String, String> map = newBTreeMap();
map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("3", map.lastKey());
SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("2", sub.lastKey()); //$NON-NLS-1$
BTreeMap t = newBTreeMap();
try {
SortedMap th = t.subMap(null,new Object());
fail("Should throw a NullPointerException");
} catch( NullPointerException npe) {
// expected
}
}
/**
* @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
*/
public void test_subMap_Iterator() {
BTreeMap<String, String> map = newBTreeMap();
String[] keys = { "1", "2", "3" };
String[] values = { "one", "two", "three" };
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], values[i]);
}
assertEquals(3, map.size());
Map subMap = map.subMap("", "test");
assertEquals(3, subMap.size());
Set entrySet = subMap.entrySet();
Iterator iter = entrySet.iterator();
int size = 0;
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
.next();
assertTrue(map.containsKey(entry.getKey()));
assertTrue(map.containsValue(entry.getValue()));
size++;
}
assertEquals(map.size(), size);
Set<String> keySet = subMap.keySet();
iter = keySet.iterator();
size = 0;
while (iter.hasNext()) {
String key = (String) iter.next();
assertTrue(map.containsKey(key));
size++;
}
assertEquals(map.size(), size);
}
/**
* @tests java.util.TreeMap#tailMap(java.lang.Object)
*/
public void test_tailMapLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.tailMap(java.lang.Object)
Map tail = tm.tailMap(objArray[900].toString());
assertTrue("Returned map of incorrect size : " + tail.size(), tail
.size() == (objArray.length - 900) + 9);
for (int i = 900; i < objArray.length; i++) {
assertTrue("Map contains incorrect entries", tail
.containsValue(objArray[i]));
}
// // Regression for Harmony-1066
// assertTrue(tail instanceof Serializable);
SortedMap<Integer,Integer> intMap,sub;
int size = 16;
intMap = newBTreeMap();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
sub = intMap.tailMap(size);
assertEquals("size should be zero",sub.size(),0);
assertTrue("submap should be empty",sub.isEmpty());
;
try{
sub.firstKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
BTreeMap t = newBTreeMap();
try {
SortedMap th = t.tailMap(null);
fail("Should throw a NullPointerException");
} catch( NullPointerException npe) {
// expected
}
try{
sub.lastKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
size = 256;
intMap = newBTreeMap();
for(int i=0; i<size; i++) {
intMap.put(i,i);
}
sub = intMap.tailMap(size);
assertEquals("size should be zero",sub.size(),0);
assertTrue("submap should be empty",sub.isEmpty());
try{
sub.firstKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
try{
sub.lastKey();
fail("java.util.NoSuchElementException should be thrown");
} catch(java.util.NoSuchElementException e) {
}
}
/**
* @tests java.util.TreeMap#values()
*/
public void test_values() {
// Test for method java.util.Collection java.util.TreeMap.values()
Collection vals = tm.values();
vals.iterator();
assertTrue("Returned collection of incorrect size",
vals.size() == objArray.length);
for (Object element : objArray) {
assertTrue("Collection contains incorrect elements", vals
.contains(element));
}
assertEquals(1000, vals.size());
int j = 0;
for (Iterator iter = vals.iterator(); iter.hasNext();) {
Object element = iter.next();
j++;
}
assertEquals(1000, j);
// vals = tm.descendingMap().values();
// vals.iterator();
assertTrue("Returned collection of incorrect size",
vals.size() == objArray.length);
for (Object element : objArray) {
assertTrue("Collection contains incorrect elements", vals
.contains(element));
}
assertEquals(1000, vals.size());
j = 0;
for (Iterator iter = vals.iterator(); iter.hasNext();) {
Object element = iter.next();
j++;
}
assertEquals(1000, j);
BTreeMap myTreeMap = newBTreeMap();
for (int i = 0; i < 100; i++) {
myTreeMap.put(objArray[i], objArray[i]);
}
Collection col = myTreeMap.values();
// contains
assertTrue("UnmodifiableCollectionTest - should contain 0", col
.contains(new Integer(0)));
assertTrue("UnmodifiableCollectionTest - should contain 50", col
.contains(new Integer(50)));
assertTrue("UnmodifiableCollectionTest - should not contain 100", !col
.contains(new Integer(100)));
// containsAll
HashSet<Integer> hs = new HashSet<Integer>();
hs.add(new Integer(0));
hs.add(new Integer(25));
hs.add(new Integer(99));
assertTrue(
"UnmodifiableCollectionTest - should contain set of 0, 25, and 99",
col.containsAll(hs));
hs.add(new Integer(100));
assertTrue(
"UnmodifiableCollectionTest - should not contain set of 0, 25, 99 and 100",
!col.containsAll(hs));
// isEmpty
assertTrue("UnmodifiableCollectionTest - should not be empty", !col
.isEmpty());
// iterator
Iterator<Integer> it = col.iterator();
SortedSet<Integer> ss = new TreeSet<Integer>();
while (it.hasNext()) {
ss.add(it.next());
}
it = ss.iterator();
for (int counter = 0; it.hasNext(); counter++) {
int nextValue = it.next().intValue();
assertTrue(
"UnmodifiableCollectionTest - Iterator returned wrong value. Wanted: "
+ counter + " got: " + nextValue,
nextValue == counter);
}
// size
assertTrue(
"UnmodifiableCollectionTest - returned wrong size. Wanted 100, got: "
+ col.size(), col.size() == 100);
// toArray
Object[] objArray;
objArray = col.toArray();
for (int counter = 0; it.hasNext(); counter++) {
assertTrue(
"UnmodifiableCollectionTest - toArray returned incorrect array",
objArray[counter] == it.next());
}
// toArray (Object[])
objArray = new Object[100];
col.toArray(objArray);
for (int counter = 0; it.hasNext(); counter++) {
assertTrue(
"UnmodifiableCollectionTest - toArray(Object) filled array incorrectly",
objArray[counter] == it.next());
}
col.remove(new Integer(0));
assertTrue(
"Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(new Integer(0)));
assertEquals(99, col.size());
j = 0;
for (Iterator iter = col.iterator(); iter.hasNext();) {
Object element = iter.next();
j++;
}
assertEquals(99, j);
}
/**
* @tests java.util.TreeMap the values() method in sub maps
*/
public void test_subMap_values_size() {
BTreeMap myTreeMap = newBTreeMap();
for (int i = 0; i < 1000; i++) {
myTreeMap.put(i, objArray[i]);
}
// Test for method values() in subMaps
Collection vals = myTreeMap.subMap(200, 400).values();
assertTrue("Returned collection of incorrect size", vals.size() == 200);
for (int i = 200; i < 400; i++) {
assertTrue("Collection contains incorrect elements" + i, vals
.contains(objArray[i]));
}
assertEquals(200,vals.toArray().length);
vals.remove(objArray[300]);
assertTrue(
"Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(objArray[300]));
assertTrue("Returned collection of incorrect size", vals.size() == 199);
assertEquals(199,vals.toArray().length);
myTreeMap.put(300, objArray[300]);
// Test for method values() in subMaps
vals = myTreeMap.headMap(400).values();
assertEquals("Returned collection of incorrect size", vals.size(), 400);
for (int i = 0; i < 400; i++) {
assertTrue("Collection contains incorrect elements "+i, vals
.contains(objArray[i]));
}
assertEquals(400,vals.toArray().length);
vals.remove(objArray[300]);
assertTrue(
"Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(objArray[300]));
assertTrue("Returned collection of incorrect size", vals.size() == 399);
assertEquals(399,vals.toArray().length);
myTreeMap.put(300, objArray[300]);
// Test for method values() in subMaps
vals = myTreeMap.tailMap(400).values();
assertEquals("Returned collection of incorrect size", vals.size(), 600);
for (int i = 400; i < 1000; i++) {
assertTrue("Collection contains incorrect elements "+i, vals
.contains(objArray[i]));
}
assertEquals(600,vals.toArray().length);
vals.remove(objArray[600]);
assertTrue(
"Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(objArray[600]));
assertTrue("Returned collection of incorrect size", vals.size() == 599);
assertEquals(599,vals.toArray().length);
myTreeMap.put(600, objArray[600]);
// Test for method values() in subMaps
vals = myTreeMap.tailMap(401).values();
assertEquals("Returned collection of incorrect size", vals.size(), 599);
for (int i = 401; i < 1000; i++) {
assertTrue("Collection contains incorrect elements "+i, vals
.contains(objArray[i]));
}
assertEquals(599,vals.toArray().length);
vals.remove(objArray[600]);
assertTrue(
"Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(objArray[600]));
assertTrue("Returned collection of incorrect size", vals.size() == 598);
assertEquals(598,vals.toArray().length);
myTreeMap.put(600, objArray[600]);
// Test for method values() in subMaps
vals = myTreeMap.headMap(401).values();
assertEquals("Returned collection of incorrect size", vals.size(), 401);
for (int i = 0; i <= 400; i++) {
assertTrue("Collection contains incorrect elements "+i, vals
.contains(objArray[i]));
}
assertEquals(401,vals.toArray().length);
vals.remove(objArray[300]);
assertTrue(
"Removing from the values collection should remove from the original map",
!myTreeMap.containsValue(objArray[300]));
assertTrue("Returned collection of incorrect size", vals.size() == 400);
assertEquals(400,vals.toArray().length);
}
/**
* @tests java.util.TreeMap#subMap()
*/
public void test_subMap_Iterator2() {
BTreeMap<String, String> map = newBTreeMap();
String[] keys = { "1", "2", "3" };
String[] values = { "one", "two", "three" };
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], values[i]);
}
assertEquals(3, map.size());
Map subMap = map.subMap("", "test");
assertEquals(3, subMap.size());
Set entrySet = subMap.entrySet();
Iterator iter = entrySet.iterator();
int size = 0;
while (iter.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
.next();
assertTrue(map.containsKey(entry.getKey()));
assertTrue(map.containsValue(entry.getValue()));
size++;
}
assertEquals(map.size(), size);
Set<String> keySet = subMap.keySet();
iter = keySet.iterator();
size = 0;
while (iter.hasNext()) {
String key = (String) iter.next();
assertTrue(map.containsKey(key));
size++;
}
assertEquals(map.size(), size);
}
/**
* @tests {@link java.util.TreeMap#firstEntry()}
*/
public void test_firstEntry() throws Exception {
Integer testint = new Integer(-1);
Integer testint10000 = new Integer(-10000);
Integer testint9999 = new Integer(-9999);
assertEquals(objArray[0].toString(), tm.firstEntry().getKey());
assertEquals(objArray[0], tm.firstEntry().getValue());
tm.put(testint.toString(), testint);
assertEquals(testint.toString(), tm.firstEntry().getKey());
assertEquals(testint, tm.firstEntry().getValue());
tm.put(testint10000.toString(), testint10000);
assertEquals(testint.toString(), tm.firstEntry().getKey());
assertEquals(testint, tm.firstEntry().getValue());
tm.put(testint9999.toString(), testint9999);
assertEquals(testint.toString(), tm.firstEntry().getKey());
Entry entry = tm.firstEntry();
assertEquals(testint, entry.getValue());
assertEntry(entry);
tm.clear();
assertNull(tm.firstEntry());
}
/**
* @tests {@link java.util.TreeMap#lastEntry()
*/
public void test_lastEntry() throws Exception {
Integer testint10000 = new Integer(10000);
Integer testint9999 = new Integer(9999);
assertEquals(objArray[999].toString(), tm.lastEntry().getKey());
assertEquals(objArray[999], tm.lastEntry().getValue());
tm.put(testint10000.toString(), testint10000);
assertEquals(objArray[999].toString(), tm.lastEntry().getKey());
assertEquals(objArray[999], tm.lastEntry().getValue());
tm.put(testint9999.toString(), testint9999);
assertEquals(testint9999.toString(), tm.lastEntry().getKey());
Entry entry = tm.lastEntry();
assertEquals(testint9999, entry.getValue());
assertEntry(entry);
tm.clear();
assertNull(tm.lastEntry());
}
/**
* @tests {@link java.util.TreeMap#pollFirstEntry()
*/
public void test_pollFirstEntry() throws Exception {
Integer testint = new Integer(-1);
Integer testint10000 = new Integer(-10000);
Integer testint9999 = new Integer(-9999);
assertEquals(objArray[0].toString(), tm.pollFirstEntry().getKey());
assertEquals(objArray[1], tm.pollFirstEntry().getValue());
assertEquals(objArray[10], tm.pollFirstEntry().getValue());
tm.put(testint.toString(), testint);
tm.put(testint10000.toString(), testint10000);
assertEquals(testint.toString(), tm.pollFirstEntry().getKey());
assertEquals(testint10000, tm.pollFirstEntry().getValue());
tm.put(testint9999.toString(), testint9999);
assertEquals(testint9999.toString(), tm.pollFirstEntry().getKey());
Entry entry = tm.pollFirstEntry();
assertEntry(entry);
assertEquals(objArray[100], entry.getValue());
tm.clear();
assertNull(tm.pollFirstEntry());
}
/**
* @tests {@link java.util.TreeMap#pollLastEntry()
*/
public void test_pollLastEntry() throws Exception {
Integer testint10000 = new Integer(10000);
Integer testint9999 = new Integer(9999);
assertEquals(objArray[999].toString(), tm.pollLastEntry().getKey());
assertEquals(objArray[998], tm.pollLastEntry().getValue());
assertEquals(objArray[997], tm.pollLastEntry().getValue());
tm.put(testint10000.toString(), testint10000);
assertEquals(objArray[996], tm.pollLastEntry().getValue());
tm.put(testint9999.toString(), testint9999);
assertEquals(testint9999.toString(), tm.pollLastEntry().getKey());
Entry entry = tm.pollLastEntry();
assertEquals(objArray[995], entry.getValue());
assertEntry(entry);
tm.clear();
assertNull(tm.pollLastEntry());
}
public void testLastFirstEntryOnEmpty(){
tm.clear();
assertNull(tm.firstEntry());
assertNull(tm.lastEntry());
}
/**
* @tests {@link java.util.TreeMap#lowerEntry(Object)
*/
public void test_lowerEntry() throws Exception {
Integer testint10000 = new Integer(10000);
Integer testint9999 = new Integer(9999);
assertEquals(objArray[999], tm.lowerEntry(testint9999.toString())
.getValue());
assertEquals(objArray[100], tm.lowerEntry(testint10000.toString())
.getValue());
tm.put(testint10000.toString(), testint10000);
tm.put(testint9999.toString(), testint9999);
assertEquals(objArray[999], tm.lowerEntry(testint9999.toString())
.getValue());
Entry entry = tm.lowerEntry(testint10000.toString());
assertEquals(objArray[100], entry.getValue());
assertEntry(entry);
try {
tm.lowerEntry(testint10000);
fail("should throw ClassCastException");
} catch (ClassCastException e) {
// expected
}
try {
tm.lowerEntry(null);
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
tm.clear();
assertNull(tm.lowerEntry(testint9999.toString()));
// assertNull(tm.lowerEntry(null));
}
/**
* @tests {@link java.util.TreeMap#lowerKey(Object)
*/