src/jdk.internal.vm.compiler/share/classes/org.graalvm.collections.test/src/org/graalvm/collections/test/EconomicMapImplTest.java
changeset 48861 47f19ff9903c
equal deleted inserted replaced
48860:5bce1b7e7800 48861:47f19ff9903c
       
     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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package org.graalvm.collections.test;
       
    26 
       
    27 import java.util.Arrays;
       
    28 import java.util.Iterator;
       
    29 
       
    30 import org.graalvm.collections.EconomicMap;
       
    31 import org.graalvm.collections.EconomicSet;
       
    32 import org.graalvm.collections.Equivalence;
       
    33 import org.graalvm.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(new Integer(0)));
       
   118     }
       
   119 
       
   120     @Test(expected = UnsupportedOperationException.class)
       
   121     public void testMapPutNull() {
       
   122         EconomicMap<Integer, Integer> map = EconomicMap.create();
       
   123         map.put(null, null);
       
   124     }
       
   125 
       
   126 }