BitSetTest.java revision 3819a76e7c1f49253f0e077bd497f149340c02b8
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.BitSet;
26
27@TestTargetClass(BitSet.class)
28public class BitSetTest extends junit.framework.TestCase {
29
30    BitSet eightbs;
31
32    /**
33     * @tests java.util.BitSet#BitSet()
34     */
35    @TestTargetNew(
36        level = TestLevel.COMPLETE,
37        notes = "",
38        method = "BitSet",
39        args = {}
40    )
41    public void test_Constructor() {
42        // Test for method java.util.BitSet()
43        BitSet bs = new BitSet();
44        // Default size for a BitSet should be 64 elements;
45
46        assertEquals("Created BitSet of incorrect size", 64, bs.size());
47        assertEquals("New BitSet had invalid string representation", "{}", bs
48                .toString());
49    }
50
51    /**
52     * @tests java.util.BitSet#BitSet(int)
53     */
54    @TestTargetNew(
55        level = TestLevel.COMPLETE,
56        notes = "",
57        method = "BitSet",
58        args = {int.class}
59    )
60    public void test_ConstructorI() {
61        // Test for method java.util.BitSet(int)
62        BitSet bs = new BitSet(128);
63        // Default size for a BitSet should be 64 elements;
64
65        assertEquals("Created BitSet of incorrect size", 128, bs.size());
66        assertTrue("New BitSet had invalid string representation: "
67                + bs.toString(), bs.toString().equals("{}"));
68
69        // All BitSets are created with elements of multiples of 64
70
71        bs = new BitSet(89);
72        assertEquals("Failed to round BitSet element size", 128, bs.size());
73
74        try {
75            bs = new BitSet(-9);
76            fail(
77                    "Failed to throw exception when creating a new BitSet with negative elements value");
78        } catch (NegativeArraySizeException e) {
79            // Correct behaviour
80        }
81    }
82
83    /**
84     * @tests java.util.BitSet#clone()
85     */
86    @TestTargetNew(
87        level = TestLevel.COMPLETE,
88        notes = "",
89        method = "clone",
90        args = {}
91    )
92    public void test_clone() {
93        // Test for method java.lang.Object java.util.BitSet.clone()
94        BitSet bs;
95        bs = (BitSet) eightbs.clone();
96        assertTrue("Clone failed to return equal BitSet", eightbs.equals(bs));
97
98    }
99
100    /**
101     * @tests java.util.BitSet#equals(java.lang.Object)
102     */
103    @TestTargetNew(
104        level = TestLevel.COMPLETE,
105        notes = "",
106        method = "equals",
107        args = {java.lang.Object.class}
108    )
109    public void test_equalsLjava_lang_Object() {
110        // Test for method boolean java.util.BitSet.equals(java.lang.Object)
111        BitSet bs;
112
113        bs = (BitSet) eightbs.clone();
114        assertTrue("Same BitSet returned false", eightbs.equals(eightbs));
115        assertTrue("Identical BitSets returned false", eightbs.equals(bs));
116        bs.clear(6);
117        assertTrue("Different BitSets returned true", !eightbs.equals(bs));
118        // Grow the BitSet
119        bs = (BitSet) eightbs.clone();
120        bs.set(128);
121        assertTrue("Different sized BitSet with higher bit set returned true",
122                !eightbs.equals(bs));
123        bs.clear(128);
124        assertTrue(
125                "Different sized BitSet with higher bits not set returned false",
126                eightbs.equals(bs));
127    }
128
129    /**
130     * @tests java.util.BitSet#hashCode()
131     */
132    @TestTargetNew(
133        level = TestLevel.COMPLETE,
134        notes = "",
135        method = "hashCode",
136        args = {}
137    )
138    public void test_hashCode() {
139        // Test for method int java.util.BitSet.hashCode()
140        BitSet bs = (BitSet) eightbs.clone();
141        bs.clear(2);
142        bs.clear(6);
143        assertTrue("BitSet returns wrong hash value: " + bs.hashCode(), bs
144                .hashCode() == 1129);
145        bs.set(10);
146        bs.clear(3);
147        assertTrue("BitSet returns wrong hash value: " + bs.hashCode(), bs
148                .hashCode() == 97);
149    }
150
151    /**
152     * @tests java.util.BitSet#clear()
153     */
154    @TestTargetNew(
155        level = TestLevel.COMPLETE,
156        notes = "",
157        method = "clear",
158        args = {}
159    )
160    public void test_clear() {
161        eightbs.clear();
162        for (int i = 0; i < 8; i++)
163            assertTrue("Clear didn't clear bit " + i, !eightbs.get(i));
164        assertEquals("Test1: Wrong length", 0, eightbs.length());
165
166        BitSet bs = new BitSet(3400);
167        bs.set(0, bs.size() - 1); // ensure all bits are 1's
168        bs.set(bs.size() - 1);
169        bs.clear();
170        assertEquals("Test2: Wrong length", 0, bs.length());
171        assertTrue("Test2: isEmpty() returned incorrect value", bs
172                .isEmpty());
173        assertEquals("Test2: cardinality() returned incorrect value", 0, bs
174                .cardinality());
175    }
176
177    /**
178     * @tests java.util.BitSet#clear(int)
179     */
180    @TestTargetNew(
181        level = TestLevel.COMPLETE,
182        notes = "",
183        method = "clear",
184        args = {int.class}
185    )
186    public void test_clearI() {
187        // Test for method void java.util.BitSet.clear(int)
188
189        eightbs.clear(7);
190        assertTrue("Failed to clear bit", !eightbs.get(7));
191
192        // Check to see all other bits are still set
193        for (int i = 0; i < 7; i++)
194            assertTrue("Clear cleared incorrect bits", eightbs.get(i));
195
196        eightbs.clear(165);
197        assertTrue("Failed to clear bit", !eightbs.get(165));
198        // Try out of range
199        try {
200            eightbs.clear(-1);
201            fail("Failed to throw expected out of bounds exception");
202        } catch (IndexOutOfBoundsException e) {
203            // Correct behaviour
204        }
205
206        BitSet bs = new BitSet(0);
207        assertTrue("Test1: Wrong length, " + bs.size(), bs.length() == 0);
208        bs.clear(0);
209        assertTrue("Test2: Wrong length" + bs.size(), bs.length() == 0);
210    }
211
212    /**
213     * @tests java.util.BitSet#clear(int, int)
214     */
215    @TestTargetNew(
216        level = TestLevel.COMPLETE,
217        notes = "",
218        method = "clear",
219        args = {int.class, int.class}
220    )
221    public void test_clearII() {
222        // Test for method void java.util.BitSet.clear(int, int)
223        // pos1 and pos2 are in the same bitset element
224        BitSet bs = new BitSet(16);
225        int initialSize = bs.size();
226        bs.set(0, initialSize);
227        bs.clear(5);
228        bs.clear(15);
229        bs.clear(7, 11);
230        for (int i = 0; i < 7; i++) {
231            if (i == 5)
232                assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
233            else
234                assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
235        }
236        for (int i = 7; i < 11; i++)
237            assertTrue("Failed to clear bit " + i, !bs.get(i));
238
239        for (int i = 11; i < initialSize; i++) {
240            if (i == 15)
241                assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
242            else
243                assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
244        }
245
246        for (int i = initialSize; i < bs.size(); i++) {
247            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
248        }
249
250        // pos1 and pos2 is in the same bitset element, boundary testing
251        bs = new BitSet(16);
252        initialSize = bs.size();
253        bs.set(0, initialSize);
254        bs.clear(7, 64);
255        assertEquals("Failed to grow BitSet", 64, bs.size());
256        for (int i = 0; i < 7; i++)
257            assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
258        for (int i = 7; i < 64; i++)
259            assertTrue("Failed to clear bit " + i, !bs.get(i));
260        for (int i = 64; i < bs.size(); i++) {
261            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
262        }
263        // more boundary testing
264        bs = new BitSet(32);
265        initialSize = bs.size();
266        bs.set(0, initialSize);
267        bs.clear(0, 64);
268        for (int i = 0; i < 64; i++)
269            assertTrue("Failed to clear bit " + i, !bs.get(i));
270        for (int i = 64; i < bs.size(); i++) {
271            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
272        }
273
274        bs = new BitSet(32);
275        initialSize = bs.size();
276        bs.set(0, initialSize);
277        bs.clear(0, 65);
278        for (int i = 0; i < 65; i++)
279            assertTrue("Failed to clear bit " + i, !bs.get(i));
280        for (int i = 65; i < bs.size(); i++) {
281            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
282        }
283
284        // pos1 and pos2 are in two sequential bitset elements
285        bs = new BitSet(128);
286        initialSize = bs.size();
287        bs.set(0, initialSize);
288        bs.clear(7);
289        bs.clear(110);
290        bs.clear(9, 74);
291        for (int i = 0; i < 9; i++) {
292            if (i == 7)
293                assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
294            else
295                assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
296        }
297        for (int i = 9; i < 74; i++)
298            assertTrue("Failed to clear bit " + i, !bs.get(i));
299        for (int i = 74; i < initialSize; i++) {
300            if (i == 110)
301                assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
302            else
303                assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
304        }
305        for (int i = initialSize; i < bs.size(); i++) {
306            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
307        }
308
309        // pos1 and pos2 are in two non-sequential bitset elements
310        bs = new BitSet(256);
311        bs.set(0, 256);
312        bs.clear(7);
313        bs.clear(255);
314        bs.clear(9, 219);
315        for (int i = 0; i < 9; i++) {
316            if (i == 7)
317                assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
318            else
319                assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
320        }
321
322        for (int i = 9; i < 219; i++)
323            assertTrue("failed to clear bit " + i, !bs.get(i));
324
325        for (int i = 219; i < 255; i++)
326            assertTrue("Shouldn't have cleared bit " + i, bs.get(i));
327
328        for (int i = 255; i < bs.size(); i++)
329            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
330
331        // test illegal args
332        bs = new BitSet(10);
333        try {
334            bs.clear(-1, 3);
335            fail(
336                    "Test1: Attempt to flip with  negative index failed to generate exception");
337        } catch (IndexOutOfBoundsException e) {
338        }
339
340        try {
341            bs.clear(2, -1);
342            fail(
343                    "Test2: Attempt to flip with negative index failed to generate exception");
344        } catch (IndexOutOfBoundsException e) {
345        }
346
347        try {
348            bs.clear(4, 2);
349            fail(
350                    "Test4: Attempt to flip with illegal args failed to generate exception");
351        } catch (IndexOutOfBoundsException e) {
352        }
353    }
354
355    /**
356     * @tests java.util.BitSet#get(int)
357     */
358    @TestTargetNew(
359        level = TestLevel.COMPLETE,
360        notes = "",
361        method = "get",
362        args = {int.class}
363    )
364    public void test_getI() {
365        // Test for method boolean java.util.BitSet.get(int)
366
367        BitSet bs = new BitSet();
368        bs.set(8);
369        assertTrue("Get returned true for index out of range", !eightbs.get(99));
370        assertTrue("Get returned false for set value", eightbs.get(3));
371        assertTrue("Get returned true for a non set value", !bs.get(0));
372
373        try {
374            bs.get(-1);
375            fail(
376                    "Attempt to get at negative index failed to generate exception");
377        } catch (IndexOutOfBoundsException e) {
378            // Correct behaviour
379        }
380
381        bs = new BitSet(1);
382        assertTrue("Access greater than size", !bs.get(64));
383
384        bs = new BitSet();
385        bs.set(63);
386        assertTrue("Test highest bit", bs.get(63));
387    }
388
389    /**
390     * @tests java.util.BitSet#get(int, int)
391     */
392    @TestTargetNew(
393        level = TestLevel.COMPLETE,
394        notes = "",
395        method = "get",
396        args = {int.class, int.class}
397    )
398    public void test_getII() {
399        // Test for method boolean java.util.BitSet.get(int, int)
400        BitSet bs, resultbs, correctbs;
401        bs = new BitSet(512);
402        bs.set(3, 9);
403        bs.set(10, 20);
404        bs.set(60, 75);
405        bs.set(121);
406        bs.set(130, 140);
407
408        // pos1 and pos2 are in the same bitset element, at index0
409        resultbs = bs.get(3, 6);
410        correctbs = new BitSet(3);
411        correctbs.set(0, 3);
412        assertTrue("Test1: Returned incorrect BitSet", resultbs
413                .equals(correctbs));
414
415        // pos1 and pos2 are in the same bitset element, at index 1
416        resultbs = bs.get(100, 125);
417        correctbs = new BitSet(25);
418        correctbs.set(21);
419        assertTrue("Test2: Returned incorrect BitSet", resultbs
420                .equals(correctbs));
421
422        // pos1 in bitset element at index 0, and pos2 in bitset element at
423        // index 1
424        resultbs = bs.get(15, 125);
425        correctbs = new BitSet(25);
426        correctbs.set(0, 5);
427        correctbs.set(45, 60);
428        correctbs.set(121 - 15);
429        assertTrue("Test3: Returned incorrect BitSet", resultbs
430                .equals(correctbs));
431
432        // pos1 in bitset element at index 1, and pos2 in bitset element at
433        // index 2
434        resultbs = bs.get(70, 145);
435        correctbs = new BitSet(75);
436        correctbs.set(0, 5);
437        correctbs.set(51);
438        correctbs.set(60, 70);
439        assertTrue("Test4: Returned incorrect BitSet", resultbs
440                .equals(correctbs));
441
442        // pos1 in bitset element at index 0, and pos2 in bitset element at
443        // index 2
444        resultbs = bs.get(5, 145);
445        correctbs = new BitSet(140);
446        correctbs.set(0, 4);
447        correctbs.set(5, 15);
448        correctbs.set(55, 70);
449        correctbs.set(116);
450        correctbs.set(125, 135);
451        assertTrue("Test5: Returned incorrect BitSet", resultbs
452                .equals(correctbs));
453
454        // pos1 in bitset element at index 0, and pos2 in bitset element at
455        // index 3
456        resultbs = bs.get(5, 250);
457        correctbs = new BitSet(200);
458        correctbs.set(0, 4);
459        correctbs.set(5, 15);
460        correctbs.set(55, 70);
461        correctbs.set(116);
462        correctbs.set(125, 135);
463        assertTrue("Test6: Returned incorrect BitSet", resultbs
464                .equals(correctbs));
465
466        assertTrue("equality principle", bs.equals(bs.get(0, bs.size())));
467
468        // more tests
469        BitSet bs2 = new BitSet(129);
470        bs2.set(0, 20);
471        bs2.set(62, 65);
472        bs2.set(121, 123);
473        resultbs = bs2.get(1, 124);
474        correctbs = new BitSet(129);
475        correctbs.set(0, 19);
476        correctbs.set(61, 64);
477        correctbs.set(120, 122);
478        assertTrue("Test6: Returned incorrect BitSet", resultbs
479                .equals(correctbs));
480
481        // equality principle with some boundary conditions
482        bs2 = new BitSet(128);
483        bs2.set(2, 20);
484        bs2.set(62);
485        bs2.set(121, 123);
486        bs2.set(127);
487        resultbs = bs2.get(0, bs2.size());
488        assertTrue("equality principle", bs2.equals(resultbs));
489
490        bs2 = new BitSet(128);
491        bs2.set(2, 20);
492        bs2.set(62);
493        bs2.set(121, 123);
494        bs2.set(127);
495        bs2.flip(0, 128);
496        resultbs = bs2.get(0, bs.size());
497        assertTrue("equality principle", bs2.equals(resultbs));
498
499        try {
500            bs2.get(-1, 0);
501            fail("IndexOutOfBoundsException expected");
502        } catch (IndexOutOfBoundsException e) {
503            //expected
504        }
505
506        try {
507            bs2.get(bs2.size()/2, 0);
508            fail("IndexOutOfBoundsException expected");
509        } catch (IndexOutOfBoundsException e) {
510            //expected
511        }
512
513        try {
514            bs2.get(bs2.size()/2, -1);
515            fail("IndexOutOfBoundsException expected");
516        } catch (IndexOutOfBoundsException e) {
517            //expected
518        }
519    }
520
521    /**
522     * @tests java.util.BitSet#set(int)
523     */
524    @TestTargetNew(
525        level = TestLevel.COMPLETE,
526        notes = "",
527        method = "set",
528        args = {int.class}
529    )
530    public void test_setI() {
531        // Test for method void java.util.BitSet.set(int)
532
533        BitSet bs = new BitSet();
534        bs.set(8);
535        assertTrue("Failed to set bit", bs.get(8));
536
537        try {
538            bs.set(-1);
539            fail(
540                    "Attempt to set at negative index failed to generate exception");
541        } catch (IndexOutOfBoundsException e) {
542            // Correct behaviour
543        }
544
545        // Try setting a bit on a 64 boundary
546        bs.set(128);
547        assertEquals("Failed to grow BitSet", 192, bs.size());
548        assertTrue("Failed to set bit", bs.get(128));
549
550        bs = new BitSet(64);
551        for (int i = bs.size(); --i >= 0;) {
552            bs.set(i);
553            assertTrue("Incorrectly set", bs.get(i));
554            assertTrue("Incorrect length", bs.length() == (i + 1));
555            for (int j = bs.size(); --j > i;)
556                assertTrue("Incorrectly set bit " + j, !bs.get(j));
557            for (int j = i; --j >= 0;)
558                assertTrue("Incorrectly set bit " + j, !bs.get(j));
559            bs.clear(i);
560        }
561
562        bs = new BitSet(0);
563        assertTrue("Test1: Wrong length, " + bs.size(), bs.length() == 0);
564        bs.set(0);
565        assertTrue("Test2: Wrong length" + bs.size(), bs.length() == 1);
566    }
567
568    /**
569     * @tests java.util.BitSet#set(int, boolean)
570     */
571    @TestTargetNew(
572        level = TestLevel.COMPLETE,
573        notes = "",
574        method = "set",
575        args = {int.class, boolean.class}
576    )
577    public void test_setIZ() {
578        // Test for method void java.util.BitSet.set(int, boolean)
579        eightbs.set(5, false);
580        assertTrue("Should have set bit 5 to true", !eightbs.get(5));
581
582        eightbs.set(5, true);
583        assertTrue("Should have set bit 5 to false", eightbs.get(5));
584
585        try {
586            eightbs.set(-5, false);
587            fail("IndexOutOfBoundsException expected");
588        } catch (IndexOutOfBoundsException e) {
589            //expected
590        }
591    }
592
593    /**
594     * @tests java.util.BitSet#set(int, int)
595     */
596    @TestTargetNew(
597        level = TestLevel.COMPLETE,
598        notes = "",
599        method = "set",
600        args = {int.class, int.class}
601    )
602    public void test_setII() {
603        // Test for method void java.util.BitSet.set(int, int)
604        // pos1 and pos2 are in the same bitset element
605        BitSet bs = new BitSet(16);
606        bs.set(5);
607        bs.set(15);
608        bs.set(7, 11);
609        for (int i = 0; i < 7; i++) {
610            if (i == 5)
611                assertTrue("Shouldn't have flipped bit " + i, bs.get(i));
612            else
613                assertTrue("Shouldn't have set bit " + i, !bs.get(i));
614        }
615        for (int i = 7; i < 11; i++)
616            assertTrue("Failed to set bit " + i, bs.get(i));
617        for (int i = 11; i < bs.size(); i++) {
618            if (i == 15)
619                assertTrue("Shouldn't have flipped bit " + i, bs.get(i));
620            else
621                assertTrue("Shouldn't have set bit " + i, !bs.get(i));
622        }
623
624        // pos1 and pos2 is in the same bitset element, boundary testing
625        bs = new BitSet(16);
626        bs.set(7, 64);
627        assertEquals("Failed to grow BitSet", 64, bs.size());
628        for (int i = 0; i < 7; i++)
629            assertTrue("Shouldn't have set bit " + i, !bs.get(i));
630        for (int i = 7; i < 64; i++)
631            assertTrue("Failed to set bit " + i, bs.get(i));
632        assertTrue("Shouldn't have set bit 64", !bs.get(64));
633
634        // more boundary testing
635        bs = new BitSet(32);
636        bs.set(0, 64);
637        for (int i = 0; i < 64; i++)
638            assertTrue("Failed to set bit " + i, bs.get(i));
639        assertTrue("Shouldn't have set bit 64", !bs.get(64));
640
641        bs = new BitSet(32);
642        bs.set(0, 65);
643        for (int i = 0; i < 65; i++)
644            assertTrue("Failed to set bit " + i, bs.get(i));
645        assertTrue("Shouldn't have set bit 65", !bs.get(65));
646
647        // pos1 and pos2 are in two sequential bitset elements
648        bs = new BitSet(128);
649        bs.set(7);
650        bs.set(110);
651        bs.set(9, 74);
652        for (int i = 0; i < 9; i++) {
653            if (i == 7)
654                assertTrue("Shouldn't have flipped bit " + i, bs.get(i));
655            else
656                assertTrue("Shouldn't have set bit " + i, !bs.get(i));
657        }
658        for (int i = 9; i < 74; i++)
659            assertTrue("Failed to set bit " + i, bs.get(i));
660        for (int i = 74; i < bs.size(); i++) {
661            if (i == 110)
662                assertTrue("Shouldn't have flipped bit " + i, bs.get(i));
663            else
664                assertTrue("Shouldn't have set bit " + i, !bs.get(i));
665        }
666
667        // pos1 and pos2 are in two non-sequential bitset elements
668        bs = new BitSet(256);
669        bs.set(7);
670        bs.set(255);
671        bs.set(9, 219);
672        for (int i = 0; i < 9; i++) {
673            if (i == 7)
674                assertTrue("Shouldn't have set flipped " + i, bs.get(i));
675            else
676                assertTrue("Shouldn't have set bit " + i, !bs.get(i));
677        }
678
679        for (int i = 9; i < 219; i++)
680            assertTrue("failed to set bit " + i, bs.get(i));
681
682        for (int i = 219; i < 255; i++)
683            assertTrue("Shouldn't have set bit " + i, !bs.get(i));
684
685        assertTrue("Shouldn't have flipped bit 255", bs.get(255));
686
687        // test illegal args
688        bs = new BitSet(10);
689        try {
690            bs.set(-1, 3);
691            fail(
692                    "Test1: Attempt to flip with  negative index failed to generate exception");
693        } catch (IndexOutOfBoundsException e) {
694        }
695
696        try {
697            bs.set(2, -1);
698            fail(
699                    "Test2: Attempt to flip with negative index failed to generate exception");
700        } catch (IndexOutOfBoundsException e) {
701        }
702
703        try {
704            bs.set(4, 2);
705            fail(
706                    "Test4: Attempt to flip with illegal args failed to generate exception");
707        } catch (IndexOutOfBoundsException e) {
708        }
709    }
710
711    /**
712     * @tests java.util.BitSet#set(int, int, boolean)
713     */
714    @TestTargetNew(
715        level = TestLevel.COMPLETE,
716        notes = "",
717        method = "set",
718        args = {int.class, int.class, boolean.class}
719    )
720    public void test_setIIZ() {
721        // Test for method void java.util.BitSet.set(int, int, boolean)
722        eightbs.set(3, 6, false);
723        assertTrue("Should have set bits 3, 4, and 5 to false", !eightbs.get(3)
724                && !eightbs.get(4) && !eightbs.get(5));
725
726        eightbs.set(3, 6, true);
727        assertTrue("Should have set bits 3, 4, and 5 to true", eightbs.get(3)
728                && eightbs.get(4) && eightbs.get(5));
729
730        try {
731            eightbs.set(-3, 6, false);
732            fail("IndexOutOfBoundsException expected");
733        } catch (IndexOutOfBoundsException e) {
734            //expected
735        }
736
737        try {
738            eightbs.set(3, -6, false);
739            fail("IndexOutOfBoundsException expected");
740        } catch (IndexOutOfBoundsException e) {
741            //expected
742        }
743
744        try {
745            eightbs.set(6, 3, false);
746            fail("IndexOutOfBoundsException expected");
747        } catch (IndexOutOfBoundsException e) {
748            //expected
749        }
750    }
751
752    /**
753     * @tests java.util.BitSet#flip(int)
754     */
755    @TestTargetNew(
756        level = TestLevel.COMPLETE,
757        notes = "",
758        method = "flip",
759        args = {int.class}
760    )
761    public void test_flipI() {
762        // Test for method void java.util.BitSet.flip(int)
763        BitSet bs = new BitSet();
764        bs.clear(8);
765        bs.clear(9);
766        bs.set(10);
767        bs.flip(9);
768        assertTrue("Failed to flip bit", !bs.get(8));
769        assertTrue("Failed to flip bit", bs.get(9));
770        assertTrue("Failed to flip bit", bs.get(10));
771
772        bs.set(8);
773        bs.set(9);
774        bs.clear(10);
775        bs.flip(9);
776        assertTrue("Failed to flip bit", bs.get(8));
777        assertTrue("Failed to flip bit", !bs.get(9));
778        assertTrue("Failed to flip bit", !bs.get(10));
779
780        try {
781            bs.flip(-1);
782            fail(
783                    "Attempt to flip at negative index failed to generate exception");
784        } catch (IndexOutOfBoundsException e) {
785            // Correct behaviour
786        }
787
788        // Try setting a bit on a 64 boundary
789        bs.flip(128);
790        assertEquals("Failed to grow BitSet", 192, bs.size());
791        assertTrue("Failed to flip bit", bs.get(128));
792
793        bs = new BitSet(64);
794        for (int i = bs.size(); --i >= 0;) {
795            bs.flip(i);
796            assertTrue("Test1: Incorrectly flipped bit" + i, bs.get(i));
797            assertTrue("Incorrect length", bs.length() == (i + 1));
798            for (int j = bs.size(); --j > i;)
799                assertTrue("Test2: Incorrectly flipped bit" + j, !bs.get(j));
800            for (int j = i; --j >= 0;)
801                assertTrue("Test3: Incorrectly flipped bit" + j, !bs.get(j));
802            bs.flip(i);
803        }
804
805        BitSet bs0 = new BitSet(0);
806        assertEquals("Test1: Wrong size", 0, bs0.size());
807        assertEquals("Test1: Wrong length", 0, bs0.length());
808
809        bs0.flip(0);
810        assertEquals("Test2: Wrong size", 64, bs0.size());
811        assertEquals("Test2: Wrong length", 1, bs0.length());
812
813        bs0.flip(63);
814        assertEquals("Test3: Wrong size", 64, bs0.size());
815        assertEquals("Test3: Wrong length", 64, bs0.length());
816
817        eightbs.flip(7);
818        assertTrue("Failed to flip bit 7", !eightbs.get(7));
819
820        // Check to see all other bits are still set
821        for (int i = 0; i < 7; i++)
822            assertTrue("Flip flipped incorrect bits", eightbs.get(i));
823
824        eightbs.flip(127);
825        assertTrue("Failed to flip bit 127", eightbs.get(127));
826
827        eightbs.flip(127);
828        assertTrue("Failed to flip bit 127", !eightbs.get(127));
829    }
830
831    /**
832     * @tests java.util.BitSet#flip(int, int)
833     */
834    @TestTargetNew(
835        level = TestLevel.COMPLETE,
836        notes = "",
837        method = "flip",
838        args = {int.class, int.class}
839    )
840    public void test_flipII() {
841        // Test for method void java.util.BitSet.flip(int, int)
842        // pos1 and pos2 are in the same bitset element
843        BitSet bs = new BitSet(16);
844        bs.set(7);
845        bs.set(10);
846        bs.flip(7, 11);
847        for (int i = 0; i < 7; i++)
848            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
849        assertTrue("Failed to flip bit 7", !bs.get(7));
850        assertTrue("Failed to flip bit 8", bs.get(8));
851        assertTrue("Failed to flip bit 9", bs.get(9));
852        assertTrue("Failed to flip bit 10", !bs.get(10));
853        for (int i = 11; i < bs.size(); i++)
854            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
855
856        // pos1 and pos2 is in the same bitset element, boundary testing
857        bs = new BitSet(16);
858        bs.set(7);
859        bs.set(10);
860        bs.flip(7, 64);
861        assertEquals("Failed to grow BitSet", 64, bs.size());
862        for (int i = 0; i < 7; i++)
863            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
864        assertTrue("Failed to flip bit 7", !bs.get(7));
865        assertTrue("Failed to flip bit 8", bs.get(8));
866        assertTrue("Failed to flip bit 9", bs.get(9));
867        assertTrue("Failed to flip bit 10", !bs.get(10));
868        for (int i = 11; i < 64; i++)
869            assertTrue("failed to flip bit " + i, bs.get(i));
870        assertTrue("Shouldn't have flipped bit 64", !bs.get(64));
871
872        // more boundary testing
873        bs = new BitSet(32);
874        bs.flip(0, 64);
875        for (int i = 0; i < 64; i++)
876            assertTrue("Failed to flip bit " + i, bs.get(i));
877        assertTrue("Shouldn't have flipped bit 64", !bs.get(64));
878
879        bs = new BitSet(32);
880        bs.flip(0, 65);
881        for (int i = 0; i < 65; i++)
882            assertTrue("Failed to flip bit " + i, bs.get(i));
883        assertTrue("Shouldn't have flipped bit 65", !bs.get(65));
884
885        // pos1 and pos2 are in two sequential bitset elements
886        bs = new BitSet(128);
887        bs.set(7);
888        bs.set(10);
889        bs.set(72);
890        bs.set(110);
891        bs.flip(9, 74);
892        for (int i = 0; i < 7; i++)
893            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
894        assertTrue("Shouldn't have flipped bit 7", bs.get(7));
895        assertTrue("Shouldn't have flipped bit 8", !bs.get(8));
896        assertTrue("Failed to flip bit 9", bs.get(9));
897        assertTrue("Failed to flip bit 10", !bs.get(10));
898        for (int i = 11; i < 72; i++)
899            assertTrue("failed to flip bit " + i, bs.get(i));
900        assertTrue("Failed to flip bit 72", !bs.get(72));
901        assertTrue("Failed to flip bit 73", bs.get(73));
902        for (int i = 74; i < 110; i++)
903            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
904        assertTrue("Shouldn't have flipped bit 110", bs.get(110));
905        for (int i = 111; i < bs.size(); i++)
906            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
907
908        // pos1 and pos2 are in two non-sequential bitset elements
909        bs = new BitSet(256);
910        bs.set(7);
911        bs.set(10);
912        bs.set(72);
913        bs.set(110);
914        bs.set(181);
915        bs.set(220);
916        bs.flip(9, 219);
917        for (int i = 0; i < 7; i++)
918            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
919        assertTrue("Shouldn't have flipped bit 7", bs.get(7));
920        assertTrue("Shouldn't have flipped bit 8", !bs.get(8));
921        assertTrue("Failed to flip bit 9", bs.get(9));
922        assertTrue("Failed to flip bit 10", !bs.get(10));
923        for (int i = 11; i < 72; i++)
924            assertTrue("failed to flip bit " + i, bs.get(i));
925        assertTrue("Failed to flip bit 72", !bs.get(72));
926        for (int i = 73; i < 110; i++)
927            assertTrue("failed to flip bit " + i, bs.get(i));
928        assertTrue("Failed to flip bit 110", !bs.get(110));
929        for (int i = 111; i < 181; i++)
930            assertTrue("failed to flip bit " + i, bs.get(i));
931        assertTrue("Failed to flip bit 181", !bs.get(181));
932        for (int i = 182; i < 219; i++)
933            assertTrue("failed to flip bit " + i, bs.get(i));
934        assertTrue("Shouldn't have flipped bit 219", !bs.get(219));
935        assertTrue("Shouldn't have flipped bit 220", bs.get(220));
936        for (int i = 221; i < bs.size(); i++)
937            assertTrue("Shouldn't have flipped bit " + i, !bs.get(i));
938
939        // test illegal args
940        bs = new BitSet(10);
941        try {
942            bs.flip(-1, 3);
943            fail(
944                    "Test1: Attempt to flip with  negative index failed to generate exception");
945        } catch (IndexOutOfBoundsException e) {
946        }
947
948        try {
949            bs.flip(2, -1);
950            fail(
951                    "Test2: Attempt to flip with negative index failed to generate exception");
952        } catch (IndexOutOfBoundsException e) {
953        }
954
955        try {
956            bs.flip(4, 2);
957            fail(
958                    "Test4: Attempt to flip with illegal args failed to generate exception");
959        } catch (IndexOutOfBoundsException e) {
960        }
961    }
962
963    /**
964     * @tests java.util.BitSet#set(int, int)
965     * @tests java.util.BitSet#cardinality()
966     * @tests java.util.BitSet#get(int)
967     * @tests java.util.BitSet#flip(int, int)
968     * @tests java.util.BitSet#clear(int,int)
969     */
970    @TestTargets({
971        @TestTargetNew(
972            level = TestLevel.PARTIAL_COMPLETE,
973            notes = "",
974            method = "flip",
975            args = {int.class, int.class}
976        ),
977        @TestTargetNew(
978            level = TestLevel.PARTIAL_COMPLETE,
979            notes = "",
980            method = "get",
981            args = {int.class, int.class}
982        ),
983        @TestTargetNew(
984            level = TestLevel.PARTIAL_COMPLETE,
985            notes = "",
986            method = "set",
987            args = {int.class, int.class}
988        ),
989        @TestTargetNew(
990            level = TestLevel.PARTIAL_COMPLETE,
991            notes = "",
992            method = "clear",
993            args = {int.class, int.class}
994        )
995    })
996    public void test_111478() {
997        // BitSet shouldn't be modified by any of the operations below,
998        // since the affected bits for these methods are defined as inclusive of
999        // pos1, exclusive of pos2.
1000        try {
1001            eightbs.flip(0, 0);
1002            assertTrue("Bit got flipped incorrectly ", eightbs.get(0));
1003
1004            BitSet bsnew = eightbs.get(2, 2);
1005            assertEquals("BitSet retrieved incorrectly ",
1006                    0, bsnew.cardinality());
1007
1008            eightbs.set(10, 10);
1009            assertTrue("Bit got set incorrectly ", !eightbs.get(10));
1010
1011            eightbs.clear(3, 3);
1012            assertTrue("Bit cleared incorrectly ", eightbs.get(3));
1013        } catch (IndexOutOfBoundsException e) {
1014            fail("Unexpected IndexOutOfBoundsException when pos1 ==pos2");
1015        }
1016    }
1017
1018    /**
1019     * @tests java.util.BitSet#intersects(java.util.BitSet)
1020     */
1021    @TestTargetNew(
1022        level = TestLevel.COMPLETE,
1023        notes = "",
1024        method = "intersects",
1025        args = {java.util.BitSet.class}
1026    )
1027    public void test_intersectsLjava_util_BitSet() {
1028        // Test for method boolean java.util.BitSet.intersects(java.util.BitSet)
1029        BitSet bs = new BitSet(500);
1030        bs.set(5);
1031        bs.set(63);
1032        bs.set(64);
1033        bs.set(71, 110);
1034        bs.set(127, 130);
1035        bs.set(192);
1036        bs.set(450);
1037
1038        BitSet bs2 = new BitSet(8);
1039        assertFalse("Test1: intersects() returned incorrect value", bs
1040                .intersects(bs2));
1041        assertFalse("Test1: intersects() returned incorrect value", bs2
1042                .intersects(bs));
1043
1044        bs2.set(4);
1045        assertFalse("Test2: intersects() returned incorrect value", bs
1046                .intersects(bs2));
1047        assertFalse("Test2: intersects() returned incorrect value", bs2
1048                .intersects(bs));
1049
1050        bs2.clear();
1051        bs2.set(5);
1052        assertTrue("Test3: intersects() returned incorrect value", bs
1053                .intersects(bs2));
1054        assertTrue("Test3: intersects() returned incorrect value", bs2
1055                .intersects(bs));
1056
1057        bs2.clear();
1058        bs2.set(63);
1059        assertTrue("Test4: intersects() returned incorrect value", bs
1060                .intersects(bs2));
1061        assertTrue("Test4: intersects() returned incorrect value", bs2
1062                .intersects(bs));
1063
1064        bs2.clear();
1065        bs2.set(80);
1066        assertTrue("Test5: intersects() returned incorrect value", bs
1067                .intersects(bs2));
1068        assertTrue("Test5: intersects() returned incorrect value", bs2
1069                .intersects(bs));
1070
1071        bs2.clear();
1072        bs2.set(127);
1073        assertTrue("Test6: intersects() returned incorrect value", bs
1074                .intersects(bs2));
1075        assertTrue("Test6: intersects() returned incorrect value", bs2
1076                .intersects(bs));
1077
1078        bs2.clear();
1079        bs2.set(192);
1080        assertTrue("Test7: intersects() returned incorrect value", bs
1081                .intersects(bs2));
1082        assertTrue("Test7: intersects() returned incorrect value", bs2
1083                .intersects(bs));
1084
1085        bs2.clear();
1086        bs2.set(450);
1087        assertTrue("Test8: intersects() returned incorrect value", bs
1088                .intersects(bs2));
1089        assertTrue("Test8: intersects() returned incorrect value", bs2
1090                .intersects(bs));
1091
1092        bs2.clear();
1093        bs2.set(500);
1094        assertFalse("Test9: intersects() returned incorrect value", bs
1095                .intersects(bs2));
1096        assertFalse("Test9: intersects() returned incorrect value", bs2
1097                .intersects(bs));
1098    }
1099
1100    /**
1101     * @tests java.util.BitSet#and(java.util.BitSet)
1102     */
1103    @TestTargetNew(
1104        level = TestLevel.COMPLETE,
1105        notes = "",
1106        method = "and",
1107        args = {java.util.BitSet.class}
1108    )
1109    public void test_andLjava_util_BitSet() {
1110        // Test for method void java.util.BitSet.and(java.util.BitSet)
1111        BitSet bs = new BitSet(128);
1112        // Initialize the bottom half of the BitSet
1113
1114        for (int i = 64; i < 128; i++)
1115            bs.set(i);
1116        eightbs.and(bs);
1117        assertTrue("AND failed to clear bits", !eightbs.equals(bs));
1118        eightbs.set(3);
1119        bs.set(3);
1120        eightbs.and(bs);
1121        assertTrue("AND failed to maintain set bits", bs.get(3));
1122        bs.and(eightbs);
1123        for (int i = 64; i < 128; i++)
1124            assertTrue("Failed to clear extra bits in the receiver BitSet", !bs
1125                    .get(i));
1126    }
1127
1128    /**
1129     * @tests java.util.BitSet#andNot(java.util.BitSet)
1130     */
1131    @TestTargetNew(
1132        level = TestLevel.COMPLETE,
1133        notes = "",
1134        method = "andNot",
1135        args = {java.util.BitSet.class}
1136    )
1137    public void test_andNotLjava_util_BitSet() {
1138        BitSet bs = (BitSet) eightbs.clone();
1139        bs.clear(5);
1140        BitSet bs2 = new BitSet();
1141        bs2.set(2);
1142        bs2.set(3);
1143        bs.andNot(bs2);
1144        assertEquals("Incorrect bitset after andNot",
1145                "{0, 1, 4, 6, 7}", bs.toString());
1146
1147        bs = new BitSet(0);
1148        bs.andNot(bs2);
1149        assertEquals("Incorrect size", 0, bs.size());
1150    }
1151
1152    /**
1153     * @tests java.util.BitSet#or(java.util.BitSet)
1154     */
1155    @TestTargetNew(
1156        level = TestLevel.COMPLETE,
1157        notes = "",
1158        method = "or",
1159        args = {java.util.BitSet.class}
1160    )
1161    public void test_orLjava_util_BitSet() {
1162        // Test for method void java.util.BitSet.or(java.util.BitSet)
1163        BitSet bs = new BitSet(128);
1164        bs.or(eightbs);
1165        for (int i = 0; i < 8; i++)
1166            assertTrue("OR failed to set bits", bs.get(i));
1167
1168        bs = new BitSet(0);
1169        bs.or(eightbs);
1170        for (int i = 0; i < 8; i++)
1171            assertTrue("OR(0) failed to set bits", bs.get(i));
1172
1173        eightbs.clear(5);
1174        bs = new BitSet(128);
1175        bs.or(eightbs);
1176        assertTrue("OR set a bit which should be off", !bs.get(5));
1177    }
1178
1179    /**
1180     * @tests java.util.BitSet#xor(java.util.BitSet)
1181     */
1182    @TestTargetNew(
1183        level = TestLevel.COMPLETE,
1184        notes = "",
1185        method = "xor",
1186        args = {java.util.BitSet.class}
1187    )
1188    public void test_xorLjava_util_BitSet() {
1189        // Test for method void java.util.BitSet.xor(java.util.BitSet)
1190
1191        BitSet bs = (BitSet) eightbs.clone();
1192        bs.xor(eightbs);
1193        for (int i = 0; i < 8; i++)
1194            assertTrue("XOR failed to clear bits", !bs.get(i));
1195
1196        bs.xor(eightbs);
1197        for (int i = 0; i < 8; i++)
1198            assertTrue("XOR failed to set bits", bs.get(i));
1199
1200        bs = new BitSet(0);
1201        bs.xor(eightbs);
1202        for (int i = 0; i < 8; i++)
1203            assertTrue("XOR(0) failed to set bits", bs.get(i));
1204
1205        bs = new BitSet();
1206        bs.set(63);
1207        assertEquals("Test highest bit", "{63}", bs.toString());
1208    }
1209
1210    /**
1211     * @tests java.util.BitSet#size()
1212     */
1213    @TestTargetNew(
1214        level = TestLevel.COMPLETE,
1215        notes = "",
1216        method = "size",
1217        args = {}
1218    )
1219    public void test_size() {
1220        // Test for method int java.util.BitSet.size()
1221        assertEquals("Returned incorrect size", 64, eightbs.size());
1222        eightbs.set(129);
1223        assertTrue("Returned incorrect size", eightbs.size() >= 129);
1224
1225    }
1226
1227    /**
1228     * @tests java.util.BitSet#toString()
1229     */
1230    @TestTargetNew(
1231        level = TestLevel.COMPLETE,
1232        notes = "",
1233        method = "toString",
1234        args = {}
1235    )
1236    public void test_toString() {
1237        // Test for method java.lang.String java.util.BitSet.toString()
1238        assertEquals("Returned incorrect string representation", "{0, 1, 2, 3, 4, 5, 6, 7}", eightbs
1239                .toString());
1240        eightbs.clear(2);
1241        assertEquals("Returned incorrect string representation", "{0, 1, 3, 4, 5, 6, 7}", eightbs
1242                .toString());
1243    }
1244
1245    /**
1246     * @tests java.util.BitSet#length()
1247     */
1248    @TestTargetNew(
1249        level = TestLevel.COMPLETE,
1250        notes = "",
1251        method = "length",
1252        args = {}
1253    )
1254    public void test_length() {
1255        BitSet bs = new BitSet();
1256        assertTrue("BitSet returned wrong length--wanted 0, got: "
1257                + bs.length(), bs.length() == 0);
1258        bs.set(5);
1259        assertTrue("BitSet returned wrong length--wanted 6, got: "
1260                + bs.length(), bs.length() == 6);
1261        bs.set(10);
1262        assertTrue("BitSet returned wrong length--wanted 11, got: "
1263                + bs.length(), bs.length() == 11);
1264        bs.set(432);
1265        assertTrue("BitSet returned wrong length--wanted 433, got: "
1266                + bs.length(), bs.length() == 433);
1267        bs.set(300);
1268        assertTrue("BitSet returned wrong length--wanted 433 (again), got: "
1269                + bs.length(), bs.length() == 433);
1270    }
1271
1272    /**
1273     * @tests java.util.BitSet#nextSetBit(int)
1274     */
1275    @TestTargetNew(
1276        level = TestLevel.COMPLETE,
1277        notes = "",
1278        method = "nextSetBit",
1279        args = {int.class}
1280    )
1281    public void test_nextSetBitI() {
1282        // Test for method int java.util.BitSet.nextSetBit()
1283        BitSet bs = new BitSet(500);
1284        bs.set(5);
1285        bs.set(32);
1286        bs.set(63);
1287        bs.set(64);
1288        bs.set(71, 110);
1289        bs.set(127, 130);
1290        bs.set(193);
1291        bs.set(450);
1292        try {
1293            bs.nextSetBit(-1);
1294            fail("Expected IndexOutOfBoundsException for negative index");
1295        } catch (IndexOutOfBoundsException e) {
1296        }
1297        assertEquals("nextSetBit() returned the wrong value", 5, bs
1298                .nextSetBit(0));
1299        assertEquals("nextSetBit() returned the wrong value", 5, bs
1300                .nextSetBit(5));
1301        assertEquals("nextSetBit() returned the wrong value", 32, bs
1302                .nextSetBit(6));
1303        assertEquals("nextSetBit() returned the wrong value", 32, bs
1304                .nextSetBit(32));
1305        assertEquals("nextSetBit() returned the wrong value", 63, bs
1306                .nextSetBit(33));
1307
1308        // boundary tests
1309        assertEquals("nextSetBit() returned the wrong value", 63, bs
1310                .nextSetBit(63));
1311        assertEquals("nextSetBit() returned the wrong value", 64, bs
1312                .nextSetBit(64));
1313
1314        // at bitset element 1
1315        assertEquals("nextSetBit() returned the wrong value", 71, bs
1316                .nextSetBit(65));
1317        assertEquals("nextSetBit() returned the wrong value", 71, bs
1318                .nextSetBit(71));
1319        assertEquals("nextSetBit() returned the wrong value", 72, bs
1320                .nextSetBit(72));
1321        assertEquals("nextSetBit() returned the wrong value", 127, bs
1322                .nextSetBit(110));
1323
1324        // boundary tests
1325        assertEquals("nextSetBit() returned the wrong value", 127, bs
1326                .nextSetBit(127));
1327        assertEquals("nextSetBit() returned the wrong value", 128, bs
1328                .nextSetBit(128));
1329
1330        // at bitset element 2
1331        assertEquals("nextSetBit() returned the wrong value", 193, bs
1332                .nextSetBit(130));
1333
1334        assertEquals("nextSetBit() returned the wrong value", 193, bs
1335                .nextSetBit(191));
1336        assertEquals("nextSetBit() returned the wrong value", 193, bs
1337                .nextSetBit(192));
1338        assertEquals("nextSetBit() returned the wrong value", 193, bs
1339                .nextSetBit(193));
1340        assertEquals("nextSetBit() returned the wrong value", 450, bs
1341                .nextSetBit(194));
1342        assertEquals("nextSetBit() returned the wrong value", 450, bs
1343                .nextSetBit(255));
1344        assertEquals("nextSetBit() returned the wrong value", 450, bs
1345                .nextSetBit(256));
1346        assertEquals("nextSetBit() returned the wrong value", 450, bs
1347                .nextSetBit(450));
1348
1349        assertEquals("nextSetBit() returned the wrong value", -1, bs
1350                .nextSetBit(451));
1351        assertEquals("nextSetBit() returned the wrong value", -1, bs
1352                .nextSetBit(511));
1353        assertEquals("nextSetBit() returned the wrong value", -1, bs
1354                .nextSetBit(512));
1355        assertEquals("nextSetBit() returned the wrong value", -1, bs
1356                .nextSetBit(800));
1357    }
1358
1359    /**
1360     * @tests java.util.BitSet#nextClearBit(int)
1361     */
1362    @TestTargetNew(
1363        level = TestLevel.COMPLETE,
1364        notes = "",
1365        method = "nextClearBit",
1366        args = {int.class}
1367    )
1368    public void test_nextClearBitI() {
1369        // Test for method int java.util.BitSet.nextSetBit()
1370        BitSet bs = new BitSet(500);
1371        // ensure all the bits from 0 to bs.size() - 1 are set to true
1372        bs.set(0, bs.size() - 1);
1373        bs.set(bs.size() - 1);
1374        bs.clear(5);
1375        bs.clear(32);
1376        bs.clear(63);
1377        bs.clear(64);
1378        bs.clear(71, 110);
1379        bs.clear(127, 130);
1380        bs.clear(193);
1381        bs.clear(450);
1382        try {
1383            bs.nextClearBit(-1);
1384            fail("Expected IndexOutOfBoundsException for negative index");
1385        } catch (IndexOutOfBoundsException e) {
1386        }
1387        assertEquals("nextClearBit() returned the wrong value", 5, bs
1388                .nextClearBit(0));
1389        assertEquals("nextClearBit() returned the wrong value", 5, bs
1390                .nextClearBit(5));
1391        assertEquals("nextClearBit() returned the wrong value", 32, bs
1392                .nextClearBit(6));
1393        assertEquals("nextClearBit() returned the wrong value", 32, bs
1394                .nextClearBit(32));
1395        assertEquals("nextClearBit() returned the wrong value", 63, bs
1396                .nextClearBit(33));
1397
1398        // boundary tests
1399        assertEquals("nextClearBit() returned the wrong value", 63, bs
1400                .nextClearBit(63));
1401        assertEquals("nextClearBit() returned the wrong value", 64, bs
1402                .nextClearBit(64));
1403
1404        // at bitset element 1
1405        assertEquals("nextClearBit() returned the wrong value", 71, bs
1406                .nextClearBit(65));
1407        assertEquals("nextClearBit() returned the wrong value", 71, bs
1408                .nextClearBit(71));
1409        assertEquals("nextClearBit() returned the wrong value", 72, bs
1410                .nextClearBit(72));
1411        assertEquals("nextClearBit() returned the wrong value", 127, bs
1412                .nextClearBit(110));
1413
1414        // boundary tests
1415        assertEquals("nextClearBit() returned the wrong value", 127, bs
1416                .nextClearBit(127));
1417        assertEquals("nextClearBit() returned the wrong value", 128, bs
1418                .nextClearBit(128));
1419
1420        // at bitset element 2
1421        assertEquals("nextClearBit() returned the wrong value", 193, bs
1422                .nextClearBit(130));
1423        assertEquals("nextClearBit() returned the wrong value", 193, bs
1424                .nextClearBit(191));
1425
1426        assertEquals("nextClearBit() returned the wrong value", 193, bs
1427                .nextClearBit(192));
1428        assertEquals("nextClearBit() returned the wrong value", 193, bs
1429                .nextClearBit(193));
1430        assertEquals("nextClearBit() returned the wrong value", 450, bs
1431                .nextClearBit(194));
1432        assertEquals("nextClearBit() returned the wrong value", 450, bs
1433                .nextClearBit(255));
1434        assertEquals("nextClearBit() returned the wrong value", 450, bs
1435                .nextClearBit(256));
1436        assertEquals("nextClearBit() returned the wrong value", 450, bs
1437                .nextClearBit(450));
1438
1439        // bitset has 1 still the end of bs.size() -1, but calling nextClearBit
1440        // with any index value after the last true bit should return bs.size()
1441        assertEquals("nextClearBit() returned the wrong value", 512, bs
1442                .nextClearBit(451));
1443        assertEquals("nextClearBit() returned the wrong value", 512, bs
1444                .nextClearBit(511));
1445        assertEquals("nextClearBit() returned the wrong value", 512, bs
1446                .nextClearBit(512));
1447
1448        // if the index is larger than bs.size(), nextClearBit should return
1449        // index
1450        assertEquals("nextClearBit() returned the wrong value", 513, bs
1451                .nextClearBit(513));
1452        assertEquals("nextClearBit() returned the wrong value", 800, bs
1453                .nextClearBit(800));
1454    }
1455
1456    /**
1457     * @tests java.util.BitSet#isEmpty()
1458     */
1459    @TestTargetNew(
1460        level = TestLevel.COMPLETE,
1461        notes = "",
1462        method = "isEmpty",
1463        args = {}
1464    )
1465    public void test_isEmpty() {
1466        BitSet bs = new BitSet(500);
1467        assertTrue("Test: isEmpty() returned wrong value", bs.isEmpty());
1468
1469        // at bitset element 0
1470        bs.set(3);
1471        assertFalse("Test0: isEmpty() returned wrong value", bs
1472                .isEmpty());
1473
1474        // at bitset element 1
1475        bs.clear();
1476        bs.set(12);
1477        assertFalse("Test1: isEmpty() returned wrong value", bs
1478                .isEmpty());
1479
1480        // at bitset element 2
1481        bs.clear();
1482        bs.set(128);
1483        assertFalse("Test2: isEmpty() returned wrong value", bs
1484                .isEmpty());
1485
1486        // boundary testing
1487        bs.clear();
1488        bs.set(459);
1489        assertFalse("Test3: isEmpty() returned wrong value", bs
1490                .isEmpty());
1491
1492        bs.clear();
1493        bs.set(511);
1494        assertFalse("Test4: isEmpty() returned wrong value", bs
1495                .isEmpty());
1496    }
1497
1498    /**
1499     * @tests java.util.BitSet#cardinality()
1500     */
1501    @TestTargetNew(
1502        level = TestLevel.COMPLETE,
1503        notes = "",
1504        method = "cardinality",
1505        args = {}
1506    )
1507    public void test_cardinality() {
1508        // test for method int java.util.BitSet.cardinality()
1509        BitSet bs = new BitSet(500);
1510        bs.set(5);
1511        bs.set(32);
1512        bs.set(63);
1513        bs.set(64);
1514        bs.set(71, 110);
1515        bs.set(127, 130);
1516        bs.set(193);
1517        bs.set(450);
1518        assertEquals("cardinality() returned wrong value", 48, bs.cardinality());
1519
1520        bs.flip(0, 500);
1521        assertEquals("cardinality() returned wrong value", 452, bs
1522                .cardinality());
1523
1524        bs.clear();
1525        assertEquals("cardinality() returned wrong value", 0, bs.cardinality());
1526
1527        bs.set(0, 500);
1528        assertEquals("cardinality() returned wrong value", 500, bs
1529                .cardinality());
1530    }
1531
1532    /**
1533     * helper method to display the contents of a bitset
1534     *
1535     */
1536    private static void printBitset(BitSet bs) {
1537        System.out.println();
1538        for (int i = bs.size() - 1; i >= 0; i--) {
1539            if (bs.get(i))
1540                System.out.print(1);
1541            else
1542                System.out.print(0);
1543        }
1544    }
1545
1546    /**
1547     * Sets up the fixture, for example, open a network connection. This method
1548     * is called before a test is executed.
1549     */
1550    protected void setUp() {
1551
1552        eightbs = new BitSet();
1553
1554        for (int i = 0; i < 8; i++)
1555            eightbs.set(i);
1556    }
1557
1558    /**
1559     * Tears down the fixture, for example, close a network connection. This
1560     * method is called after a test is executed.
1561     */
1562    protected void tearDown() {
1563    }
1564}
1565