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.luni.tests.java.util; 19 20import java.util.Arrays; 21import java.util.HashSet; 22import java.util.Iterator; 23import java.util.Set; 24import java.io.ObjectOutputStream; 25import java.io.ByteArrayOutputStream; 26import java.io.IOException; 27import java.io.Serializable; 28import java.lang.reflect.Method; 29import java.lang.reflect.InvocationTargetException; 30 31import org.apache.harmony.testframework.serialization.SerializationTest; 32 33public class HashSetTest extends junit.framework.TestCase { 34 35 HashSet hs; 36 37 static Object[] objArray; 38 { 39 objArray = new Object[1000]; 40 for (int i = 0; i < objArray.length; i++) 41 objArray[i] = new Integer(i); 42 } 43 44 /** 45 * @tests java.util.HashSet#HashSet() 46 */ 47 public void test_Constructor() { 48 // Test for method java.util.HashSet() 49 HashSet hs2 = new HashSet(); 50 assertEquals("Created incorrect HashSet", 0, hs2.size()); 51 } 52 53 /** 54 * @tests java.util.HashSet#HashSet(int) 55 */ 56 public void test_ConstructorI() { 57 // Test for method java.util.HashSet(int) 58 HashSet hs2 = new HashSet(5); 59 assertEquals("Created incorrect HashSet", 0, hs2.size()); 60 try { 61 new HashSet(-1); 62 } catch (IllegalArgumentException e) { 63 return; 64 } 65 fail( 66 "Failed to throw IllegalArgumentException for capacity < 0"); 67 } 68 69 /** 70 * @tests java.util.HashSet#HashSet(int, float) 71 */ 72 public void test_ConstructorIF() { 73 // Test for method java.util.HashSet(int, float) 74 HashSet hs2 = new HashSet(5, (float) 0.5); 75 assertEquals("Created incorrect HashSet", 0, hs2.size()); 76 try { 77 new HashSet(0, 0); 78 } catch (IllegalArgumentException e) { 79 return; 80 } 81 fail( 82 "Failed to throw IllegalArgumentException for initial load factor <= 0"); 83 } 84 85 /** 86 * @tests java.util.HashSet#HashSet(java.util.Collection) 87 */ 88 public void test_ConstructorLjava_util_Collection() { 89 // Test for method java.util.HashSet(java.util.Collection) 90 HashSet hs2 = new HashSet(Arrays.asList(objArray)); 91 for (int counter = 0; counter < objArray.length; counter++) 92 assertTrue("HashSet does not contain correct elements", hs 93 .contains(objArray[counter])); 94 assertTrue("HashSet created from collection incorrect size", 95 hs2.size() == objArray.length); 96 } 97 98 /** 99 * @tests java.util.HashSet#add(java.lang.Object) 100 */ 101 public void test_addLjava_lang_Object() { 102 // Test for method boolean java.util.HashSet.add(java.lang.Object) 103 int size = hs.size(); 104 hs.add(new Integer(8)); 105 assertTrue("Added element already contained by set", hs.size() == size); 106 hs.add(new Integer(-9)); 107 assertTrue("Failed to increment set size after add", 108 hs.size() == size + 1); 109 assertTrue("Failed to add element to set", hs.contains(new Integer(-9))); 110 } 111 112 /** 113 * @tests java.util.HashSet#clear() 114 */ 115 public void test_clear() { 116 // Test for method void java.util.HashSet.clear() 117 Set orgSet = (Set) hs.clone(); 118 hs.clear(); 119 Iterator i = orgSet.iterator(); 120 assertEquals("Returned non-zero size after clear", 0, hs.size()); 121 while (i.hasNext()) 122 assertTrue("Failed to clear set", !hs.contains(i.next())); 123 } 124 125 /** 126 * @tests java.util.HashSet#clone() 127 */ 128 public void test_clone() { 129 // Test for method java.lang.Object java.util.HashSet.clone() 130 HashSet hs2 = (HashSet) hs.clone(); 131 assertTrue("clone returned an equivalent HashSet", hs != hs2); 132 assertTrue("clone did not return an equal HashSet", hs.equals(hs2)); 133 } 134 135 /** 136 * @tests java.util.HashSet#contains(java.lang.Object) 137 */ 138 public void test_containsLjava_lang_Object() { 139 // Test for method boolean java.util.HashSet.contains(java.lang.Object) 140 assertTrue("Returned false for valid object", hs.contains(objArray[90])); 141 assertTrue("Returned true for invalid Object", !hs 142 .contains(new Object())); 143 144 HashSet s = new HashSet(); 145 s.add(null); 146 assertTrue("Cannot handle null", s.contains(null)); 147 } 148 149 /** 150 * @tests java.util.HashSet#isEmpty() 151 */ 152 public void test_isEmpty() { 153 // Test for method boolean java.util.HashSet.isEmpty() 154 assertTrue("Empty set returned false", new HashSet().isEmpty()); 155 assertTrue("Non-empty set returned true", !hs.isEmpty()); 156 } 157 158 /** 159 * @tests java.util.HashSet#iterator() 160 */ 161 public void test_iterator() { 162 // Test for method java.util.Iterator java.util.HashSet.iterator() 163 Iterator i = hs.iterator(); 164 int x = 0; 165 while (i.hasNext()) { 166 assertTrue("Failed to iterate over all elements", hs.contains(i 167 .next())); 168 ++x; 169 } 170 assertTrue("Returned iteration of incorrect size", hs.size() == x); 171 172 HashSet s = new HashSet(); 173 s.add(null); 174 assertNull("Cannot handle null", s.iterator().next()); 175 } 176 177 /** 178 * @tests java.util.HashSet#remove(java.lang.Object) 179 */ 180 public void test_removeLjava_lang_Object() { 181 // Test for method boolean java.util.HashSet.remove(java.lang.Object) 182 int size = hs.size(); 183 hs.remove(new Integer(98)); 184 assertTrue("Failed to remove element", !hs.contains(new Integer(98))); 185 assertTrue("Failed to decrement set size", hs.size() == size - 1); 186 187 HashSet s = new HashSet(); 188 s.add(null); 189 assertTrue("Cannot handle null", s.remove(null)); 190 } 191 192 /** 193 * @tests java.util.HashSet#size() 194 */ 195 public void test_size() { 196 // Test for method int java.util.HashSet.size() 197 assertTrue("Returned incorrect size", hs.size() == (objArray.length + 1)); 198 hs.clear(); 199 assertEquals("Cleared set returned non-zero size", 0, hs.size()); 200 } 201 202 /** 203 * @tests java.util.AbstractCollection#toString() 204 */ 205 public void test_toString() { 206 HashSet s = new HashSet(); 207 s.add(s); 208 String result = s.toString(); 209 assertTrue("should contain self ref", result.indexOf("(this") > -1); 210 } 211 212 /** 213 * @tests java.util.HashSet#SerializationTest 214 */ 215 public void test_Serialization() throws Exception{ 216 HashSet<String> hs = new HashSet<String>(); 217 hs.add("hello"); 218 hs.add("world"); 219 SerializationTest.verifySelf(hs, comparator); 220 SerializationTest.verifyGolden(this, hs, comparator); 221 } 222 223 /** 224 * Sets up the fixture, for example, open a network connection. This method 225 * is called before a test is executed. 226 */ 227 protected void setUp() { 228 hs = new HashSet(); 229 for (int i = 0; i < objArray.length; i++) 230 hs.add(objArray[i]); 231 hs.add(null); 232 } 233 234 /** 235 * Tears down the fixture, for example, close a network connection. This 236 * method is called after a test is executed. 237 */ 238 protected void tearDown() { 239 } 240 241 private static final SerializationTest.SerializableAssert comparator = new 242 SerializationTest.SerializableAssert() { 243 public void assertDeserialized(Serializable initial, Serializable deserialized) { 244 HashSet<String> initialHs = (HashSet<String>) initial; 245 HashSet<String> deseriaHs = (HashSet<String>) deserialized; 246 assertEquals("should be equal", initialHs.size(), deseriaHs.size()); 247 assertEquals("should be equal", initialHs, deseriaHs); 248 } 249 250 }; 251} 252