LinkedHashSetTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package tests.api.java.util; 18 19import dalvik.annotation.TestTarget; 20import dalvik.annotation.TestInfo; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetClass; 23 24import java.util.Arrays; 25import java.util.Iterator; 26import java.util.LinkedHashSet; 27import java.util.Set; 28 29/** 30 * @tests java.util.LinkedHashSet 31 */ 32 33@TestTargetClass(java.util.LinkedHashSet.class) 34public class LinkedHashSetTest extends junit.framework.TestCase { 35 36 LinkedHashSet hs; 37 38 static Object[] objArray; 39 { 40 objArray = new Object[1000]; 41 for (int i = 0; i < objArray.length; i++) 42 objArray[i] = new Integer(i); 43 } 44 45 /** 46 * @tests java.util.LinkedHashSet#LinkedHashSet() 47 */ 48 @TestInfo( 49 level = TestLevel.COMPLETE, 50 purpose = "", 51 targets = { 52 @TestTarget( 53 methodName = "LinkedHashSet", 54 methodArgs = {} 55 ) 56 }) 57 public void test_Constructor() { 58 // Test for method java.util.LinkedHashSet() 59 LinkedHashSet hs2 = new LinkedHashSet(); 60 assertEquals("Created incorrect LinkedHashSet", 0, hs2.size()); 61 } 62 63 /** 64 * @tests java.util.LinkedHashSet#LinkedHashSet(int) 65 */ 66 @TestInfo( 67 level = TestLevel.PARTIAL, 68 purpose = "Doesn't verify IllegalArgumentException.", 69 targets = { 70 @TestTarget( 71 methodName = "LinkedHashSet", 72 methodArgs = {int.class} 73 ) 74 }) 75 public void test_ConstructorI() { 76 // Test for method java.util.LinkedHashSet(int) 77 LinkedHashSet hs2 = new LinkedHashSet(5); 78 assertEquals("Created incorrect LinkedHashSet", 0, hs2.size()); 79 try { 80 new LinkedHashSet(-1); 81 } catch (IllegalArgumentException e) { 82 return; 83 } 84 fail( 85 "Failed to throw IllegalArgumentException for capacity < 0"); 86 } 87 88 /** 89 * @tests java.util.LinkedHashSet#LinkedHashSet(int, float) 90 */ 91 @TestInfo( 92 level = TestLevel.PARTIAL, 93 purpose = "Doesn't verify IllegalArgumentException.", 94 targets = { 95 @TestTarget( 96 methodName = "LinkedHashSet", 97 methodArgs = {int.class, float.class} 98 ) 99 }) 100 public void test_ConstructorIF() { 101 // Test for method java.util.LinkedHashSet(int, float) 102 LinkedHashSet hs2 = new LinkedHashSet(5, (float) 0.5); 103 assertEquals("Created incorrect LinkedHashSet", 0, hs2.size()); 104 try { 105 new LinkedHashSet(0, 0); 106 } catch (IllegalArgumentException e) { 107 return; 108 } 109 fail( 110 "Failed to throw IllegalArgumentException for initial load factor <= 0"); 111 } 112 113 /** 114 * @tests java.util.LinkedHashSet#LinkedHashSet(java.util.Collection) 115 */ 116 @TestInfo( 117 level = TestLevel.PARTIAL, 118 purpose = "Doesn't verify NullPointerException.", 119 targets = { 120 @TestTarget( 121 methodName = "LinkedHashSet", 122 methodArgs = {java.util.Collection.class} 123 ) 124 }) 125 public void test_ConstructorLjava_util_Collection() { 126 // Test for method java.util.LinkedHashSet(java.util.Collection) 127 LinkedHashSet hs2 = new LinkedHashSet(Arrays.asList(objArray)); 128 for (int counter = 0; counter < objArray.length; counter++) 129 assertTrue("LinkedHashSet does not contain correct elements", hs 130 .contains(objArray[counter])); 131 assertTrue("LinkedHashSet created from collection incorrect size", hs2 132 .size() == objArray.length); 133 } 134 135 /** 136 * @tests java.util.LinkedHashSet#add(java.lang.Object) 137 */ 138 @TestInfo( 139 level = TestLevel.COMPLETE, 140 purpose = "", 141 targets = { 142 @TestTarget( 143 methodName = "add", 144 methodArgs = {Object.class} 145 ) 146 }) 147 public void test_addLjava_lang_Object() { 148 // Test for method boolean java.util.LinkedHashSet.add(java.lang.Object) 149 int size = hs.size(); 150 hs.add(new Integer(8)); 151 assertTrue("Added element already contained by set", hs.size() == size); 152 hs.add(new Integer(-9)); 153 assertTrue("Failed to increment set size after add", 154 hs.size() == size + 1); 155 assertTrue("Failed to add element to set", hs.contains(new Integer(-9))); 156 } 157 158 /** 159 * @tests java.util.LinkedHashSet#clear() 160 */ 161 @TestInfo( 162 level = TestLevel.COMPLETE, 163 purpose = "", 164 targets = { 165 @TestTarget( 166 methodName = "clear", 167 methodArgs = {} 168 ) 169 }) 170 public void test_clear() { 171 // Test for method void java.util.LinkedHashSet.clear() 172 Set orgSet = (Set) hs.clone(); 173 hs.clear(); 174 Iterator i = orgSet.iterator(); 175 assertEquals("Returned non-zero size after clear", 0, hs.size()); 176 while (i.hasNext()) 177 assertTrue("Failed to clear set", !hs.contains(i.next())); 178 } 179 180 /** 181 * @tests java.util.LinkedHashSet#clone() 182 */ 183 @TestInfo( 184 level = TestLevel.COMPLETE, 185 purpose = "", 186 targets = { 187 @TestTarget( 188 methodName = "clone", 189 methodArgs = {} 190 ) 191 }) 192 public void test_clone() { 193 // Test for method java.lang.Object java.util.LinkedHashSet.clone() 194 LinkedHashSet hs2 = (LinkedHashSet) hs.clone(); 195 assertTrue("clone returned an equivalent LinkedHashSet", hs != hs2); 196 assertTrue("clone did not return an equal LinkedHashSet", hs 197 .equals(hs2)); 198 } 199 200 /** 201 * @tests java.util.LinkedHashSet#contains(java.lang.Object) 202 */ 203 @TestInfo( 204 level = TestLevel.PARTIAL, 205 purpose = "Verifies null as a parameter.", 206 targets = { 207 @TestTarget( 208 methodName = "contains", 209 methodArgs = {Object.class} 210 ) 211 }) 212 public void test_containsLjava_lang_Object() { 213 // Test for method boolean 214 // java.util.LinkedHashSet.contains(java.lang.Object) 215 assertTrue("Returned false for valid object", hs.contains(objArray[90])); 216 assertTrue("Returned true for invalid Object", !hs 217 .contains(new Object())); 218 219 LinkedHashSet s = new LinkedHashSet(); 220 s.add(null); 221 assertTrue("Cannot handle null", s.contains(null)); 222 } 223 224 /** 225 * @tests java.util.LinkedHashSet#isEmpty() 226 */ 227 @TestInfo( 228 level = TestLevel.COMPLETE, 229 purpose = "", 230 targets = { 231 @TestTarget( 232 methodName = "isEmpty", 233 methodArgs = {} 234 ) 235 }) 236 public void test_isEmpty() { 237 // Test for method boolean java.util.LinkedHashSet.isEmpty() 238 assertTrue("Empty set returned false", new LinkedHashSet().isEmpty()); 239 assertTrue("Non-empty set returned true", !hs.isEmpty()); 240 } 241 242 /** 243 * @tests java.util.LinkedHashSet#iterator() 244 */ 245 @TestInfo( 246 level = TestLevel.COMPLETE, 247 purpose = "", 248 targets = { 249 @TestTarget( 250 methodName = "iterator", 251 methodArgs = {} 252 ) 253 }) 254 public void test_iterator() { 255 // Test for method java.util.Iterator java.util.LinkedHashSet.iterator() 256 Iterator i = hs.iterator(); 257 int x = 0; 258 int j; 259 for (j = 0; i.hasNext(); j++) { 260 Object oo = i.next(); 261 if (oo != null) { 262 Integer ii = (Integer) oo; 263 assertTrue("Incorrect element found", ii.intValue() == j); 264 } else { 265 assertTrue("Cannot find null", hs.contains(oo)); 266 } 267 ++x; 268 } 269 assertTrue("Returned iteration of incorrect size", hs.size() == x); 270 271 LinkedHashSet s = new LinkedHashSet(); 272 s.add(null); 273 assertNull("Cannot handle null", s.iterator().next()); 274 } 275 276 /** 277 * @tests java.util.LinkedHashSet#remove(java.lang.Object) 278 */ 279 @TestInfo( 280 level = TestLevel.PARTIAL, 281 purpose = "Doesn't verify ClassCastException, NullPointerException, " + 282 "UnsupportedOperationException.", 283 targets = { 284 @TestTarget( 285 methodName = "remove", 286 methodArgs = {Object.class} 287 ) 288 }) 289 public void test_removeLjava_lang_Object() { 290 // Test for method boolean 291 // java.util.LinkedHashSet.remove(java.lang.Object) 292 int size = hs.size(); 293 hs.remove(new Integer(98)); 294 assertTrue("Failed to remove element", !hs.contains(new Integer(98))); 295 assertTrue("Failed to decrement set size", hs.size() == size - 1); 296 297 LinkedHashSet s = new LinkedHashSet(); 298 s.add(null); 299 assertTrue("Cannot handle null", s.remove(null)); 300 } 301 302 /** 303 * @tests java.util.LinkedHashSet#size() 304 */ 305 @TestInfo( 306 level = TestLevel.COMPLETE, 307 purpose = "", 308 targets = { 309 @TestTarget( 310 methodName = "size", 311 methodArgs = {} 312 ) 313 }) 314 public void test_size() { 315 // Test for method int java.util.LinkedHashSet.size() 316 assertTrue("Returned incorrect size", hs.size() == (objArray.length + 1)); 317 hs.clear(); 318 assertEquals("Cleared set returned non-zero size", 0, hs.size()); 319 } 320 321 /** 322 * Sets up the fixture, for example, open a network connection. This method 323 * is called before a test is executed. 324 */ 325 protected void setUp() { 326 hs = new LinkedHashSet(); 327 for (int i = 0; i < objArray.length; i++) 328 hs.add(objArray[i]); 329 hs.add(null); 330 } 331 332 /** 333 * Tears down the fixture, for example, close a network connection. This 334 * method is called after a test is executed. 335 */ 336 protected void tearDown() { 337 } 338} 339