1 /* 2 * Copyright (c) 2014, 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 26 package test.com.sun.glass.ui.monocle; 27 28 import com.sun.glass.ui.monocle.IntSetShim; 29 import org.junit.Assert; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.Parameterized; 33 34 import java.util.Arrays; 35 import java.util.Collection; 36 import java.util.HashSet; 37 import java.util.Set; 38 import java.util.stream.Collectors; 39 40 @RunWith(Parameterized.class) 41 public class IntSetTest { 42 43 private Integer[] array; 44 45 @Parameterized.Parameters 46 public static Collection<Object[]> data() { 47 Integer[][] sets = { 48 { 1 }, 49 { 1, 2 }, 50 { 1, 2, 3}, 51 { 1, 1 }, 52 { 1, 1, 1 }, 53 { 1, 1, 2 }, 54 }; 55 return Arrays.asList(sets).stream() 56 .map(d -> new Object[] { d }) 57 .collect(Collectors.toList()); 58 } 59 60 public IntSetTest(Integer[] array) { 61 this.array = array; 62 } 63 64 private int[] getIntSetAsArray(IntSetShim s) { 65 int[] a = new int[s.size()]; 66 for (int i = 0; i < s.size(); i++) { 67 a[i] = s.get(i); 68 } 69 Arrays.sort(a); 70 return a; 71 } 72 73 private int[] getHashSetAsArray(Set<Integer> set) { 74 return set.stream().sorted().mapToInt(x -> x).toArray(); 75 } 76 77 private void assertSet(Set<Integer> expected, IntSetShim actual) { 78 Assert.assertArrayEquals( 79 "Expected: " + expected + ", found " + actual, 80 getHashSetAsArray(expected), 81 getIntSetAsArray(actual)); 82 } 83 84 @Test 85 public void testAddInOrderRemoveInOrder() { 86 IntSetShim set = new IntSetShim(); 87 Set<Integer> hashSet = new HashSet<>(); 88 assertSet(hashSet, set); 89 for (int i = 0; i < array.length; i++) { 90 set.addInt(array[i]); 91 hashSet.add(array[i]); 92 assertSet(hashSet, set); 93 } 94 for (int i = 0; i < array.length; i++) { 95 set.removeInt(array[i]); 96 hashSet.remove(array[i]); 97 assertSet(hashSet, set); 98 } 99 } 100 101 @Test 102 public void testAddInOrderRemoveInReverse() { 103 IntSetShim set = new IntSetShim(); 104 Set<Integer> hashSet = new HashSet<>(); 105 assertSet(hashSet, set); 106 for (int i = 0; i < array.length; i++) { 107 set.addInt(array[i]); 108 hashSet.add(array[i]); 109 assertSet(hashSet, set); 110 } 111 for (int i = array.length - 1; i >= 0; i--) { 112 set.removeInt(array[i]); 113 hashSet.remove(array[i]); 114 assertSet(hashSet, set); 115 } 116 } 117 118 @Test 119 public void testAddInReverseRemoveInOrder() { 120 IntSetShim set = new IntSetShim(); 121 Set<Integer> hashSet = new HashSet<>(); 122 assertSet(hashSet, set); 123 for (int i = array.length - 1; i >= 0; i--) { 124 set.addInt(array[i]); 125 hashSet.add(array[i]); 126 assertSet(hashSet, set); 127 } 128 for (int i = 0; i < array.length; i++) { 129 set.removeInt(array[i]); 130 hashSet.remove(array[i]); 131 assertSet(hashSet, set); 132 } 133 } 134 135 @Test 136 public void testAddInReverseRemoveInReverse() { 137 IntSetShim set = new IntSetShim(); 138 Set<Integer> hashSet = new HashSet<>(); 139 assertSet(hashSet, set); 140 for (int i = array.length - 1; i >= 0; i--) { 141 set.addInt(array[i]); 142 hashSet.add(array[i]); 143 assertSet(hashSet, set); 144 } 145 for (int i = array.length - 1; i >= 0; i--) { 146 set.removeInt(array[i]); 147 hashSet.remove(array[i]); 148 assertSet(hashSet, set); 149 } 150 } 151 152 }