1/*
2 * Copyright (C) 2011 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.util;
18
19import java.io.Serializable;
20import java.util.ArrayList;
21import java.util.Collections;
22import java.util.Comparator;
23import java.util.Enumeration;
24import java.util.HashMap;
25import java.util.Iterator;
26import java.util.ListIterator;
27import java.util.Map;
28import java.util.NoSuchElementException;
29import java.util.Spliterator;
30
31import junit.framework.TestCase;
32
33public final class CollectionsTest extends TestCase {
34
35    private static final Object NOT_A_STRING = new Object();
36    private static final Object A_STRING = "string";
37
38    public void testEmptyEnumeration() {
39        Enumeration<Object> e = Collections.emptyEnumeration();
40        assertFalse(e instanceof Serializable);
41        assertFalse(e.hasMoreElements());
42        try {
43            e.nextElement();
44            fail();
45        } catch (NoSuchElementException expected) {
46        }
47    }
48
49    public void testEmptyIterator() {
50        testEmptyIterator(Collections.emptyIterator());
51        testEmptyIterator(Collections.emptyList().iterator());
52        testEmptyIterator(Collections.emptySet().iterator());
53        testEmptyIterator(Collections.emptyMap().keySet().iterator());
54        testEmptyIterator(Collections.emptyMap().entrySet().iterator());
55        testEmptyIterator(Collections.emptyMap().values().iterator());
56    }
57
58    private void testEmptyIterator(Iterator<?> i) {
59        assertFalse(i instanceof Serializable);
60        assertFalse(i.hasNext());
61        try {
62            i.next();
63            fail();
64        } catch (NoSuchElementException expected) {
65        }
66        try {
67            i.remove();
68            fail();
69        } catch (IllegalStateException expected) {
70        }
71    }
72
73    public void testEmptyListIterator() {
74        testEmptyListIterator(Collections.emptyListIterator());
75        testEmptyListIterator(Collections.emptyList().listIterator());
76        testEmptyListIterator(Collections.emptyList().listIterator(0));
77    }
78
79    private void testEmptyListIterator(ListIterator<?> i) {
80        assertFalse(i instanceof Serializable);
81        assertFalse(i.hasNext());
82        assertFalse(i.hasPrevious());
83        assertEquals(0, i.nextIndex());
84        try {
85            i.next();
86            fail();
87        } catch (NoSuchElementException expected) {
88        }
89        assertEquals(-1, i.previousIndex());
90        try {
91            i.previous();
92            fail();
93        } catch (NoSuchElementException expected) {
94        }
95        try {
96            i.add(null);
97            fail();
98        } catch (UnsupportedOperationException expected) {
99        }
100        try {
101            i.remove();
102            fail();
103        } catch (IllegalStateException expected) {
104        }
105    }
106
107    public static final class ArrayListInheritor<T> extends ArrayList<T> {
108        public ArrayListInheritor(int capacity) {
109            super(capacity);
110        }
111    }
112
113    public void testSort_leavesModcountUnmodified() {
114        // This tests the fast path for ArrayLists where we can get away without
115        // a copy.
116        ArrayList<String> list = new ArrayList<String>(16);
117        list.add("coven");
118        list.add("asylum");
119        list.add("murder house");
120        list.add("freak show");
121
122        Iterator<String> it = list.iterator();
123        it.next();
124        Collections.sort(list);
125        it.next();
126
127        list = new ArrayListInheritor<String>(16);
128        list.add("apples");
129        list.add("oranges");
130        list.add("pineapples");
131        list.add("bacon");
132
133        it = list.iterator();
134        it.next();
135        Collections.sort(list);
136        it.next();
137    }
138
139    /**
140     * A value type whose {@code compareTo} method returns one of {@code 0},
141     * {@code Integer.MIN_VALUE} and {@code Integer.MAX_VALUE}.
142     */
143    static final class IntegerWithExtremeComparator
144            implements Comparable<IntegerWithExtremeComparator> {
145        private final int value;
146
147        public IntegerWithExtremeComparator(int value) {
148            this.value = value;
149        }
150
151        @Override
152        public int compareTo(IntegerWithExtremeComparator another) {
153            if (another.value == this.value) {
154                return 0;
155            } else if (another.value > this.value) {
156                return Integer.MIN_VALUE;
157            } else {
158                return Integer.MAX_VALUE;
159            }
160        }
161    }
162
163    // http://b/19749094
164    public void testBinarySearch_comparatorThatReturnsMinAndMaxValue() {
165        ArrayList<Integer> list = new ArrayList<Integer>(16);
166        list.add(4);
167        list.add(9);
168        list.add(11);
169        list.add(14);
170        list.add(16);
171
172        int index = Collections.binarySearch(list, 9, new Comparator<Integer>() {
173            @Override
174            public int compare(Integer lhs, Integer rhs) {
175                final int compare = lhs.compareTo(rhs);
176                if (compare == 0) {
177                    return 0;
178                } else if (compare < 0) {
179                    return Integer.MIN_VALUE;
180                } else {
181                    return Integer.MAX_VALUE;
182                }
183            }
184        });
185        assertEquals(1, index);
186
187        ArrayList<IntegerWithExtremeComparator> list2 =
188                new ArrayList<IntegerWithExtremeComparator>();
189        list2.add(new IntegerWithExtremeComparator(4));
190        list2.add(new IntegerWithExtremeComparator(9));
191        list2.add(new IntegerWithExtremeComparator(11));
192        list2.add(new IntegerWithExtremeComparator(14));
193        list2.add(new IntegerWithExtremeComparator(16));
194
195        assertEquals(1, Collections.binarySearch(list2, new IntegerWithExtremeComparator(9)));
196    }
197
198    public void testBinarySearch_emptyCollection() {
199        assertEquals(-1, Collections.binarySearch(new ArrayList<Integer>(), 9));
200
201        assertEquals(-1, Collections.binarySearch(new ArrayList<>(), 9, Integer::compareTo));
202    }
203
204    public void testSingletonSpliterator() {
205        Spliterator<String> sp = Collections.singletonList("spiff").spliterator();
206
207        assertEquals(1, sp.estimateSize());
208        assertEquals(1, sp.getExactSizeIfKnown());
209        assertNull(sp.trySplit());
210        assertEquals(true, sp.tryAdvance(value -> assertEquals("spiff", value)));
211        assertEquals(false, sp.tryAdvance(value -> fail()));
212    }
213
214    public void test_unmodifiableMap_getOrDefault() {
215        HashMap<Integer, Double> hashMap = new HashMap<>();
216        hashMap.put(2, 12.0);
217        hashMap.put(3, null);
218        Map<Integer, Double> m = Collections.unmodifiableMap(hashMap);
219        assertEquals(-1.0, m.getOrDefault(1, -1.0));
220        assertEquals(12.0, m.getOrDefault(2, -1.0));
221        assertEquals(null, m.getOrDefault(3, -1.0));
222    }
223
224    public void test_unmodifiableMap_forEach() {
225        Map<Integer, Double> hashMap = new HashMap<>();
226        Map<Integer, Double> replica = new HashMap<>();
227        hashMap.put(1, 10.0);
228        hashMap.put(2, 20.0);
229        Collections.unmodifiableMap(hashMap).forEach(replica::put);
230        assertEquals(10.0, replica.get(1));
231        assertEquals(20.0, replica.get(2));
232        assertEquals(2, replica.size());
233    }
234
235    public void test_unmodifiableMap_putIfAbsent() {
236        try {
237            Collections.unmodifiableMap(new HashMap<>()).putIfAbsent(1, 5.0);
238            fail();
239        } catch (UnsupportedOperationException expected) {
240        }
241
242        // For existing key
243        HashMap<Integer, Double> m = new HashMap<>();
244        m.put(1, 5.0);
245        try {
246            Collections.unmodifiableMap(m).putIfAbsent(1, 5.0);
247            fail();
248        } catch (UnsupportedOperationException expected) {
249        }
250    }
251
252    public void test_unmodifiableMap_remove() {
253        try {
254            Collections.unmodifiableMap(new HashMap<>()).remove(1, 5.0);
255            fail();
256        } catch (UnsupportedOperationException expected) {
257        }
258
259        // For existing key
260        HashMap<Integer, Double> m = new HashMap<>();
261        m.put(1, 5.0);
262        try {
263            Collections.unmodifiableMap(m).remove(1, 5.0);
264            fail();
265        } catch (UnsupportedOperationException expected) {
266        }
267    }
268
269    public void test_unmodifiableMap_replace$K$V$V() {
270        try {
271            Collections.unmodifiableMap(new HashMap<>()).replace(1, 5.0, 1.0);
272            fail();
273        } catch (UnsupportedOperationException expected) {
274        }
275
276        // For existing key
277        HashMap<Integer, Double> m = new HashMap<>();
278        m.put(1, 5.0);
279        try {
280            Collections.unmodifiableMap(m).replace(1, 5.0, 1.0);
281            fail();
282        } catch (UnsupportedOperationException expected) {
283        }
284    }
285
286    public void test_unmodifiableMap_replace$K$V() {
287        try {
288            Collections.unmodifiableMap(new HashMap<>()).replace(1, 5.0);
289            fail();
290        } catch (UnsupportedOperationException expected) {
291        }
292
293        // For existing key
294        HashMap<Integer, Double> m = new HashMap<>();
295        m.put(1, 5.0);
296        try {
297            Collections.unmodifiableMap(m).replace(1, 5.0);
298            fail();
299        } catch (UnsupportedOperationException expected) {
300        }
301    }
302
303    public void test_unmodifiableMap_computeIfAbsent() {
304        try {
305            Collections.unmodifiableMap(new HashMap<>()).computeIfAbsent(1, k -> 1.0);
306            fail();
307        } catch (UnsupportedOperationException expected) {
308        }
309
310        // For existing key
311        HashMap<Integer, Double> m = new HashMap<>();
312        m.put(1, 5.0);
313        try {
314            Collections.unmodifiableMap(m).computeIfAbsent(1, k -> 1.0);
315            fail();
316        } catch (UnsupportedOperationException expected) {
317        }
318    }
319
320    public void test_unmodifiableMap_computeIfPresent() {
321        try {
322            Collections.unmodifiableMap(new HashMap<>()).computeIfPresent(1, (k, v) -> 1.0);
323            fail();
324        } catch (UnsupportedOperationException expected) {
325        }
326
327        // For existing key
328        HashMap<Integer, Double> m = new HashMap<>();
329        m.put(1, 5.0);
330        try {
331            Collections.unmodifiableMap(m).computeIfPresent(1, (k, v) -> 1.0);
332            fail();
333        } catch (UnsupportedOperationException expected) {
334        }
335    }
336
337    public void test_unmodifiableMap_compute() {
338        try {
339            Collections.unmodifiableMap(new HashMap<>()).compute(1, (k, v) -> 1.0);
340            fail();
341        } catch (UnsupportedOperationException expected) {
342        }
343
344        // For existing key
345        HashMap<Integer, Double> m = new HashMap<>();
346        m.put(1, 5.0);
347        try {
348            Collections.unmodifiableMap(m).compute(1, (k, v) -> 1.0);
349            fail();
350        } catch (UnsupportedOperationException expected) {
351        }
352    }
353
354    public void test_unmodifiableMap_merge() {
355        try {
356            Collections.unmodifiableMap(new HashMap<>()).merge(1, 2.0, (k, v) -> 1.0);
357            fail();
358        } catch (UnsupportedOperationException expected) {
359        }
360
361        // For existing key
362        HashMap<Integer, Double> m = new HashMap<>();
363        m.put(1, 5.0);
364        try {
365            Collections.unmodifiableMap(m).merge(1, 2.0, (k, v) -> 1.0);
366            fail();
367        } catch (UnsupportedOperationException expected) {
368        }
369    }
370
371    public void test_EmptyMap_getOrDefault() {
372        Map<Integer, Double> m = Collections.emptyMap();
373        assertEquals(-1.0, m.getOrDefault(1, -1.0));
374        assertEquals(-1.0, m.getOrDefault(2, -1.0));
375    }
376
377    public void test_EmptyMap_forEach() {
378        try {
379            Collections.emptyMap().forEach(null);
380            fail();
381        } catch (NullPointerException expected) {
382        }
383    }
384
385    public void test_EmptyMap_putIfAbsent() {
386        try {
387            Collections.emptyMap().putIfAbsent(1, 5.0);
388            fail();
389        } catch (UnsupportedOperationException expected) {
390        }
391    }
392
393    public void test_EmptyMap_remove() {
394        try {
395            Collections.emptyMap().remove(1, 5.0);
396            fail();
397        } catch (UnsupportedOperationException expected) {
398        }
399    }
400
401    public void test_EmptyMap_replace$K$V$V() {
402        try {
403            Collections.emptyMap().replace(1, 5.0, 5.0);
404            fail();
405        } catch (UnsupportedOperationException expected) {
406        }
407    }
408
409    public void test_EmptyMap_replace$K$V() {
410        try {
411            Collections.emptyMap().replace(1, 5.0);
412            fail();
413        } catch (UnsupportedOperationException expected) {
414        }
415    }
416
417    public void test_EmptyMap_computeIfAbsent() {
418        try {
419            Collections.emptyMap().computeIfAbsent(1, k -> 5.0);
420            fail();
421        } catch (UnsupportedOperationException expected) {
422        }
423    }
424
425    public void test_EmptyMap_computeIfPresent() {
426        try {
427            Collections.emptyMap().computeIfPresent(1, (k, v) -> 5.0);
428            fail();
429        } catch (UnsupportedOperationException expected) {
430        }
431    }
432
433    public void test_EmptyMap_compute() {
434        try {
435            Collections.emptyMap().compute(1, (k, v) -> 5.0);
436            fail();
437        } catch (UnsupportedOperationException expected) {
438        }
439    }
440
441    public void test_EmptyMap_merge() {
442        try {
443            Collections.emptyMap().merge(1, 5.0, (k, v) -> 5.0);
444            fail();
445        } catch (UnsupportedOperationException expected) {
446        }
447    }
448
449    public void test_SingletonMap_getOrDefault() {
450        Map<Integer, Double> m = Collections.singletonMap(1, 11.0);
451        assertEquals(11.0, m.getOrDefault(1, -1.0));
452        assertEquals(-1.0, m.getOrDefault(2, -1.0));
453    }
454
455    public void test_SingletonMap_forEach() {
456        Map<Integer, Double> m = new HashMap<>();
457        Collections.singletonMap(1, 11.0).forEach(m::put);
458        assertEquals(11.0, m.getOrDefault(1, -1.0));
459        assertEquals(1, m.size());
460    }
461
462    public void test_SingletonMap_putIfAbsent() {
463        try {
464            Collections.singletonMap(1, 11.0).putIfAbsent(1, 5.0);
465            fail();
466        } catch (UnsupportedOperationException expected) {
467        }
468    }
469
470    public void test_SingletonMap_remove() {
471        try {
472            Collections.singletonMap(1, 11.0).remove(1, 5.0);
473            fail();
474        } catch (UnsupportedOperationException expected) {
475        }
476    }
477
478    public void test_SingletonMap_replace$K$V$V() {
479        try {
480            Collections.singletonMap(1, 11.0).replace(1, 5.0, 5.0);
481            fail();
482        } catch (UnsupportedOperationException expected) {
483        }
484    }
485
486    public void test_SingletonMap_replace$K$V() {
487        try {
488            Collections.singletonMap(1, 11.0).replace(1, 5.0);
489            fail();
490        } catch (UnsupportedOperationException expected) {
491        }
492    }
493
494    public void test_SingletonMap_computeIfAbsent() {
495        try {
496            Collections.singletonMap(1, 11.0).computeIfAbsent(1, k -> 5.0);
497            fail();
498        } catch (UnsupportedOperationException expected) {
499        }
500    }
501
502    public void test_SingletonMap_computeIfPresent() {
503        try {
504            Collections.singletonMap(1, 11.0).computeIfPresent(1, (k, v) -> 5.0);
505            fail();
506        } catch (UnsupportedOperationException expected) {
507        }
508    }
509
510    public void test_SingletonMap_compute() {
511        try {
512            Collections.singletonMap(1, 11.0).compute(1, (k, v) -> 5.0);
513            fail();
514        } catch (UnsupportedOperationException expected) {
515        }
516    }
517
518    public void test_SingletonMap_merge() {
519        try {
520            Collections.singletonMap(1, 11.0).merge(1, 5.0, (k, v) -> 5.0);
521            fail();
522        } catch (UnsupportedOperationException expected) {
523        }
524    }
525
526    public void test_SynchronizedList_replaceAll() {
527        ListDefaultMethodTester.test_replaceAll(Collections.synchronizedList(new ArrayList<>()));
528    }
529
530    public void test_SynchronizedList_sort() {
531        ListDefaultMethodTester.test_sort(Collections.synchronizedList(new ArrayList<>()));
532    }
533
534    public void test_CheckedList_replaceAll() {
535        ListDefaultMethodTester.test_replaceAll(Collections.checkedList(new ArrayList<>(), Integer.class));
536    }
537
538    public void test_CheckedList_sort() {
539        ListDefaultMethodTester.test_sort(Collections.checkedList(new ArrayList<>(), Double.class));
540    }
541
542    public void test_EmptyList_replaceAll() {
543        Collections.emptyList().replaceAll(k -> 1);
544
545        try {
546            Collections.emptyList().replaceAll(null);
547            fail();
548        } catch (NullPointerException expected) {
549        }
550    }
551
552    public void test_EmptyList_sort() {
553        Collections.emptyList().sort((k1, k2) -> 1);
554    }
555
556    public void test_unmodifiableList_replaceAll() {
557        try {
558            Collections.unmodifiableList(new ArrayList<>()).replaceAll(k -> 1);
559            fail();
560        } catch (UnsupportedOperationException expected) {
561        }
562
563        // with non empty list
564
565        try {
566            ArrayList l = new ArrayList();
567            l.add(1);
568            l.add(2);
569            Collections.unmodifiableList(l).replaceAll(k -> 1);
570            fail();
571        } catch (UnsupportedOperationException expected) {
572        }
573    }
574
575    public void test_unmodifiableList_sort() {
576        try {
577            Collections.unmodifiableList(new ArrayList<>()).sort((k1, k2) -> 1);
578            fail();
579        } catch (UnsupportedOperationException expected) {
580        }
581
582        // with non empty list
583
584        try {
585            ArrayList l = new ArrayList();
586            l.add(1);
587            l.add(2);
588            Collections.unmodifiableList(l).sort((k1, k2) -> 1);
589            fail();
590        } catch (UnsupportedOperationException expected) {
591        }
592    }
593
594    public void test_SingletonList_replaceAll() {
595        try {
596            Collections.singletonList(1).replaceAll(k -> 2);
597            fail();
598        } catch (UnsupportedOperationException expected) {
599        }
600    }
601
602    public void test_SingletonList_sort() {
603        Collections.singletonList(1).sort((k1, k2) -> 2);
604    }
605
606    public void test_CheckedMap_replaceAll() {
607        Map<Integer, Integer> map = new HashMap<>();
608        Map checkedMap = Collections.checkedMap(map, Integer.class, Integer.class);
609        checkedMap.put(1, 10);
610        checkedMap.put(2, 20);
611        checkedMap.put(3, 30);
612        checkedMap.replaceAll((k, v) -> (Integer)k + (Integer)v);
613        assertEquals(11, checkedMap.get(1));
614        assertEquals(22, checkedMap.get(2));
615        assertEquals(33, checkedMap.get(3));
616        assertEquals(3, checkedMap.size());
617    }
618
619    public void test_CheckedMap_putIfAbsent() {
620        Map<Integer, Double> map = new HashMap<>();
621        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
622        MapDefaultMethodTester.test_putIfAbsent(checkedMap, true /* acceptsNullKey */,
623                true /* acceptsNullValue */);
624
625        // Without generics to check the typeCheck implementation
626        Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
627
628        // When key is present
629        checkedMap2.putIfAbsent(1, A_STRING);
630        try {
631            checkedMap2.putIfAbsent(1, NOT_A_STRING);
632        } catch (ClassCastException expected) {}
633
634        // When key is absent
635        checkedMap2.clear();
636        try {
637            checkedMap2.putIfAbsent(1, NOT_A_STRING);
638        } catch (ClassCastException expected) {}
639    }
640
641    public void test_CheckedMap_remove() {
642        Map<Integer, Double> map = new HashMap<>();
643        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
644        MapDefaultMethodTester.test_remove(checkedMap, true /* acceptsNullKey */,
645                true /* acceptsNullValue */);
646    }
647
648    public void test_CheckedMap_replace$K$V$V() {
649        Map<Integer, Double> map = new HashMap<>();
650        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
651        MapDefaultMethodTester.test_replace$K$V$V(checkedMap, true /* acceptsNullKey */,
652                true /* acceptsNullValue */);
653
654        // Without generics to check the typeCheck implementation
655        Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
656        checkedMap2.put(1, A_STRING);
657
658        try {
659            checkedMap2.replace(1, NOT_A_STRING);
660        } catch (ClassCastException expected) {}
661    }
662
663    public void test_CheckedMap_replace$K$V() {
664        Map<Integer, Double> map = new HashMap<>();
665        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
666        MapDefaultMethodTester.test_replace$K$V(checkedMap, true /* acceptsNullKey */,
667                true /* acceptsNullValue */);
668
669        // Without generics to check the typeCheck implementation
670        Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
671        checkedMap2.put(1, A_STRING);
672
673        try {
674            checkedMap2.replace(1, 1, NOT_A_STRING);
675        } catch (ClassCastException expected) {}
676    }
677
678    public void test_CheckedMap_computeIfAbsent() {
679        Map<Integer, Double> map = new HashMap<>();
680        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
681        MapDefaultMethodTester.test_computeIfAbsent(checkedMap, true /* acceptsNullKey */,
682                true /* acceptsNullValue */);
683
684        // Without generics to check the typeCheck implementation
685        Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
686        checkedMap2.put(1, A_STRING);
687
688        // When key is present
689        try {
690            checkedMap2.computeIfAbsent(1, k -> NOT_A_STRING);
691        } catch (ClassCastException expected) {}
692
693        // When key is absent
694        checkedMap2.clear();
695        try {
696            checkedMap2.computeIfAbsent(1, k -> NOT_A_STRING);
697        } catch (ClassCastException expected) {}
698    }
699
700    public void test_CheckedMap_computeIfPresent() {
701        Map<Integer, Double> map = new HashMap<>();
702        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
703        MapDefaultMethodTester.test_computeIfPresent(checkedMap, true /* acceptsNullKey */);
704
705        // Without generics to check the typeCheck implementation
706        Map m = new HashMap();
707        Map checkedMap2 = Collections.checkedMap(m, Integer.class, String.class);
708        checkedMap2.put(1, A_STRING);
709
710        try {
711            checkedMap2.computeIfPresent(1, (k, v) -> NOT_A_STRING);
712        } catch (ClassCastException expected) {}
713    }
714
715    public void test_CheckedMap_compute() {
716        Map<Integer, Double> map = new HashMap<>();
717        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
718        MapDefaultMethodTester.test_compute(checkedMap, true /* acceptsNullKey */);
719
720        Map checkedMap2 = Collections.checkedMap(new HashMap(), Integer.class, String.class);
721        checkedMap2.put(1, A_STRING);
722        try {
723            checkedMap2.compute(1, (k, v) -> NOT_A_STRING);
724        } catch (ClassCastException expected) {}
725    }
726
727    public void test_CheckedMap_merge() {
728        Map<Integer, Double> map = new HashMap<>();
729        Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
730        MapDefaultMethodTester.test_merge(checkedMap, true /* acceptsNullKey */);
731
732        // Without generics to check the typeCheck implementation
733        Map checkedMap2 =
734                Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
735        checkedMap2.put(1, A_STRING);
736
737        try {
738            checkedMap2.merge(1, A_STRING, (v1, v2) -> NOT_A_STRING);
739        } catch (ClassCastException expected) {}
740    }
741}
742