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