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.ArrayList;
  44 import java.util.Arrays;
  45 import java.util.Iterator;
  46 
  47 import jdk.internal.vm.compiler.collections.EconomicSet;
  48 import jdk.internal.vm.compiler.collections.Equivalence;
  49 import org.junit.Assert;
  50 import org.junit.Test;
  51 
  52 public class EconomicSetTest {
  53 
  54     @Test
  55     public void testUtilities() {
  56         EconomicSet<Integer> set = EconomicSet.create(0);
  57         set.add(0);
  58         Assert.assertTrue(set.add(1));
  59         Assert.assertEquals(set.size(), 2);
  60         Assert.assertFalse(set.add(1));
  61         Assert.assertEquals(set.size(), 2);
  62         set.remove(1);
  63         Assert.assertEquals(set.size(), 1);
  64         set.remove(2);
  65         Assert.assertEquals(set.size(), 1);
  66         Assert.assertTrue(set.add(1));
  67         set.clear();
  68         Assert.assertEquals(set.size(), 0);
  69     }
  70 
  71     @Test
  72     public void testAddAll() {
  73         EconomicSet<Integer> set = EconomicSet.create();
  74         set.addAll(Arrays.asList(0, 1, 0));
  75         Assert.assertEquals(set.size(), 2);
  76 
  77         EconomicSet<Integer> newSet = EconomicSet.create();
  78         newSet.addAll(Arrays.asList(1, 2));
  79         Assert.assertEquals(newSet.size(), 2);
  80         newSet.addAll(set);
  81         Assert.assertEquals(newSet.size(), 3);
  82     }
  83 
  84     @Test
  85     public void testRemoveAll() {
  86         EconomicSet<Integer> set = EconomicSet.create();
  87         set.addAll(Arrays.asList(0, 1));
  88 
  89         set.removeAll(Arrays.asList(1, 2));
  90         Assert.assertEquals(set.size(), 1);
  91 
  92         set.removeAll(EconomicSet.create(set));
  93         Assert.assertEquals(set.size(), 0);
  94     }
  95 
  96     @Test
  97     public void testRetainAll() {
  98         EconomicSet<Integer> set = EconomicSet.create();
  99         set.addAll(Arrays.asList(0, 1, 2));
 100 
 101         EconomicSet<Integer> newSet = EconomicSet.create();
 102         newSet.addAll(Arrays.asList(2, 3));
 103 
 104         set.retainAll(newSet);
 105         Assert.assertEquals(set.size(), 1);
 106     }
 107 
 108     @Test
 109     public void testToArray() {
 110         EconomicSet<Integer> set = EconomicSet.create();
 111         set.addAll(Arrays.asList(0, 1));
 112         Assert.assertArrayEquals(set.toArray(new Integer[2]), new Integer[]{0, 1});
 113     }
 114 
 115     @Test
 116     public void testToString() {
 117         EconomicSet<Integer> set = EconomicSet.create();
 118         set.addAll(Arrays.asList(0, 1));
 119         Assert.assertEquals(set.toString(), "set(size=2, {0,1})");
 120     }
 121 
 122     @Test(expected = UnsupportedOperationException.class)
 123     public void testToUnalignedArray() {
 124         Assert.assertArrayEquals(EconomicSet.create().toArray(new Integer[2]), new Integer[0]);
 125     }
 126 
 127     @Test
 128     public void testSetRemoval() {
 129         ArrayList<Integer> initialList = new ArrayList<>();
 130         ArrayList<Integer> removalList = new ArrayList<>();
 131         ArrayList<Integer> finalList = new ArrayList<>();
 132         EconomicSet<Integer> set = EconomicSet.create(Equivalence.IDENTITY);
 133         set.add(1);
 134         set.add(2);
 135         set.add(3);
 136         set.add(4);
 137         set.add(5);
 138         set.add(6);
 139         set.add(7);
 140         set.add(8);
 141         set.add(9);
 142         Iterator<Integer> i1 = set.iterator();
 143         while (i1.hasNext()) {
 144             initialList.add(i1.next());
 145         }
 146         int size = 0;
 147         Iterator<Integer> i2 = set.iterator();
 148         while (i2.hasNext()) {
 149             Integer elem = i2.next();
 150             if (size++ < 8) {
 151                 i2.remove();
 152             }
 153             removalList.add(elem);
 154         }
 155         Iterator<Integer> i3 = set.iterator();
 156         while (i3.hasNext()) {
 157             finalList.add(i3.next());
 158         }
 159         Assert.assertEquals(initialList, removalList);
 160         Assert.assertEquals(1, finalList.size());
 161         Assert.assertEquals(newInteger(9), finalList.get(0));
 162     }
 163 
 164     @SuppressWarnings({"deprecation", "unused"})
 165     private static Integer newInteger(int value) {
 166         return new Integer(value);
 167     }
 168 
 169 }