src/jdk.internal.vm.compiler/share/classes/org.graalvm.collections.test/src/org/graalvm/collections/test/EconomicSetTest.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.ArrayList;
       
    28 import java.util.Arrays;
       
    29 import java.util.Iterator;
       
    30 
       
    31 import org.graalvm.collections.EconomicSet;
       
    32 import org.graalvm.collections.Equivalence;
       
    33 import org.junit.Assert;
       
    34 import org.junit.Test;
       
    35 
       
    36 public class EconomicSetTest {
       
    37 
       
    38     @Test
       
    39     public void testUtilities() {
       
    40         EconomicSet<Integer> set = EconomicSet.create(0);
       
    41         set.add(0);
       
    42         Assert.assertTrue(set.add(1));
       
    43         Assert.assertEquals(set.size(), 2);
       
    44         Assert.assertFalse(set.add(1));
       
    45         Assert.assertEquals(set.size(), 2);
       
    46         set.remove(1);
       
    47         Assert.assertEquals(set.size(), 1);
       
    48         set.remove(2);
       
    49         Assert.assertEquals(set.size(), 1);
       
    50         Assert.assertTrue(set.add(1));
       
    51         set.clear();
       
    52         Assert.assertEquals(set.size(), 0);
       
    53     }
       
    54 
       
    55     @Test
       
    56     public void testAddAll() {
       
    57         EconomicSet<Integer> set = EconomicSet.create();
       
    58         set.addAll(Arrays.asList(0, 1, 0));
       
    59         Assert.assertEquals(set.size(), 2);
       
    60 
       
    61         EconomicSet<Integer> newSet = EconomicSet.create();
       
    62         newSet.addAll(Arrays.asList(1, 2));
       
    63         Assert.assertEquals(newSet.size(), 2);
       
    64         newSet.addAll(set);
       
    65         Assert.assertEquals(newSet.size(), 3);
       
    66     }
       
    67 
       
    68     @Test
       
    69     public void testRemoveAll() {
       
    70         EconomicSet<Integer> set = EconomicSet.create();
       
    71         set.addAll(Arrays.asList(0, 1));
       
    72 
       
    73         set.removeAll(Arrays.asList(1, 2));
       
    74         Assert.assertEquals(set.size(), 1);
       
    75 
       
    76         set.removeAll(EconomicSet.create(set));
       
    77         Assert.assertEquals(set.size(), 0);
       
    78     }
       
    79 
       
    80     @Test
       
    81     public void testRetainAll() {
       
    82         EconomicSet<Integer> set = EconomicSet.create();
       
    83         set.addAll(Arrays.asList(0, 1, 2));
       
    84 
       
    85         EconomicSet<Integer> newSet = EconomicSet.create();
       
    86         newSet.addAll(Arrays.asList(2, 3));
       
    87 
       
    88         set.retainAll(newSet);
       
    89         Assert.assertEquals(set.size(), 1);
       
    90     }
       
    91 
       
    92     @Test
       
    93     public void testToArray() {
       
    94         EconomicSet<Integer> set = EconomicSet.create();
       
    95         set.addAll(Arrays.asList(0, 1));
       
    96         Assert.assertArrayEquals(set.toArray(new Integer[2]), new Integer[]{0, 1});
       
    97     }
       
    98 
       
    99     @Test
       
   100     public void testToString() {
       
   101         EconomicSet<Integer> set = EconomicSet.create();
       
   102         set.addAll(Arrays.asList(0, 1));
       
   103         Assert.assertEquals(set.toString(), "set(size=2, {0,1})");
       
   104     }
       
   105 
       
   106     @Test(expected = UnsupportedOperationException.class)
       
   107     public void testToUnalignedArray() {
       
   108         Assert.assertArrayEquals(EconomicSet.create().toArray(new Integer[2]), new Integer[0]);
       
   109     }
       
   110 
       
   111     @Test
       
   112     public void testSetRemoval() {
       
   113         ArrayList<Integer> initialList = new ArrayList<>();
       
   114         ArrayList<Integer> removalList = new ArrayList<>();
       
   115         ArrayList<Integer> finalList = new ArrayList<>();
       
   116         EconomicSet<Integer> set = EconomicSet.create(Equivalence.IDENTITY);
       
   117         set.add(1);
       
   118         set.add(2);
       
   119         set.add(3);
       
   120         set.add(4);
       
   121         set.add(5);
       
   122         set.add(6);
       
   123         set.add(7);
       
   124         set.add(8);
       
   125         set.add(9);
       
   126         Iterator<Integer> i1 = set.iterator();
       
   127         while (i1.hasNext()) {
       
   128             initialList.add(i1.next());
       
   129         }
       
   130         int size = 0;
       
   131         Iterator<Integer> i2 = set.iterator();
       
   132         while (i2.hasNext()) {
       
   133             Integer elem = i2.next();
       
   134             if (size++ < 8) {
       
   135                 i2.remove();
       
   136             }
       
   137             removalList.add(elem);
       
   138         }
       
   139         Iterator<Integer> i3 = set.iterator();
       
   140         while (i3.hasNext()) {
       
   141             finalList.add(i3.next());
       
   142         }
       
   143         Assert.assertEquals(initialList, removalList);
       
   144         Assert.assertEquals(1, finalList.size());
       
   145         Assert.assertEquals(new Integer(9), finalList.get(0));
       
   146     }
       
   147 
       
   148 }