1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.util;
18
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.EnumSet;
22import java.util.Iterator;
23import java.util.NoSuchElementException;
24import java.util.Set;
25
26import junit.framework.TestCase;
27
28import org.apache.harmony.testframework.serialization.SerializationTest;
29
30public class EnumSetTest extends TestCase {
31
32    static enum EnumWithInnerClass {
33        a, b, c, d, e, f {
34        },
35    }
36
37    enum EnumWithAllInnerClass {
38        a {},
39        b {},
40    }
41
42    static enum EnumFoo {
43        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,
44    }
45
46    static enum EmptyEnum {
47        // expected
48    }
49
50    static enum HugeEnumWithInnerClass {
51        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{},
52    }
53
54    static enum HugeEnum {
55        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,
56    }
57
58    static enum HugeEnumCount {
59        NO1, NO2, NO3, NO4, NO5, NO6, NO7, NO8, NO9, NO10, NO11, NO12, NO13, NO14, NO15, NO16, NO17, NO18, NO19, NO20,
60        NO21, NO22, NO23, NO24, NO25, NO26, NO27, NO28, NO29, NO30, NO31, NO32, NO33, NO34, NO35, NO36, NO37, NO38, NO39, NO40,
61        NO41, NO42, NO43, NO44, NO45, NO46, NO47, NO48, NO49, NO50, NO51, NO52, NO53, NO54, NO55, NO56, NO57, NO58, NO59, NO60,
62        NO61, NO62, NO63, NO64, NO65, NO66, NO67, NO68, NO69, NO70, NO71, NO72, NO73, NO74, NO75, NO76, NO77, NO78, NO79, NO80,
63        NO81, NO82, NO83, NO84, NO85, NO86, NO87, NO88, NO89, NO90, NO91, NO92, NO93, NO94, NO95, NO96, NO97, NO98, NO99, NO100,
64        NO101, NO102, NO103, NO104, NO105, NO106, NO107, NO108, NO109, NO110, NO111, NO112, NO113, NO114, NO115, NO116, NO117, NO118, NO119, NO120,
65        NO121, NO122, NO123, NO124, NO125, NO126, NO127, NO128, NO129, NO130,
66    }
67
68
69    /**
70     * @tests 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"); //$NON-NLS-1$
77        } catch (NullPointerException e) {
78            // expected
79        }
80
81        try {
82            EnumSet.noneOf(Enum.class);
83            fail("Should throw ClassCastException"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
107        } catch (ClassCastException e) {
108            // expected
109        }
110
111        EnumSet<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet
112            .noneOf(HugeEnumWithInnerClass.class);
113        assertNotNull(hugeSetWithInnerClass);
114    }
115
116    /**
117     * @tests 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     * @tests 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"); //$NON-NLS-1$
167        } catch (NullPointerException e) {
168            // expected
169        }
170
171        try {
172            EnumSet.allOf(Enum.class);
173            fail("Should throw ClassCastException"); //$NON-NLS-1$
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()); //$NON-NLS-1$
180
181        assertFalse(
182                "enumSet should not contain null value", enumSet.contains(null)); //$NON-NLS-1$
183        assertTrue(
184                "enumSet should contain EnumFoo.a", enumSet.contains(EnumFoo.a)); //$NON-NLS-1$
185        assertTrue(
186                "enumSet should contain EnumFoo.b", enumSet.contains(EnumFoo.b)); //$NON-NLS-1$
187
188        enumSet.add(EnumFoo.a);
189        assertEquals("Should be equal", 64, enumSet.size()); //$NON-NLS-1$
190
191        EnumSet<EnumFoo> anotherSet = EnumSet.allOf(EnumFoo.class);
192        assertEquals("Should be equal", enumSet, anotherSet); //$NON-NLS-1$
193        assertNotSame("Should not be identical", enumSet, anotherSet); //$NON-NLS-1$
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     * @tests 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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
233        } catch (ClassCastException e) {
234            // expected
235        }
236
237        set.clear();
238        try {
239            set.add(null);
240            fail("Should throw NullPointerException"); //$NON-NLS-1$
241        } catch (NullPointerException e) {
242            // expected
243        }
244
245        boolean result = set.add(EnumFoo.a);
246        assertEquals("Size should be 1:", 1, set.size()); //$NON-NLS-1$
247        assertTrue("Return value should be true", result); //$NON-NLS-1$
248
249        result = set.add(EnumFoo.a);
250        assertEquals("Size should be 1:", 1, set.size()); //$NON-NLS-1$
251        assertFalse("Return value should be false", result); //$NON-NLS-1$
252
253        set.add(EnumFoo.b);
254        assertEquals("Size should be 2:", 2, set.size()); //$NON-NLS-1$
255
256        rawSet = set;
257        try {
258            rawSet.add(EnumWithAllInnerClass.a);
259            fail("Should throw ClassCastException"); //$NON-NLS-1$
260        } catch(ClassCastException e) {
261            // expected
262        }
263
264        try {
265            rawSet.add(EnumWithInnerClass.a);
266            fail("Should throw ClassCastException"); //$NON-NLS-1$
267        } catch(ClassCastException e) {
268            // expected
269        }
270
271        try {
272            rawSet.add(new Object());
273            fail("Should throw ClassCastException"); //$NON-NLS-1$
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"); //$NON-NLS-1$
289        } catch (NullPointerException e) {
290            // expected
291        }
292
293        rawSet = hugeSet;
294        try {
295            rawSet.add(HugeEnumWithInnerClass.b);
296            fail("Should throw ClassCastException"); //$NON-NLS-1$
297        } catch (ClassCastException e) {
298            // expected
299        }
300
301        try {
302            rawSet.add(new Object());
303            fail("Should throw ClassCastException"); //$NON-NLS-1$
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     * @tests 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()); //$NON-NLS-1$
324
325        try {
326            set.addAll(null);
327            fail("Should throw NullPointerException"); //$NON-NLS-1$
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); //$NON-NLS-1$
345        assertEquals("Size should be 2:", 2, set.size()); //$NON-NLS-1$
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"); //$NON-NLS-1$
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); //$NON-NLS-1$
365        assertEquals("Size of set should be 2", 2, set.size()); //$NON-NLS-1$
366
367        try {
368            fullSet.addAll(null);
369            fail("Should throw NullPointerException"); //$NON-NLS-1$
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"); //$NON-NLS-1$
382        } catch (ClassCastException e) {
383            // expected
384        }
385        Set<EnumWithInnerClass> setWithSubclass = fullSetWithSubclass;
386        result = setWithSubclass.addAll(setWithSubclass);
387        assertFalse("Should return false", result); //$NON-NLS-1$
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); //$NON-NLS-1$
397
398        anotherSetWithSubclass.remove(EnumWithInnerClass.a);
399        result = setWithSubclass.addAll(anotherSetWithSubclass);
400        assertFalse("Should return false", result); //$NON-NLS-1$
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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     * @tests java.util.EnumSet#remove(Object)
476     */
477    public void test_remove_LObject() {
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); //$NON-NLS-1$
486
487        result = set.remove(EnumFoo.a);
488        assertTrue("Should return true", result); //$NON-NLS-1$
489        result = set.remove(EnumFoo.a);
490        assertFalse("Should return false", result); //$NON-NLS-1$
491
492        assertEquals("Size of set should be 63:", 63, set.size()); //$NON-NLS-1$
493
494        result = set.remove(EnumWithInnerClass.a);
495        assertFalse("Should return false", result); //$NON-NLS-1$
496        result = set.remove(EnumWithInnerClass.f);
497        assertFalse("Should return false", result); //$NON-NLS-1$
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); //$NON-NLS-1$
504
505        result = hugeSet.remove(HugeEnum.a);
506        assertTrue("Should return true", result); //$NON-NLS-1$
507        result = hugeSet.remove(HugeEnum.a);
508        assertFalse("Should return false", result); //$NON-NLS-1$
509
510        assertEquals("Size of set should be 64:", 64, hugeSet.size()); //$NON-NLS-1$
511
512        result = hugeSet.remove(HugeEnumWithInnerClass.a);
513        assertFalse("Should return false", result); //$NON-NLS-1$
514        result = hugeSet.remove(HugeEnumWithInnerClass.f);
515        assertFalse("Should return false", result); //$NON-NLS-1$
516    }
517
518    /**
519     * @tests 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)); //$NON-NLS-1$
529        assertFalse(
530                "Should return false", set.equals(new Object())); //$NON-NLS-1$
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)); //$NON-NLS-1$
538
539        anotherSet.remove(EnumFoo.a);
540        assertFalse(
541                "Should return false", set.equals(anotherSet)); //$NON-NLS-1$
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)); //$NON-NLS-1$
552
553        setWithInnerClass.clear();
554        set.clear();
555        assertTrue("Should be equal", set.equals(setWithInnerClass)); //$NON-NLS-1$
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     * @tests 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()); //$NON-NLS-1$
585
586        set.clear();
587
588        assertEquals("Size should be 0", 0, set.size()); //$NON-NLS-1$
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     * @tests 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()); //$NON-NLS-1$
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()); //$NON-NLS-1$
617    }
618
619    /**
620     * @tests 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"); //$NON-NLS-1$
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()); //$NON-NLS-1$
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()); //$NON-NLS-1$
643        assertTrue("complementOfE should contain EnumWithSubclass.a:", //$NON-NLS-1$
644                complementOfE.contains(EnumWithInnerClass.a));
645        assertTrue("complementOfE should contain EnumWithSubclass.b:", //$NON-NLS-1$
646                complementOfE.contains(EnumWithInnerClass.b));
647        assertTrue("complementOfE should contain EnumWithSubclass.c:", //$NON-NLS-1$
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"); //$NON-NLS-1$
664        } catch (NullPointerException npe) {
665            // expected
666        }
667    }
668
669    /**
670     * @tests 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); //$NON-NLS-1$
680
681        result = set.contains(EnumFoo.a);
682        assertTrue("Should contain EnumFoo.a", result); //$NON-NLS-1$
683        result = set.contains(EnumFoo.ll);
684        assertTrue("Should contain EnumFoo.ll", result); //$NON-NLS-1$
685
686        result = set.contains(EnumFoo.b);
687        assertTrue("Should contain EnumFoo.b", result); //$NON-NLS-1$
688
689        result = set.contains(new Object());
690        assertFalse("Should not contain Object instance", result); //$NON-NLS-1$
691
692        result = set.contains(EnumWithInnerClass.a);
693        assertFalse("Should not contain EnumWithSubclass.a", result); //$NON-NLS-1$
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()); //$NON-NLS-1$
701        assertTrue("set should contain EnumFoo.aa", set.contains(EnumFoo.aa)); //$NON-NLS-1$
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); //$NON-NLS-1$
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     * @tests 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"); //$NON-NLS-1$
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); //$NON-NLS-1$
764
765        Collection rawCollection = new ArrayList();
766        result = set.containsAll(rawCollection);
767        assertTrue("Should contain empty collection:", result); //$NON-NLS-1$
768
769        rawCollection.add(1);
770        result = set.containsAll(rawCollection);
771        assertFalse("Should return false", result); //$NON-NLS-1$
772
773        rawCollection.add(EnumWithInnerClass.a);
774        result = set.containsAll(rawCollection);
775        assertFalse("Should return false", result); //$NON-NLS-1$
776
777        EnumSet rawSet = EnumSet.noneOf(EnumFoo.class);
778        result = set.containsAll(rawSet);
779        assertTrue("Should contain empty set", result); //$NON-NLS-1$
780
781        emptySet = EnumSet.noneOf(EmptyEnum.class);
782        result = set.containsAll(emptySet);
783        assertTrue("No class cast should be performed on empty set", result); //$NON-NLS-1$
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); //$NON-NLS-1$
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); //$NON-NLS-1$
794
795        set.clear();
796        try {
797            set.containsAll(null);
798            fail("Should throw NullPointerException"); //$NON-NLS-1$
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); //$NON-NLS-1$
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); //$NON-NLS-1$
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"); //$NON-NLS-1$
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); //$NON-NLS-1$
848
849        rawCollection.add(1);
850        result = hugeSet.containsAll(rawCollection);
851        assertFalse("Should return false", result); //$NON-NLS-1$
852
853        rawCollection.add(EnumWithInnerClass.a);
854        result = set.containsAll(rawCollection);
855        assertFalse("Should return false", result); //$NON-NLS-1$
856
857        rawSet = EnumSet.noneOf(HugeEnum.class);
858        result = hugeSet.containsAll(rawSet);
859        assertTrue("Should contain empty set", result); //$NON-NLS-1$
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); //$NON-NLS-1$
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); //$NON-NLS-1$
870
871        hugeSet.clear();
872        try {
873            hugeSet.containsAll(null);
874            fail("Should throw NullPointerException"); //$NON-NLS-1$
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); //$NON-NLS-1$
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); //$NON-NLS-1$
889    }
890
891    /**
892     * @tests 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"); //$NON-NLS-1$
899        } catch (NullPointerException npe) {
900            // expected
901        }
902
903        Collection collection = new ArrayList();
904        try {
905            EnumSet.copyOf(collection);
906            fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
907        } catch (IllegalArgumentException e) {
908            // expected
909        }
910
911        collection.add(new Object());
912        try {
913            EnumSet.copyOf(collection);
914            fail("Should throw ClassCastException"); //$NON-NLS-1$
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:", //$NON-NLS-1$
924                1, copyOfEnumCollection.size());
925        assertTrue("copyOfEnumCollection should contain EnumFoo.b:", //$NON-NLS-1$
926                copyOfEnumCollection.contains(EnumFoo.b));
927
928        enumCollection.add(null);
929        assertEquals("Size of enumCollection should be 2:", //$NON-NLS-1$
930                2, enumCollection.size());
931
932        try {
933            copyOfEnumCollection = EnumSet.copyOf(enumCollection);
934            fail("Should throw NullPointerException"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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"); //$NON-NLS-1$
973        } catch(ClassCastException e) {
974            // expected
975        }
976    }
977
978    /**
979     * @tests 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", //$NON-NLS-1$
989                enumSet.size(), copyOfE.size());
990
991        assertTrue("EnumWithSubclass.a should be contained in copyOfE", //$NON-NLS-1$
992                copyOfE.contains(EnumWithInnerClass.a));
993        assertTrue("EnumWithSubclass.f should be contained in copyOfE", //$NON-NLS-1$
994                copyOfE.contains(EnumWithInnerClass.f));
995
996        Object[] enumValue = copyOfE.toArray();
997        assertSame("enumValue[0] should be identical with EnumWithSubclass.a", //$NON-NLS-1$
998                enumValue[0], EnumWithInnerClass.a);
999        assertSame("enumValue[1] should be identical with EnumWithSubclass.f", //$NON-NLS-1$
1000                enumValue[1], EnumWithInnerClass.f);
1001
1002        try {
1003            EnumSet.copyOf((EnumSet) null);
1004            fail("Should throw NullPointerException"); //$NON-NLS-1$
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     * @tests 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"); //$NON-NLS-1$
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()); //$NON-NLS-1$
1040
1041        try {
1042            set.removeAll(null);
1043            fail("Should throw NullPointerException"); //$NON-NLS-1$
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); //$NON-NLS-1$
1053        assertEquals("Size of set should be 63", 63, set.size()); //$NON-NLS-1$
1054
1055        collection = new ArrayList();
1056        result = set.removeAll(collection);
1057        assertFalse("Should return false", result); //$NON-NLS-1$
1058
1059        Set<EmptyEnum> emptySet = EnumSet.noneOf(EmptyEnum.class);
1060        result = set.removeAll(emptySet);
1061        assertFalse("Should return false", result); //$NON-NLS-1$
1062
1063        EnumSet<EnumFoo> emptyFooSet = EnumSet.noneOf(EnumFoo.class);
1064        result = set.removeAll(emptyFooSet);
1065        assertFalse("Should return false", result); //$NON-NLS-1$
1066
1067        emptyFooSet.add(EnumFoo.a);
1068        result = set.removeAll(emptyFooSet);
1069        assertFalse("Should return false", result); //$NON-NLS-1$
1070
1071        Set<EnumWithInnerClass> setWithSubclass = EnumSet
1072                .noneOf(EnumWithInnerClass.class);
1073        result = set.removeAll(setWithSubclass);
1074        assertFalse("Should return false", result); //$NON-NLS-1$
1075
1076        setWithSubclass.add(EnumWithInnerClass.a);
1077        result = set.removeAll(setWithSubclass);
1078        assertFalse("Should return false", result); //$NON-NLS-1$
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); //$NON-NLS-1$
1086        assertEquals("Size of set should be 63:", 63, set.size()); //$NON-NLS-1$
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); //$NON-NLS-1$
1099
1100        anotherSetWithInnerClass.add(EnumWithInnerClass.a);
1101        result = anotherSetWithInnerClass.removeAll(setWithInnerClass);
1102        assertTrue("Should return true", result); //$NON-NLS-1$
1103        assertEquals("Size of anotherSetWithInnerClass should remain 2", //$NON-NLS-1$
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); //$NON-NLS-1$
1110
1111        Set rawSet = EnumSet.allOf(EnumWithAllInnerClass.class);
1112        result = rawSet.removeAll(EnumSet.allOf(EnumFoo.class));
1113        assertFalse("Should return false", result); //$NON-NLS-1$
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); //$NON-NLS-1$
1121        assertEquals("Size of setWithInnerClass should be 1", 1, setWithInnerClass.size()); //$NON-NLS-1$
1122
1123        result = setWithInnerClass.contains(EnumWithInnerClass.f);
1124        assertTrue("Should return true", result); //$NON-NLS-1$
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); //$NON-NLS-1$
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     * @tests 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"); //$NON-NLS-1$
1209        } catch (NullPointerException e) {
1210            // expected
1211        }
1212
1213        set.clear();
1214        boolean result = set.retainAll(null);
1215        assertFalse("Should return false", result); //$NON-NLS-1$
1216
1217        Collection rawCollection = new ArrayList();
1218        result = set.retainAll(rawCollection);
1219        assertFalse("Should return false", result); //$NON-NLS-1$
1220
1221        rawCollection.add(EnumFoo.a);
1222        result = set.retainAll(rawCollection);
1223        assertFalse("Should return false", result); //$NON-NLS-1$
1224
1225        rawCollection.add(EnumWithInnerClass.a);
1226        result = set.retainAll(rawCollection);
1227        assertFalse("Should return false", result); //$NON-NLS-1$
1228        assertEquals("Size of set should be 0:", 0, set.size()); //$NON-NLS-1$
1229
1230        rawCollection.remove(EnumFoo.a);
1231        result = set.retainAll(rawCollection);
1232        assertFalse("Should return false", result); //$NON-NLS-1$
1233
1234        Set<EnumFoo> anotherSet = EnumSet.allOf(EnumFoo.class);
1235        result = set.retainAll(anotherSet);
1236        assertFalse("Should return false", result); //$NON-NLS-1$
1237        assertEquals("Size of set should be 0", 0, set.size()); //$NON-NLS-1$
1238
1239        Set<EnumWithInnerClass> setWithInnerClass = EnumSet
1240                .allOf(EnumWithInnerClass.class);
1241        result = set.retainAll(setWithInnerClass);
1242        assertFalse("Should return false", result); //$NON-NLS-1$
1243        assertEquals("Size of set should be 0", 0, set.size()); //$NON-NLS-1$
1244
1245        setWithInnerClass = EnumSet.noneOf(EnumWithInnerClass.class);
1246        result = set.retainAll(setWithInnerClass);
1247        assertFalse("Should return false", result); //$NON-NLS-1$
1248
1249        Set<EmptyEnum> emptySet = EnumSet.allOf(EmptyEnum.class);
1250        result = set.retainAll(emptySet);
1251        assertFalse("Should return false", result); //$NON-NLS-1$
1252
1253        Set<EnumWithAllInnerClass> setWithAllInnerClass = EnumSet
1254                .allOf(EnumWithAllInnerClass.class);
1255        result = set.retainAll(setWithAllInnerClass);
1256        assertFalse("Should return false", result); //$NON-NLS-1$
1257
1258        set.add(EnumFoo.a);
1259        result = set.retainAll(setWithInnerClass);
1260        assertTrue("Should return true", result); //$NON-NLS-1$
1261        assertEquals("Size of set should be 0", 0, set.size()); //$NON-NLS-1$
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); //$NON-NLS-1$
1272        result = setWithInnerClass.contains(EnumWithInnerClass.e);
1273        assertTrue("Should contain EnumWithInnerClass.e", result); //$NON-NLS-1$
1274        result = setWithInnerClass.contains(EnumWithInnerClass.b);
1275        assertFalse("Should not contain EnumWithInnerClass.b", result); //$NON-NLS-1$
1276        assertEquals("Size of set should be 1:", 1, setWithInnerClass.size()); //$NON-NLS-1$
1277
1278        anotherSetWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class);
1279        result = setWithInnerClass.retainAll(anotherSetWithInnerClass);
1280
1281        assertFalse("Return value should be false", result); //$NON-NLS-1$
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); //$NON-NLS-1$
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); //$NON-NLS-1$
1295        assertEquals("size should be 0", 0, set.size()); //$NON-NLS-1$
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"); //$NON-NLS-1$
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); //$NON-NLS-1$
1364        result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.b);
1365        assertFalse("Should not contain HugeEnumWithInnerClass.b", result); //$NON-NLS-1$
1366        assertEquals("Size of hugeSet should be 1:", 1, hugeSetWithInnerClass.size()); //$NON-NLS-1$
1367
1368        anotherHugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class);
1369        result = hugeSetWithInnerClass.retainAll(anotherHugeSetWithInnerClass);
1370
1371        assertFalse("Return value should be false", result); //$NON-NLS-1$
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     * @tests 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); //$NON-NLS-1$
1399        try {
1400            iterator.remove();
1401            fail("Should throw IllegalStateException"); //$NON-NLS-1$
1402        } catch (IllegalStateException e) {
1403            // expectedd
1404        }
1405
1406        assertTrue("Should has next element:", iterator.hasNext()); //$NON-NLS-1$
1407        assertSame("Should be identical", EnumFoo.a, iterator.next()); //$NON-NLS-1$
1408        iterator.remove();
1409        assertTrue("Should has next element:", iterator.hasNext()); //$NON-NLS-1$
1410        assertSame("Should be identical", EnumFoo.b, iterator.next()); //$NON-NLS-1$
1411        assertFalse("Should not has next element:", iterator.hasNext()); //$NON-NLS-1$
1412        assertFalse("Should not has next element:", iterator.hasNext()); //$NON-NLS-1$
1413
1414        assertEquals("Size should be 1:", 1, set.size()); //$NON-NLS-1$
1415
1416        try {
1417            iterator.next();
1418            fail("Should throw NoSuchElementException"); //$NON-NLS-1$
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()); //$NON-NLS-1$
1426        iterator.remove();
1427        try {
1428            iterator.remove();
1429            fail("Should throw IllegalStateException"); //$NON-NLS-1$
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"); //$NON-NLS-1$
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()); //$NON-NLS-1$
1449
1450        assertTrue("Should return true", iteratorWithSubclass.hasNext()); //$NON-NLS-1$
1451        assertSame("Should be same", EnumWithInnerClass.b, iteratorWithSubclass.next()); //$NON-NLS-1$
1452
1453        setWithSubclass.remove(EnumWithInnerClass.c);
1454        assertTrue("Should return true", iteratorWithSubclass.hasNext()); //$NON-NLS-1$
1455        assertSame("Should be same", EnumWithInnerClass.c, iteratorWithSubclass.next()); //$NON-NLS-1$
1456
1457        assertTrue("Should return true", iteratorWithSubclass.hasNext()); //$NON-NLS-1$
1458        assertSame("Should be same", EnumWithInnerClass.d, iteratorWithSubclass.next()); //$NON-NLS-1$
1459
1460        setWithSubclass.add(EnumWithInnerClass.e);
1461        assertTrue("Should return true", iteratorWithSubclass.hasNext()); //$NON-NLS-1$
1462        assertSame("Should be same", EnumWithInnerClass.f, iteratorWithSubclass.next()); //$NON-NLS-1$
1463
1464        set = EnumSet.noneOf(EnumFoo.class);
1465        iterator = set.iterator();
1466        try {
1467            iterator.next();
1468            fail("Should throw NoSuchElementException"); //$NON-NLS-1$
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()); //$NON-NLS-1$
1476        assertEquals("Size of set should be 1", 1, set.size()); //$NON-NLS-1$
1477        iterator.remove();
1478        assertEquals("Size of set should be 0", 0, set.size()); //$NON-NLS-1$
1479        assertFalse("Should return false", set.contains(EnumFoo.a)); //$NON-NLS-1$
1480
1481        set.add(EnumFoo.a);
1482        set.add(EnumFoo.b);
1483        iterator = set.iterator();
1484        assertEquals("Should be equals", EnumFoo.a, iterator.next()); //$NON-NLS-1$
1485        iterator.remove();
1486        try {
1487            iterator.remove();
1488            fail("Should throw IllegalStateException"); //$NON-NLS-1$
1489        } catch(IllegalStateException e) {
1490            // expected
1491        }
1492
1493        assertTrue("Should have next element", iterator.hasNext()); //$NON-NLS-1$
1494        try {
1495            iterator.remove();
1496            fail("Should throw IllegalStateException"); //$NON-NLS-1$
1497        } catch (IllegalStateException e) {
1498            // expected
1499        }
1500        assertEquals("Size of set should be 1", 1, set.size()); //$NON-NLS-1$
1501        assertTrue("Should have next element", iterator.hasNext()); //$NON-NLS-1$
1502        assertEquals("Should return EnumFoo.b", EnumFoo.b, iterator.next()); //$NON-NLS-1$
1503        set.remove(EnumFoo.b);
1504        assertEquals("Size of set should be 0", 0, set.size()); //$NON-NLS-1$
1505        iterator.remove();
1506        assertFalse("Should return false", set.contains(EnumFoo.a)); //$NON-NLS-1$
1507
1508        // RI's bug, EnumFoo.b should not exist at the moment.
1509        assertFalse("Should return false", set.contains(EnumFoo.b)); //$NON-NLS-1$
1510
1511        // test enum type with more than 64 elements
1512        Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class);
1513        hugeSet.add(HugeEnum.a);
1514        hugeSet.add(HugeEnum.b);
1515
1516        Iterator<HugeEnum> hIterator = hugeSet.iterator();
1517        Iterator<HugeEnum> anotherHugeIterator = hugeSet.iterator();
1518        assertNotSame(hIterator, anotherHugeIterator);
1519        try {
1520            hIterator.remove();
1521            fail("Should throw IllegalStateException"); //$NON-NLS-1$
1522        } catch (IllegalStateException e) {
1523            // expectedd
1524        }
1525
1526        assertTrue(hIterator.hasNext());
1527        assertSame(HugeEnum.a, hIterator.next());
1528        hIterator.remove();
1529        assertTrue(hIterator.hasNext());
1530        assertSame(HugeEnum.b, hIterator.next());
1531        assertFalse(hIterator.hasNext());
1532        assertFalse(hIterator.hasNext());
1533
1534        assertEquals(1, hugeSet.size());
1535
1536        try {
1537            hIterator.next();
1538            fail("Should throw NoSuchElementException"); //$NON-NLS-1$
1539        } catch (NoSuchElementException e) {
1540            // expected
1541        }
1542
1543        Set<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet
1544                .allOf(HugeEnumWithInnerClass.class);
1545        hugeSetWithSubclass.remove(HugeEnumWithInnerClass.e);
1546        Iterator<HugeEnumWithInnerClass> hugeIteratorWithSubclass = hugeSetWithSubclass
1547                .iterator();
1548        assertSame(HugeEnumWithInnerClass.a, hugeIteratorWithSubclass.next());
1549
1550        assertTrue(hugeIteratorWithSubclass.hasNext());
1551        assertSame(HugeEnumWithInnerClass.b, hugeIteratorWithSubclass.next());
1552
1553        setWithSubclass.remove(HugeEnumWithInnerClass.c);
1554        assertTrue(hugeIteratorWithSubclass.hasNext());
1555        assertSame(HugeEnumWithInnerClass.c, hugeIteratorWithSubclass.next());
1556
1557        assertTrue(hugeIteratorWithSubclass.hasNext());
1558        assertSame(HugeEnumWithInnerClass.d, hugeIteratorWithSubclass.next());
1559
1560        hugeSetWithSubclass.add(HugeEnumWithInnerClass.e);
1561        assertTrue(hugeIteratorWithSubclass.hasNext());
1562        assertSame(HugeEnumWithInnerClass.f, hugeIteratorWithSubclass.next());
1563
1564        hugeSet = EnumSet.noneOf(HugeEnum.class);
1565        hIterator = hugeSet.iterator();
1566        try {
1567            hIterator.next();
1568            fail("Should throw NoSuchElementException"); //$NON-NLS-1$
1569        } catch (NoSuchElementException e) {
1570            // expected
1571        }
1572
1573        hugeSet.add(HugeEnum.a);
1574        hIterator = hugeSet.iterator();
1575        assertEquals(HugeEnum.a, hIterator.next());
1576        assertEquals(1, hugeSet.size());
1577        hIterator.remove();
1578        assertEquals(0, hugeSet.size());
1579        assertFalse(hugeSet.contains(HugeEnum.a));
1580
1581        hugeSet.add(HugeEnum.a);
1582        hugeSet.add(HugeEnum.b);
1583        hIterator = hugeSet.iterator();
1584        hIterator.next();
1585        hIterator.remove();
1586
1587        assertTrue(hIterator.hasNext());
1588        try {
1589            hIterator.remove();
1590            fail("Should throw IllegalStateException"); //$NON-NLS-1$
1591        } catch (IllegalStateException e) {
1592            // expected
1593        }
1594        assertEquals(1, hugeSet.size());
1595        assertTrue(hIterator.hasNext());
1596        assertEquals(HugeEnum.b, hIterator.next());
1597        hugeSet.remove(HugeEnum.b);
1598        assertEquals(0, hugeSet.size());
1599        hIterator.remove();
1600        assertFalse(hugeSet.contains(HugeEnum.a));
1601        // RI's bug, EnumFoo.b should not exist at the moment.
1602        assertFalse("Should return false", set.contains(EnumFoo.b)); //$NON-NLS-1$
1603
1604        // Regression for HARMONY-4728
1605        hugeSet = EnumSet.allOf(HugeEnum.class);
1606        hIterator = hugeSet.iterator();
1607        for( int i = 0; i < 63; i++) {
1608            hIterator.next();
1609        }
1610        assertSame(HugeEnum.ll, hIterator.next());
1611    }
1612
1613    /**
1614     * @tests java.util.EnumSet#of(E)
1615     */
1616    public void test_Of_E() {
1617        EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a);
1618        assertEquals("enumSet should have length 1:", 1, enumSet.size()); //$NON-NLS-1$
1619
1620        assertTrue("enumSet should contain EnumWithSubclass.a:", //$NON-NLS-1$
1621                enumSet.contains(EnumWithInnerClass.a));
1622
1623        try {
1624            EnumSet.of((EnumWithInnerClass) null);
1625            fail("Should throw NullPointerException"); //$NON-NLS-1$
1626        } catch (NullPointerException npe) {
1627            // expected
1628        }
1629
1630        // test enum type with more than 64 elements
1631        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a);
1632        assertEquals(1, hugeEnumSet.size());
1633
1634        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a));
1635    }
1636
1637    /**
1638     * @tests java.util.EnumSet#of(E, E)
1639     */
1640    public void test_Of_EE() {
1641        EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
1642                EnumWithInnerClass.b);
1643        assertEquals("enumSet should have length 2:", 2, enumSet.size()); //$NON-NLS-1$
1644
1645        assertTrue("enumSet should contain EnumWithSubclass.a:", //$NON-NLS-1$
1646                enumSet.contains(EnumWithInnerClass.a));
1647        assertTrue("enumSet should contain EnumWithSubclass.b:", //$NON-NLS-1$
1648                enumSet.contains(EnumWithInnerClass.b));
1649
1650        try {
1651            EnumSet.of((EnumWithInnerClass) null, EnumWithInnerClass.a);
1652            fail("Should throw NullPointerException"); //$NON-NLS-1$
1653        } catch (NullPointerException npe) {
1654            // expected
1655        }
1656
1657        try {
1658            EnumSet.of( EnumWithInnerClass.a, (EnumWithInnerClass) null);
1659            fail("Should throw NullPointerException"); //$NON-NLS-1$
1660        } catch (NullPointerException npe) {
1661            // expected
1662        }
1663
1664        try {
1665            EnumSet.of( (EnumWithInnerClass) null, (EnumWithInnerClass) null);
1666            fail("Should throw NullPointerException"); //$NON-NLS-1$
1667        } catch (NullPointerException npe) {
1668            // expected
1669        }
1670
1671        enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.a);
1672        assertEquals("Size of enumSet should be 1", //$NON-NLS-1$
1673                1, enumSet.size());
1674
1675        // test enum type with more than 64 elements
1676        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a,
1677                HugeEnumWithInnerClass.b);
1678        assertEquals(2, hugeEnumSet.size());
1679
1680        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a));
1681        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.b));
1682
1683        try {
1684            EnumSet.of((HugeEnumWithInnerClass) null, HugeEnumWithInnerClass.a);
1685            fail("Should throw NullPointerException"); //$NON-NLS-1$
1686        } catch (NullPointerException npe) {
1687            // expected
1688        }
1689
1690        try {
1691            EnumSet.of( HugeEnumWithInnerClass.a, (HugeEnumWithInnerClass) null);
1692            fail("Should throw NullPointerException"); //$NON-NLS-1$
1693        } catch (NullPointerException npe) {
1694            // expected
1695        }
1696
1697        try {
1698            EnumSet.of( (HugeEnumWithInnerClass) null, (HugeEnumWithInnerClass) null);
1699            fail("Should throw NullPointerException"); //$NON-NLS-1$
1700        } catch (NullPointerException npe) {
1701            // expected
1702        }
1703
1704        hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.a);
1705        assertEquals(1, hugeEnumSet.size());
1706    }
1707
1708    /**
1709     * @tests java.util.EnumSet#of(E, E, E)
1710     */
1711    public void test_Of_EEE() {
1712        EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
1713                EnumWithInnerClass.b, EnumWithInnerClass.c);
1714        assertEquals("Size of enumSet should be 3:", 3, enumSet.size()); //$NON-NLS-1$
1715
1716        assertTrue(
1717                "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
1718        assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); //$NON-NLS-1$
1719
1720        try {
1721            EnumSet.of((EnumWithInnerClass) null, null, null);
1722            fail("Should throw NullPointerException"); //$NON-NLS-1$
1723        } catch (NullPointerException npe) {
1724            // expected
1725        }
1726
1727        enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.b,
1728                EnumWithInnerClass.b);
1729        assertEquals("enumSet should contain 2 elements:", 2, enumSet.size()); //$NON-NLS-1$
1730
1731        // test enum type with more than 64 elements
1732        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a,
1733                HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c);
1734        assertEquals(3, hugeEnumSet.size());
1735
1736        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a));
1737        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.c));
1738
1739        try {
1740            EnumSet.of((HugeEnumWithInnerClass) null, null, null);
1741            fail("Should throw NullPointerException"); //$NON-NLS-1$
1742        } catch (NullPointerException npe) {
1743            // expected
1744        }
1745
1746        hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.b,
1747                HugeEnumWithInnerClass.b);
1748        assertEquals(2, hugeEnumSet.size());
1749    }
1750
1751    /**
1752     * @tests java.util.EnumSet#of(E, E, E, E)
1753     */
1754    public void test_Of_EEEE() {
1755        EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
1756                EnumWithInnerClass.b, EnumWithInnerClass.c,
1757                EnumWithInnerClass.d);
1758        assertEquals("Size of enumSet should be 4", 4, enumSet.size()); //$NON-NLS-1$
1759
1760        assertTrue(
1761                "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
1762        assertTrue("enumSet should contain EnumWithSubclass.d:", enumSet //$NON-NLS-1$
1763                .contains(EnumWithInnerClass.d));
1764
1765        try {
1766            EnumSet.of((EnumWithInnerClass) null, null, null, null);
1767            fail("Should throw NullPointerException"); //$NON-NLS-1$
1768        } catch (NullPointerException npe) {
1769            // expected
1770        }
1771
1772        // test enum type with more than 64 elements
1773        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a,
1774                HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c,
1775                HugeEnumWithInnerClass.d);
1776        assertEquals(4, hugeEnumSet.size());
1777
1778        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a));
1779        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.d));
1780
1781        try {
1782            EnumSet.of((HugeEnumWithInnerClass) null, null, null, null);
1783            fail("Should throw NullPointerException"); //$NON-NLS-1$
1784        } catch (NullPointerException npe) {
1785            // expected
1786        }
1787    }
1788
1789    /**
1790     * @tests java.util.EnumSet#of(E, E, E, E, E)
1791     */
1792    public void test_Of_EEEEE() {
1793        EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
1794                EnumWithInnerClass.b, EnumWithInnerClass.c,
1795                EnumWithInnerClass.d, EnumWithInnerClass.e);
1796        assertEquals("Size of enumSet should be 5:", 5, enumSet.size()); //$NON-NLS-1$
1797
1798        assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
1799        assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.e)); //$NON-NLS-1$
1800
1801        try {
1802            EnumSet.of((EnumWithInnerClass) null, null, null, null, null);
1803            fail("Should throw NullPointerException"); //$NON-NLS-1$
1804        } catch (NullPointerException npe) {
1805            // expected
1806        }
1807
1808        // test enum with more than 64 elements
1809        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a,
1810                HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c,
1811                HugeEnumWithInnerClass.d, HugeEnumWithInnerClass.e);
1812        assertEquals(5, hugeEnumSet.size());
1813
1814        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a));
1815        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.e));
1816
1817        try {
1818            EnumSet.of((HugeEnumWithInnerClass) null, null, null, null, null);
1819            fail("Should throw NullPointerException"); //$NON-NLS-1$
1820        } catch (NullPointerException npe) {
1821            // expected
1822        }
1823    }
1824
1825    /**
1826     * @tests java.util.EnumSet#of(E, E...)
1827     */
1828    public void test_Of_EEArray() {
1829        EnumWithInnerClass[] enumArray = new EnumWithInnerClass[] {
1830                EnumWithInnerClass.b, EnumWithInnerClass.c };
1831        EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a,
1832                enumArray);
1833        assertEquals("Should be equal", 3, enumSet.size()); //$NON-NLS-1$
1834
1835        assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); //$NON-NLS-1$
1836        assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); //$NON-NLS-1$
1837
1838        try {
1839            EnumSet.of(EnumWithInnerClass.a, (EnumWithInnerClass[])null);
1840            fail("Should throw NullPointerException"); //$NON-NLS-1$
1841        } catch (NullPointerException npe) {
1842            // expected
1843        }
1844
1845        EnumFoo[] foos = {EnumFoo.a, EnumFoo.c, EnumFoo.d};
1846        EnumSet<EnumFoo> set = EnumSet.of(EnumFoo.c, foos);
1847        assertEquals("size of set should be 1", 3, set.size()); //$NON-NLS-1$
1848        assertTrue("Should contain EnumFoo.a", set.contains(EnumFoo.a)); //$NON-NLS-1$
1849        assertTrue("Should contain EnumFoo.c", set.contains(EnumFoo.c)); //$NON-NLS-1$
1850        assertTrue("Should contain EnumFoo.d", set.contains(EnumFoo.d)); //$NON-NLS-1$
1851
1852        // test enum type with more than 64 elements
1853        HugeEnumWithInnerClass[] hugeEnumArray = new HugeEnumWithInnerClass[] {
1854                HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c };
1855        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a,
1856                hugeEnumArray);
1857        assertEquals(3, hugeEnumSet.size());
1858
1859        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a));
1860        assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.c));
1861
1862        try {
1863            EnumSet.of(HugeEnumWithInnerClass.a, (HugeEnumWithInnerClass[])null);
1864            fail("Should throw NullPointerException"); //$NON-NLS-1$
1865        } catch (NullPointerException npe) {
1866            // expected
1867        }
1868
1869        HugeEnumWithInnerClass[] huges = {HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.c, HugeEnumWithInnerClass.d};
1870        EnumSet<HugeEnumWithInnerClass> hugeSet = EnumSet.of(HugeEnumWithInnerClass.c, huges);
1871        assertEquals(3, hugeSet.size());
1872        assertTrue(hugeSet.contains(HugeEnumWithInnerClass.a));
1873        assertTrue(hugeSet.contains(HugeEnumWithInnerClass.c));
1874        assertTrue(hugeSet.contains(HugeEnumWithInnerClass.d));
1875    }
1876
1877    /**
1878     * @tests java.util.EnumSet#range(E, E)
1879     */
1880    public void test_Range_EE() {
1881        try {
1882            EnumSet.range(EnumWithInnerClass.c, null);
1883            fail("Should throw NullPointerException"); //$NON-NLS-1$
1884        } catch (NullPointerException e) {
1885            // expected
1886        }
1887
1888        try {
1889            EnumSet.range(null, EnumWithInnerClass.c);
1890            fail("Should throw NullPointerException"); //$NON-NLS-1$
1891        } catch (NullPointerException e) {
1892            // expected
1893        }
1894
1895        try {
1896            EnumSet.range(null, (EnumWithInnerClass) null);
1897            fail("Should throw NullPointerException"); //$NON-NLS-1$
1898        } catch (NullPointerException e) {
1899            // expected
1900        }
1901
1902        try {
1903            EnumSet.range(EnumWithInnerClass.b, EnumWithInnerClass.a);
1904            fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
1905        } catch (IllegalArgumentException e) {
1906            // expected
1907        }
1908
1909        EnumSet<EnumWithInnerClass> enumSet = EnumSet.range(
1910                EnumWithInnerClass.a, EnumWithInnerClass.a);
1911        assertEquals("Size of enumSet should be 1", 1, enumSet.size()); //$NON-NLS-1$
1912
1913        enumSet = EnumSet.range(
1914                EnumWithInnerClass.a, EnumWithInnerClass.c);
1915        assertEquals("Size of enumSet should be 3", 3, enumSet.size()); //$NON-NLS-1$
1916
1917        // test enum with more than 64 elements
1918        try {
1919            EnumSet.range(HugeEnumWithInnerClass.c, null);
1920            fail("Should throw NullPointerException"); //$NON-NLS-1$
1921        } catch (NullPointerException e) {
1922            // expected
1923        }
1924
1925        try {
1926            EnumSet.range(null, HugeEnumWithInnerClass.c);
1927            fail("Should throw NullPointerException"); //$NON-NLS-1$
1928        } catch (NullPointerException e) {
1929            // expected
1930        }
1931
1932        try {
1933            EnumSet.range(null, (HugeEnumWithInnerClass) null);
1934            fail("Should throw NullPointerException"); //$NON-NLS-1$
1935        } catch (NullPointerException e) {
1936            // expected
1937        }
1938
1939        try {
1940            EnumSet.range(HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.a);
1941            fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
1942        } catch (IllegalArgumentException e) {
1943            // expected
1944        }
1945
1946        EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.range(
1947                HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.a);
1948        assertEquals(1, hugeEnumSet.size());
1949
1950        hugeEnumSet = EnumSet.range(
1951                HugeEnumWithInnerClass.c, HugeEnumWithInnerClass.aa);
1952        assertEquals(51, hugeEnumSet.size());
1953
1954        hugeEnumSet = EnumSet.range(
1955                HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.mm);
1956        assertEquals(65, hugeEnumSet.size());
1957
1958        hugeEnumSet = EnumSet.range(
1959                HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.mm);
1960        assertEquals(64, hugeEnumSet.size());
1961    }
1962
1963    /**
1964     * @tests java.util.EnumSet#clone()
1965     */
1966    public void test_Clone() {
1967        EnumSet<EnumFoo> enumSet = EnumSet.allOf(EnumFoo.class);
1968        EnumSet<EnumFoo> clonedEnumSet = enumSet.clone();
1969        assertEquals(enumSet, clonedEnumSet);
1970        assertNotSame(enumSet, clonedEnumSet);
1971        assertTrue(clonedEnumSet.contains(EnumFoo.a));
1972        assertTrue(clonedEnumSet.contains(EnumFoo.b));
1973        assertEquals(64, clonedEnumSet.size());
1974
1975        // test enum type with more than 64 elements
1976        EnumSet<HugeEnum> hugeEnumSet = EnumSet.allOf(HugeEnum.class);
1977        EnumSet<HugeEnum> hugeClonedEnumSet = hugeEnumSet.clone();
1978        assertEquals(hugeEnumSet, hugeClonedEnumSet);
1979        assertNotSame(hugeEnumSet, hugeClonedEnumSet);
1980        assertTrue(hugeClonedEnumSet.contains(HugeEnum.a));
1981        assertTrue(hugeClonedEnumSet.contains(HugeEnum.b));
1982        assertEquals(65, hugeClonedEnumSet.size());
1983
1984        hugeClonedEnumSet.remove(HugeEnum.a);
1985        assertEquals(64, hugeClonedEnumSet.size());
1986        assertFalse(hugeClonedEnumSet.contains(HugeEnum.a));
1987        assertEquals(65, hugeEnumSet.size());
1988        assertTrue(hugeEnumSet.contains(HugeEnum.a));
1989    }
1990
1991    /**
1992     * @tests java.util.EnumSet#Serialization()
1993     */
1994    public void test_serialization() throws Exception {
1995        EnumSet<EnumFoo> set = EnumSet.allOf(EnumFoo.class);
1996        SerializationTest.verifySelf(set);
1997    }
1998
1999    /**
2000     * @tests serialization/deserialization compatibility with RI.
2001     */
2002    @SuppressWarnings( { "unchecked", "boxing" })
2003    public void testSerializationCompatibility() throws Exception {
2004        EnumSet<EnumFoo> set = EnumSet.allOf(EnumFoo.class);
2005        SerializationTest.verifyGolden(this, set);
2006    }
2007}
2008