1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package jdk.internal.vm.compiler.collections.test; 42 43 import java.util.Arrays; 44 import java.util.Iterator; 45 46 import jdk.internal.vm.compiler.collections.EconomicMap; 47 import jdk.internal.vm.compiler.collections.EconomicSet; 48 import jdk.internal.vm.compiler.collections.Equivalence; 49 import jdk.internal.vm.compiler.collections.UnmodifiableEconomicSet; 50 import org.junit.Assert; 51 import org.junit.Test; 52 53 public class EconomicMapImplTest { 54 55 @Test(expected = UnsupportedOperationException.class) 56 public void testRemoveNull() { 57 EconomicMap<Integer, Integer> map = EconomicMap.create(10); 58 map.removeKey(null); 59 } 60 61 @Test 62 public void testInitFromHashSet() { 63 UnmodifiableEconomicSet<Integer> set = new UnmodifiableEconomicSet<Integer>() { 64 65 @Override 66 public boolean contains(Integer element) { 67 return element == 0; 68 } 69 70 @Override 71 public int size() { 72 return 1; 73 } 74 75 @Override 76 public boolean isEmpty() { 77 return false; 78 } 79 80 @Override 81 public Iterator<Integer> iterator() { 82 return new Iterator<Integer>() { 83 84 private boolean visited = false; 85 86 @Override 87 public boolean hasNext() { 88 return !visited; 89 } 90 91 @Override 92 public Integer next() { 93 if (visited) { 94 return null; 95 } else { 96 visited = true; 97 return 1; 98 } 99 } 100 }; 101 } 102 }; 103 104 EconomicSet<Integer> newSet = EconomicSet.create(Equivalence.DEFAULT, set); 105 Assert.assertEquals(newSet.size(), 1); 106 } 107 108 @Test 109 public void testCopyHash() { 110 EconomicSet<Integer> set = EconomicSet.create(Equivalence.IDENTITY); 111 set.addAll(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); 112 EconomicSet<Integer> newSet = EconomicSet.create(Equivalence.IDENTITY, set); 113 Assert.assertEquals(newSet.size(), 10); 114 newSet.remove(8); 115 newSet.remove(9); 116 Assert.assertEquals(newSet.size(), 8); 117 } 118 119 @Test 120 public void testNewEquivalence() { 121 EconomicSet<Integer> set = EconomicSet.create(new Equivalence() { 122 @Override 123 public boolean equals(Object a, Object b) { 124 return false; 125 } 126 127 @Override 128 public int hashCode(Object o) { 129 return 0; 130 } 131 }); 132 set.addAll(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); 133 Assert.assertTrue(set.add(newInteger(0))); 134 } 135 136 @SuppressWarnings({"deprecation", "unused"}) 137 private static Integer newInteger(int value) { 138 return new Integer(value); 139 } 140 141 @Test(expected = UnsupportedOperationException.class) 142 public void testMapPutNull() { 143 EconomicMap<Integer, Integer> map = EconomicMap.create(); 144 map.put(null, null); 145 } 146 147 }