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 * The Universal Permissive License (UPL), Version 1.0 6 * 7 * Subject to the condition set forth below, permission is hereby granted to any 8 * person obtaining a copy of this software, associated documentation and/or 9 * data (collectively the "Software"), free of charge and under any and all 10 * copyright rights in the Software, and any and all patent rights owned or 11 * freely licensable by each licensor hereunder covering either (i) the 12 * unmodified Software as contributed to or provided by such licensor, or (ii) 13 * the Larger Works (as defined below), to deal in both 14 * 15 * (a) the Software, and 16 * 17 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if 18 * one is included with the Software each a "Larger Work" to which the Software 19 * is contributed by such licensors), 20 * 21 * without restriction, including without limitation the rights to copy, create 22 * derivative works of, display, perform, and distribute the Software and make, 23 * use, sell, offer for sale, import, export, have made, and have sold the 24 * Software and the Larger Work(s), and to sublicense the foregoing rights on 25 * either these or other terms. 26 * 27 * This license is subject to the following condition: 28 * 29 * The above copyright notice and either this complete permission notice or at a 30 * minimum a reference to the UPL must be included in all copies or substantial 31 * portions of the Software. 32 * 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 39 * SOFTWARE. 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 }