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