1 /*
   2  * Copyright (c) 2017, 2017, 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 package jdk.internal.vm.compiler.collections.test;
  26 
  27 import java.util.Arrays;
  28 import java.util.Iterator;
  29 
  30 import jdk.internal.vm.compiler.collections.EconomicMap;
  31 import jdk.internal.vm.compiler.collections.EconomicSet;
  32 import jdk.internal.vm.compiler.collections.Equivalence;
  33 import jdk.internal.vm.compiler.collections.UnmodifiableEconomicSet;
  34 import org.junit.Assert;
  35 import org.junit.Test;
  36 
  37 public class EconomicMapImplTest {
  38 
  39     @Test(expected = UnsupportedOperationException.class)
  40     public void testRemoveNull() {
  41         EconomicMap<Integer, Integer> map = EconomicMap.create(10);
  42         map.removeKey(null);
  43     }
  44 
  45     @Test
  46     public void testInitFromHashSet() {
  47         UnmodifiableEconomicSet<Integer> set = new UnmodifiableEconomicSet<Integer>() {
  48 
  49             @Override
  50             public boolean contains(Integer element) {
  51                 return element == 0;
  52             }
  53 
  54             @Override
  55             public int size() {
  56                 return 1;
  57             }
  58 
  59             @Override
  60             public boolean isEmpty() {
  61                 return false;
  62             }
  63 
  64             @Override
  65             public Iterator<Integer> iterator() {
  66                 return new Iterator<Integer>() {
  67 
  68                     private boolean visited = false;
  69 
  70                     @Override
  71                     public boolean hasNext() {
  72                         return !visited;
  73                     }
  74 
  75                     @Override
  76                     public Integer next() {
  77                         if (visited) {
  78                             return null;
  79                         } else {
  80                             visited = true;
  81                             return 1;
  82                         }
  83                     }
  84                 };
  85             }
  86         };
  87 
  88         EconomicSet<Integer> newSet = EconomicSet.create(Equivalence.DEFAULT, set);
  89         Assert.assertEquals(newSet.size(), 1);
  90     }
  91 
  92     @Test
  93     public void testCopyHash() {
  94         EconomicSet<Integer> set = EconomicSet.create(Equivalence.IDENTITY);
  95         set.addAll(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
  96         EconomicSet<Integer> newSet = EconomicSet.create(Equivalence.IDENTITY, set);
  97         Assert.assertEquals(newSet.size(), 10);
  98         newSet.remove(8);
  99         newSet.remove(9);
 100         Assert.assertEquals(newSet.size(), 8);
 101     }
 102 
 103     @Test
 104     public void testNewEquivalence() {
 105         EconomicSet<Integer> set = EconomicSet.create(new Equivalence() {
 106             @Override
 107             public boolean equals(Object a, Object b) {
 108                 return false;
 109             }
 110 
 111             @Override
 112             public int hashCode(Object o) {
 113                 return 0;
 114             }
 115         });
 116         set.addAll(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
 117         Assert.assertTrue(set.add(newInteger(0)));
 118     }
 119 
 120     @SuppressWarnings("deprecation")
 121     private static Integer newInteger(int value) {
 122         return new Integer(value);
 123     }
 124 
 125     @Test(expected = UnsupportedOperationException.class)
 126     public void testMapPutNull() {
 127         EconomicMap<Integer, Integer> map = EconomicMap.create();
 128         map.put(null, null);
 129     }
 130 
 131 }