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