1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.util; 19 20import java.util.ArrayList; 21import java.util.Arrays; 22import java.util.ConcurrentModificationException; 23import java.util.HashMap; 24import java.util.HashSet; 25import java.util.Iterator; 26import java.util.List; 27import java.util.Set; 28import java.io.ObjectOutputStream; 29import java.io.ByteArrayOutputStream; 30import java.io.IOException; 31import java.io.Serializable; 32import java.lang.reflect.Method; 33import java.lang.reflect.InvocationTargetException; 34import java.util.Spliterator; 35 36import libcore.java.util.SpliteratorTester; 37import org.apache.harmony.testframework.serialization.SerializationTest; 38 39public class HashSetTest extends junit.framework.TestCase { 40 41 HashSet hs; 42 43 Object[] objArray; 44 45 /** 46 * java.util.HashSet#HashSet() 47 */ 48 public void test_Constructor() { 49 // Test for method java.util.HashSet() 50 HashSet hs2 = new HashSet(); 51 assertEquals("Created incorrect HashSet", 0, hs2.size()); 52 } 53 54 /** 55 * java.util.HashSet#HashSet(int) 56 */ 57 public void test_ConstructorI() { 58 // Test for method java.util.HashSet(int) 59 HashSet hs2 = new HashSet(5); 60 assertEquals("Created incorrect HashSet", 0, hs2.size()); 61 try { 62 new HashSet(-1); 63 } catch (IllegalArgumentException e) { 64 return; 65 } 66 fail( 67 "Failed to throw IllegalArgumentException for capacity < 0"); 68 } 69 70 /** 71 * java.util.HashSet#HashSet(int, float) 72 */ 73 public void test_ConstructorIF() { 74 // Test for method java.util.HashSet(int, float) 75 HashSet hs2 = new HashSet(5, (float) 0.5); 76 assertEquals("Created incorrect HashSet", 0, hs2.size()); 77 try { 78 new HashSet(0, 0); 79 } catch (IllegalArgumentException e) { 80 return; 81 } 82 fail( 83 "Failed to throw IllegalArgumentException for initial load factor <= 0"); 84 } 85 86 /** 87 * java.util.HashSet#HashSet(java.util.Collection) 88 */ 89 public void test_ConstructorLjava_util_Collection() { 90 // Test for method java.util.HashSet(java.util.Collection) 91 HashSet hs2 = new HashSet(Arrays.asList(objArray)); 92 for (int counter = 0; counter < objArray.length; counter++) 93 assertTrue("HashSet does not contain correct elements", hs 94 .contains(objArray[counter])); 95 assertTrue("HashSet created from collection incorrect size", 96 hs2.size() == objArray.length); 97 98 try { 99 new HashSet(null); 100 fail("NullPointerException expected"); 101 } catch (NullPointerException e) { 102 //expected 103 } 104 } 105 106 /** 107 * java.util.HashSet#add(java.lang.Object) 108 */ 109 public void test_addLjava_lang_Object() { 110 // Test for method boolean java.util.HashSet.add(java.lang.Object) 111 int size = hs.size(); 112 hs.add(new Integer(8)); 113 assertTrue("Added element already contained by set", hs.size() == size); 114 hs.add(new Integer(-9)); 115 assertTrue("Failed to increment set size after add", 116 hs.size() == size + 1); 117 assertTrue("Failed to add element to set", hs.contains(new Integer(-9))); 118 } 119 120 /** 121 * java.util.HashSet#clear() 122 */ 123 public void test_clear() { 124 // Test for method void java.util.HashSet.clear() 125 Set orgSet = (Set) hs.clone(); 126 hs.clear(); 127 Iterator i = orgSet.iterator(); 128 assertEquals("Returned non-zero size after clear", 0, hs.size()); 129 while (i.hasNext()) 130 assertTrue("Failed to clear set", !hs.contains(i.next())); 131 } 132 133 /** 134 * java.util.HashSet#clone() 135 */ 136 public void test_clone() { 137 // Test for method java.lang.Object java.util.HashSet.clone() 138 HashSet hs2 = (HashSet) hs.clone(); 139 assertTrue("clone returned an equivalent HashSet", hs != hs2); 140 assertTrue("clone did not return an equal HashSet", hs.equals(hs2)); 141 } 142 143 /** 144 * java.util.HashSet#contains(java.lang.Object) 145 */ 146 public void test_containsLjava_lang_Object() { 147 // Test for method boolean java.util.HashSet.contains(java.lang.Object) 148 assertTrue("Returned false for valid object", hs.contains(objArray[90])); 149 assertTrue("Returned true for invalid Object", !hs 150 .contains(new Object())); 151 152 HashSet s = new HashSet(); 153 s.add(null); 154 assertTrue("Cannot handle null", s.contains(null)); 155 } 156 157 /** 158 * java.util.HashSet#isEmpty() 159 */ 160 public void test_isEmpty() { 161 // Test for method boolean java.util.HashSet.isEmpty() 162 assertTrue("Empty set returned false", new HashSet().isEmpty()); 163 assertTrue("Non-empty set returned true", !hs.isEmpty()); 164 } 165 166 /** 167 * java.util.HashSet#iterator() 168 */ 169 public void test_iterator() { 170 // Test for method java.util.Iterator java.util.HashSet.iterator() 171 Iterator i = hs.iterator(); 172 int x = 0; 173 while (i.hasNext()) { 174 assertTrue("Failed to iterate over all elements", hs.contains(i 175 .next())); 176 ++x; 177 } 178 assertTrue("Returned iteration of incorrect size", hs.size() == x); 179 180 HashSet s = new HashSet(); 181 s.add(null); 182 assertNull("Cannot handle null", s.iterator().next()); 183 } 184 185 /** 186 * java.util.HashSet#remove(java.lang.Object) 187 */ 188 public void test_removeLjava_lang_Object() { 189 // Test for method boolean java.util.HashSet.remove(java.lang.Object) 190 int size = hs.size(); 191 hs.remove(new Integer(98)); 192 assertTrue("Failed to remove element", !hs.contains(new Integer(98))); 193 assertTrue("Failed to decrement set size", hs.size() == size - 1); 194 195 HashSet s = new HashSet(); 196 s.add(null); 197 assertTrue("Cannot handle null", s.remove(null)); 198 assertFalse(hs.remove(new Integer(-98))); 199 } 200 201 /** 202 * java.util.HashSet#size() 203 */ 204 public void test_size() { 205 // Test for method int java.util.HashSet.size() 206 assertTrue("Returned incorrect size", hs.size() == (objArray.length + 1)); 207 hs.clear(); 208 assertEquals("Cleared set returned non-zero size", 0, hs.size()); 209 } 210 211 /** 212 * java.util.HashSet#SerializationTest 213 */ 214 public void test_Serialization() throws Exception{ 215 HashSet<String> hs = new HashSet<String>(); 216 hs.add("hello"); 217 hs.add("world"); 218 SerializationTest.verifySelf(hs, comparator); 219 SerializationTest.verifyGolden(this, hs, comparator); 220 } 221 222 /* 223 * Bug 26294011 224 */ 225 public void test_empty_clone() throws Exception { 226 HashSet<Integer> emptyHs = new HashSet<Integer>(); 227 HashSet<Integer> cloned = (HashSet) emptyHs.clone(); 228 cloned.add(new Integer(8)); 229 } 230 231 public void test_forEach() throws Exception { 232 HashSet<Integer> hs = new HashSet<>(); 233 hs.add(0); 234 hs.add(1); 235 hs.add(2); 236 237 HashSet<Integer> output = new HashSet<>(); 238 hs.forEach(k -> output.add(k)); 239 240 assertEquals(hs, output); 241 } 242 243 public void test_forEach_NPE() throws Exception { 244 HashSet<String> set = new HashSet<>(); 245 try { 246 set.forEach(null); 247 fail(); 248 } catch(NullPointerException expected) {} 249 } 250 251 public void test_forEach_CME() throws Exception { 252 HashSet<String> set = new HashSet<>(); 253 set.add("one"); 254 set.add("two"); 255 try { 256 set.forEach(new java.util.function.Consumer<String>() { 257 @Override 258 public void accept(String k) {set.add("foo");} 259 }); 260 fail(); 261 } catch(ConcurrentModificationException expected) {} 262 } 263 264 public void test_spliterator() throws Exception { 265 HashSet<String> hashSet = new HashSet<>(); 266 List<String> keys = Arrays.asList( 267 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"); 268 hashSet.addAll(keys); 269 270 ArrayList<String> expectedKeys = new ArrayList<>(keys); 271 SpliteratorTester.runBasicIterationTests_unordered(hashSet.spliterator(), expectedKeys, 272 String::compareTo); 273 SpliteratorTester.runBasicSplitTests(hashSet, expectedKeys); 274 SpliteratorTester.testSpliteratorNPE(hashSet.spliterator()); 275 276 assertTrue(hashSet.spliterator().hasCharacteristics(Spliterator.DISTINCT)); 277 SpliteratorTester.runDistinctTests(keys); 278 SpliteratorTester.assertSupportsTrySplit(hashSet); 279 } 280 281 /** 282 * Sets up the fixture, for example, open a network connection. This method 283 * is called before a test is executed. 284 */ 285 protected void setUp() { 286 objArray = new Object[1000]; 287 for (int i = 0; i < objArray.length; i++) { 288 objArray[i] = new Integer(i); 289 } 290 291 hs = new HashSet(); 292 for (int i = 0; i < objArray.length; i++) { 293 hs.add(objArray[i]); 294 } 295 296 hs.add(null); 297 } 298 299 /** 300 * Tears down the fixture, for example, close a network connection. This 301 * method is called after a test is executed. 302 */ 303 protected void tearDown() { 304 hs = null; 305 objArray = null; 306 } 307 308 private static final SerializationTest.SerializableAssert comparator = new 309 SerializationTest.SerializableAssert() { 310 public void assertDeserialized(Serializable initial, Serializable deserialized) { 311 HashSet<String> initialHs = (HashSet<String>) initial; 312 HashSet<String> deseriaHs = (HashSet<String>) deserialized; 313 assertEquals("should be equal", initialHs.size(), deseriaHs.size()); 314 assertEquals("should be equal", initialHs, deseriaHs); 315 } 316 317 }; 318} 319