1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNotSame;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertSame;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectOutputStream;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35
36 import org.apache.commons.collections4.BulkTest;
37 import org.apache.commons.collections4.IterableMap;
38 import org.apache.commons.collections4.MapIterator;
39 import org.apache.commons.collections4.iterators.AbstractMapIteratorTest;
40 import org.junit.jupiter.api.Test;
41
42
43
44
45
46
47
48 public class Flat3MapTest<K, V> extends AbstractIterableMapTest<K, V> {
49
50 public class TestFlatMapIterator extends AbstractMapIteratorTest<K, V> {
51
52 @Override
53 public V[] addSetValues() {
54 return getNewSampleValues();
55 }
56
57 @Override
58 public Map<K, V> getConfirmedMap() {
59
60 return getConfirmed();
61 }
62
63 @Override
64 public IterableMap<K, V> getMap() {
65
66 return Flat3MapTest.this.getMap();
67 }
68
69 @Override
70 public MapIterator<K, V> makeEmptyIterator() {
71 resetEmpty();
72 return Flat3MapTest.this.getMap().mapIterator();
73 }
74
75 @Override
76 public MapIterator<K, V> makeObject() {
77 resetFull();
78 return Flat3MapTest.this.getMap().mapIterator();
79 }
80
81 @Override
82 public boolean supportsRemove() {
83 return isRemoveSupported();
84 }
85
86 @Override
87 public boolean supportsSetValue() {
88 return isSetValueSupported();
89 }
90
91 @Override
92 public void verify() {
93 super.verify();
94 Flat3MapTest.this.verify();
95 }
96 }
97
98 private static final Integer ONE = Integer.valueOf(1);
99 private static final Integer TWO = Integer.valueOf(2);
100 private static final Integer THREE = Integer.valueOf(3);
101 private static final String TEN = "10";
102 private static final String TWENTY = "20";
103
104 private static final String THIRTY = "30";
105
106 @Override
107 public BulkTest bulkTestMapIterator() {
108 return new TestFlatMapIterator();
109 }
110
111 @Override
112 public String getCompatibilityVersion() {
113 return "4";
114 }
115
116 @Override
117 public Flat3Map<K, V> makeObject() {
118 return new Flat3Map<>();
119 }
120
121 private void putAndRemove(final Map<K, V> map) {
122 map.put((K) "A", (V) "one");
123 map.put((K) "B", (V) "two");
124 map.put((K) "C", (V) "three");
125 final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
126
127 final Map.Entry<K, V> mapEntry1 = it.next();
128 final Map.Entry<K, V> mapEntry2 = it.next();
129 final Map.Entry<K, V> mapEntry3 = it.next();
130 it.remove();
131 assertEquals(2, map.size());
132 assertEquals("one", map.get("A"));
133 assertEquals("two", map.get("B"));
134 assertNull(map.get("C"));
135 }
136
137 @Test
138 @SuppressWarnings("unchecked")
139 public void testClone2() {
140 final Flat3Map<K, V> map = makeObject();
141 assertEquals(0, map.size());
142 map.put((K) ONE, (V) TEN);
143 map.put((K) TWO, (V) TWENTY);
144 assertEquals(2, map.size());
145 assertTrue(map.containsKey(ONE));
146 assertTrue(map.containsKey(TWO));
147 assertSame(TEN, map.get(ONE));
148 assertSame(TWENTY, map.get(TWO));
149
150
151 final Flat3Map<K, V> cloned = map.clone();
152 assertEquals(2, cloned.size());
153 assertTrue(cloned.containsKey(ONE));
154 assertTrue(cloned.containsKey(TWO));
155 assertSame(TEN, cloned.get(ONE));
156 assertSame(TWENTY, cloned.get(TWO));
157
158
159 map.put((K) TEN, (V) ONE);
160 map.put((K) TWENTY, (V) TWO);
161 assertEquals(4, map.size());
162 assertEquals(2, cloned.size());
163 assertTrue(cloned.containsKey(ONE));
164 assertTrue(cloned.containsKey(TWO));
165 assertSame(TEN, cloned.get(ONE));
166 assertSame(TWENTY, cloned.get(TWO));
167 }
168
169 @Test
170 @SuppressWarnings("unchecked")
171 public void testClone4() {
172 final Flat3Map<K, V> map = makeObject();
173 assertEquals(0, map.size());
174 map.put((K) ONE, (V) TEN);
175 map.put((K) TWO, (V) TWENTY);
176 map.put((K) TEN, (V) ONE);
177 map.put((K) TWENTY, (V) TWO);
178
179
180 final Flat3Map<K, V> cloned = map.clone();
181 assertEquals(4, map.size());
182 assertEquals(4, cloned.size());
183 assertTrue(cloned.containsKey(ONE));
184 assertTrue(cloned.containsKey(TWO));
185 assertTrue(cloned.containsKey(TEN));
186 assertTrue(cloned.containsKey(TWENTY));
187 assertSame(TEN, cloned.get(ONE));
188 assertSame(TWENTY, cloned.get(TWO));
189 assertSame(ONE, cloned.get(TEN));
190 assertSame(TWO, cloned.get(TWENTY));
191
192
193 map.clear();
194 assertEquals(0, map.size());
195 assertEquals(4, cloned.size());
196 assertTrue(cloned.containsKey(ONE));
197 assertTrue(cloned.containsKey(TWO));
198 assertTrue(cloned.containsKey(TEN));
199 assertTrue(cloned.containsKey(TWENTY));
200 assertSame(TEN, cloned.get(ONE));
201 assertSame(TWENTY, cloned.get(TWO));
202 assertSame(ONE, cloned.get(TEN));
203 assertSame(TWO, cloned.get(TWENTY));
204 }
205
206 @Test
207 public void testCollections261() {
208 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
209 m.put(Integer.valueOf(1), Integer.valueOf(1));
210 m.put(Integer.valueOf(0), Integer.valueOf(0));
211 assertEquals(Integer.valueOf(1), m.remove(Integer.valueOf(1)));
212 assertEquals(Integer.valueOf(0), m.remove(Integer.valueOf(0)));
213
214 m.put(Integer.valueOf(2), Integer.valueOf(2));
215 m.put(Integer.valueOf(1), Integer.valueOf(1));
216 m.put(Integer.valueOf(0), Integer.valueOf(0));
217 assertEquals(Integer.valueOf(2), m.remove(Integer.valueOf(2)));
218 assertEquals(Integer.valueOf(1), m.remove(Integer.valueOf(1)));
219 assertEquals(Integer.valueOf(0), m.remove(Integer.valueOf(0)));
220 }
221
222 @Test
223 public void testContainsKey1() {
224 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
225
226 m.put(ONE, ONE);
227 m.put(TWO, TWO);
228 m.put(null, THREE);
229 final boolean contains = m.containsKey(null);
230 assertTrue(contains);
231 }
232
233 @Test
234 public void testContainsKey2() {
235 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
236
237 m.put(ONE, ONE);
238 m.put(null, TWO);
239 final boolean contains = m.containsKey(null);
240 assertTrue(contains);
241 }
242
243 @Test
244 public void testContainsKey3() {
245 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
246
247 m.put(null, ONE);
248 final boolean contains = m.containsKey(null);
249 assertTrue(contains);
250 }
251
252 @Test
253 public void testContainsValue1() {
254 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
255
256 m.put(ONE, ONE);
257 m.put(TWO, TWO);
258 m.put(THREE, null);
259 final boolean contains = m.containsValue(null);
260 assertTrue(contains);
261 }
262
263 @Test
264 public void testContainsValue2() {
265 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
266
267 m.put(ONE, ONE);
268 m.put(TWO, null);
269 final boolean contains = m.containsValue(null);
270 assertTrue(contains);
271 }
272
273 @Test
274 public void testContainsValue3() {
275 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
276
277 m.put(ONE, null);
278 final boolean contains = m.containsValue(null);
279 assertTrue(contains);
280 }
281
282 @Test
283 @SuppressWarnings("unchecked")
284 public void testEntryIteratorSetValue1() throws Exception {
285 final Flat3Map<K, V> map = makeObject();
286 map.put((K) ONE, (V) TEN);
287 map.put((K) TWO, (V) TWENTY);
288 map.put((K) THREE, (V) THIRTY);
289
290 final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
291 final Map.Entry<K, V> entry = it.next();
292 entry.setValue((V) "NewValue");
293 assertEquals(3, map.size());
294 assertTrue(map.containsKey(ONE));
295 assertTrue(map.containsKey(TWO));
296 assertTrue(map.containsKey(THREE));
297 assertEquals("NewValue", map.get(ONE));
298 assertEquals(TWENTY, map.get(TWO));
299 assertEquals(THIRTY, map.get(THREE));
300 }
301
302 @Test
303 @SuppressWarnings("unchecked")
304 public void testEntryIteratorSetValue2() throws Exception {
305 final Flat3Map<K, V> map = makeObject();
306 map.put((K) ONE, (V) TEN);
307 map.put((K) TWO, (V) TWENTY);
308 map.put((K) THREE, (V) THIRTY);
309
310 final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
311 it.next();
312 final Map.Entry<K, V> entry = it.next();
313 entry.setValue((V) "NewValue");
314 assertEquals(3, map.size());
315 assertTrue(map.containsKey(ONE));
316 assertTrue(map.containsKey(TWO));
317 assertTrue(map.containsKey(THREE));
318 assertEquals(TEN, map.get(ONE));
319 assertEquals("NewValue", map.get(TWO));
320 assertEquals(THIRTY, map.get(THREE));
321 }
322
323 @Test
324 @SuppressWarnings("unchecked")
325 public void testEntryIteratorSetValue3() throws Exception {
326 final Flat3Map<K, V> map = makeObject();
327 map.put((K) ONE, (V) TEN);
328 map.put((K) TWO, (V) TWENTY);
329 map.put((K) THREE, (V) THIRTY);
330
331 final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
332 it.next();
333 it.next();
334 final Map.Entry<K, V> entry = it.next();
335 entry.setValue((V) "NewValue");
336 assertEquals(3, map.size());
337 assertTrue(map.containsKey(ONE));
338 assertTrue(map.containsKey(TWO));
339 assertTrue(map.containsKey(THREE));
340 assertEquals(TEN, map.get(ONE));
341 assertEquals(TWENTY, map.get(TWO));
342 assertEquals("NewValue", map.get(THREE));
343 }
344
345 @Test
346 public void testEntrySet() {
347
348 putAndRemove(new LinkedHashMap<>());
349
350 putAndRemove(new Flat3Map<>());
351 }
352
353 @Test
354 @SuppressWarnings("unchecked")
355 public void testEquals1() {
356 final Flat3Map<K, V> map1 = makeObject();
357 map1.put((K) "a", (V) "testA");
358 map1.put((K) "b", (V) "testB");
359 final Flat3Map<K, V> map2 = makeObject();
360 map2.put((K) "a", (V) "testB");
361 map2.put((K) "b", (V) "testA");
362 assertFalse(map1.equals(map2));
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376 @Test
377 @SuppressWarnings("unchecked")
378 public void testEquals2() {
379 final Flat3Map<K, V> map1 = makeObject();
380 map1.put((K) "a", (V) "testA");
381 map1.put((K) "b", (V) "testB");
382 final Flat3Map<K, V> map2 = makeObject();
383 map2.put((K) "a", (V) "testB");
384 map2.put((K) "c", (V) "testA");
385 assertFalse(map1.equals(map2));
386 }
387
388 @Test
389 public void testGet1() {
390 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
391 final Object obj;
392
393 m.put(null, ONE);
394 obj = m.get(null);
395 assertSame(ONE, obj);
396 }
397
398 @Test
399 public void testGet2() {
400 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
401 final Object obj;
402
403 m.put(ONE, ONE);
404 m.put(null, TWO);
405 obj = m.get(null);
406 assertSame(TWO, obj);
407 }
408
409 @Test
410 public void testGet3() {
411 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
412 final Object obj;
413
414 m.put(ONE, ONE);
415 m.put(TWO, TWO);
416 m.put(null, THREE);
417 obj = m.get(null);
418 assertSame(THREE, obj);
419 }
420
421 @Test
422 @SuppressWarnings("unchecked")
423 public void testMapIteratorSetValue1() throws Exception {
424 final Flat3Map<K, V> map = makeObject();
425 map.put((K) ONE, (V) TEN);
426 map.put((K) TWO, (V) TWENTY);
427 map.put((K) THREE, (V) THIRTY);
428
429 final MapIterator<K, V> it = map.mapIterator();
430 it.next();
431 it.setValue((V) "NewValue");
432 assertEquals(3, map.size());
433 assertTrue(map.containsKey(ONE));
434 assertTrue(map.containsKey(TWO));
435 assertTrue(map.containsKey(THREE));
436 assertEquals("NewValue", map.get(ONE));
437 assertEquals(TWENTY, map.get(TWO));
438 assertEquals(THIRTY, map.get(THREE));
439 }
440
441 @Test
442 @SuppressWarnings("unchecked")
443 public void testMapIteratorSetValue2() throws Exception {
444 final Flat3Map<K, V> map = makeObject();
445 map.put((K) ONE, (V) TEN);
446 map.put((K) TWO, (V) TWENTY);
447 map.put((K) THREE, (V) THIRTY);
448
449 final MapIterator<K, V> it = map.mapIterator();
450 it.next();
451 it.next();
452 it.setValue((V) "NewValue");
453 assertEquals(3, map.size());
454 assertTrue(map.containsKey(ONE));
455 assertTrue(map.containsKey(TWO));
456 assertTrue(map.containsKey(THREE));
457 assertEquals(TEN, map.get(ONE));
458 assertEquals("NewValue", map.get(TWO));
459 assertEquals(THIRTY, map.get(THREE));
460 }
461
462 @Test
463 @SuppressWarnings("unchecked")
464 public void testMapIteratorSetValue3() throws Exception {
465 final Flat3Map<K, V> map = makeObject();
466 map.put((K) ONE, (V) TEN);
467 map.put((K) TWO, (V) TWENTY);
468 map.put((K) THREE, (V) THIRTY);
469
470 final MapIterator<K, V> it = map.mapIterator();
471 it.next();
472 it.next();
473 it.next();
474 it.setValue((V) "NewValue");
475 assertEquals(3, map.size());
476 assertTrue(map.containsKey(ONE));
477 assertTrue(map.containsKey(TWO));
478 assertTrue(map.containsKey(THREE));
479 assertEquals(TEN, map.get(ONE));
480 assertEquals(TWENTY, map.get(TWO));
481 assertEquals("NewValue", map.get(THREE));
482 }
483
484 @Test
485 public void testNewInstance1() {
486 final Map<Integer, Integer> orig = new HashMap<>();
487 orig.put(ONE, ONE);
488 orig.put(TWO, TWO);
489
490 final Flat3Map<Integer, Integer> m = new Flat3Map<>(orig);
491
492 assertEquals(orig, m);
493 assertEquals(2, m.size());
494 }
495
496 @Test
497 public void testPut1() {
498 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
499
500 m.put(ONE, ONE);
501 m.put(TWO, TWO);
502 m.put(null, THREE);
503 final Object old = m.put(null, ONE);
504 assertEquals(THREE, old);
505 assertEquals(ONE, m.get(null));
506 }
507
508 @Test
509 public void testPut2() {
510 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
511
512 m.put(ONE, ONE);
513 m.put(null, THREE);
514 final Object old = m.put(null, ONE);
515 assertEquals(THREE, old);
516 assertEquals(ONE, m.get(null));
517 }
518
519 @Test
520 public void testPut3() {
521 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
522
523 m.put(null, THREE);
524 final Object old = m.put(null, ONE);
525 assertEquals(THREE, old);
526 assertNull(m.get(ONE));
527 }
528
529 @Test
530 public void testPut4() {
531 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
532
533 m.put(ONE, ONE);
534 m.put(TWO, TWO);
535 m.put(THREE, THREE);
536 final Object old = m.put(THREE, ONE);
537 assertEquals(THREE, old);
538 assertEquals(ONE, m.get(THREE));
539 }
540
541 @Test
542 public void testPut5() {
543 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
544
545 m.put(ONE, ONE);
546 m.put(TWO, THREE);
547 final Object old = m.put(TWO, ONE);
548 assertEquals(THREE, old);
549 assertEquals(ONE, m.get(TWO));
550 }
551
552 @Test
553 public void testPut6() {
554 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
555
556 m.put(ONE, THREE);
557 final Object old = m.put(ONE, ONE);
558 assertEquals(THREE, old);
559 assertEquals(ONE, m.get(ONE));
560 }
561
562 @Test
563 public void testRemove1() {
564 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
565
566
567 Object obj = m.remove(44);
568 assertNull(obj);
569
570 m.put(ONE, ONE);
571 obj = m.remove(ONE);
572 assertSame(ONE, obj);
573 assertEquals(0, m.size());
574
575
576 obj = m.get(ONE);
577 assertNull(obj);
578
579 m.put(ONE, ONE);
580 m.put(TWO, TWO);
581 m.put(THREE, THREE);
582
583 obj = m.remove(ONE);
584 assertSame(ONE, obj);
585
586 obj = m.get(ONE);
587 assertNull(obj);
588 obj = m.get(TWO);
589 assertSame(TWO, obj);
590 }
591
592 @Test
593 public void testRemove10() {
594 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
595 final Object obj;
596
597 m.put(ONE, ONE);
598 m.put(TWO, TWO);
599
600 obj = m.remove(null);
601 assertNull(obj);
602 }
603
604 @Test
605 public void testRemove11() {
606 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
607 final Object obj;
608
609 m.put(ONE, ONE);
610 m.put(TWO, TWO);
611 m.put(THREE, THREE);
612
613 obj = m.remove(null);
614 assertNull(obj);
615 }
616
617 @Test
618 public void testRemove12() {
619 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
620 final Object obj;
621
622 m.put(ONE, ONE);
623 m.put(TWO, TWO);
624 m.put(THREE, THREE);
625
626 obj = m.remove(42);
627 assertNull(obj);
628 }
629
630 @Test
631 public void testRemove13() {
632 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
633 final Object obj;
634
635 m.put(ONE, ONE);
636 m.put(TWO, TWO);
637
638 obj = m.remove(42);
639 assertNull(obj);
640 }
641
642 @Test
643 public void testRemove2() {
644 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
645 Object obj;
646
647 m.put(ONE, ONE);
648 m.put(TWO, TWO);
649 m.put(THREE, THREE);
650
651 obj = m.remove(ONE);
652 assertSame(ONE, obj);
653
654 obj = m.get(ONE);
655 assertNull(obj);
656 obj = m.get(TWO);
657 assertSame(TWO, obj);
658 obj = m.get(THREE);
659 assertSame(THREE, obj);
660 }
661
662 @Test
663 public void testRemove3() {
664 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
665 Object obj;
666
667 m.put(ONE, ONE);
668 m.put(TWO, TWO);
669 m.put(THREE, THREE);
670
671 obj = m.remove(TWO);
672 assertSame(TWO, obj);
673
674 obj = m.get(ONE);
675 assertSame(ONE, obj);
676 obj = m.get(TWO);
677 assertNull(obj);
678 obj = m.get(THREE);
679 assertSame(THREE, obj);
680 }
681
682 @Test
683 public void testRemove4() {
684 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
685 Object obj;
686
687 m.put(ONE, ONE);
688 m.put(TWO, TWO);
689 m.put(THREE, THREE);
690
691 obj = m.remove(THREE);
692 assertSame(THREE, obj);
693
694 obj = m.get(ONE);
695 assertSame(ONE, obj);
696 obj = m.get(TWO);
697 assertSame(TWO, obj);
698 obj = m.get(THREE);
699 assertNull(obj);
700 }
701
702 @Test
703 public void testRemove5() {
704 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
705 Object obj;
706
707 m.put(null, ONE);
708
709 obj = m.remove(null);
710 assertSame(ONE, obj);
711
712 obj = m.get(null);
713 assertNull(obj);
714 }
715
716 @Test
717 public void testRemove6() {
718 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
719 Object obj;
720
721 m.put(ONE, ONE);
722 m.put(null, TWO);
723
724 obj = m.remove(null);
725 assertSame(TWO, obj);
726
727 obj = m.get(ONE);
728 assertSame(ONE, obj);
729 obj = m.get(null);
730 assertNull(obj);
731 }
732
733 @Test
734 public void testRemove7() {
735 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
736 Object obj;
737
738 m.put(null, ONE);
739 m.put(TWO, TWO);
740
741 obj = m.remove(null);
742 assertSame(ONE, obj);
743
744 obj = m.get(null);
745 assertNull(obj);
746 obj = m.get(TWO);
747 assertSame(TWO, obj);
748 }
749
750 @Test
751 public void testRemove8() {
752 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
753 Object obj;
754
755 m.put(ONE, ONE);
756 m.put(TWO, TWO);
757 m.put(null, THREE);
758
759 obj = m.remove(null);
760 assertSame(THREE, obj);
761
762 obj = m.get(ONE);
763 assertSame(ONE, obj);
764 obj = m.get(TWO);
765 assertSame(TWO, obj);
766 obj = m.get(null);
767 assertNull(obj);
768 }
769
770 @Test
771 public void testRemove9() {
772 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
773 final Object obj;
774
775 m.put(ONE, ONE);
776
777 obj = m.remove(null);
778 assertNull(obj);
779 }
780
781 @Test
782 public void testSerialization0() throws Exception {
783 final Flat3Map<K, V> map = makeObject();
784 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
785 final ObjectOutputStream out = new ObjectOutputStream(bout);
786 out.writeObject(map);
787 final byte[] bytes = bout.toByteArray();
788 out.close();
789 final ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
790 final ObjectInputStream in = new ObjectInputStream(bin);
791 final Flat3Map<?, ?> ser = (Flat3Map<?, ?>) in.readObject();
792 in.close();
793 assertEquals(0, map.size());
794 assertEquals(0, ser.size());
795 }
796
797 @Test
798 @SuppressWarnings("unchecked")
799 public void testSerialization2() throws Exception {
800 final Flat3Map<K, V> map = makeObject();
801 map.put((K) ONE, (V) TEN);
802 map.put((K) TWO, (V) TWENTY);
803
804 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
805 final ObjectOutputStream out = new ObjectOutputStream(bout);
806 out.writeObject(map);
807 final byte[] bytes = bout.toByteArray();
808 out.close();
809 final ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
810 final ObjectInputStream in = new ObjectInputStream(bin);
811 final Flat3Map<?, ?> ser = (Flat3Map<?, ?>) in.readObject();
812 in.close();
813 assertEquals(2, map.size());
814 assertEquals(2, ser.size());
815 assertTrue(ser.containsKey(ONE));
816 assertTrue(ser.containsKey(TWO));
817 assertEquals(TEN, ser.get(ONE));
818 assertEquals(TWENTY, ser.get(TWO));
819 }
820
821 @Test
822 @SuppressWarnings("unchecked")
823 public void testSerialization4() throws Exception {
824 final Flat3Map<K, V> map = makeObject();
825 map.put((K) ONE, (V) TEN);
826 map.put((K) TWO, (V) TWENTY);
827 map.put((K) TEN, (V) ONE);
828 map.put((K) TWENTY, (V) TWO);
829
830 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
831 final ObjectOutputStream out = new ObjectOutputStream(bout);
832 out.writeObject(map);
833 final byte[] bytes = bout.toByteArray();
834 out.close();
835 final ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
836 final ObjectInputStream in = new ObjectInputStream(bin);
837 final Flat3Map<?, ?> ser = (Flat3Map<?, ?>) in.readObject();
838 in.close();
839 assertEquals(4, map.size());
840 assertEquals(4, ser.size());
841 assertTrue(ser.containsKey(ONE));
842 assertTrue(ser.containsKey(TWO));
843 assertTrue(ser.containsKey(TEN));
844 assertTrue(ser.containsKey(TWENTY));
845 assertEquals(TEN, ser.get(ONE));
846 assertEquals(TWENTY, ser.get(TWO));
847 assertEquals(ONE, ser.get(TEN));
848 assertEquals(TWO, ser.get(TWENTY));
849 }
850
851 @Test
852 public void testToString() {
853 final Flat3Map<Integer, Integer> m = new Flat3Map<>();
854 final String string0 = m.toString();
855 assertNotNull(string0);
856 m.put(Integer.valueOf(1), Integer.valueOf(1));
857 final String string1 = m.toString();
858 assertNotNull(string1);
859 assertNotSame(string0, string1);
860 m.put(Integer.valueOf(0), Integer.valueOf(0));
861 final String string2 = m.toString();
862 assertNotNull(string2);
863 assertNotSame(string0, string2);
864 assertNotSame(string1, string2);
865 m.put(Integer.valueOf(2), Integer.valueOf(2));
866 final String string3 = m.toString();
867 assertNotNull(string3);
868 assertNotSame(string0, string3);
869 assertNotSame(string1, string3);
870 assertNotSame(string2, string3);
871 }
872 }