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