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