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