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 17 package org.apache.harmony.tests.java.util; 18 19 import java.util.ArrayList; 20 import java.util.Arrays; 21 import java.util.Collection; 22 import java.util.EnumSet; 23 import java.util.Iterator; 24 import java.util.NoSuchElementException; 25 import java.util.Set; 26 27 import junit.framework.TestCase; 28 29 import org.apache.harmony.testframework.serialization.SerializationTest; 30 31 public class EnumSetTest extends TestCase { 32 static final boolean disableRIBugs = true; 33 34 static enum EnumWithInnerClass { 35 a, b, c, d, e, f { 36 }, 37 } 38 39 enum EnumWithAllInnerClass { 40 a {}, 41 b {}, 42 } 43 44 static enum EnumFoo { 45 a, b,c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, 46 } 47 48 static enum EmptyEnum { 49 // expected 50 } 51 52 static enum HugeEnumWithInnerClass { 53 a{}, b{}, c{}, d{}, e{}, f{}, g{}, h{}, i{}, j{}, k{}, l{}, m{}, n{}, o{}, p{}, q{}, r{}, s{}, t{}, u{}, v{}, w{}, x{}, y{}, z{}, A{}, B{}, C{}, D{}, E{}, F{}, G{}, H{}, I{}, J{}, K{}, L{}, M{}, N{}, O{}, P{}, Q{}, R{}, S{}, T{}, U{}, V{}, W{}, X{}, Y{}, Z{}, aa{}, bb{}, cc{}, dd{}, ee{}, ff{}, gg{}, hh{}, ii{}, jj{}, kk{}, ll{}, mm{}, 54 } 55 56 static enum HugeEnum { 57 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, 58 } 59 60 static enum HugeEnumCount { 61 NO1, NO2, NO3, NO4, NO5, NO6, NO7, NO8, NO9, NO10, NO11, NO12, NO13, NO14, NO15, NO16, NO17, NO18, NO19, NO20, 62 NO21, NO22, NO23, NO24, NO25, NO26, NO27, NO28, NO29, NO30, NO31, NO32, NO33, NO34, NO35, NO36, NO37, NO38, NO39, NO40, 63 NO41, NO42, NO43, NO44, NO45, NO46, NO47, NO48, NO49, NO50, NO51, NO52, NO53, NO54, NO55, NO56, NO57, NO58, NO59, NO60, 64 NO61, NO62, NO63, NO64, NO65, NO66, NO67, NO68, NO69, NO70, NO71, NO72, NO73, NO74, NO75, NO76, NO77, NO78, NO79, NO80, 65 NO81, NO82, NO83, NO84, NO85, NO86, NO87, NO88, NO89, NO90, NO91, NO92, NO93, NO94, NO95, NO96, NO97, NO98, NO99, NO100, 66 NO101, NO102, NO103, NO104, NO105, NO106, NO107, NO108, NO109, NO110, NO111, NO112, NO113, NO114, NO115, NO116, NO117, NO118, NO119, NO120, 67 NO121, NO122, NO123, NO124, NO125, NO126, NO127, NO128, NO129, NO130, 68 } 69 70 /** 71 * java.util.EnumSet#noneOf(java.lang.Class) 72 */ 73 @SuppressWarnings("unchecked") test_NoneOf_LClass()74 public void test_NoneOf_LClass() { 75 try { 76 EnumSet.noneOf((Class) null); 77 fail("Should throw NullPointerException"); 78 } catch (NullPointerException e) { 79 // expected 80 } 81 82 try { 83 EnumSet.noneOf(Enum.class); 84 fail("Should throw ClassCastException"); 85 } catch (ClassCastException cce) { 86 // expected 87 } 88 89 Class<EnumWithAllInnerClass> c = (Class<EnumWithAllInnerClass>) EnumWithAllInnerClass.a 90 .getClass(); 91 try { 92 EnumSet.noneOf(c); 93 fail("Should throw ClassCastException"); 94 } catch (ClassCastException e) { 95 // expected 96 } 97 98 EnumSet<EnumWithAllInnerClass> setWithInnerClass = EnumSet 99 .noneOf(EnumWithAllInnerClass.class); 100 assertNotNull(setWithInnerClass); 101 102 // test enum type with more than 64 elements 103 Class<HugeEnumWithInnerClass> hc = (Class<HugeEnumWithInnerClass>) HugeEnumWithInnerClass.a 104 .getClass(); 105 try { 106 EnumSet.noneOf(hc); 107 fail("Should throw ClassCastException"); 108 } catch (ClassCastException e) { 109 // expected 110 } 111 112 EnumSet<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 113 .noneOf(HugeEnumWithInnerClass.class); 114 assertNotNull(hugeSetWithInnerClass); 115 } 116 117 /** 118 * java.util.HugeEnumSet#iterator() 119 */ test_iterator_HugeEnumSet()120 public void test_iterator_HugeEnumSet() { 121 EnumSet<HugeEnumCount> set; 122 Object[] array; 123 124 // Test HugeEnumSet with 65 elements 125 // which is more than the bits of Long 126 set = EnumSet.range(HugeEnumCount.NO1, HugeEnumCount.NO65); 127 array = set.toArray(); 128 for (HugeEnumCount count : set) { 129 assertEquals(count, (HugeEnumCount) array[count.ordinal()]); 130 } 131 132 // Test HugeEnumSet with 130 elements 133 // which is more than twice of the bits of Long 134 set = EnumSet.range(HugeEnumCount.NO1, HugeEnumCount.NO130); 135 array = set.toArray(); 136 for (HugeEnumCount count : set) { 137 assertEquals(count, (HugeEnumCount) array[count.ordinal()]); 138 } 139 } 140 testRemoveIteratorRemoveFromHugeEnumSet()141 public void testRemoveIteratorRemoveFromHugeEnumSet() { 142 EnumSet<HugeEnumCount> set = EnumSet.noneOf(HugeEnumCount.class); 143 set.add(HugeEnumCount.NO64); 144 set.add(HugeEnumCount.NO65); 145 set.add(HugeEnumCount.NO128); 146 Iterator<HugeEnumCount> iterator = set.iterator(); 147 assertTrue(iterator.hasNext()); 148 assertEquals(HugeEnumCount.NO64, iterator.next()); 149 assertTrue(iterator.hasNext()); 150 iterator.remove(); 151 assertEquals(HugeEnumCount.NO65, iterator.next()); 152 assertTrue(iterator.hasNext()); 153 assertEquals(HugeEnumCount.NO128, iterator.next()); 154 assertFalse(iterator.hasNext()); 155 assertEquals(EnumSet.of(HugeEnumCount.NO65, HugeEnumCount.NO128), set); 156 iterator.remove(); 157 assertEquals(EnumSet.of(HugeEnumCount.NO65), set); 158 } 159 160 /** 161 * java.util.EnumSet#allOf(java.lang.Class) 162 */ 163 @SuppressWarnings("unchecked") test_AllOf_LClass()164 public void test_AllOf_LClass() { 165 try { 166 EnumSet.allOf((Class) null); 167 fail("Should throw NullPointerException"); 168 } catch (NullPointerException e) { 169 // expected 170 } 171 172 try { 173 EnumSet.allOf(Enum.class); 174 fail("Should throw ClassCastException"); 175 } catch (ClassCastException cce) { 176 // expected 177 } 178 179 EnumSet<EnumFoo> enumSet = EnumSet.allOf(EnumFoo.class); 180 assertEquals("Size of enumSet should be 64", 64, enumSet.size()); 181 182 assertFalse( 183 "enumSet should not contain null value", enumSet.contains(null)); 184 assertTrue( 185 "enumSet should contain EnumFoo.a", enumSet.contains(EnumFoo.a)); 186 assertTrue( 187 "enumSet should contain EnumFoo.b", enumSet.contains(EnumFoo.b)); 188 189 enumSet.add(EnumFoo.a); 190 assertEquals("Should be equal", 64, enumSet.size()); 191 192 EnumSet<EnumFoo> anotherSet = EnumSet.allOf(EnumFoo.class); 193 assertEquals("Should be equal", enumSet, anotherSet); 194 assertNotSame("Should not be identical", enumSet, anotherSet); 195 196 // test enum with more than 64 elements 197 EnumSet<HugeEnum> hugeEnumSet = EnumSet.allOf(HugeEnum.class); 198 assertEquals(65, hugeEnumSet.size()); 199 200 assertFalse(hugeEnumSet.contains(null)); 201 assertTrue(hugeEnumSet.contains(HugeEnum.a)); 202 assertTrue(hugeEnumSet.contains(HugeEnum.b)); 203 204 hugeEnumSet.add(HugeEnum.a); 205 assertEquals(65, hugeEnumSet.size()); 206 207 EnumSet<HugeEnum> anotherHugeSet = EnumSet.allOf(HugeEnum.class); 208 assertEquals(hugeEnumSet, anotherHugeSet); 209 assertNotSame(hugeEnumSet, anotherHugeSet); 210 } 211 212 /** 213 * java.util.EnumSet#add(E) 214 */ 215 @SuppressWarnings("unchecked") test_add_E()216 public void test_add_E() { 217 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 218 set.add(EnumFoo.a); 219 set.add(EnumFoo.b); 220 221 try { 222 set.add(null); 223 fail("Should throw NullPointerException"); 224 } catch (NullPointerException e) { 225 // expected 226 } 227 228 // test enum type with more than 64 elements 229 Set rawSet = set; 230 try { 231 rawSet.add(HugeEnumWithInnerClass.b); 232 fail("Should throw ClassCastException"); 233 } catch (ClassCastException e) { 234 // expected 235 } 236 237 set.clear(); 238 try { 239 set.add(null); 240 fail("Should throw NullPointerException"); 241 } catch (NullPointerException e) { 242 // expected 243 } 244 245 boolean result = set.add(EnumFoo.a); 246 assertEquals("Size should be 1:", 1, set.size()); 247 assertTrue("Return value should be true", result); 248 249 result = set.add(EnumFoo.a); 250 assertEquals("Size should be 1:", 1, set.size()); 251 assertFalse("Return value should be false", result); 252 253 set.add(EnumFoo.b); 254 assertEquals("Size should be 2:", 2, set.size()); 255 256 rawSet = set; 257 try { 258 rawSet.add(EnumWithAllInnerClass.a); 259 fail("Should throw ClassCastException"); 260 } catch(ClassCastException e) { 261 // expected 262 } 263 264 try { 265 rawSet.add(EnumWithInnerClass.a); 266 fail("Should throw ClassCastException"); 267 } catch(ClassCastException e) { 268 // expected 269 } 270 271 try { 272 rawSet.add(new Object()); 273 fail("Should throw ClassCastException"); 274 } catch(ClassCastException e) { 275 // expected 276 } 277 278 // test enum type with more than 64 elements 279 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 280 result = hugeSet.add(HugeEnum.a); 281 assertTrue(result); 282 283 result = hugeSet.add(HugeEnum.a); 284 assertFalse(result); 285 286 try { 287 hugeSet.add(null); 288 fail("Should throw NullPointerException"); 289 } catch (NullPointerException e) { 290 // expected 291 } 292 293 rawSet = hugeSet; 294 try { 295 rawSet.add(HugeEnumWithInnerClass.b); 296 fail("Should throw ClassCastException"); 297 } catch (ClassCastException e) { 298 // expected 299 } 300 301 try { 302 rawSet.add(new Object()); 303 fail("Should throw ClassCastException"); 304 } catch (ClassCastException e) { 305 // expected 306 } 307 308 result = hugeSet.add(HugeEnum.mm); 309 assertTrue(result); 310 result = hugeSet.add(HugeEnum.mm); 311 assertFalse(result); 312 assertEquals(2, hugeSet.size()); 313 314 } 315 316 /** 317 * java.util.EnumSet#addAll(Collection) 318 */ 319 @SuppressWarnings( { "unchecked", "boxing" }) test_addAll_LCollection()320 public void test_addAll_LCollection() { 321 322 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 323 assertEquals("Size should be 0:", 0, set.size()); 324 325 try { 326 set.addAll(null); 327 fail("Should throw NullPointerException"); 328 } catch (NullPointerException e) { 329 // expected 330 } 331 332 Set emptySet = EnumSet.noneOf(EmptyEnum.class); 333 Enum[] elements = EmptyEnum.class.getEnumConstants(); 334 for(int i = 0; i < elements.length; i++) { 335 emptySet.add(elements[i]); 336 } 337 boolean result = set.addAll(emptySet); 338 assertFalse(result); 339 340 Collection<EnumFoo> collection = new ArrayList<EnumFoo>(); 341 collection.add(EnumFoo.a); 342 collection.add(EnumFoo.b); 343 result = set.addAll(collection); 344 assertTrue("addAll should be successful", result); 345 assertEquals("Size should be 2:", 2, set.size()); 346 347 set = EnumSet.noneOf(EnumFoo.class); 348 349 Collection rawCollection = new ArrayList<Integer>(); 350 result = set.addAll(rawCollection); 351 assertFalse(result); 352 rawCollection.add(1); 353 try { 354 set.addAll(rawCollection); 355 fail("Should throw ClassCastException"); 356 } catch (ClassCastException e) { 357 // expected 358 } 359 360 Set<EnumFoo> fullSet = EnumSet.noneOf(EnumFoo.class); 361 fullSet.add(EnumFoo.a); 362 fullSet.add(EnumFoo.b); 363 result = set.addAll(fullSet); 364 assertTrue("addAll should be successful", result); 365 assertEquals("Size of set should be 2", 2, set.size()); 366 367 try { 368 fullSet.addAll(null); 369 fail("Should throw NullPointerException"); 370 } catch (NullPointerException e) { 371 // expected 372 } 373 374 Set fullSetWithSubclass = EnumSet.noneOf(EnumWithInnerClass.class); 375 elements = EnumWithInnerClass.class.getEnumConstants(); 376 for(int i = 0; i < elements.length; i++) { 377 fullSetWithSubclass.add(elements[i]); 378 } 379 try { 380 set.addAll(fullSetWithSubclass); 381 fail("Should throw ClassCastException"); 382 } catch (ClassCastException e) { 383 // expected 384 } 385 Set<EnumWithInnerClass> setWithSubclass = fullSetWithSubclass; 386 result = setWithSubclass.addAll(setWithSubclass); 387 assertFalse("Should return false", result); 388 389 Set<EnumWithInnerClass> anotherSetWithSubclass = EnumSet 390 .noneOf(EnumWithInnerClass.class); 391 elements = EnumWithInnerClass.class.getEnumConstants(); 392 for(int i = 0; i < elements.length; i++) { 393 anotherSetWithSubclass.add((EnumWithInnerClass) elements[i]); 394 } 395 result = setWithSubclass.addAll(anotherSetWithSubclass); 396 assertFalse("Should return false", result); 397 398 anotherSetWithSubclass.remove(EnumWithInnerClass.a); 399 result = setWithSubclass.addAll(anotherSetWithSubclass); 400 assertFalse("Should return false", result); 401 402 // test enum type with more than 64 elements 403 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 404 assertEquals(0, hugeSet.size()); 405 406 try { 407 hugeSet.addAll(null); 408 fail("Should throw NullPointerException"); 409 } catch (NullPointerException e) { 410 // expected 411 } 412 413 hugeSet = EnumSet.allOf(HugeEnum.class); 414 result = hugeSet.addAll(hugeSet); 415 assertFalse(result); 416 417 hugeSet = EnumSet.noneOf(HugeEnum.class); 418 Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>(); 419 hugeCollection.add(HugeEnum.a); 420 hugeCollection.add(HugeEnum.b); 421 result = hugeSet.addAll(hugeCollection); 422 assertTrue(result); 423 assertEquals(2, set.size()); 424 425 hugeSet = EnumSet.noneOf(HugeEnum.class); 426 427 rawCollection = new ArrayList<Integer>(); 428 result = hugeSet.addAll(rawCollection); 429 assertFalse(result); 430 rawCollection.add(1); 431 try { 432 hugeSet.addAll(rawCollection); 433 fail("Should throw ClassCastException"); 434 } catch (ClassCastException e) { 435 // expected 436 } 437 438 EnumSet<HugeEnum> aHugeSet = EnumSet.noneOf(HugeEnum.class); 439 aHugeSet.add(HugeEnum.a); 440 aHugeSet.add(HugeEnum.b); 441 result = hugeSet.addAll(aHugeSet); 442 assertTrue(result); 443 assertEquals(2, hugeSet.size()); 444 445 try { 446 aHugeSet.addAll(null); 447 fail("Should throw NullPointerException"); 448 } catch (NullPointerException e) { 449 // expected 450 } 451 452 Set hugeSetWithSubclass = EnumSet.allOf(HugeEnumWithInnerClass.class); 453 try { 454 hugeSet.addAll(hugeSetWithSubclass); 455 fail("Should throw ClassCastException"); 456 } catch (ClassCastException e) { 457 // expected 458 } 459 Set<HugeEnumWithInnerClass> hugeSetWithInnerSubclass = hugeSetWithSubclass; 460 result = hugeSetWithInnerSubclass.addAll(hugeSetWithInnerSubclass); 461 assertFalse(result); 462 463 Set<HugeEnumWithInnerClass> anotherHugeSetWithSubclass = EnumSet 464 .allOf(HugeEnumWithInnerClass.class); 465 result = hugeSetWithSubclass.addAll(anotherHugeSetWithSubclass); 466 assertFalse(result); 467 468 anotherHugeSetWithSubclass.remove(HugeEnumWithInnerClass.a); 469 result = setWithSubclass.addAll(anotherSetWithSubclass); 470 assertFalse(result); 471 472 } 473 474 /** 475 * java.util.EnumSet#remove(Object) 476 */ 477 @SuppressWarnings("CollectionIncompatibleType") test_remove_LOject()478 public void test_remove_LOject() { 479 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 480 Enum[] elements = EnumFoo.class.getEnumConstants(); 481 for(int i = 0; i < elements.length; i++) { 482 set.add((EnumFoo) elements[i]); 483 } 484 485 boolean result = set.remove(null); 486 assertFalse("'set' does not contain null", result); 487 488 result = set.remove(EnumFoo.a); 489 assertTrue("Should return true", result); 490 result = set.remove(EnumFoo.a); 491 assertFalse("Should return false", result); 492 493 assertEquals("Size of set should be 63:", 63, set.size()); 494 495 result = set.remove(EnumWithInnerClass.a); 496 assertFalse("Should return false", result); 497 result = set.remove(EnumWithInnerClass.f); 498 assertFalse("Should return false", result); 499 500 // test enum with more than 64 elements 501 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 502 503 result = hugeSet.remove(null); 504 assertFalse("'set' does not contain null", result); 505 506 result = hugeSet.remove(HugeEnum.a); 507 assertTrue("Should return true", result); 508 result = hugeSet.remove(HugeEnum.a); 509 assertFalse("Should return false", result); 510 511 assertEquals("Size of set should be 64:", 64, hugeSet.size()); 512 513 result = hugeSet.remove(HugeEnumWithInnerClass.a); 514 assertFalse("Should return false", result); 515 result = hugeSet.remove(HugeEnumWithInnerClass.f); 516 assertFalse("Should return false", result); 517 } 518 519 /** 520 * java.util.EnumSet#equals(Object) 521 */ test_equals_LObject()522 public void test_equals_LObject() { 523 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 524 Enum[] elements = EnumFoo.class.getEnumConstants(); 525 for(int i = 0; i < elements.length; i++) { 526 set.add((EnumFoo) elements[i]); 527 } 528 529 assertFalse("Should return false", set.equals(null)); 530 assertFalse( 531 "Should return false", set.equals(new Object())); 532 533 Set<EnumFoo> anotherSet = EnumSet.noneOf(EnumFoo.class); 534 elements = EnumFoo.class.getEnumConstants(); 535 for(int i = 0; i < elements.length; i++) { 536 anotherSet.add((EnumFoo) elements[i]); 537 } 538 assertTrue("Should return true", set.equals(anotherSet)); 539 540 anotherSet.remove(EnumFoo.a); 541 assertFalse( 542 "Should return false", set.equals(anotherSet)); 543 544 Set<EnumWithInnerClass> setWithInnerClass = EnumSet 545 .noneOf(EnumWithInnerClass.class); 546 elements = EnumWithInnerClass.class.getEnumConstants(); 547 for(int i = 0; i < elements.length; i++) { 548 setWithInnerClass.add((EnumWithInnerClass) elements[i]); 549 } 550 551 assertFalse( 552 "Should return false", set.equals(setWithInnerClass)); 553 554 setWithInnerClass.clear(); 555 set.clear(); 556 assertTrue("Should be equal", set.equals(setWithInnerClass)); 557 558 // test enum type with more than 64 elements 559 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 560 assertTrue(hugeSet.equals(set)); 561 562 hugeSet = EnumSet.allOf(HugeEnum.class); 563 assertFalse(hugeSet.equals(null)); 564 assertFalse(hugeSet.equals(new Object())); 565 566 Set<HugeEnum> anotherHugeSet = EnumSet.allOf(HugeEnum.class); 567 anotherHugeSet.remove(HugeEnum.a); 568 assertFalse(hugeSet.equals(anotherHugeSet)); 569 570 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 571 .allOf(HugeEnumWithInnerClass.class); 572 assertFalse(hugeSet.equals(hugeSetWithInnerClass)); 573 hugeSetWithInnerClass.clear(); 574 hugeSet.clear(); 575 assertTrue(hugeSet.equals(hugeSetWithInnerClass)); 576 } 577 578 /** 579 * java.util.EnumSet#clear() 580 */ test_clear()581 public void test_clear() { 582 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 583 set.add(EnumFoo.a); 584 set.add(EnumFoo.b); 585 assertEquals("Size should be 2", 2, set.size()); 586 587 set.clear(); 588 589 assertEquals("Size should be 0", 0, set.size()); 590 591 // test enum type with more than 64 elements 592 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 593 assertEquals(65, hugeSet.size()); 594 595 boolean result = hugeSet.contains(HugeEnum.aa); 596 assertTrue(result); 597 598 hugeSet.clear(); 599 assertEquals(0, hugeSet.size()); 600 result = hugeSet.contains(HugeEnum.aa); 601 assertFalse(result); 602 } 603 604 /** 605 * java.util.EnumSet#size() 606 */ test_size()607 public void test_size() { 608 assertEmpty(EnumSet.noneOf(EnumFoo.class)); 609 assertSize(2, EnumSet.of(EnumFoo.a, EnumFoo.b)); 610 611 // enum type with more than 64 elements 612 assertEmpty(EnumSet.noneOf(HugeEnum.class)); 613 assertSize(2, EnumSet.of(HugeEnum.a, HugeEnum.bb)); 614 assertSize(65, EnumSet.allOf(HugeEnum.class)); 615 } 616 test_size_modification_regular()617 public void test_size_modification_regular() { 618 EnumSet<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 619 assertEmpty(set); 620 set.addAll(Arrays.asList(EnumFoo.a, EnumFoo.b)); 621 assertSize(2, set); 622 set.add(EnumFoo.c); 623 assertSize(3, set); 624 set.remove(EnumFoo.d); 625 assertSize(3, set); 626 set.remove(EnumFoo.b); 627 assertSize(2, set); 628 set.clear(); 629 assertEmpty(set); 630 } 631 test_size_modification_jumbo()632 public void test_size_modification_jumbo() { 633 EnumSet<HugeEnum> set = EnumSet.allOf(HugeEnum.class); 634 assertSize(65, set); 635 set.remove(HugeEnum.b); 636 assertSize(64, set); 637 set.clear(); 638 assertEmpty(set); 639 } 640 assertEmpty(EnumSet<?> set)641 private static void assertEmpty(EnumSet<?> set) { 642 assertSize(0, set); 643 } 644 assertSize(int expectedSize, Set<?> set)645 private static void assertSize(int expectedSize, Set<?> set) { 646 assertEquals(expectedSize, set.size()); 647 assertEquals(expectedSize == 0, set.isEmpty()); 648 } 649 650 /** 651 * java.util.EnumSet#complementOf(java.util.EnumSet) 652 */ test_ComplementOf_LEnumSet()653 public void test_ComplementOf_LEnumSet() { 654 655 try { 656 EnumSet.complementOf((EnumSet<EnumFoo>) null); 657 fail("Should throw NullPointerException"); 658 } catch (NullPointerException npe) { 659 // expected 660 } 661 662 EnumSet<EnumWithInnerClass> set = EnumSet 663 .noneOf(EnumWithInnerClass.class); 664 set.add(EnumWithInnerClass.d); 665 set.add(EnumWithInnerClass.e); 666 set.add(EnumWithInnerClass.f); 667 668 assertEquals("Size should be 3:", 3, set.size()); 669 670 EnumSet<EnumWithInnerClass> complementOfE = EnumSet.complementOf(set); 671 assertTrue(set.contains(EnumWithInnerClass.d)); 672 assertEquals( 673 "complementOfE should have size 3", 3, complementOfE.size()); 674 assertTrue("complementOfE should contain EnumWithSubclass.a:", 675 complementOfE.contains(EnumWithInnerClass.a)); 676 assertTrue("complementOfE should contain EnumWithSubclass.b:", 677 complementOfE.contains(EnumWithInnerClass.b)); 678 assertTrue("complementOfE should contain EnumWithSubclass.c:", 679 complementOfE.contains(EnumWithInnerClass.c)); 680 681 // test enum type with more than 64 elements 682 EnumSet<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 683 assertEquals(0, hugeSet.size()); 684 Set<HugeEnum> complementHugeSet = EnumSet.complementOf(hugeSet); 685 assertEquals(65, complementHugeSet.size()); 686 687 hugeSet.add(HugeEnum.A); 688 hugeSet.add(HugeEnum.mm); 689 complementHugeSet = EnumSet.complementOf(hugeSet); 690 assertEquals(63, complementHugeSet.size()); 691 692 try { 693 EnumSet.complementOf((EnumSet<HugeEnum>) null); 694 fail("Should throw NullPointerException"); 695 } catch (NullPointerException npe) { 696 // expected 697 } 698 } 699 700 /** 701 * java.util.EnumSet#contains(Object) 702 */ 703 @SuppressWarnings("CollectionIncompatibleType") test_contains_LObject()704 public void test_contains_LObject() { 705 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 706 Enum[] elements = EnumFoo.class.getEnumConstants(); 707 for(int i = 0; i < elements.length; i++) { 708 set.add((EnumFoo)elements[i]); 709 } 710 boolean result = set.contains(null); 711 assertFalse("Should not contain null:", result); 712 713 result = set.contains(EnumFoo.a); 714 assertTrue("Should contain EnumFoo.a", result); 715 result = set.contains(EnumFoo.ll); 716 assertTrue("Should contain EnumFoo.ll", result); 717 718 result = set.contains(EnumFoo.b); 719 assertTrue("Should contain EnumFoo.b", result); 720 721 result = set.contains(new Object()); 722 assertFalse("Should not contain Object instance", result); 723 724 result = set.contains(EnumWithInnerClass.a); 725 assertFalse("Should not contain EnumWithSubclass.a", result); 726 727 set = EnumSet.noneOf(EnumFoo.class); 728 set.add(EnumFoo.aa); 729 set.add(EnumFoo.bb); 730 set.add(EnumFoo.cc); 731 732 assertEquals("Size of set should be 3", 3, set.size()); 733 assertTrue("set should contain EnumFoo.aa", set.contains(EnumFoo.aa)); 734 735 Set<EnumWithInnerClass> setWithSubclass = EnumSet 736 .noneOf(EnumWithInnerClass.class); 737 setWithSubclass.add(EnumWithInnerClass.a); 738 setWithSubclass.add(EnumWithInnerClass.b); 739 setWithSubclass.add(EnumWithInnerClass.c); 740 setWithSubclass.add(EnumWithInnerClass.d); 741 setWithSubclass.add(EnumWithInnerClass.e); 742 setWithSubclass.add(EnumWithInnerClass.f); 743 result = setWithSubclass.contains(EnumWithInnerClass.f); 744 assertTrue("Should contain EnumWithSubclass.f", result); 745 746 // test enum type with more than 64 elements 747 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 748 hugeSet.add(HugeEnum.a); 749 result = hugeSet.contains(HugeEnum.a); 750 assertTrue(result); 751 752 result = hugeSet.contains(HugeEnum.b); 753 assertTrue(result); 754 755 result = hugeSet.contains(null); 756 assertFalse(result); 757 758 result = hugeSet.contains(HugeEnum.a); 759 assertTrue(result); 760 761 result = hugeSet.contains(HugeEnum.ll); 762 assertTrue(result); 763 764 result = hugeSet.contains(new Object()); 765 assertFalse(result); 766 767 result = hugeSet.contains(Enum.class); 768 assertFalse(result); 769 770 } 771 772 /** 773 * java.util.EnumSet#containsAll(Collection) 774 */ 775 @SuppressWarnings( { "unchecked", "boxing", "CollectionIncompatibleType" }) test_containsAll_LCollection()776 public void test_containsAll_LCollection() { 777 EnumSet<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 778 Enum[] elements = EnumFoo.class.getEnumConstants(); 779 for(int i = 0; i < elements.length; i++) { 780 set.add((EnumFoo)elements[i]); 781 } 782 try { 783 set.containsAll(null); 784 fail("Should throw NullPointerException"); 785 } catch (NullPointerException e) { 786 // expected 787 } 788 789 EnumSet<EmptyEnum> emptySet = EnumSet.noneOf(EmptyEnum.class); 790 elements = EmptyEnum.class.getEnumConstants(); 791 for(int i = 0; i < elements.length; i++) { 792 emptySet.add((EmptyEnum)elements[i]); 793 } 794 boolean result = set.containsAll(emptySet); 795 assertTrue("Should return true", result); 796 797 Collection rawCollection = new ArrayList(); 798 result = set.containsAll(rawCollection); 799 assertTrue("Should contain empty collection:", result); 800 801 rawCollection.add(1); 802 result = set.containsAll(rawCollection); 803 assertFalse("Should return false", result); 804 805 rawCollection.add(EnumWithInnerClass.a); 806 result = set.containsAll(rawCollection); 807 assertFalse("Should return false", result); 808 809 EnumSet rawSet = EnumSet.noneOf(EnumFoo.class); 810 result = set.containsAll(rawSet); 811 assertTrue("Should contain empty set", result); 812 813 emptySet = EnumSet.noneOf(EmptyEnum.class); 814 result = set.containsAll(emptySet); 815 assertTrue("No class cast should be performed on empty set", result); 816 817 Collection<EnumFoo> collection = new ArrayList<EnumFoo>(); 818 collection.add(EnumFoo.a); 819 result = set.containsAll(collection); 820 assertTrue("Should contain all elements in collection", result); 821 822 EnumSet<EnumFoo> fooSet = EnumSet.noneOf(EnumFoo.class); 823 fooSet.add(EnumFoo.a); 824 result = set.containsAll(fooSet); 825 assertTrue("Should return true", result); 826 827 set.clear(); 828 try { 829 set.containsAll(null); 830 fail("Should throw NullPointerException"); 831 } catch (NullPointerException e) { 832 // expected 833 } 834 835 Collection<EnumWithInnerClass> collectionWithSubclass = new ArrayList<EnumWithInnerClass>(); 836 collectionWithSubclass.add(EnumWithInnerClass.a); 837 result = set.containsAll(collectionWithSubclass); 838 assertFalse("Should return false", result); 839 840 EnumSet<EnumWithInnerClass> setWithSubclass = EnumSet 841 .noneOf(EnumWithInnerClass.class); 842 setWithSubclass.add(EnumWithInnerClass.a); 843 result = set.containsAll(setWithSubclass); 844 assertFalse("Should return false", result); 845 846 // test enum type with more than 64 elements 847 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 848 hugeSet.add(HugeEnum.a); 849 hugeSet.add(HugeEnum.b); 850 hugeSet.add(HugeEnum.aa); 851 hugeSet.add(HugeEnum.bb); 852 hugeSet.add(HugeEnum.cc); 853 hugeSet.add(HugeEnum.dd); 854 855 Set<HugeEnum> anotherHugeSet = EnumSet.noneOf(HugeEnum.class); 856 hugeSet.add(HugeEnum.b); 857 hugeSet.add(HugeEnum.cc); 858 result = hugeSet.containsAll(anotherHugeSet); 859 assertTrue(result); 860 861 try { 862 hugeSet.containsAll(null); 863 fail("Should throw NullPointerException"); 864 } catch(NullPointerException e) { 865 // expected 866 } 867 868 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 869 .noneOf(HugeEnumWithInnerClass.class); 870 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.a); 871 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.b); 872 result = hugeSetWithInnerClass.containsAll(hugeSetWithInnerClass); 873 assertTrue(result); 874 result = hugeSet.containsAll(hugeSetWithInnerClass); 875 assertFalse(result); 876 877 rawCollection = new ArrayList(); 878 result = hugeSet.containsAll(rawCollection); 879 assertTrue("Should contain empty collection:", result); 880 881 rawCollection.add(1); 882 result = hugeSet.containsAll(rawCollection); 883 assertFalse("Should return false", result); 884 885 rawCollection.add(EnumWithInnerClass.a); 886 result = set.containsAll(rawCollection); 887 assertFalse("Should return false", result); 888 889 rawSet = EnumSet.noneOf(HugeEnum.class); 890 result = hugeSet.containsAll(rawSet); 891 assertTrue("Should contain empty set", result); 892 893 EnumSet<HugeEnumWithInnerClass> emptyHugeSet 894 = EnumSet.noneOf(HugeEnumWithInnerClass.class); 895 result = hugeSet.containsAll(emptyHugeSet); 896 assertTrue("No class cast should be performed on empty set", result); 897 898 Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>(); 899 hugeCollection.add(HugeEnum.a); 900 result = hugeSet.containsAll(hugeCollection); 901 assertTrue("Should contain all elements in collection", result); 902 903 hugeSet.clear(); 904 try { 905 hugeSet.containsAll(null); 906 fail("Should throw NullPointerException"); 907 } catch (NullPointerException e) { 908 // expected 909 } 910 911 Collection<HugeEnumWithInnerClass> hugeCollectionWithSubclass = new ArrayList<HugeEnumWithInnerClass>(); 912 hugeCollectionWithSubclass.add(HugeEnumWithInnerClass.a); 913 result = hugeSet.containsAll(hugeCollectionWithSubclass); 914 assertFalse("Should return false", result); 915 916 EnumSet<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet 917 .noneOf(HugeEnumWithInnerClass.class); 918 hugeSetWithSubclass.add(HugeEnumWithInnerClass.a); 919 result = hugeSet.containsAll(hugeSetWithSubclass); 920 assertFalse("Should return false", result); 921 } 922 923 /** 924 * java.util.EnumSet#copyOf(java.util.Collection) 925 */ 926 @SuppressWarnings("unchecked") test_CopyOf_LCollection()927 public void test_CopyOf_LCollection() { 928 try { 929 EnumSet.copyOf((Collection) null); 930 fail("Should throw NullPointerException"); 931 } catch (NullPointerException npe) { 932 // expected 933 } 934 935 Collection collection = new ArrayList(); 936 try { 937 EnumSet.copyOf(collection); 938 fail("Should throw IllegalArgumentException"); 939 } catch (IllegalArgumentException e) { 940 // expected 941 } 942 943 collection.add(new Object()); 944 try { 945 EnumSet.copyOf(collection); 946 fail("Should throw ClassCastException"); 947 } catch (ClassCastException e) { 948 // expected 949 } 950 951 Collection<EnumFoo> enumCollection = new ArrayList<EnumFoo>(); 952 enumCollection.add(EnumFoo.b); 953 954 EnumSet<EnumFoo> copyOfEnumCollection = EnumSet.copyOf(enumCollection); 955 assertEquals("Size of copyOfEnumCollection should be 1:", 956 1, copyOfEnumCollection.size()); 957 assertTrue("copyOfEnumCollection should contain EnumFoo.b:", 958 copyOfEnumCollection.contains(EnumFoo.b)); 959 960 enumCollection.add(null); 961 assertEquals("Size of enumCollection should be 2:", 962 2, enumCollection.size()); 963 964 try { 965 copyOfEnumCollection = EnumSet.copyOf(enumCollection); 966 fail("Should throw NullPointerException"); 967 } catch (NullPointerException npe) { 968 // expected 969 } 970 971 Collection rawEnumCollection = new ArrayList(); 972 rawEnumCollection.add(EnumFoo.a); 973 rawEnumCollection.add(EnumWithInnerClass.a); 974 try { 975 EnumSet.copyOf(rawEnumCollection); 976 fail("Should throw ClassCastException"); 977 } catch(ClassCastException e) { 978 // expected 979 } 980 981 // test enum type with more than 64 elements 982 Collection<HugeEnum> hugeEnumCollection = new ArrayList<HugeEnum>(); 983 hugeEnumCollection.add(HugeEnum.b); 984 985 EnumSet<HugeEnum> copyOfHugeEnumCollection = EnumSet.copyOf(hugeEnumCollection); 986 assertEquals(1, copyOfHugeEnumCollection.size()); 987 assertTrue(copyOfHugeEnumCollection.contains(HugeEnum.b)); 988 989 hugeEnumCollection.add(null); 990 assertEquals(2, hugeEnumCollection.size()); 991 992 try { 993 copyOfHugeEnumCollection = EnumSet.copyOf(hugeEnumCollection); 994 fail("Should throw NullPointerException"); 995 } catch (NullPointerException npe) { 996 // expected 997 } 998 999 rawEnumCollection = new ArrayList(); 1000 rawEnumCollection.add(HugeEnum.a); 1001 rawEnumCollection.add(HugeEnumWithInnerClass.a); 1002 try { 1003 EnumSet.copyOf(rawEnumCollection); 1004 fail("Should throw ClassCastException"); 1005 } catch(ClassCastException e) { 1006 // expected 1007 } 1008 } 1009 1010 /** 1011 * java.util.EnumSet#copyOf(java.util.EnumSet) 1012 */ 1013 @SuppressWarnings("unchecked") test_CopyOf_LEnumSet()1014 public void test_CopyOf_LEnumSet() { 1015 EnumSet<EnumWithInnerClass> enumSet = EnumSet 1016 .noneOf(EnumWithInnerClass.class); 1017 enumSet.add(EnumWithInnerClass.a); 1018 enumSet.add(EnumWithInnerClass.f); 1019 EnumSet<EnumWithInnerClass> copyOfE = EnumSet.copyOf(enumSet); 1020 assertEquals("Size of enumSet and copyOfE should be equal", 1021 enumSet.size(), copyOfE.size()); 1022 1023 assertTrue("EnumWithSubclass.a should be contained in copyOfE", 1024 copyOfE.contains(EnumWithInnerClass.a)); 1025 assertTrue("EnumWithSubclass.f should be contained in copyOfE", 1026 copyOfE.contains(EnumWithInnerClass.f)); 1027 1028 Object[] enumValue = copyOfE.toArray(); 1029 assertSame("enumValue[0] should be identical with EnumWithSubclass.a", 1030 enumValue[0], EnumWithInnerClass.a); 1031 assertSame("enumValue[1] should be identical with EnumWithSubclass.f", 1032 enumValue[1], EnumWithInnerClass.f); 1033 1034 try { 1035 EnumSet.copyOf((EnumSet) null); 1036 fail("Should throw NullPointerException"); 1037 } catch (NullPointerException npe) { 1038 // expected 1039 } 1040 1041 // test enum type with more than 64 elements 1042 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet 1043 .noneOf(HugeEnumWithInnerClass.class); 1044 hugeEnumSet.add(HugeEnumWithInnerClass.a); 1045 hugeEnumSet.add(HugeEnumWithInnerClass.f); 1046 EnumSet<HugeEnumWithInnerClass> copyOfHugeEnum = EnumSet.copyOf(hugeEnumSet); 1047 assertEquals(enumSet.size(), copyOfE.size()); 1048 1049 assertTrue(copyOfHugeEnum.contains(HugeEnumWithInnerClass.a)); 1050 assertTrue(copyOfHugeEnum.contains(HugeEnumWithInnerClass.f)); 1051 1052 Object[] hugeEnumValue = copyOfHugeEnum.toArray(); 1053 assertSame(hugeEnumValue[0], HugeEnumWithInnerClass.a); 1054 assertSame(hugeEnumValue[1], HugeEnumWithInnerClass.f); 1055 } 1056 1057 /** 1058 * java.util.EnumSet#removeAll(Collection) 1059 */ 1060 @SuppressWarnings({ "unchecked", "CollectionIncompatibleType" }) test_removeAll_LCollection()1061 public void test_removeAll_LCollection() { 1062 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 1063 try { 1064 set.removeAll(null); 1065 fail("Should throw NullPointerException"); 1066 } catch (NullPointerException e) { 1067 // expected 1068 } 1069 1070 set = EnumSet.allOf(EnumFoo.class); 1071 assertEquals("Size of set should be 64:", 64, set.size()); 1072 1073 try { 1074 set.removeAll(null); 1075 fail("Should throw NullPointerException"); 1076 } catch (NullPointerException e) { 1077 // expected 1078 } 1079 1080 Collection<EnumFoo> collection = new ArrayList<EnumFoo>(); 1081 collection.add(EnumFoo.a); 1082 1083 boolean result = set.removeAll(collection); 1084 assertTrue("Should return true", result); 1085 assertEquals("Size of set should be 63", 63, set.size()); 1086 1087 collection = new ArrayList(); 1088 result = set.removeAll(collection); 1089 assertFalse("Should return false", result); 1090 1091 Set<EmptyEnum> emptySet = EnumSet.noneOf(EmptyEnum.class); 1092 result = set.removeAll(emptySet); 1093 assertFalse("Should return false", result); 1094 1095 EnumSet<EnumFoo> emptyFooSet = EnumSet.noneOf(EnumFoo.class); 1096 result = set.removeAll(emptyFooSet); 1097 assertFalse("Should return false", result); 1098 1099 emptyFooSet.add(EnumFoo.a); 1100 result = set.removeAll(emptyFooSet); 1101 assertFalse("Should return false", result); 1102 1103 Set<EnumWithInnerClass> setWithSubclass = EnumSet 1104 .noneOf(EnumWithInnerClass.class); 1105 result = set.removeAll(setWithSubclass); 1106 assertFalse("Should return false", result); 1107 1108 setWithSubclass.add(EnumWithInnerClass.a); 1109 result = set.removeAll(setWithSubclass); 1110 assertFalse("Should return false", result); 1111 1112 Set<EnumFoo> anotherSet = EnumSet.noneOf(EnumFoo.class); 1113 anotherSet.add(EnumFoo.a); 1114 1115 set = EnumSet.allOf(EnumFoo.class); 1116 result = set.removeAll(anotherSet); 1117 assertTrue("Should return true", result); 1118 assertEquals("Size of set should be 63:", 63, set.size()); 1119 1120 Set<EnumWithInnerClass> setWithInnerClass = EnumSet 1121 .noneOf(EnumWithInnerClass.class); 1122 setWithInnerClass.add(EnumWithInnerClass.a); 1123 setWithInnerClass.add(EnumWithInnerClass.b); 1124 1125 Set<EnumWithInnerClass> anotherSetWithInnerClass = EnumSet 1126 .noneOf(EnumWithInnerClass.class); 1127 anotherSetWithInnerClass.add(EnumWithInnerClass.c); 1128 anotherSetWithInnerClass.add(EnumWithInnerClass.d); 1129 result = anotherSetWithInnerClass.removeAll(setWithInnerClass); 1130 assertFalse("Should return false", result); 1131 1132 anotherSetWithInnerClass.add(EnumWithInnerClass.a); 1133 result = anotherSetWithInnerClass.removeAll(setWithInnerClass); 1134 assertTrue("Should return true", result); 1135 assertEquals("Size of anotherSetWithInnerClass should remain 2", 1136 2, anotherSetWithInnerClass.size()); 1137 1138 anotherSetWithInnerClass.remove(EnumWithInnerClass.c); 1139 anotherSetWithInnerClass.remove(EnumWithInnerClass.d); 1140 result = anotherSetWithInnerClass.remove(setWithInnerClass); 1141 assertFalse("Should return false", result); 1142 1143 Set rawSet = EnumSet.allOf(EnumWithAllInnerClass.class); 1144 result = rawSet.removeAll(EnumSet.allOf(EnumFoo.class)); 1145 assertFalse("Should return false", result); 1146 1147 setWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1148 anotherSetWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1149 setWithInnerClass.remove(EnumWithInnerClass.a); 1150 anotherSetWithInnerClass.remove(EnumWithInnerClass.f); 1151 result = setWithInnerClass.removeAll(anotherSetWithInnerClass); 1152 assertTrue("Should return true", result); 1153 assertEquals("Size of setWithInnerClass should be 1", 1, setWithInnerClass.size()); 1154 1155 result = setWithInnerClass.contains(EnumWithInnerClass.f); 1156 assertTrue("Should return true", result); 1157 1158 // test enum type with more than 64 elements 1159 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 1160 1161 Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>(); 1162 hugeCollection.add(HugeEnum.a); 1163 1164 result = hugeSet.removeAll(hugeCollection); 1165 assertTrue(result); 1166 assertEquals(64, hugeSet.size()); 1167 1168 collection = new ArrayList(); 1169 result = hugeSet.removeAll(collection); 1170 assertFalse(result); 1171 1172 Set<HugeEnum> emptyHugeSet = EnumSet.noneOf(HugeEnum.class); 1173 result = hugeSet.removeAll(emptyHugeSet); 1174 assertFalse(result); 1175 1176 Set<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet 1177 .noneOf(HugeEnumWithInnerClass.class); 1178 result = hugeSet.removeAll(hugeSetWithSubclass); 1179 assertFalse(result); 1180 1181 hugeSetWithSubclass.add(HugeEnumWithInnerClass.a); 1182 result = hugeSet.removeAll(hugeSetWithSubclass); 1183 assertFalse(result); 1184 1185 Set<HugeEnum> anotherHugeSet = EnumSet.noneOf(HugeEnum.class); 1186 anotherHugeSet.add(HugeEnum.a); 1187 1188 hugeSet = EnumSet.allOf(HugeEnum.class); 1189 result = hugeSet.removeAll(anotherHugeSet); 1190 assertTrue(result); 1191 assertEquals(63, set.size()); 1192 1193 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 1194 .noneOf(HugeEnumWithInnerClass.class); 1195 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.a); 1196 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.b); 1197 1198 Set<HugeEnumWithInnerClass> anotherHugeSetWithInnerClass = EnumSet 1199 .noneOf(HugeEnumWithInnerClass.class); 1200 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.c); 1201 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.d); 1202 result = anotherHugeSetWithInnerClass.removeAll(setWithInnerClass); 1203 assertFalse("Should return false", result); 1204 1205 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.a); 1206 result = anotherHugeSetWithInnerClass.removeAll(hugeSetWithInnerClass); 1207 assertTrue(result); 1208 assertEquals(2, anotherHugeSetWithInnerClass.size()); 1209 1210 anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.c); 1211 anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.d); 1212 result = anotherHugeSetWithInnerClass.remove(hugeSetWithInnerClass); 1213 assertFalse(result); 1214 1215 rawSet = EnumSet.allOf(HugeEnumWithInnerClass.class); 1216 result = rawSet.removeAll(EnumSet.allOf(HugeEnum.class)); 1217 assertFalse(result); 1218 1219 hugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1220 anotherHugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1221 hugeSetWithInnerClass.remove(HugeEnumWithInnerClass.a); 1222 anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.f); 1223 result = hugeSetWithInnerClass.removeAll(anotherHugeSetWithInnerClass); 1224 assertTrue(result); 1225 assertEquals(1, hugeSetWithInnerClass.size()); 1226 1227 result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.f); 1228 assertTrue(result); 1229 } 1230 1231 /** 1232 * java.util.EnumSet#retainAll(Collection) 1233 */ 1234 @SuppressWarnings({ "unchecked", "CollectionIncompatibleType" }) test_retainAll_LCollection()1235 public void test_retainAll_LCollection() { 1236 Set<EnumFoo> set = EnumSet.allOf(EnumFoo.class); 1237 1238 try { 1239 set.retainAll(null); 1240 fail("Should throw NullPointerException"); 1241 } catch (NullPointerException e) { 1242 // expected 1243 } 1244 1245 set.clear(); 1246 try { 1247 set.retainAll(null); 1248 fail("Should throw NullPointerException"); 1249 } catch (NullPointerException e) { 1250 // expected 1251 } 1252 1253 Collection rawCollection = new ArrayList(); 1254 boolean result = set.retainAll(rawCollection); 1255 assertFalse("Should return false", result); 1256 1257 rawCollection.add(EnumFoo.a); 1258 result = set.retainAll(rawCollection); 1259 assertFalse("Should return false", result); 1260 1261 rawCollection.add(EnumWithInnerClass.a); 1262 result = set.retainAll(rawCollection); 1263 assertFalse("Should return false", result); 1264 assertEquals("Size of set should be 0:", 0, set.size()); 1265 1266 rawCollection.remove(EnumFoo.a); 1267 result = set.retainAll(rawCollection); 1268 assertFalse("Should return false", result); 1269 1270 Set<EnumFoo> anotherSet = EnumSet.allOf(EnumFoo.class); 1271 result = set.retainAll(anotherSet); 1272 assertFalse("Should return false", result); 1273 assertEquals("Size of set should be 0", 0, set.size()); 1274 1275 Set<EnumWithInnerClass> setWithInnerClass = EnumSet 1276 .allOf(EnumWithInnerClass.class); 1277 result = set.retainAll(setWithInnerClass); 1278 assertFalse("Should return false", result); 1279 assertEquals("Size of set should be 0", 0, set.size()); 1280 1281 setWithInnerClass = EnumSet.noneOf(EnumWithInnerClass.class); 1282 result = set.retainAll(setWithInnerClass); 1283 assertFalse("Should return false", result); 1284 1285 Set<EmptyEnum> emptySet = EnumSet.allOf(EmptyEnum.class); 1286 result = set.retainAll(emptySet); 1287 assertFalse("Should return false", result); 1288 1289 Set<EnumWithAllInnerClass> setWithAllInnerClass = EnumSet 1290 .allOf(EnumWithAllInnerClass.class); 1291 result = set.retainAll(setWithAllInnerClass); 1292 assertFalse("Should return false", result); 1293 1294 set.add(EnumFoo.a); 1295 result = set.retainAll(setWithInnerClass); 1296 assertTrue("Should return true", result); 1297 assertEquals("Size of set should be 0", 0, set.size()); 1298 1299 setWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1300 setWithInnerClass.remove(EnumWithInnerClass.f); 1301 Set<EnumWithInnerClass> anotherSetWithInnerClass = EnumSet 1302 .noneOf(EnumWithInnerClass.class); 1303 anotherSetWithInnerClass.add(EnumWithInnerClass.e); 1304 anotherSetWithInnerClass.add(EnumWithInnerClass.f); 1305 1306 result = setWithInnerClass.retainAll(anotherSetWithInnerClass); 1307 assertTrue("Should return true", result); 1308 result = setWithInnerClass.contains(EnumWithInnerClass.e); 1309 assertTrue("Should contain EnumWithInnerClass.e", result); 1310 result = setWithInnerClass.contains(EnumWithInnerClass.b); 1311 assertFalse("Should not contain EnumWithInnerClass.b", result); 1312 assertEquals("Size of set should be 1:", 1, setWithInnerClass.size()); 1313 1314 anotherSetWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1315 result = setWithInnerClass.retainAll(anotherSetWithInnerClass); 1316 1317 assertFalse("Return value should be false", result); 1318 1319 rawCollection = new ArrayList(); 1320 rawCollection.add(EnumWithInnerClass.e); 1321 rawCollection.add(EnumWithInnerClass.f); 1322 result = setWithInnerClass.retainAll(rawCollection); 1323 assertFalse("Should return false", result); 1324 1325 set = EnumSet.allOf(EnumFoo.class); 1326 set.remove(EnumFoo.a); 1327 anotherSet = EnumSet.noneOf(EnumFoo.class); 1328 anotherSet.add(EnumFoo.a); 1329 result = set.retainAll(anotherSet); 1330 assertTrue("Should return true", result); 1331 assertEquals("size should be 0", 0, set.size()); 1332 1333 // test enum type with more than 64 elements 1334 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 1335 1336 try { 1337 hugeSet.retainAll(null); 1338 fail("Should throw NullPointerException"); 1339 } catch (NullPointerException e) { 1340 // expected 1341 } 1342 1343 hugeSet.clear(); 1344 try { 1345 hugeSet.retainAll(null); 1346 fail("Should throw NullPointerException"); 1347 } catch (NullPointerException e) { 1348 // expected 1349 } 1350 1351 rawCollection = new ArrayList(); 1352 result = hugeSet.retainAll(rawCollection); 1353 assertFalse(result); 1354 1355 rawCollection.add(HugeEnum.a); 1356 result = hugeSet.retainAll(rawCollection); 1357 assertFalse(result); 1358 1359 rawCollection.add(HugeEnumWithInnerClass.a); 1360 result = hugeSet.retainAll(rawCollection); 1361 assertFalse(result); 1362 assertEquals(0, set.size()); 1363 1364 rawCollection.remove(HugeEnum.a); 1365 result = set.retainAll(rawCollection); 1366 assertFalse(result); 1367 1368 Set<HugeEnum> anotherHugeSet = EnumSet.allOf(HugeEnum.class); 1369 result = hugeSet.retainAll(anotherHugeSet); 1370 assertFalse(result); 1371 assertEquals(0, hugeSet.size()); 1372 1373 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 1374 .allOf(HugeEnumWithInnerClass.class); 1375 result = hugeSet.retainAll(hugeSetWithInnerClass); 1376 assertFalse(result); 1377 assertEquals(0, hugeSet.size()); 1378 1379 hugeSetWithInnerClass = EnumSet.noneOf(HugeEnumWithInnerClass.class); 1380 result = hugeSet.retainAll(hugeSetWithInnerClass); 1381 assertFalse(result); 1382 1383 Set<HugeEnumWithInnerClass> hugeSetWithAllInnerClass = EnumSet 1384 .allOf(HugeEnumWithInnerClass.class); 1385 result = hugeSet.retainAll(hugeSetWithAllInnerClass); 1386 assertFalse(result); 1387 1388 hugeSet.add(HugeEnum.a); 1389 result = hugeSet.retainAll(hugeSetWithInnerClass); 1390 assertTrue(result); 1391 assertEquals(0, hugeSet.size()); 1392 1393 hugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1394 hugeSetWithInnerClass.remove(HugeEnumWithInnerClass.f); 1395 Set<HugeEnumWithInnerClass> anotherHugeSetWithInnerClass = EnumSet 1396 .noneOf(HugeEnumWithInnerClass.class); 1397 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.e); 1398 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.f); 1399 1400 result = hugeSetWithInnerClass.retainAll(anotherHugeSetWithInnerClass); 1401 assertTrue(result); 1402 result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.e); 1403 assertTrue("Should contain HugeEnumWithInnerClass.e", result); 1404 result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.b); 1405 assertFalse("Should not contain HugeEnumWithInnerClass.b", result); 1406 assertEquals("Size of hugeSet should be 1:", 1, hugeSetWithInnerClass.size()); 1407 1408 anotherHugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1409 result = hugeSetWithInnerClass.retainAll(anotherHugeSetWithInnerClass); 1410 1411 assertFalse("Return value should be false", result); 1412 1413 rawCollection = new ArrayList(); 1414 rawCollection.add(HugeEnumWithInnerClass.e); 1415 rawCollection.add(HugeEnumWithInnerClass.f); 1416 result = hugeSetWithInnerClass.retainAll(rawCollection); 1417 assertFalse(result); 1418 1419 hugeSet = EnumSet.allOf(HugeEnum.class); 1420 hugeSet.remove(HugeEnum.a); 1421 anotherHugeSet = EnumSet.noneOf(HugeEnum.class); 1422 anotherHugeSet.add(HugeEnum.a); 1423 result = hugeSet.retainAll(anotherHugeSet); 1424 assertTrue(result); 1425 assertEquals(0, hugeSet.size()); 1426 } 1427 1428 /** 1429 * java.util.EnumSet#iterator() 1430 */ 1431 @SuppressWarnings("CollectionIncompatibleType") test_iterator()1432 public void test_iterator() { 1433 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 1434 set.add(EnumFoo.a); 1435 set.add(EnumFoo.b); 1436 1437 Iterator<EnumFoo> iterator = set.iterator(); 1438 Iterator<EnumFoo> anotherIterator = set.iterator(); 1439 assertNotSame("Should not be same", iterator, anotherIterator); 1440 try { 1441 iterator.remove(); 1442 fail("Should throw IllegalStateException"); 1443 } catch (IllegalStateException e) { 1444 // expectedd 1445 } 1446 1447 assertTrue("Should has next element:", iterator.hasNext()); 1448 assertSame("Should be identical", EnumFoo.a, iterator.next()); 1449 iterator.remove(); 1450 assertTrue("Should has next element:", iterator.hasNext()); 1451 assertSame("Should be identical", EnumFoo.b, iterator.next()); 1452 assertFalse("Should not has next element:", iterator.hasNext()); 1453 assertFalse("Should not has next element:", iterator.hasNext()); 1454 1455 assertEquals("Size should be 1:", 1, set.size()); 1456 1457 try { 1458 iterator.next(); 1459 fail("Should throw NoSuchElementException"); 1460 } catch (NoSuchElementException e) { 1461 // expected 1462 } 1463 set = EnumSet.noneOf(EnumFoo.class); 1464 set.add(EnumFoo.a); 1465 iterator = set.iterator(); 1466 assertEquals("Should be equal", EnumFoo.a, iterator.next()); 1467 iterator.remove(); 1468 try { 1469 iterator.remove(); 1470 fail("Should throw IllegalStateException"); 1471 } catch(IllegalStateException e) { 1472 // expected 1473 } 1474 1475 Set<EmptyEnum> emptySet = EnumSet.allOf(EmptyEnum.class); 1476 Iterator<EmptyEnum> emptyIterator = emptySet.iterator(); 1477 try { 1478 emptyIterator.next(); 1479 fail("Should throw NoSuchElementException"); 1480 } catch (NoSuchElementException e) { 1481 // expected 1482 } 1483 1484 Set<EnumWithInnerClass> setWithSubclass = EnumSet 1485 .allOf(EnumWithInnerClass.class); 1486 setWithSubclass.remove(EnumWithInnerClass.e); 1487 Iterator<EnumWithInnerClass> iteratorWithSubclass = setWithSubclass 1488 .iterator(); 1489 assertSame("Should be same", EnumWithInnerClass.a, iteratorWithSubclass.next()); 1490 1491 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1492 assertSame("Should be same", EnumWithInnerClass.b, iteratorWithSubclass.next()); 1493 1494 setWithSubclass.remove(EnumWithInnerClass.c); 1495 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1496 assertSame("Should be same", EnumWithInnerClass.c, iteratorWithSubclass.next()); 1497 1498 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1499 assertSame("Should be same", EnumWithInnerClass.d, iteratorWithSubclass.next()); 1500 1501 setWithSubclass.add(EnumWithInnerClass.e); 1502 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1503 assertSame("Should be same", EnumWithInnerClass.f, iteratorWithSubclass.next()); 1504 1505 set = EnumSet.noneOf(EnumFoo.class); 1506 iterator = set.iterator(); 1507 try { 1508 iterator.next(); 1509 fail("Should throw NoSuchElementException"); 1510 } catch (NoSuchElementException e) { 1511 // expected 1512 } 1513 1514 set.add(EnumFoo.a); 1515 iterator = set.iterator(); 1516 assertEquals("Should return EnumFoo.a", EnumFoo.a, iterator.next()); 1517 assertEquals("Size of set should be 1", 1, set.size()); 1518 iterator.remove(); 1519 assertEquals("Size of set should be 0", 0, set.size()); 1520 assertFalse("Should return false", set.contains(EnumFoo.a)); 1521 1522 set.add(EnumFoo.a); 1523 set.add(EnumFoo.b); 1524 iterator = set.iterator(); 1525 assertEquals("Should be equals", EnumFoo.a, iterator.next()); 1526 iterator.remove(); 1527 try { 1528 iterator.remove(); 1529 fail("Should throw IllegalStateException"); 1530 } catch(IllegalStateException e) { 1531 // expected 1532 } 1533 1534 assertTrue("Should have next element", iterator.hasNext()); 1535 try { 1536 iterator.remove(); 1537 fail("Should throw IllegalStateException"); 1538 } catch (IllegalStateException e) { 1539 // expected 1540 } 1541 assertEquals("Size of set should be 1", 1, set.size()); 1542 assertTrue("Should have next element", iterator.hasNext()); 1543 assertEquals("Should return EnumFoo.b", EnumFoo.b, iterator.next()); 1544 set.remove(EnumFoo.b); 1545 assertEquals("Size of set should be 0", 0, set.size()); 1546 iterator.remove(); 1547 assertFalse("Should return false", set.contains(EnumFoo.a)); 1548 1549 // RI's bug, EnumFoo.b should not exist at the moment. 1550 if (!disableRIBugs) { 1551 assertFalse("Should return false", set.contains(EnumFoo.b)); 1552 } 1553 1554 // test enum type with more than 64 elements 1555 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 1556 hugeSet.add(HugeEnum.a); 1557 hugeSet.add(HugeEnum.b); 1558 1559 Iterator<HugeEnum> hIterator = hugeSet.iterator(); 1560 Iterator<HugeEnum> anotherHugeIterator = hugeSet.iterator(); 1561 assertNotSame(hIterator, anotherHugeIterator); 1562 try { 1563 hIterator.remove(); 1564 fail("Should throw IllegalStateException"); 1565 } catch (IllegalStateException e) { 1566 // expectedd 1567 } 1568 1569 assertTrue(hIterator.hasNext()); 1570 assertSame(HugeEnum.a, hIterator.next()); 1571 hIterator.remove(); 1572 assertTrue(hIterator.hasNext()); 1573 assertSame(HugeEnum.b, hIterator.next()); 1574 assertFalse(hIterator.hasNext()); 1575 assertFalse(hIterator.hasNext()); 1576 1577 assertEquals(1, hugeSet.size()); 1578 1579 try { 1580 hIterator.next(); 1581 fail("Should throw NoSuchElementException"); 1582 } catch (NoSuchElementException e) { 1583 // expected 1584 } 1585 1586 Set<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet 1587 .allOf(HugeEnumWithInnerClass.class); 1588 hugeSetWithSubclass.remove(HugeEnumWithInnerClass.e); 1589 Iterator<HugeEnumWithInnerClass> hugeIteratorWithSubclass = hugeSetWithSubclass 1590 .iterator(); 1591 assertSame(HugeEnumWithInnerClass.a, hugeIteratorWithSubclass.next()); 1592 1593 assertTrue(hugeIteratorWithSubclass.hasNext()); 1594 assertSame(HugeEnumWithInnerClass.b, hugeIteratorWithSubclass.next()); 1595 1596 setWithSubclass.remove(HugeEnumWithInnerClass.c); 1597 assertTrue(hugeIteratorWithSubclass.hasNext()); 1598 assertSame(HugeEnumWithInnerClass.c, hugeIteratorWithSubclass.next()); 1599 1600 assertTrue(hugeIteratorWithSubclass.hasNext()); 1601 assertSame(HugeEnumWithInnerClass.d, hugeIteratorWithSubclass.next()); 1602 1603 hugeSetWithSubclass.add(HugeEnumWithInnerClass.e); 1604 assertTrue(hugeIteratorWithSubclass.hasNext()); 1605 assertSame(HugeEnumWithInnerClass.f, hugeIteratorWithSubclass.next()); 1606 1607 hugeSet = EnumSet.noneOf(HugeEnum.class); 1608 hIterator = hugeSet.iterator(); 1609 try { 1610 hIterator.next(); 1611 fail("Should throw NoSuchElementException"); 1612 } catch (NoSuchElementException e) { 1613 // expected 1614 } 1615 1616 hugeSet.add(HugeEnum.a); 1617 hIterator = hugeSet.iterator(); 1618 assertEquals(HugeEnum.a, hIterator.next()); 1619 assertEquals(1, hugeSet.size()); 1620 hIterator.remove(); 1621 assertEquals(0, hugeSet.size()); 1622 assertFalse(hugeSet.contains(HugeEnum.a)); 1623 1624 hugeSet.add(HugeEnum.a); 1625 hugeSet.add(HugeEnum.b); 1626 hIterator = hugeSet.iterator(); 1627 hIterator.next(); 1628 hIterator.remove(); 1629 1630 assertTrue(hIterator.hasNext()); 1631 try { 1632 hIterator.remove(); 1633 fail("Should throw IllegalStateException"); 1634 } catch (IllegalStateException e) { 1635 // expected 1636 } 1637 assertEquals(1, hugeSet.size()); 1638 assertTrue(hIterator.hasNext()); 1639 assertEquals(HugeEnum.b, hIterator.next()); 1640 hugeSet.remove(HugeEnum.b); 1641 assertEquals(0, hugeSet.size()); 1642 hIterator.remove(); 1643 assertFalse(hugeSet.contains(HugeEnum.a)); 1644 // RI's bug, EnumFoo.b should not exist at the moment. 1645 if(!disableRIBugs) { 1646 assertFalse("Should return false", set.contains(EnumFoo.b)); 1647 } 1648 } 1649 1650 /** 1651 * java.util.EnumSet#of(E) 1652 */ test_Of_E()1653 public void test_Of_E() { 1654 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a); 1655 assertEquals("enumSet should have length 1:", 1, enumSet.size()); 1656 1657 assertTrue("enumSet should contain EnumWithSubclass.a:", 1658 enumSet.contains(EnumWithInnerClass.a)); 1659 1660 try { 1661 EnumSet.of((EnumWithInnerClass) null); 1662 fail("Should throw NullPointerException"); 1663 } catch (NullPointerException npe) { 1664 // expected 1665 } 1666 1667 // test enum type with more than 64 elements 1668 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a); 1669 assertEquals(1, hugeEnumSet.size()); 1670 1671 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1672 } 1673 1674 /** 1675 * java.util.EnumSet#of(E, E) 1676 */ test_Of_EE()1677 public void test_Of_EE() { 1678 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1679 EnumWithInnerClass.b); 1680 assertEquals("enumSet should have length 2:", 2, enumSet.size()); 1681 1682 assertTrue("enumSet should contain EnumWithSubclass.a:", 1683 enumSet.contains(EnumWithInnerClass.a)); 1684 assertTrue("enumSet should contain EnumWithSubclass.b:", 1685 enumSet.contains(EnumWithInnerClass.b)); 1686 1687 try { 1688 EnumSet.of((EnumWithInnerClass) null, EnumWithInnerClass.a); 1689 fail("Should throw NullPointerException"); 1690 } catch (NullPointerException npe) { 1691 // expected 1692 } 1693 1694 try { 1695 EnumSet.of( EnumWithInnerClass.a, (EnumWithInnerClass) null); 1696 fail("Should throw NullPointerException"); 1697 } catch (NullPointerException npe) { 1698 // expected 1699 } 1700 1701 try { 1702 EnumSet.of( (EnumWithInnerClass) null, (EnumWithInnerClass) null); 1703 fail("Should throw NullPointerException"); 1704 } catch (NullPointerException npe) { 1705 // expected 1706 } 1707 1708 enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.a); 1709 assertEquals("Size of enumSet should be 1", 1710 1, enumSet.size()); 1711 1712 // test enum type with more than 64 elements 1713 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1714 HugeEnumWithInnerClass.b); 1715 assertEquals(2, hugeEnumSet.size()); 1716 1717 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1718 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.b)); 1719 1720 try { 1721 EnumSet.of((HugeEnumWithInnerClass) null, HugeEnumWithInnerClass.a); 1722 fail("Should throw NullPointerException"); 1723 } catch (NullPointerException npe) { 1724 // expected 1725 } 1726 1727 try { 1728 EnumSet.of( HugeEnumWithInnerClass.a, (HugeEnumWithInnerClass) null); 1729 fail("Should throw NullPointerException"); 1730 } catch (NullPointerException npe) { 1731 // expected 1732 } 1733 1734 try { 1735 EnumSet.of( (HugeEnumWithInnerClass) null, (HugeEnumWithInnerClass) null); 1736 fail("Should throw NullPointerException"); 1737 } catch (NullPointerException npe) { 1738 // expected 1739 } 1740 1741 hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.a); 1742 assertEquals(1, hugeEnumSet.size()); 1743 } 1744 1745 /** 1746 * java.util.EnumSet#of(E, E, E) 1747 */ test_Of_EEE()1748 public void test_Of_EEE() { 1749 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1750 EnumWithInnerClass.b, EnumWithInnerClass.c); 1751 assertEquals("Size of enumSet should be 3:", 3, enumSet.size()); 1752 1753 assertTrue( 1754 "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); 1755 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); 1756 1757 try { 1758 EnumSet.of((EnumWithInnerClass) null, null, null); 1759 fail("Should throw NullPointerException"); 1760 } catch (NullPointerException npe) { 1761 // expected 1762 } 1763 1764 enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.b, 1765 EnumWithInnerClass.b); 1766 assertEquals("enumSet should contain 2 elements:", 2, enumSet.size()); 1767 1768 // test enum type with more than 64 elements 1769 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1770 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c); 1771 assertEquals(3, hugeEnumSet.size()); 1772 1773 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1774 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.c)); 1775 1776 try { 1777 EnumSet.of((HugeEnumWithInnerClass) null, null, null); 1778 fail("Should throw NullPointerException"); 1779 } catch (NullPointerException npe) { 1780 // expected 1781 } 1782 1783 hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.b, 1784 HugeEnumWithInnerClass.b); 1785 assertEquals(2, hugeEnumSet.size()); 1786 } 1787 1788 /** 1789 * java.util.EnumSet#of(E, E, E, E) 1790 */ test_Of_EEEE()1791 public void test_Of_EEEE() { 1792 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1793 EnumWithInnerClass.b, EnumWithInnerClass.c, 1794 EnumWithInnerClass.d); 1795 assertEquals("Size of enumSet should be 4", 4, enumSet.size()); 1796 1797 assertTrue( 1798 "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); 1799 assertTrue("enumSet should contain EnumWithSubclass.d:", enumSet 1800 .contains(EnumWithInnerClass.d)); 1801 1802 try { 1803 EnumSet.of((EnumWithInnerClass) null, null, null, null); 1804 fail("Should throw NullPointerException"); 1805 } catch (NullPointerException npe) { 1806 // expected 1807 } 1808 1809 // test enum type with more than 64 elements 1810 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1811 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c, 1812 HugeEnumWithInnerClass.d); 1813 assertEquals(4, hugeEnumSet.size()); 1814 1815 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1816 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.d)); 1817 1818 try { 1819 EnumSet.of((HugeEnumWithInnerClass) null, null, null, null); 1820 fail("Should throw NullPointerException"); 1821 } catch (NullPointerException npe) { 1822 // expected 1823 } 1824 } 1825 1826 /** 1827 * java.util.EnumSet#of(E, E, E, E, E) 1828 */ test_Of_EEEEE()1829 public void test_Of_EEEEE() { 1830 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1831 EnumWithInnerClass.b, EnumWithInnerClass.c, 1832 EnumWithInnerClass.d, EnumWithInnerClass.e); 1833 assertEquals("Size of enumSet should be 5:", 5, enumSet.size()); 1834 1835 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); 1836 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.e)); 1837 1838 try { 1839 EnumSet.of((EnumWithInnerClass) null, null, null, null, null); 1840 fail("Should throw NullPointerException"); 1841 } catch (NullPointerException npe) { 1842 // expected 1843 } 1844 1845 // test enum with more than 64 elements 1846 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1847 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c, 1848 HugeEnumWithInnerClass.d, HugeEnumWithInnerClass.e); 1849 assertEquals(5, hugeEnumSet.size()); 1850 1851 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1852 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.e)); 1853 1854 try { 1855 EnumSet.of((HugeEnumWithInnerClass) null, null, null, null, null); 1856 fail("Should throw NullPointerException"); 1857 } catch (NullPointerException npe) { 1858 // expected 1859 } 1860 } 1861 1862 /** 1863 * java.util.EnumSet#of(E, E...) 1864 */ test_Of_EEArray()1865 public void test_Of_EEArray() { 1866 EnumWithInnerClass[] enumArray = new EnumWithInnerClass[] { 1867 EnumWithInnerClass.b, EnumWithInnerClass.c }; 1868 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1869 enumArray); 1870 assertEquals("Should be equal", 3, enumSet.size()); 1871 1872 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); 1873 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); 1874 1875 try { 1876 EnumSet.of(EnumWithInnerClass.a, (EnumWithInnerClass[])null); 1877 fail("Should throw NullPointerException"); 1878 } catch (NullPointerException npe) { 1879 // expected 1880 } 1881 1882 EnumFoo[] foos = {EnumFoo.a, EnumFoo.c, EnumFoo.d}; 1883 EnumSet<EnumFoo> set = EnumSet.of(EnumFoo.c, foos); 1884 assertEquals("size of set should be 1", 3, set.size()); 1885 assertTrue("Should contain EnumFoo.a", set.contains(EnumFoo.a)); 1886 assertTrue("Should contain EnumFoo.c", set.contains(EnumFoo.c)); 1887 assertTrue("Should contain EnumFoo.d", set.contains(EnumFoo.d)); 1888 1889 // test enum type with more than 64 elements 1890 HugeEnumWithInnerClass[] hugeEnumArray = new HugeEnumWithInnerClass[] { 1891 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c }; 1892 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1893 hugeEnumArray); 1894 assertEquals(3, hugeEnumSet.size()); 1895 1896 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1897 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.c)); 1898 1899 try { 1900 EnumSet.of(HugeEnumWithInnerClass.a, (HugeEnumWithInnerClass[])null); 1901 fail("Should throw NullPointerException"); 1902 } catch (NullPointerException npe) { 1903 // expected 1904 } 1905 1906 HugeEnumWithInnerClass[] huges = {HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.c, HugeEnumWithInnerClass.d}; 1907 EnumSet<HugeEnumWithInnerClass> hugeSet = EnumSet.of(HugeEnumWithInnerClass.c, huges); 1908 assertEquals(3, hugeSet.size()); 1909 assertTrue(hugeSet.contains(HugeEnumWithInnerClass.a)); 1910 assertTrue(hugeSet.contains(HugeEnumWithInnerClass.c)); 1911 assertTrue(hugeSet.contains(HugeEnumWithInnerClass.d)); 1912 } 1913 1914 /** 1915 * java.util.EnumSet#range(E, E) 1916 */ test_Range_EE()1917 public void test_Range_EE() { 1918 try { 1919 EnumSet.range(EnumWithInnerClass.c, null); 1920 fail("Should throw NullPointerException"); 1921 } catch (NullPointerException e) { 1922 // expected 1923 } 1924 1925 try { 1926 EnumSet.range(null, EnumWithInnerClass.c); 1927 fail("Should throw NullPointerException"); 1928 } catch (NullPointerException e) { 1929 // expected 1930 } 1931 1932 try { 1933 EnumSet.range(null, (EnumWithInnerClass) null); 1934 fail("Should throw NullPointerException"); 1935 } catch (NullPointerException e) { 1936 // expected 1937 } 1938 1939 try { 1940 EnumSet.range(EnumWithInnerClass.b, EnumWithInnerClass.a); 1941 fail("Should throw IllegalArgumentException"); 1942 } catch (IllegalArgumentException e) { 1943 // expected 1944 } 1945 1946 EnumSet<EnumWithInnerClass> enumSet = EnumSet.range( 1947 EnumWithInnerClass.a, EnumWithInnerClass.a); 1948 assertEquals("Size of enumSet should be 1", 1, enumSet.size()); 1949 1950 enumSet = EnumSet.range( 1951 EnumWithInnerClass.a, EnumWithInnerClass.c); 1952 assertEquals("Size of enumSet should be 3", 3, enumSet.size()); 1953 1954 // test enum with more than 64 elements 1955 try { 1956 EnumSet.range(HugeEnumWithInnerClass.c, null); 1957 fail("Should throw NullPointerException"); 1958 } catch (NullPointerException e) { 1959 // expected 1960 } 1961 1962 try { 1963 EnumSet.range(null, HugeEnumWithInnerClass.c); 1964 fail("Should throw NullPointerException"); 1965 } catch (NullPointerException e) { 1966 // expected 1967 } 1968 1969 try { 1970 EnumSet.range(null, (HugeEnumWithInnerClass) null); 1971 fail("Should throw NullPointerException"); 1972 } catch (NullPointerException e) { 1973 // expected 1974 } 1975 1976 try { 1977 EnumSet.range(HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.a); 1978 fail("Should throw IllegalArgumentException"); 1979 } catch (IllegalArgumentException e) { 1980 // expected 1981 } 1982 1983 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.range( 1984 HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.a); 1985 assertEquals(1, hugeEnumSet.size()); 1986 1987 hugeEnumSet = EnumSet.range( 1988 HugeEnumWithInnerClass.c, HugeEnumWithInnerClass.aa); 1989 assertEquals(51, hugeEnumSet.size()); 1990 1991 hugeEnumSet = EnumSet.range( 1992 HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.mm); 1993 assertEquals(65, hugeEnumSet.size()); 1994 1995 hugeEnumSet = EnumSet.range( 1996 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.mm); 1997 assertEquals(64, hugeEnumSet.size()); 1998 } 1999 2000 /** 2001 * java.util.EnumSet#clone() 2002 */ test_Clone()2003 public void test_Clone() { 2004 EnumSet<EnumFoo> enumSet = EnumSet.allOf(EnumFoo.class); 2005 EnumSet<EnumFoo> clonedEnumSet = enumSet.clone(); 2006 assertEquals(enumSet, clonedEnumSet); 2007 assertNotSame(enumSet, clonedEnumSet); 2008 assertTrue(clonedEnumSet.contains(EnumFoo.a)); 2009 assertTrue(clonedEnumSet.contains(EnumFoo.b)); 2010 assertEquals(64, clonedEnumSet.size()); 2011 2012 // test enum type with more than 64 elements 2013 EnumSet<HugeEnum> hugeEnumSet = EnumSet.allOf(HugeEnum.class); 2014 EnumSet<HugeEnum> hugeClonedEnumSet = hugeEnumSet.clone(); 2015 assertEquals(hugeEnumSet, hugeClonedEnumSet); 2016 assertNotSame(hugeEnumSet, hugeClonedEnumSet); 2017 assertTrue(hugeClonedEnumSet.contains(HugeEnum.a)); 2018 assertTrue(hugeClonedEnumSet.contains(HugeEnum.b)); 2019 assertEquals(65, hugeClonedEnumSet.size()); 2020 2021 hugeClonedEnumSet.remove(HugeEnum.a); 2022 assertEquals(64, hugeClonedEnumSet.size()); 2023 assertFalse(hugeClonedEnumSet.contains(HugeEnum.a)); 2024 assertEquals(65, hugeEnumSet.size()); 2025 assertTrue(hugeEnumSet.contains(HugeEnum.a)); 2026 } 2027 2028 /** 2029 * java.util.EnumSet#Serialization() 2030 */ test_serialization()2031 public void test_serialization() throws Exception { 2032 EnumSet<EnumFoo> set = EnumSet.allOf(EnumFoo.class); 2033 SerializationTest.verifySelf(set); 2034 } 2035 2036 /** 2037 * serialization/deserialization compatibility with RI. 2038 */ 2039 @SuppressWarnings( { "unchecked", "boxing" }) testSerializationCompatibility()2040 public void testSerializationCompatibility() throws Exception { 2041 EnumSet<EnumFoo> set = EnumSet.allOf(EnumFoo.class); 2042 SerializationTest.verifyGolden(this, set); 2043 } 2044 } 2045