View Javadoc
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    * https://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, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  package org.apache.commons.lang3.util;
19  
20  import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException;
21  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotEquals;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  import static org.junit.jupiter.api.Assertions.fail;
28  
29  import java.util.BitSet;
30  
31  import org.apache.commons.lang3.AbstractLangTest;
32  import org.apache.commons.lang3.ArrayUtils;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests {@link FluentBitSet}.
38   * <p>
39   * Test code originally from Apache Harmony for FluentBitSet and adapted.
40   * </p>
41   */
42  class FluentBitSetTest extends AbstractLangTest {
43  
44      private BitSet eightBs;
45      private FluentBitSet eightFbs;
46  
47      /**
48       * BeforeEach.
49       */
50      @BeforeEach
51      public void beforeEach() {
52  
53          eightFbs = newInstance();
54  
55          for (int i = 0; i < 8; i++) {
56              eightFbs.set(i);
57          }
58          eightBs = eightFbs.bitSet();
59      }
60  
61      private FluentBitSet newInstance() {
62          return new FluentBitSet();
63      }
64  
65      private FluentBitSet newInstance(final int nbits) {
66          return new FluentBitSet(nbits);
67      }
68  
69      /**
70       * Tests {@link FluentBitSet#and(FluentBitSet)}.
71       */
72      @Test
73      void test_and() {
74          // Test for method void java.util.BitSet.and(BitSet)
75          final FluentBitSet bs = newInstance(128);
76          // Initialize the bottom half of the BitSet
77  
78          for (int i = 64; i < 128; i++) {
79              bs.set(i);
80          }
81          eightFbs.and(bs);
82          assertNotEquals(eightFbs, bs, "AND failed to clear bits");
83          eightFbs.set(3);
84          bs.set(3);
85          eightFbs.and(bs);
86          assertTrue(bs.get(3), "AND failed to maintain set bits");
87          bs.and(eightFbs);
88          for (int i = 64; i < 128; i++) {
89              assertFalse(bs.get(i), "Failed to clear extra bits in the receiver BitSet");
90          }
91      }
92  
93      /**
94       * Tests {@link FluentBitSet#and(BitSet)}.
95       */
96      @Test
97      void test_and_BitSet() {
98          // Test for method void java.util.BitSet.and(BitSet)
99          final FluentBitSet bs = newInstance(128);
100         // Initialize the bottom half of the BitSet
101 
102         for (int i = 64; i < 128; i++) {
103             bs.set(i);
104         }
105         eightFbs.and(bs.bitSet());
106         assertNotEquals(eightFbs, bs, "AND failed to clear bits");
107         eightFbs.set(3);
108         bs.set(3);
109         eightFbs.and(bs.bitSet());
110         assertTrue(bs.get(3), "AND failed to maintain set bits");
111         bs.and(eightBs);
112         for (int i = 64; i < 128; i++) {
113             assertFalse(bs.get(i), "Failed to clear extra bits in the receiver BitSet");
114         }
115     }
116 
117     /**
118      * Tests {@link FluentBitSet#andNot(BitSet)}.
119      */
120     @Test
121     void test_andNot() {
122         FluentBitSet bs = (FluentBitSet) eightFbs.clone();
123         bs.clear(5);
124         final FluentBitSet bs2 = newInstance();
125         bs2.set(2);
126         bs2.set(3);
127         bs.andNot(bs2);
128         assertEquals("{0, 1, 4, 6, 7}", bs.toString(), "Incorrect bitset after andNot");
129 
130         bs = newInstance(0);
131         bs.andNot(bs2);
132         assertEquals(0, bs.size(), "Incorrect size");
133     }
134 
135     /**
136      * Tests {@link FluentBitSet#andNot(BitSet)}.
137      */
138     @Test
139     void test_andNot_BitSet() {
140         FluentBitSet bs = (FluentBitSet) eightFbs.clone();
141         bs.clear(5);
142         final FluentBitSet bs2 = newInstance();
143         bs2.set(2);
144         bs2.set(3);
145         bs.andNot(bs2.bitSet());
146         assertEquals("{0, 1, 4, 6, 7}", bs.toString(), "Incorrect bitset after andNot");
147 
148         bs = newInstance(0);
149         bs.andNot(bs2.bitSet());
150         assertEquals(0, bs.size(), "Incorrect size");
151     }
152 
153     /**
154      * Tests {@link FluentBitSet#cardinality()}.
155      */
156     @Test
157     void test_cardinality() {
158         // test for method int java.util.BitSet.cardinality()
159         final FluentBitSet bs = newInstance(500);
160         bs.set(5);
161         bs.set(32);
162         bs.set(63);
163         bs.set(64);
164         bs.set(71, 110);
165         bs.set(127, 130);
166         bs.set(193);
167         bs.set(450);
168         assertEquals(48, bs.cardinality(), "cardinality() returned wrong value");
169 
170         bs.flip(0, 500);
171         assertEquals(452, bs.cardinality(), "cardinality() returned wrong value");
172 
173         bs.clear();
174         assertEquals(0, bs.cardinality(), "cardinality() returned wrong value");
175 
176         bs.set(0, 500);
177         assertEquals(500, bs.cardinality(), "cardinality() returned wrong value");
178     }
179 
180     /**
181      * Tests {@link FluentBitSet#clear()}.
182      */
183     @Test
184     void test_clear() {
185         eightFbs.clear();
186         for (int i = 0; i < 8; i++) {
187             assertFalse(eightFbs.get(i), "Clear didn't clear bit " + i);
188         }
189         assertEquals(0, eightFbs.length(), "Test1: Wrong length");
190 
191         final FluentBitSet bs = newInstance(3400);
192         bs.set(0, bs.size() - 1); // ensure all bits are 1's
193         bs.set(bs.size() - 1);
194         bs.clear();
195         assertEquals(0, bs.length(), "Test2: Wrong length");
196         assertTrue(bs.isEmpty(), "Test2: isEmpty() returned incorrect value");
197         assertEquals(0, bs.cardinality(), "Test2: cardinality() returned incorrect value");
198     }
199 
200     /**
201      * Tests {@link FluentBitSet#clear(int)}.
202      */
203     @Test
204     void test_clearI() {
205         // Test for method void java.util.BitSet.clear(int)
206 
207         eightFbs.clear(7);
208         assertFalse(eightFbs.get(7), "Failed to clear bit");
209 
210         // Check to see all other bits are still set
211         for (int i = 0; i < 7; i++) {
212             assertTrue(eightFbs.get(i), "Clear cleared incorrect bits");
213         }
214 
215         eightFbs.clear(165);
216         assertFalse(eightFbs.get(165), "Failed to clear bit");
217         // Try out of range
218         assertIndexOutOfBoundsException(() -> eightFbs.clear(-1));
219 
220         final FluentBitSet bs = newInstance(0);
221         assertEquals(0, bs.length(), "Test1: Wrong length,");
222         assertEquals(0, bs.size(), "Test1: Wrong size,");
223 
224         bs.clear(0);
225         assertEquals(0, bs.length(), "Test2: Wrong length,");
226         assertEquals(0, bs.size(), "Test2: Wrong size,");
227 
228         bs.clear(60);
229         assertEquals(0, bs.length(), "Test3: Wrong length,");
230         assertEquals(0, bs.size(), "Test3: Wrong size,");
231 
232         bs.clear(120);
233         assertEquals(0, bs.size(), "Test4: Wrong size,");
234         assertEquals(0, bs.length(), "Test4: Wrong length,");
235 
236         bs.set(25);
237         assertEquals(64, bs.size(), "Test5: Wrong size,");
238         assertEquals(26, bs.length(), "Test5: Wrong length,");
239 
240         bs.clear(80);
241         assertEquals(64, bs.size(), "Test6: Wrong size,");
242         assertEquals(26, bs.length(), "Test6: Wrong length,");
243 
244         bs.clear(25);
245         assertEquals(64, bs.size(), "Test7: Wrong size,");
246         assertEquals(0, bs.length(), "Test7: Wrong length,");
247     }
248 
249     /**
250      * Tests {@link FluentBitSet#clear(int, int)}.
251      */
252     @Test
253     void test_clearII() {
254         // Regression for HARMONY-98
255         final FluentBitSet bitset = newInstance();
256         for (int i = 0; i < 20; i++) {
257             bitset.set(i);
258         }
259         bitset.clear(10, 10);
260 
261         // Test for method void java.util.BitSet.clear(int, int)
262         // pos1 and pos2 are in the same bitset element
263         FluentBitSet bs = newInstance(16);
264         int initialSize = bs.size();
265         bs.set(0, initialSize);
266         bs.clear(5);
267         bs.clear(15);
268         bs.clear(7, 11);
269         for (int i = 0; i < 7; i++) {
270             if (i == 5) {
271                 assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
272             } else {
273                 assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
274             }
275         }
276         for (int i = 7; i < 11; i++) {
277             assertFalse(bs.get(i), "Failed to clear bit " + i);
278         }
279 
280         for (int i = 11; i < initialSize; i++) {
281             if (i == 15) {
282                 assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
283             } else {
284                 assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
285             }
286         }
287 
288         for (int i = initialSize; i < bs.size(); i++) {
289             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
290         }
291 
292         // pos1 and pos2 is in the same bitset element, boundary testing
293         bs = newInstance(16);
294         initialSize = bs.size();
295         bs.set(0, initialSize);
296         bs.clear(7, 64);
297         assertEquals(64, bs.size(), "Failed to grow BitSet");
298         for (int i = 0; i < 7; i++) {
299             assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
300         }
301         for (int i = 7; i < 64; i++) {
302             assertFalse(bs.get(i), "Failed to clear bit " + i);
303         }
304         for (int i = 64; i < bs.size(); i++) {
305             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
306         }
307         // more boundary testing
308         bs = newInstance(32);
309         initialSize = bs.size();
310         bs.set(0, initialSize);
311         bs.clear(0, 64);
312         for (int i = 0; i < 64; i++) {
313             assertFalse(bs.get(i), "Failed to clear bit " + i);
314         }
315         for (int i = 64; i < bs.size(); i++) {
316             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
317         }
318 
319         bs = newInstance(32);
320         initialSize = bs.size();
321         bs.set(0, initialSize);
322         bs.clear(0, 65);
323         for (int i = 0; i < 65; i++) {
324             assertFalse(bs.get(i), "Failed to clear bit " + i);
325         }
326         for (int i = 65; i < bs.size(); i++) {
327             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
328         }
329 
330         // pos1 and pos2 are in two sequential bitset elements
331         bs = newInstance(128);
332         initialSize = bs.size();
333         bs.set(0, initialSize);
334         bs.clear(7);
335         bs.clear(110);
336         bs.clear(9, 74);
337         for (int i = 0; i < 9; i++) {
338             if (i == 7) {
339                 assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
340             } else {
341                 assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
342             }
343         }
344         for (int i = 9; i < 74; i++) {
345             assertFalse(bs.get(i), "Failed to clear bit " + i);
346         }
347         for (int i = 74; i < initialSize; i++) {
348             if (i == 110) {
349                 assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
350             } else {
351                 assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
352             }
353         }
354         for (int i = initialSize; i < bs.size(); i++) {
355             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
356         }
357 
358         // pos1 and pos2 are in two non-sequential bitset elements
359         bs = newInstance(256);
360         bs.set(0, 256);
361         bs.clear(7);
362         bs.clear(255);
363         bs.clear(9, 219);
364         for (int i = 0; i < 9; i++) {
365             if (i == 7) {
366                 assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
367             } else {
368                 assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
369             }
370         }
371 
372         for (int i = 9; i < 219; i++) {
373             assertFalse(bs.get(i), "failed to clear bit " + i);
374         }
375 
376         for (int i = 219; i < 255; i++) {
377             assertTrue(bs.get(i), "Shouldn't have cleared bit " + i);
378         }
379 
380         for (int i = 255; i < bs.size(); i++) {
381             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
382         }
383 
384         // test illegal args
385         bs = newInstance(10);
386         assertIndexOutOfBoundsException(() -> newInstance(10).clear(-1, 3),
387             "Test1: Attempt to flip with negative index failed to generate exception");
388 
389         assertIndexOutOfBoundsException(() -> newInstance(10).clear(2, -1),
390             "Test2: Attempt to flip with negative index failed to generate exception");
391 
392         bs.set(2, 4);
393         bs.clear(2, 2);
394         assertTrue(bs.get(2), "Bit got cleared incorrectly ");
395 
396         assertIndexOutOfBoundsException(() -> newInstance(10).clear(4, 2),
397             "Test4: Attempt to flip with illegal args failed to generate exception");
398 
399         bs = newInstance(0);
400         assertEquals(0, bs.length(), "Test1: Wrong length,");
401         assertEquals(0, bs.size(), "Test1: Wrong size,");
402 
403         bs.clear(0, 2);
404         assertEquals(0, bs.length(), "Test2: Wrong length,");
405         assertEquals(0, bs.size(), "Test2: Wrong size,");
406 
407         bs.clear(60, 64);
408         assertEquals(0, bs.length(), "Test3: Wrong length,");
409         assertEquals(0, bs.size(), "Test3: Wrong size,");
410 
411         bs.clear(64, 120);
412         assertEquals(0, bs.length(), "Test4: Wrong length,");
413         assertEquals(0, bs.size(), "Test4: Wrong size,");
414 
415         bs.set(25);
416         assertEquals(26, bs.length(), "Test5: Wrong length,");
417         assertEquals(64, bs.size(), "Test5: Wrong size,");
418 
419         bs.clear(60, 64);
420         assertEquals(26, bs.length(), "Test6: Wrong length,");
421         assertEquals(64, bs.size(), "Test6: Wrong size,");
422 
423         bs.clear(64, 120);
424         assertEquals(64, bs.size(), "Test7: Wrong size,");
425         assertEquals(26, bs.length(), "Test7: Wrong length,");
426 
427         bs.clear(80);
428         assertEquals(64, bs.size(), "Test8: Wrong size,");
429         assertEquals(26, bs.length(), "Test8: Wrong length,");
430 
431         bs.clear(25);
432         assertEquals(64, bs.size(), "Test9: Wrong size,");
433         assertEquals(0, bs.length(), "Test9: Wrong length,");
434     }
435 
436     /**
437      * Tests {@link FluentBitSet#clear(int...)}.
438      */
439     @Test
440     void test_clearIntArray() {
441         // Test for method void java.util.BitSet.clear(int)
442 
443         eightFbs.clear(new int[] {7});
444         assertFalse(eightFbs.get(7), "Failed to clear bit");
445 
446         // Check to see all other bits are still set
447         for (int i = 0; i < 7; i++) {
448             assertTrue(eightFbs.get(i), "Clear cleared incorrect bits");
449         }
450 
451         eightFbs.clear(165);
452         assertFalse(eightFbs.get(165), "Failed to clear bit");
453         // Try out of range
454         assertIndexOutOfBoundsException(() -> eightFbs.clear(-1));
455 
456         final FluentBitSet bs = newInstance(0);
457         assertEquals(0, bs.length(), "Test1: Wrong length,");
458         assertEquals(0, bs.size(), "Test1: Wrong size,");
459 
460         bs.clear(new int[] {0});
461         assertEquals(0, bs.length(), "Test2: Wrong length,");
462         assertEquals(0, bs.size(), "Test2: Wrong size,");
463 
464         bs.clear(new int[] {60});
465         assertEquals(0, bs.length(), "Test3: Wrong length,");
466         assertEquals(0, bs.size(), "Test3: Wrong size,");
467 
468         bs.clear(new int[] {120});
469         assertEquals(0, bs.size(), "Test4: Wrong size,");
470         assertEquals(0, bs.length(), "Test4: Wrong length,");
471 
472         bs.set(25);
473         assertEquals(64, bs.size(), "Test5: Wrong size,");
474         assertEquals(26, bs.length(), "Test5: Wrong length,");
475 
476         bs.clear(new int[] {80});
477         assertEquals(64, bs.size(), "Test6: Wrong size,");
478         assertEquals(26, bs.length(), "Test6: Wrong length,");
479 
480         bs.clear(new int[] {25});
481         assertEquals(64, bs.size(), "Test7: Wrong size,");
482         assertEquals(0, bs.length(), "Test7: Wrong length,");
483     }
484 
485     /**
486      * Tests FluentBitSet#clone()
487      */
488     @Test
489     void test_clone() {
490         final FluentBitSet bs = (FluentBitSet) eightFbs.clone();
491         assertEquals(bs, eightFbs, "clone failed to return equal BitSet");
492     }
493 
494     /**
495      * Tests {@link FluentBitSet#FluentBitSet()}.
496      */
497     @Test
498     void test_Constructor() {
499         final FluentBitSet bs = newInstance();
500         assertEquals(64, bs.size(), "Create FluentBitSet of incorrect size");
501         assertEquals("{}", bs.toString(), "New FluentBitSet had invalid string representation");
502     }
503 
504     /**
505      * Tests {@link FluentBitSet#FluentBitSet(int)}.
506      */
507     @Test
508     void test_ConstructorInt() {
509         FluentBitSet bs = newInstance(128);
510         assertEquals(128, bs.size(), "Create FluentBitSet of incorrect size");
511         assertEquals("{}", bs.toString(), "New FluentBitSet had invalid string representation: " + bs);
512         // All BitSets are created with elements of multiples of 64
513         bs = newInstance(89);
514         assertEquals(128, bs.size(), "Failed to round FluentBitSet element size");
515 
516         assertThrows(NegativeArraySizeException.class, () -> newInstance(-9));
517     }
518 
519     /**
520      * Tests {@link FluentBitSet#equals(java.lang.Object)}.
521      */
522     @Test
523     void test_equals() {
524         FluentBitSet bs;
525         bs = (FluentBitSet) eightFbs.clone();
526         assertEquals(eightFbs, eightFbs, "Same FluentBitSet returned false");
527         assertEquals(bs, eightFbs, "Identical FluentBitSet returned false");
528         bs.clear(6);
529         assertNotEquals(bs, eightFbs, "Different BitSets returned true");
530         assertFalse(eightFbs.equals(null), "Different BitSets returned true");
531         assertFalse(eightFbs.equals(new Object()), "Different BitSets returned true");
532 
533         bs = (FluentBitSet) eightFbs.clone();
534         bs.set(128);
535         assertNotEquals(bs, eightFbs, "Different sized FluentBitSet with higher bit set returned true");
536         bs.clear(128);
537         assertEquals(bs, eightFbs, "Different sized FluentBitSet with higher bits not set returned false");
538     }
539 
540     /**
541      * Tests {@link FluentBitSet#flip(int)}.
542      */
543     @Test
544     void test_flipI() {
545         // Test for method void java.util.BitSet.flip(int)
546         FluentBitSet bs = newInstance();
547         bs.clear(8);
548         bs.clear(9);
549         bs.set(10);
550         bs.flip(9);
551         assertFalse(bs.get(8), "Failed to flip bit");
552         assertTrue(bs.get(9), "Failed to flip bit");
553         assertTrue(bs.get(10), "Failed to flip bit");
554 
555         bs.set(8);
556         bs.set(9);
557         bs.clear(10);
558         bs.flip(9);
559         assertTrue(bs.get(8), "Failed to flip bit");
560         assertFalse(bs.get(9), "Failed to flip bit");
561         assertFalse(bs.get(10), "Failed to flip bit");
562 
563         assertIndexOutOfBoundsException(() -> newInstance().flip(-1), "Attempt to flip at negative index failed to generate exception");
564 
565         // Try setting a bit on a 64 boundary
566         bs.flip(128);
567         assertEquals(192, bs.size(), "Failed to grow BitSet");
568         assertTrue(bs.get(128), "Failed to flip bit");
569 
570         bs = newInstance(64);
571         for (int i = bs.size(); --i >= 0;) {
572             bs.flip(i);
573             assertTrue(bs.get(i), "Test1: Incorrectly flipped bit" + i);
574             assertEquals(i + 1, bs.length(), "Incorrect length");
575             for (int j = bs.size(); --j > i;) {
576                 assertFalse(bs.get(j), "Test2: Incorrectly flipped bit" + j);
577             }
578             for (int j = i; --j >= 0;) {
579                 assertFalse(bs.get(j), "Test3: Incorrectly flipped bit" + j);
580             }
581             bs.flip(i);
582         }
583 
584         final FluentBitSet bs0 = newInstance(0);
585         assertEquals(0, bs0.size(), "Test1: Wrong size");
586         assertEquals(0, bs0.length(), "Test1: Wrong length");
587 
588         bs0.flip(0);
589         assertEquals(bs0.size(), 64, "Test2: Wrong size");
590         assertEquals(1, bs0.length(), "Test2: Wrong length");
591 
592         bs0.flip(63);
593         assertEquals(64, bs0.size(), "Test3: Wrong size");
594         assertEquals(64, bs0.length(), "Test3: Wrong length");
595 
596         eightFbs.flip(7);
597         assertFalse(eightFbs.get(7), "Failed to flip bit 7");
598 
599         // Check to see all other bits are still set
600         for (int i = 0; i < 7; i++) {
601             assertTrue(eightFbs.get(i), "Flip flipped incorrect bits");
602         }
603 
604         eightFbs.flip(127);
605         assertTrue(eightFbs.get(127), "Failed to flip bit 127");
606 
607         eightFbs.flip(127);
608         assertFalse(eightFbs.get(127), "Failed to flip bit 127");
609     }
610 
611     /**
612      * Tests {@link FluentBitSet#clear(int, int)}.
613      */
614     @Test
615     void test_flipII() {
616         final FluentBitSet bitset = newInstance();
617         for (int i = 0; i < 20; i++) {
618             bitset.set(i);
619         }
620         bitset.flip(10, 10);
621 
622         // Test for method void java.util.BitSet.flip(int, int)
623         // pos1 and pos2 are in the same bitset element
624         FluentBitSet bs = newInstance(16);
625         bs.set(7);
626         bs.set(10);
627         bs.flip(7, 11);
628         for (int i = 0; i < 7; i++) {
629             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
630         }
631         assertFalse(bs.get(7), "Failed to flip bit 7");
632         assertTrue(bs.get(8), "Failed to flip bit 8");
633         assertTrue(bs.get(9), "Failed to flip bit 9");
634         assertFalse(bs.get(10), "Failed to flip bit 10");
635         for (int i = 11; i < bs.size(); i++) {
636             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
637         }
638 
639         // pos1 and pos2 is in the same bitset element, boundary testing
640         bs = newInstance(16);
641         bs.set(7);
642         bs.set(10);
643         bs.flip(7, 64);
644         assertEquals(64, bs.size(), "Failed to grow BitSet");
645         for (int i = 0; i < 7; i++) {
646             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
647         }
648         assertFalse(bs.get(7), "Failed to flip bit 7");
649         assertTrue(bs.get(8), "Failed to flip bit 8");
650         assertTrue(bs.get(9), "Failed to flip bit 9");
651         assertFalse(bs.get(10), "Failed to flip bit 10");
652         for (int i = 11; i < 64; i++) {
653             assertTrue(bs.get(i), "failed to flip bit " + i);
654         }
655         assertFalse(bs.get(64), "Shouldn't have flipped bit 64");
656 
657         // more boundary testing
658         bs = newInstance(32);
659         bs.flip(0, 64);
660         for (int i = 0; i < 64; i++) {
661             assertTrue(bs.get(i), "Failed to flip bit " + i);
662         }
663         assertFalse(bs.get(64), "Shouldn't have flipped bit 64");
664 
665         bs = newInstance(32);
666         bs.flip(0, 65);
667         for (int i = 0; i < 65; i++) {
668             assertTrue(bs.get(i), "Failed to flip bit " + i);
669         }
670         assertFalse(bs.get(65), "Shouldn't have flipped bit 65");
671 
672         // pos1 and pos2 are in two sequential bitset elements
673         bs = newInstance(128);
674         bs.set(7);
675         bs.set(10);
676         bs.set(72);
677         bs.set(110);
678         bs.flip(9, 74);
679         for (int i = 0; i < 7; i++) {
680             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
681         }
682         assertTrue(bs.get(7), "Shouldn't have flipped bit 7");
683         assertFalse(bs.get(8), "Shouldn't have flipped bit 8");
684         assertTrue(bs.get(9), "Failed to flip bit 9");
685         assertFalse(bs.get(10), "Failed to flip bit 10");
686         for (int i = 11; i < 72; i++) {
687             assertTrue(bs.get(i), "failed to flip bit " + i);
688         }
689         assertFalse(bs.get(72), "Failed to flip bit 72");
690         assertTrue(bs.get(73), "Failed to flip bit 73");
691         for (int i = 74; i < 110; i++) {
692             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
693         }
694         assertTrue(bs.get(110), "Shouldn't have flipped bit 110");
695         for (int i = 111; i < bs.size(); i++) {
696             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
697         }
698 
699         // pos1 and pos2 are in two non-sequential bitset elements
700         bs = newInstance(256);
701         bs.set(7);
702         bs.set(10);
703         bs.set(72);
704         bs.set(110);
705         bs.set(181);
706         bs.set(220);
707         bs.flip(9, 219);
708         for (int i = 0; i < 7; i++) {
709             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
710         }
711         assertTrue(bs.get(7), "Shouldn't have flipped bit 7");
712         assertFalse(bs.get(8), "Shouldn't have flipped bit 8");
713         assertTrue(bs.get(9), "Failed to flip bit 9");
714         assertFalse(bs.get(10), "Failed to flip bit 10");
715         for (int i = 11; i < 72; i++) {
716             assertTrue(bs.get(i), "failed to flip bit " + i);
717         }
718         assertFalse(bs.get(72), "Failed to flip bit 72");
719         for (int i = 73; i < 110; i++) {
720             assertTrue(bs.get(i), "failed to flip bit " + i);
721         }
722         assertFalse(bs.get(110), "Failed to flip bit 110");
723         for (int i = 111; i < 181; i++) {
724             assertTrue(bs.get(i), "failed to flip bit " + i);
725         }
726         assertFalse(bs.get(181), "Failed to flip bit 181");
727         for (int i = 182; i < 219; i++) {
728             assertTrue(bs.get(i), "failed to flip bit " + i);
729         }
730         assertFalse(bs.get(219), "Shouldn't have flipped bit 219");
731         assertTrue(bs.get(220), "Shouldn't have flipped bit 220");
732         for (int i = 221; i < bs.size(); i++) {
733             assertFalse(bs.get(i), "Shouldn't have flipped bit " + i);
734         }
735 
736         // test illegal args
737         bs = newInstance(10);
738         try {
739             bs.flip(-1, 3);
740             fail("Test1: Attempt to flip with  negative index failed to generate exception");
741         } catch (final IndexOutOfBoundsException e) {
742             // correct behavior
743         }
744 
745         try {
746             bs.flip(2, -1);
747             fail("Test2: Attempt to flip with negative index failed to generate exception");
748         } catch (final IndexOutOfBoundsException e) {
749             // correct behavior
750         }
751 
752         try {
753             bs.flip(4, 2);
754             fail("Test4: Attempt to flip with illegal args failed to generate exception");
755         } catch (final IndexOutOfBoundsException e) {
756             // correct behavior
757         }
758     }
759 
760     /**
761      * Tests {@link FluentBitSet#get(int)}.
762      */
763     @Test
764     void test_getI() {
765         // Test for method boolean java.util.BitSet.get(int)
766 
767         FluentBitSet bs = newInstance();
768         bs.set(8);
769         assertFalse(eightFbs.get(99), "Get returned true for index out of range");
770         assertTrue(eightFbs.get(3), "Get returned false for set value");
771         assertFalse(bs.get(0), "Get returned true for a non set value");
772 
773         assertIndexOutOfBoundsException(() -> newInstance().get(-1), "Attempt to get at negative index failed to generate exception");
774 
775         bs = newInstance(1);
776         assertFalse(bs.get(64), "Access greater than size");
777 
778         bs = newInstance();
779         bs.set(63);
780         assertTrue(bs.get(63), "Test highest bit");
781 
782         bs = newInstance(0);
783         assertEquals(0, bs.length(), "Test1: Wrong length,");
784         assertEquals(0, bs.size(), "Test1: Wrong size,");
785 
786         bs.get(2);
787         assertEquals(0, bs.length(), "Test2: Wrong length,");
788         assertEquals(0, bs.size(), "Test2: Wrong size,");
789 
790         bs.get(70);
791         assertEquals(0, bs.length(), "Test3: Wrong length,");
792         assertEquals(0, bs.size(), "Test3: Wrong size,");
793 
794     }
795 
796     /**
797      * Tests {@link FluentBitSet#get(int, int)}.
798      */
799     @Test
800     void test_getII() {
801         final FluentBitSet bitset = newInstance(30);
802         bitset.get(3, 3);
803 
804         // Test for method boolean java.util.BitSet.get(int, int)
805         FluentBitSet bs = newInstance(512);
806         bs.set(3, 9);
807         bs.set(10, 20);
808         bs.set(60, 75);
809         bs.set(121);
810         bs.set(130, 140);
811 
812         // pos1 and pos2 are in the same bitset element, at index0
813         FluentBitSet resultbs = bs.get(3, 6);
814         FluentBitSet correctbs = newInstance(3);
815         correctbs.set(0, 3);
816         assertEquals(correctbs, resultbs, "Test1: Returned incorrect BitSet");
817 
818         // pos1 and pos2 are in the same bitset element, at index 1
819         resultbs = bs.get(100, 125);
820         correctbs = newInstance(25);
821         correctbs.set(21);
822         assertEquals(correctbs, resultbs, "Test2: Returned incorrect BitSet");
823 
824         // pos1 in bitset element at index 0, and pos2 in bitset element at
825         // index 1
826         resultbs = bs.get(15, 125);
827         correctbs = newInstance(25);
828         correctbs.set(0, 5);
829         correctbs.set(45, 60);
830         correctbs.set(121 - 15);
831         assertEquals(correctbs, resultbs, "Test3: Returned incorrect BitSet");
832 
833         // pos1 in bitset element at index 1, and pos2 in bitset element at
834         // index 2
835         resultbs = bs.get(70, 145);
836         correctbs = newInstance(75);
837         correctbs.set(0, 5);
838         correctbs.set(51);
839         correctbs.set(60, 70);
840         assertEquals(correctbs, resultbs, "Test4: Returned incorrect BitSet");
841 
842         // pos1 in bitset element at index 0, and pos2 in bitset element at
843         // index 2
844         resultbs = bs.get(5, 145);
845         correctbs = newInstance(140);
846         correctbs.set(0, 4);
847         correctbs.set(5, 15);
848         correctbs.set(55, 70);
849         correctbs.set(116);
850         correctbs.set(125, 135);
851         assertEquals(correctbs, resultbs, "Test5: Returned incorrect BitSet");
852 
853         // pos1 in bitset element at index 0, and pos2 in bitset element at
854         // index 3
855         resultbs = bs.get(5, 250);
856         correctbs = newInstance(200);
857         correctbs.set(0, 4);
858         correctbs.set(5, 15);
859         correctbs.set(55, 70);
860         correctbs.set(116);
861         correctbs.set(125, 135);
862         assertEquals(correctbs, resultbs, "Test6: Returned incorrect BitSet");
863 
864         assertEquals(bs.get(0, bs.size()), bs, "equality principle 1 ");
865 
866         // more tests
867         FluentBitSet bs2 = newInstance(129);
868         bs2.set(0, 20);
869         bs2.set(62, 65);
870         bs2.set(121, 123);
871         resultbs = bs2.get(1, 124);
872         correctbs = newInstance(129);
873         correctbs.set(0, 19);
874         correctbs.set(61, 64);
875         correctbs.set(120, 122);
876         assertEquals(correctbs, resultbs, "Test7: Returned incorrect BitSet");
877 
878         // equality principle with some boundary conditions
879         bs2 = newInstance(128);
880         bs2.set(2, 20);
881         bs2.set(62);
882         bs2.set(121, 123);
883         bs2.set(127);
884         resultbs = bs2.get(0, bs2.size());
885         assertEquals(resultbs, bs2, "equality principle 2 ");
886 
887         bs2 = newInstance(128);
888         bs2.set(2, 20);
889         bs2.set(62);
890         bs2.set(121, 123);
891         bs2.set(127);
892         bs2.flip(0, 128);
893         resultbs = bs2.get(0, bs.size());
894         assertEquals(resultbs, bs2, "equality principle 3 ");
895 
896         bs = newInstance(0);
897         assertEquals(0, bs.length(), "Test1: Wrong length,");
898         assertEquals(0, bs.size(), "Test1: Wrong size,");
899 
900         bs.get(0, 2);
901         assertEquals(0, bs.length(), "Test2: Wrong length,");
902         assertEquals(0, bs.size(), "Test2: Wrong size,");
903 
904         bs.get(60, 64);
905         assertEquals(0, bs.length(), "Test3: Wrong length,");
906         assertEquals(0, bs.size(), "Test3: Wrong size,");
907 
908         bs.get(64, 120);
909         assertEquals(0, bs.length(), "Test4: Wrong length,");
910         assertEquals(0, bs.size(), "Test4: Wrong size,");
911 
912         bs.set(25);
913         assertEquals(26, bs.length(), "Test5: Wrong length,");
914         assertEquals(64, bs.size(), "Test5: Wrong size,");
915 
916         bs.get(60, 64);
917         assertEquals(26, bs.length(), "Test6: Wrong length,");
918         assertEquals(64, bs.size(), "Test6: Wrong size,");
919 
920         bs.get(64, 120);
921         assertEquals(64, bs.size(), "Test7: Wrong size,");
922         assertEquals(26, bs.length(), "Test7: Wrong length,");
923 
924         bs.get(80);
925         assertEquals(64, bs.size(), "Test8: Wrong size,");
926         assertEquals(26, bs.length(), "Test8: Wrong length,");
927 
928         bs.get(25);
929         assertEquals(64, bs.size(), "Test9: Wrong size,");
930         assertEquals(26, bs.length(), "Test9: Wrong length,");
931 
932     }
933 
934     /**
935      * Tests {@link FluentBitSet#hashCode()}.
936      */
937     @Test
938     void test_hashCode() {
939         // Test for method int java.util.BitSet.hashCode()
940         final FluentBitSet bs = (FluentBitSet) eightFbs.clone();
941         bs.clear(2);
942         bs.clear(6);
943         assertEquals(bs.bitSet().hashCode(), bs.hashCode(), "BitSet returns wrong hash value");
944         bs.set(10);
945         bs.clear(3);
946         assertEquals(97, bs.hashCode(), "BitSet returns wrong hash value");
947     }
948 
949     /**
950      * Tests {@link FluentBitSet#intersects(FluentBitSet)}.
951      */
952     @Test
953     void test_intersects() {
954         // Test for method boolean java.util.BitSet.intersects(BitSet)
955         final FluentBitSet bs = newInstance(500);
956         bs.set(5);
957         bs.set(63);
958         bs.set(64);
959         bs.set(71, 110);
960         bs.set(127, 130);
961         bs.set(192);
962         bs.set(450);
963 
964         final FluentBitSet bs2 = newInstance(8);
965         assertFalse(bs.intersects(bs2), "Test1: intersects() returned incorrect value");
966         assertFalse(bs2.intersects(bs), "Test1: intersects() returned incorrect value");
967 
968         bs2.set(4);
969         assertFalse(bs.intersects(bs2), "Test2: intersects() returned incorrect value");
970         assertFalse(bs2.intersects(bs), "Test2: intersects() returned incorrect value");
971 
972         bs2.clear();
973         bs2.set(5);
974         assertTrue(bs.intersects(bs2), "Test3: intersects() returned incorrect value");
975         assertTrue(bs2.intersects(bs), "Test3: intersects() returned incorrect value");
976 
977         bs2.clear();
978         bs2.set(63);
979         assertTrue(bs.intersects(bs2), "Test4: intersects() returned incorrect value");
980         assertTrue(bs2.intersects(bs), "Test4: intersects() returned incorrect value");
981 
982         bs2.clear();
983         bs2.set(80);
984         assertTrue(bs.intersects(bs2), "Test5: intersects() returned incorrect value");
985         assertTrue(bs2.intersects(bs), "Test5: intersects() returned incorrect value");
986 
987         bs2.clear();
988         bs2.set(127);
989         assertTrue(bs.intersects(bs2), "Test6: intersects() returned incorrect value");
990         assertTrue(bs2.intersects(bs), "Test6: intersects() returned incorrect value");
991 
992         bs2.clear();
993         bs2.set(192);
994         assertTrue(bs.intersects(bs2), "Test7: intersects() returned incorrect value");
995         assertTrue(bs2.intersects(bs), "Test7: intersects() returned incorrect value");
996 
997         bs2.clear();
998         bs2.set(450);
999         assertTrue(bs.intersects(bs2), "Test8: intersects() returned incorrect value");
1000         assertTrue(bs2.intersects(bs), "Test8: intersects() returned incorrect value");
1001 
1002         bs2.clear();
1003         bs2.set(500);
1004         assertFalse(bs.intersects(bs2), "Test9: intersects() returned incorrect value");
1005         assertFalse(bs2.intersects(bs), "Test9: intersects() returned incorrect value");
1006     }
1007 
1008     /**
1009      * Tests {@link FluentBitSet#intersects(BitSet)}.
1010      */
1011     @Test
1012     void test_intersects_BitSet() {
1013         // Test for method boolean java.util.BitSet.intersects(BitSet)
1014         final FluentBitSet bs = newInstance(500);
1015         bs.set(5);
1016         bs.set(63);
1017         bs.set(64);
1018         bs.set(71, 110);
1019         bs.set(127, 130);
1020         bs.set(192);
1021         bs.set(450);
1022 
1023         final FluentBitSet bs2 = newInstance(8);
1024         assertFalse(bs.intersects(bs2.bitSet()), "Test1: intersects() returned incorrect value");
1025         assertFalse(bs2.intersects(bs.bitSet()), "Test1: intersects() returned incorrect value");
1026 
1027         bs2.set(4);
1028         assertFalse(bs.intersects(bs2.bitSet()), "Test2: intersects() returned incorrect value");
1029         assertFalse(bs2.intersects(bs.bitSet()), "Test2: intersects() returned incorrect value");
1030 
1031         bs2.clear();
1032         bs2.set(5);
1033         assertTrue(bs.intersects(bs2.bitSet()), "Test3: intersects() returned incorrect value");
1034         assertTrue(bs2.intersects(bs.bitSet()), "Test3: intersects() returned incorrect value");
1035 
1036         bs2.clear();
1037         bs2.set(63);
1038         assertTrue(bs.intersects(bs2.bitSet()), "Test4: intersects() returned incorrect value");
1039         assertTrue(bs2.intersects(bs.bitSet()), "Test4: intersects() returned incorrect value");
1040 
1041         bs2.clear();
1042         bs2.set(80);
1043         assertTrue(bs.intersects(bs2.bitSet()), "Test5: intersects() returned incorrect value");
1044         assertTrue(bs2.intersects(bs.bitSet()), "Test5: intersects() returned incorrect value");
1045 
1046         bs2.clear();
1047         bs2.set(127);
1048         assertTrue(bs.intersects(bs2.bitSet()), "Test6: intersects() returned incorrect value");
1049         assertTrue(bs2.intersects(bs.bitSet()), "Test6: intersects() returned incorrect value");
1050 
1051         bs2.clear();
1052         bs2.set(192);
1053         assertTrue(bs.intersects(bs2.bitSet()), "Test7: intersects() returned incorrect value");
1054         assertTrue(bs2.intersects(bs.bitSet()), "Test7: intersects() returned incorrect value");
1055 
1056         bs2.clear();
1057         bs2.set(450);
1058         assertTrue(bs.intersects(bs2.bitSet()), "Test8: intersects() returned incorrect value");
1059         assertTrue(bs2.intersects(bs.bitSet()), "Test8: intersects() returned incorrect value");
1060 
1061         bs2.clear();
1062         bs2.set(500);
1063         assertFalse(bs.intersects(bs2.bitSet()), "Test9: intersects() returned incorrect value");
1064         assertFalse(bs2.intersects(bs.bitSet()), "Test9: intersects() returned incorrect value");
1065     }
1066 
1067     /**
1068      * Tests {@link FluentBitSet#isEmpty()}.
1069      */
1070     @Test
1071     void test_isEmpty() {
1072         final FluentBitSet bs = newInstance(500);
1073         assertTrue(bs.isEmpty(), "Test: isEmpty() returned wrong value");
1074 
1075         // at bitset element 0
1076         bs.set(3);
1077         assertFalse(bs.isEmpty(), "Test0: isEmpty() returned wrong value");
1078 
1079         // at bitset element 1
1080         bs.clear();
1081         bs.set(12);
1082         assertFalse(bs.isEmpty(), "Test1: isEmpty() returned wrong value");
1083 
1084         // at bitset element 2
1085         bs.clear();
1086         bs.set(128);
1087         assertFalse(bs.isEmpty(), "Test2: isEmpty() returned wrong value");
1088 
1089         // boundary testing
1090         bs.clear();
1091         bs.set(459);
1092         assertFalse(bs.isEmpty(), "Test3: isEmpty() returned wrong value");
1093 
1094         bs.clear();
1095         bs.set(511);
1096         assertFalse(bs.isEmpty(), "Test4: isEmpty() returned wrong value");
1097     }
1098 
1099     /**
1100      * Tests {@link FluentBitSet#length()}.
1101      */
1102     @Test
1103     void test_length() {
1104         final FluentBitSet bs = newInstance();
1105         assertEquals(0, bs.length(), "BitSet returned wrong length");
1106         bs.set(5);
1107         assertEquals(6, bs.length(), "BitSet returned wrong length");
1108         bs.set(10);
1109         assertEquals(11, bs.length(), "BitSet returned wrong length");
1110         bs.set(432);
1111         assertEquals(433, bs.length(), "BitSet returned wrong length");
1112         bs.set(300);
1113         assertEquals(433, bs.length(), "BitSet returned wrong length");
1114     }
1115 
1116     /**
1117      * Tests {@link FluentBitSet#nextClearBit(int)}.
1118      */
1119     @Test
1120     void test_nextClearBitI() {
1121         // Test for method int java.util.BitSet.nextSetBit()
1122         final FluentBitSet bs = newInstance(500);
1123         bs.set(0, bs.size() - 1); // ensure all the bits from 0 to bs.size()
1124                                   // -1
1125         bs.set(bs.size() - 1); // are set to true
1126         bs.clear(5);
1127         bs.clear(32);
1128         bs.clear(63);
1129         bs.clear(64);
1130         bs.clear(71, 110);
1131         bs.clear(127, 130);
1132         bs.clear(193);
1133         bs.clear(450);
1134         try {
1135             bs.nextClearBit(-1);
1136             fail("Expected IndexOutOfBoundsException for negative index");
1137         } catch (final IndexOutOfBoundsException e) {
1138             // correct behavior
1139         }
1140         assertEquals(5, bs.nextClearBit(0), "nextClearBit() returned the wrong value");
1141         assertEquals(5, bs.nextClearBit(5), "nextClearBit() returned the wrong value");
1142         assertEquals(32, bs.nextClearBit(6), "nextClearBit() returned the wrong value");
1143         assertEquals(32, bs.nextClearBit(32), "nextClearBit() returned the wrong value");
1144         assertEquals(63, bs.nextClearBit(33), "nextClearBit() returned the wrong value");
1145 
1146         // boundary tests
1147         assertEquals(63, bs.nextClearBit(63), "nextClearBit() returned the wrong value");
1148         assertEquals(64, bs.nextClearBit(64), "nextClearBit() returned the wrong value");
1149 
1150         // at bitset element 1
1151         assertEquals(71, bs.nextClearBit(65), "nextClearBit() returned the wrong value");
1152         assertEquals(71, bs.nextClearBit(71), "nextClearBit() returned the wrong value");
1153         assertEquals(72, bs.nextClearBit(72), "nextClearBit() returned the wrong value");
1154         assertEquals(127, bs.nextClearBit(110), "nextClearBit() returned the wrong value");
1155 
1156         // boundary tests
1157         assertEquals(127, bs.nextClearBit(127), "nextClearBit() returned the wrong value");
1158         assertEquals(128, bs.nextClearBit(128), "nextClearBit() returned the wrong value");
1159 
1160         // at bitset element 2
1161         assertEquals(193, bs.nextClearBit(130), "nextClearBit() returned the wrong value");
1162         assertEquals(193, bs.nextClearBit(191), "nextClearBit() returned the wrong value");
1163 
1164         assertEquals(193, bs.nextClearBit(192), "nextClearBit() returned the wrong value");
1165         assertEquals(193, bs.nextClearBit(193), "nextClearBit() returned the wrong value");
1166         assertEquals(450, bs.nextClearBit(194), "nextClearBit() returned the wrong value");
1167         assertEquals(450, bs.nextClearBit(255), "nextClearBit() returned the wrong value");
1168         assertEquals(450, bs.nextClearBit(256), "nextClearBit() returned the wrong value");
1169         assertEquals(450, bs.nextClearBit(450), "nextClearBit() returned the wrong value");
1170 
1171         // bitset has 1 still the end of bs.size() -1, but calling nextClearBit
1172         // with any index value
1173         // after the last true bit should return bs.size(),
1174         assertEquals(512, bs.nextClearBit(451), "nextClearBit() returned the wrong value");
1175         assertEquals(512, bs.nextClearBit(511), "nextClearBit() returned the wrong value");
1176         assertEquals(512, bs.nextClearBit(512), "nextClearBit() returned the wrong value");
1177 
1178         // if the index is larger than bs.size(), nextClearBit should return
1179         // index;
1180         assertEquals(513, bs.nextClearBit(513), "nextClearBit() returned the wrong value");
1181         assertEquals(800, bs.nextClearBit(800), "nextClearBit() returned the wrong value");
1182     }
1183 
1184     /**
1185      * Tests {@link FluentBitSet#nextSetBit(int)}.
1186      */
1187     @Test
1188     void test_nextSetBitI() {
1189         // Test for method int java.util.BitSet.nextSetBit()
1190         final FluentBitSet bs = newInstance(500);
1191         bs.set(5);
1192         bs.set(32);
1193         bs.set(63);
1194         bs.set(64);
1195         bs.set(71, 110);
1196         bs.set(127, 130);
1197         bs.set(193);
1198         bs.set(450);
1199         try {
1200             bs.nextSetBit(-1);
1201             fail("Expected IndexOutOfBoundsException for negative index");
1202         } catch (final IndexOutOfBoundsException e) {
1203             // correct behavior
1204         }
1205         assertEquals(5, bs.nextSetBit(0), "nextSetBit() returned the wrong value");
1206         assertEquals(5, bs.nextSetBit(5), "nextSetBit() returned the wrong value");
1207         assertEquals(32, bs.nextSetBit(6), "nextSetBit() returned the wrong value");
1208         assertEquals(32, bs.nextSetBit(32), "nextSetBit() returned the wrong value");
1209         assertEquals(63, bs.nextSetBit(33), "nextSetBit() returned the wrong value");
1210 
1211         // boundary tests
1212         assertEquals(63, bs.nextSetBit(63), "nextSetBit() returned the wrong value");
1213         assertEquals(64, bs.nextSetBit(64), "nextSetBit() returned the wrong value");
1214 
1215         // at bitset element 1
1216         assertEquals(71, bs.nextSetBit(65), "nextSetBit() returned the wrong value");
1217         assertEquals(71, bs.nextSetBit(71), "nextSetBit() returned the wrong value");
1218         assertEquals(72, bs.nextSetBit(72), "nextSetBit() returned the wrong value");
1219         assertEquals(127, bs.nextSetBit(110), "nextSetBit() returned the wrong value");
1220 
1221         // boundary tests
1222         assertEquals(127, bs.nextSetBit(127), "nextSetBit() returned the wrong value");
1223         assertEquals(128, bs.nextSetBit(128), "nextSetBit() returned the wrong value");
1224 
1225         // at bitset element 2
1226         assertEquals(193, bs.nextSetBit(130), "nextSetBit() returned the wrong value");
1227 
1228         assertEquals(193, bs.nextSetBit(191), "nextSetBit() returned the wrong value");
1229         assertEquals(193, bs.nextSetBit(192), "nextSetBit() returned the wrong value");
1230         assertEquals(193, bs.nextSetBit(193), "nextSetBit() returned the wrong value");
1231         assertEquals(450, bs.nextSetBit(194), "nextSetBit() returned the wrong value");
1232         assertEquals(450, bs.nextSetBit(255), "nextSetBit() returned the wrong value");
1233         assertEquals(450, bs.nextSetBit(256), "nextSetBit() returned the wrong value");
1234         assertEquals(450, bs.nextSetBit(450), "nextSetBit() returned the wrong value");
1235 
1236         assertEquals(-1, bs.nextSetBit(451), "nextSetBit() returned the wrong value");
1237         assertEquals(-1, bs.nextSetBit(511), "nextSetBit() returned the wrong value");
1238         assertEquals(-1, bs.nextSetBit(512), "nextSetBit() returned the wrong value");
1239         assertEquals(-1, bs.nextSetBit(800), "nextSetBit() returned the wrong value");
1240     }
1241 
1242     /**
1243      * Tests {@link FluentBitSet#or(FluentBitSet)}.
1244      */
1245     @Test
1246     void test_or() {
1247         // Test for method void java.util.BitSet.or(BitSet)
1248         FluentBitSet bs = newInstance(128);
1249         bs.or(eightFbs);
1250         for (int i = 0; i < 8; i++) {
1251             assertTrue(bs.get(i), "OR failed to set bits");
1252         }
1253 
1254         bs = newInstance(0);
1255         bs.or(eightFbs);
1256         for (int i = 0; i < 8; i++) {
1257             assertTrue(bs.get(i), "OR(0) failed to set bits");
1258         }
1259 
1260         eightFbs.clear(5);
1261         bs = newInstance(128);
1262         bs.or(eightFbs);
1263         assertFalse(bs.get(5), "OR set a bit which should be off");
1264     }
1265 
1266     /**
1267      * Tests {@link FluentBitSet#or(BitSet)}.
1268      */
1269     @Test
1270     void test_or_BitSet() {
1271         // Test for method void java.util.BitSet.or(BitSet)
1272         FluentBitSet bs = newInstance(128);
1273         bs.or(eightFbs.bitSet());
1274         for (int i = 0; i < 8; i++) {
1275             assertTrue(bs.get(i), "OR failed to set bits");
1276         }
1277 
1278         bs = newInstance(0);
1279         bs.or(eightFbs.bitSet());
1280         for (int i = 0; i < 8; i++) {
1281             assertTrue(bs.get(i), "OR(0) failed to set bits");
1282         }
1283 
1284         eightFbs.clear(5);
1285         bs = newInstance(128);
1286         bs.or(eightFbs.bitSet());
1287         assertFalse(bs.get(5), "OR set a bit which should be off");
1288     }
1289 
1290     /**
1291      * Tests {@link FluentBitSet#or(FluentBitSet)}.
1292      */
1293     @Test
1294     void test_or_FluentBitSetArray() {
1295         // Test for method void java.util.BitSet.or(BitSet)
1296         FluentBitSet bs = newInstance(128);
1297         bs.or(new FluentBitSet[] {eightFbs});
1298         for (int i = 0; i < 8; i++) {
1299             assertTrue(bs.get(i), "OR failed to set bits");
1300         }
1301 
1302         bs = newInstance(0);
1303         bs.or(new FluentBitSet[] {eightFbs});
1304         for (int i = 0; i < 8; i++) {
1305             assertTrue(bs.get(i), "OR(0) failed to set bits");
1306         }
1307 
1308         eightFbs.clear(5);
1309         bs = newInstance(128);
1310         bs.or(new FluentBitSet[] {eightFbs});
1311         assertFalse(bs.get(5), "OR set a bit which should be off");
1312     }
1313 
1314     /**
1315      * Tests {@link FluentBitSet#previousClearBit(int)}.
1316      */
1317     @Test
1318     void test_previousClearBit() {
1319         final FluentBitSet bs = newInstance();
1320         assertEquals(1, bs.previousClearBit(1), "previousClearBit");
1321     }
1322 
1323     /**
1324      * Tests {@link FluentBitSet#previousSetBit(int)}.
1325      */
1326     @Test
1327     void test_previousSetBit() {
1328         final FluentBitSet bs = newInstance();
1329         assertEquals(-1, bs.previousSetBit(1), "previousSetBit");
1330     }
1331 
1332     /**
1333      * Tests {@link FluentBitSet#set(int, int)}.
1334      */
1335     @Test
1336     void test_setII() {
1337         final FluentBitSet bitset = newInstance(30);
1338         bitset.set(29, 29);
1339 
1340         // Test for method void java.util.BitSet.set(int, int)
1341         // pos1 and pos2 are in the same bitset element
1342         FluentBitSet bs = newInstance(16);
1343         bs.set(5);
1344         bs.set(15);
1345         bs.set(7, 11);
1346         for (int i = 0; i < 7; i++) {
1347             if (i == 5) {
1348                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1349             } else {
1350                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1351             }
1352         }
1353         for (int i = 7; i < 11; i++) {
1354             assertTrue(bs.get(i), "Failed to set bit " + i);
1355         }
1356         for (int i = 11; i < bs.size(); i++) {
1357             if (i == 15) {
1358                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1359             } else {
1360                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1361             }
1362         }
1363 
1364         // pos1 and pos2 is in the same bitset element, boundary testing
1365         bs = newInstance(16);
1366         bs.set(7, 64);
1367         assertEquals(64, bs.size(), "Failed to grow BitSet");
1368         for (int i = 0; i < 7; i++) {
1369             assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1370         }
1371         for (int i = 7; i < 64; i++) {
1372             assertTrue(bs.get(i), "Failed to set bit " + i);
1373         }
1374         assertFalse(bs.get(64), "Shouldn't have set bit 64");
1375 
1376         // more boundary testing
1377         bs = newInstance(32);
1378         bs.set(0, 64);
1379         for (int i = 0; i < 64; i++) {
1380             assertTrue(bs.get(i), "Failed to set bit " + i);
1381         }
1382         assertFalse(bs.get(64), "Shouldn't have set bit 64");
1383 
1384         bs = newInstance(32);
1385         bs.set(0, 65);
1386         for (int i = 0; i < 65; i++) {
1387             assertTrue(bs.get(i), "Failed to set bit " + i);
1388         }
1389         assertFalse(bs.get(65), "Shouldn't have set bit 65");
1390 
1391         // pos1 and pos2 are in two sequential bitset elements
1392         bs = newInstance(128);
1393         bs.set(7);
1394         bs.set(110);
1395         bs.set(9, 74);
1396         for (int i = 0; i < 9; i++) {
1397             if (i == 7) {
1398                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1399             } else {
1400                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1401             }
1402         }
1403         for (int i = 9; i < 74; i++) {
1404             assertTrue(bs.get(i), "Failed to set bit " + i);
1405         }
1406         for (int i = 74; i < bs.size(); i++) {
1407             if (i == 110) {
1408                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1409             } else {
1410                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1411             }
1412         }
1413 
1414         // pos1 and pos2 are in two non-sequential bitset elements
1415         bs = newInstance(256);
1416         bs.set(7);
1417         bs.set(255);
1418         bs.set(9, 219);
1419         for (int i = 0; i < 9; i++) {
1420             if (i == 7) {
1421                 assertTrue(bs.get(i), "Shouldn't have set flipped " + i);
1422             } else {
1423                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1424             }
1425         }
1426 
1427         for (int i = 9; i < 219; i++) {
1428             assertTrue(bs.get(i), "failed to set bit " + i);
1429         }
1430 
1431         for (int i = 219; i < 255; i++) {
1432             assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1433         }
1434 
1435         assertTrue(bs.get(255), "Shouldn't have flipped bit 255");
1436 
1437         // test illegal args
1438         bs = newInstance(10);
1439         try {
1440             bs.set(-1, 3);
1441             fail("Test1: Attempt to flip with  negative index failed to generate exception");
1442         } catch (final IndexOutOfBoundsException e) {
1443             // Correct behavior
1444         }
1445 
1446         try {
1447             bs.set(2, -1);
1448             fail("Test2: Attempt to flip with negative index failed to generate exception");
1449         } catch (final IndexOutOfBoundsException e) {
1450             // Correct behavior
1451         }
1452 
1453         bs.set(2, 2);
1454         assertFalse(bs.get(2), "Bit got set incorrectly ");
1455 
1456         try {
1457             bs.set(4, 2);
1458             fail("Test4: Attempt to flip with illegal args failed to generate exception");
1459         } catch (final IndexOutOfBoundsException e) {
1460             // Correct behavior
1461         }
1462     }
1463 
1464     /**
1465      * Tests {@link FluentBitSet#set(int, int, boolean)}.
1466      */
1467     @Test
1468     void test_setIIZ() {
1469         // Test for method void java.util.BitSet.set(int, int, boolean)
1470         eightFbs.set(3, 6, false);
1471         assertTrue(!eightFbs.get(3) && !eightFbs.get(4) && !eightFbs.get(5), "Should have set bits 3, 4, and 5 to false");
1472 
1473         eightFbs.set(3, 6, true);
1474         assertTrue(eightFbs.get(3) && eightFbs.get(4) && eightFbs.get(5), "Should have set bits 3, 4, and 5 to true");
1475 
1476     }
1477 
1478     /**
1479      * Tests {@link FluentBitSet#setInclusive(int, int)}.
1480      */
1481     @Test
1482     void test_setInclusive() {
1483         final FluentBitSet bitset = newInstance(30);
1484         bitset.set(29, 29);
1485 
1486         // Test for method void java.util.BitSet.set(int, int)
1487         // pos1 and pos2 are in the same bitset element
1488         FluentBitSet bs = newInstance(16);
1489         bs.set(5);
1490         bs.set(15);
1491         bs.setInclusive(7, 11);
1492         for (int i = 0; i < 7; i++) {
1493             if (i == 5) {
1494                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1495             } else {
1496                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1497             }
1498         }
1499         for (int i = 7; i < 12; i++) {
1500             assertTrue(bs.get(i), "Failed to set bit " + i);
1501         }
1502         for (int i = 12; i < bs.size(); i++) {
1503             if (i == 15) {
1504                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1505             } else {
1506                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1507             }
1508         }
1509 
1510         // pos1 and pos2 is in the same bitset element, boundary testing
1511         bs = newInstance(16);
1512         bs.setInclusive(7, 64);
1513         assertEquals(128, bs.size(), "Failed to grow BitSet");
1514         for (int i = 0; i < 7; i++) {
1515             assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1516         }
1517         for (int i = 7; i < 65; i++) {
1518             assertTrue(bs.get(i), "Failed to set bit " + i);
1519         }
1520         assertFalse(bs.get(65), "Shouldn't have set bit 64");
1521 
1522         // more boundary testing
1523         bs = newInstance(32);
1524         bs.setInclusive(0, 64);
1525         for (int i = 0; i < 65; i++) {
1526             assertTrue(bs.get(i), "Failed to set bit " + i);
1527         }
1528         assertFalse(bs.get(65), "Shouldn't have set bit 64");
1529 
1530         bs = newInstance(32);
1531         bs.setInclusive(0, 65);
1532         for (int i = 0; i < 66; i++) {
1533             assertTrue(bs.get(i), "Failed to set bit " + i);
1534         }
1535         assertFalse(bs.get(66), "Shouldn't have set bit 65");
1536 
1537         // pos1 and pos2 are in two sequential bitset elements
1538         bs = newInstance(128);
1539         bs.set(7);
1540         bs.set(110);
1541         bs.setInclusive(9, 74);
1542         for (int i = 0; i < 9; i++) {
1543             if (i == 7) {
1544                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1545             } else {
1546                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1547             }
1548         }
1549         for (int i = 9; i < 75; i++) {
1550             assertTrue(bs.get(i), "Failed to set bit " + i);
1551         }
1552         for (int i = 75; i < bs.size(); i++) {
1553             if (i == 110) {
1554                 assertTrue(bs.get(i), "Shouldn't have flipped bit " + i);
1555             } else {
1556                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1557             }
1558         }
1559 
1560         // pos1 and pos2 are in two non-sequential bitset elements
1561         bs = newInstance(256);
1562         bs.set(7);
1563         bs.set(255);
1564         bs.setInclusive(9, 219);
1565         for (int i = 0; i < 9; i++) {
1566             if (i == 7) {
1567                 assertTrue(bs.get(i), "Shouldn't have set flipped " + i);
1568             } else {
1569                 assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1570             }
1571         }
1572 
1573         for (int i = 9; i < 220; i++) {
1574             assertTrue(bs.get(i), "failed to set bit " + i);
1575         }
1576 
1577         for (int i = 220; i < 255; i++) {
1578             assertFalse(bs.get(i), "Shouldn't have set bit " + i);
1579         }
1580 
1581         assertTrue(bs.get(255), "Shouldn't have flipped bit 255");
1582 
1583         // test illegal args
1584         bs = newInstance(10);
1585         try {
1586             bs.setInclusive(-1, 3);
1587             fail("Test1: Attempt to flip with  negative index failed to generate exception");
1588         } catch (final IndexOutOfBoundsException e) {
1589             // Correct behavior
1590         }
1591 
1592         try {
1593             bs.setInclusive(2, -1);
1594             fail("Test2: Attempt to flip with negative index failed to generate exception");
1595         } catch (final IndexOutOfBoundsException e) {
1596             // Correct behavior
1597         }
1598 
1599         bs.setInclusive(2, 2);
1600         assertFalse(bs.get(3), "Bit got set incorrectly ");
1601 
1602         try {
1603             bs.setInclusive(4, 2);
1604             fail("Test4: Attempt to flip with illegal args failed to generate exception");
1605         } catch (final IndexOutOfBoundsException e) {
1606             // Correct behavior
1607         }
1608     }
1609 
1610     /**
1611      * Tests {@link FluentBitSet#set(int)}.
1612      */
1613     @Test
1614     void test_setInt() {
1615         // Test for method void java.util.BitSet.set(int)
1616 
1617         FluentBitSet bs = newInstance();
1618         bs.set(8);
1619         assertTrue(bs.get(8), "Failed to set bit");
1620 
1621         try {
1622             bs.set(-1);
1623             fail("Attempt to set at negative index failed to generate exception");
1624         } catch (final IndexOutOfBoundsException e) {
1625             // Correct behavior
1626         }
1627 
1628         // Try setting a bit on a 64 boundary
1629         bs.set(128);
1630         assertEquals(192, bs.size(), "Failed to grow BitSet");
1631         assertTrue(bs.get(128), "Failed to set bit");
1632 
1633         bs = newInstance(64);
1634         for (int i = bs.size(); --i >= 0;) {
1635             bs.set(i);
1636             assertTrue(bs.get(i), "Incorrectly set");
1637             assertEquals(i + 1, bs.length(), "Incorrect length");
1638             for (int j = bs.size(); --j > i;) {
1639                 assertFalse(bs.get(j), "Incorrectly set bit " + j);
1640             }
1641             for (int j = i; --j >= 0;) {
1642                 assertFalse(bs.get(j), "Incorrectly set bit " + j);
1643             }
1644             bs.clear(i);
1645         }
1646 
1647         bs = newInstance(0);
1648         assertEquals(0, bs.length(), "Test1: Wrong length");
1649         bs.set(0);
1650         assertEquals(1, bs.length(), "Test2: Wrong length");
1651     }
1652 
1653     /**
1654      * Tests {@link FluentBitSet#set(int...)}.
1655      */
1656     @Test
1657     void test_setIntArray() {
1658         // Test for method void java.util.BitSet.set(int)
1659 
1660         FluentBitSet bs = newInstance();
1661         bs.set(new int[] {8});
1662         assertTrue(bs.get(8), "Failed to set bit");
1663 
1664         try {
1665             bs.set(new int[] {-1});
1666             fail("Attempt to set at negative index failed to generate exception");
1667         } catch (final IndexOutOfBoundsException e) {
1668             // Correct behavior
1669         }
1670 
1671         // Try setting a bit on a 64 boundary
1672         bs.set(new int[] {128});
1673         assertEquals(192, bs.size(), "Failed to grow BitSet");
1674         assertTrue(bs.get(128), "Failed to set bit");
1675 
1676         bs = newInstance(64);
1677         for (int i = bs.size(); --i >= 0;) {
1678             bs.set(new int[] {i});
1679             assertTrue(bs.get(i), "Incorrectly set");
1680             assertEquals(i + 1, bs.length(), "Incorrect length");
1681             for (int j = bs.size(); --j > i;) {
1682                 assertFalse(bs.get(j), "Incorrectly set bit " + j);
1683             }
1684             for (int j = i; --j >= 0;) {
1685                 assertFalse(bs.get(j), "Incorrectly set bit " + j);
1686             }
1687             bs.clear(i);
1688         }
1689 
1690         bs = newInstance(0);
1691         assertEquals(0, bs.length(), "Test1: Wrong length");
1692         bs.set(new int[] {0});
1693         assertEquals(1, bs.length(), "Test2: Wrong length");
1694     }
1695 
1696     /**
1697      * Tests {@link FluentBitSet#set(int, boolean)}.
1698      */
1699     @Test
1700     void test_setIZ() {
1701         // Test for method void java.util.BitSet.set(int, boolean)
1702         eightFbs.set(5, false);
1703         assertFalse(eightFbs.get(5), "Should have set bit 5 to true");
1704 
1705         eightFbs.set(5, true);
1706         assertTrue(eightFbs.get(5), "Should have set bit 5 to false");
1707     }
1708 
1709     /**
1710      * Tests {@link FluentBitSet#setInclusive(int, int)}.
1711      */
1712     @Test
1713     void test_setRangeInclusive() {
1714         // Test for method int java.util.BitSet.size()
1715         assertEquals(64, eightFbs.size(), "Returned incorrect size");
1716         eightFbs.set(129);
1717         assertTrue(eightFbs.size() >= 129, "Returned incorrect size");
1718 
1719     }
1720 
1721     /**
1722      * Tests {@link FluentBitSet#size()}.
1723      */
1724     @Test
1725     void test_size() {
1726         // Test for method int java.util.BitSet.size()
1727         assertEquals(64, eightFbs.size(), "Returned incorrect size");
1728         eightFbs.set(129);
1729         assertTrue(eightFbs.size() >= 129, "Returned incorrect size");
1730 
1731     }
1732 
1733     /**
1734      * Tests {@link FluentBitSet#previousSetBit(int)}.
1735      */
1736     @Test
1737     void test_stream() {
1738         final FluentBitSet bs = newInstance();
1739         assertEquals(0, bs.stream().count(), "stream");
1740     }
1741 
1742     /**
1743      * Tests {@link FluentBitSet#previousSetBit(int)}.
1744      */
1745     @Test
1746     void test_toByteArray() {
1747         final FluentBitSet bs = newInstance();
1748         assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, bs.toByteArray(), "stream");
1749     }
1750 
1751     /**
1752      * Tests {@link FluentBitSet#previousSetBit(int)}.
1753      */
1754     @Test
1755     void test_toLongArray() {
1756         final FluentBitSet bs = newInstance();
1757         assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, bs.toLongArray(), "stream");
1758     }
1759 
1760     /**
1761      * Tests {@link FluentBitSet#toString()}.
1762      */
1763     @Test
1764     void test_toString() {
1765         // Test for method java.lang.String java.util.BitSet.toString()
1766         assertEquals("{0, 1, 2, 3, 4, 5, 6, 7}", eightFbs.toString(), "Returned incorrect string representation");
1767         eightFbs.clear(2);
1768         assertEquals("{0, 1, 3, 4, 5, 6, 7}", eightFbs.toString(), "Returned incorrect string representation");
1769     }
1770 
1771     /**
1772      * Tests {@link FluentBitSet#xor(FluentBitSet)}.
1773      */
1774     @Test
1775     void test_xor() {
1776         // Test for method void java.util.BitSet.xor(BitSet)
1777 
1778         FluentBitSet bs = (FluentBitSet) eightFbs.clone();
1779         bs.xor(eightFbs);
1780         for (int i = 0; i < 8; i++) {
1781             assertFalse(bs.get(i), "XOR failed to clear bits");
1782         }
1783 
1784         bs.xor(eightFbs);
1785         for (int i = 0; i < 8; i++) {
1786             assertTrue(bs.get(i), "XOR failed to set bits");
1787         }
1788 
1789         bs = newInstance(0);
1790         bs.xor(eightFbs);
1791         for (int i = 0; i < 8; i++) {
1792             assertTrue(bs.get(i), "XOR(0) failed to set bits");
1793         }
1794 
1795         bs = newInstance();
1796         bs.set(63);
1797         assertEquals("{63}", bs.toString(), "Test highest bit");
1798     }
1799 
1800     /**
1801      * Tests {@link FluentBitSet#xor(BitSet)}.
1802      */
1803     @Test
1804     void test_xor_BitSet() {
1805         // Test for method void java.util.BitSet.xor(BitSet)
1806 
1807         FluentBitSet bs = (FluentBitSet) eightFbs.clone();
1808         bs.xor(eightFbs.bitSet());
1809         for (int i = 0; i < 8; i++) {
1810             assertFalse(bs.get(i), "XOR failed to clear bits");
1811         }
1812 
1813         bs.xor(eightFbs.bitSet());
1814         for (int i = 0; i < 8; i++) {
1815             assertTrue(bs.get(i), "XOR failed to set bits");
1816         }
1817 
1818         bs = newInstance(0);
1819         bs.xor(eightFbs.bitSet());
1820         for (int i = 0; i < 8; i++) {
1821             assertTrue(bs.get(i), "XOR(0) failed to set bits");
1822         }
1823 
1824         bs = newInstance();
1825         bs.set(63);
1826         assertEquals("{63}", bs.toString(), "Test highest bit");
1827     }
1828 
1829 }