1 2 17 18 19 package org.apache.poi.util; 20 21 import junit.framework.*; 22 23 28 29 public class TestShortList 30 extends TestCase 31 { 32 33 38 39 public TestShortList(String name) 40 { 41 super(name); 42 } 43 44 47 48 public void testConstructors() 49 { 50 ShortList list = new ShortList(); 51 52 assertTrue(list.isEmpty()); 53 list.add(( short ) 0); 54 list.add(( short ) 1); 55 ShortList list2 = new ShortList(list); 56 57 assertEquals(list, list2); 58 ShortList list3 = new ShortList(2); 59 60 assertTrue(list3.isEmpty()); 61 } 62 63 66 67 public void testAdd() 68 { 69 ShortList list = new ShortList(); 70 short[] testArray = 71 { 72 0, 1, 2, 3, 5 73 }; 74 75 for (int j = 0; j < testArray.length; j++) 76 { 77 list.add(testArray[ j ]); 78 } 79 for (int j = 0; j < testArray.length; j++) 80 { 81 assertEquals(testArray[ j ], list.get(j)); 82 } 83 assertEquals(testArray.length, list.size()); 84 85 list.add(0, ( short ) -1); 87 assertEquals(( short ) -1, list.get(0)); 88 assertEquals(testArray.length + 1, list.size()); 89 for (int j = 0; j < testArray.length; j++) 90 { 91 assertEquals(testArray[ j ], list.get(j + 1)); 92 } 93 94 list.add(5, ( short ) 4); 96 assertEquals(( short ) 4, list.get(5)); 97 assertEquals(testArray.length + 2, list.size()); 98 for (int j = 0; j < list.size(); j++) 99 { 100 assertEquals(( short ) (j - 1), list.get(j)); 101 } 102 103 list.add(list.size(), ( short ) 6); 105 assertEquals(testArray.length + 3, list.size()); 106 for (int j = 0; j < list.size(); j++) 107 { 108 assertEquals(( short ) (j - 1), list.get(j)); 109 } 110 111 try 113 { 114 list.add(list.size() + 1, ( short ) 8); 115 fail("should have thrown exception"); 116 } 117 catch (IndexOutOfBoundsException e) 118 { 119 120 } 122 123 list = new ShortList(0); 125 for (short j = 0; j < 1000; j++) 126 { 127 list.add(j); 128 } 129 assertEquals(1000, list.size()); 130 for (short j = 0; j < 1000; j++) 131 { 132 assertEquals(j, list.get(j)); 133 } 134 list = new ShortList(0); 135 for (short j = 0; j < 1000; j++) 136 { 137 list.add(0, j); 138 } 139 assertEquals(1000, list.size()); 140 for (short j = 0; j < 1000; j++) 141 { 142 assertEquals(j, list.get(999 - j)); 143 } 144 } 145 146 149 150 public void testAddAll() 151 { 152 ShortList list = new ShortList(); 153 154 for (short j = 0; j < 5; j++) 155 { 156 list.add(j); 157 } 158 ShortList list2 = new ShortList(0); 159 160 list2.addAll(list); 161 list2.addAll(list); 162 assertEquals(2 * list.size(), list2.size()); 163 for (short j = 0; j < 5; j++) 164 { 165 assertEquals(list2.get(j), j); 166 assertEquals(list2.get(j + list.size()), j); 167 } 168 ShortList empty = new ShortList(); 169 int limit = list.size(); 170 171 for (int j = 0; j < limit; j++) 172 { 173 assertTrue(list.addAll(j, empty)); 174 assertEquals(limit, list.size()); 175 } 176 try 177 { 178 list.addAll(limit + 1, empty); 179 fail("should have thrown an exception"); 180 } 181 catch (IndexOutOfBoundsException e) 182 { 183 184 } 186 187 empty.addAll(0, list); 189 assertEquals(empty, list); 190 191 empty.addAll(1, list); 193 assertEquals(2 * list.size(), empty.size()); 194 assertEquals(list.get(0), empty.get(0)); 195 assertEquals(list.get(0), empty.get(1)); 196 assertEquals(list.get(1), empty.get(2)); 197 assertEquals(list.get(1), empty.get(6)); 198 assertEquals(list.get(2), empty.get(3)); 199 assertEquals(list.get(2), empty.get(7)); 200 assertEquals(list.get(3), empty.get(4)); 201 assertEquals(list.get(3), empty.get(8)); 202 assertEquals(list.get(4), empty.get(5)); 203 assertEquals(list.get(4), empty.get(9)); 204 205 empty.addAll(empty.size(), list); 207 assertEquals(3 * list.size(), empty.size()); 208 assertEquals(list.get(0), empty.get(0)); 209 assertEquals(list.get(0), empty.get(1)); 210 assertEquals(list.get(0), empty.get(10)); 211 assertEquals(list.get(1), empty.get(2)); 212 assertEquals(list.get(1), empty.get(6)); 213 assertEquals(list.get(1), empty.get(11)); 214 assertEquals(list.get(2), empty.get(3)); 215 assertEquals(list.get(2), empty.get(7)); 216 assertEquals(list.get(2), empty.get(12)); 217 assertEquals(list.get(3), empty.get(4)); 218 assertEquals(list.get(3), empty.get(8)); 219 assertEquals(list.get(3), empty.get(13)); 220 assertEquals(list.get(4), empty.get(5)); 221 assertEquals(list.get(4), empty.get(9)); 222 assertEquals(list.get(4), empty.get(14)); 223 } 224 225 228 229 public void testClear() 230 { 231 ShortList list = new ShortList(); 232 233 for (short j = 0; j < 500; j++) 234 { 235 list.add(j); 236 } 237 assertEquals(500, list.size()); 238 list.clear(); 239 assertEquals(0, list.size()); 240 for (short j = 0; j < 500; j++) 241 { 242 list.add(( short ) (j + 1)); 243 } 244 assertEquals(500, list.size()); 245 for (short j = 0; j < 500; j++) 246 { 247 assertEquals(j + 1, list.get(j)); 248 } 249 } 250 251 254 255 public void testContains() 256 { 257 ShortList list = new ShortList(); 258 259 for (short j = 0; j < 1000; j += 2) 260 { 261 list.add(j); 262 } 263 for (short j = 0; j < 1000; j++) 264 { 265 if (j % 2 == 0) 266 { 267 assertTrue(list.contains(j)); 268 } 269 else 270 { 271 assertTrue(!list.contains(j)); 272 } 273 } 274 } 275 276 279 280 public void testContainsAll() 281 { 282 ShortList list = new ShortList(); 283 284 assertTrue(list.containsAll(list)); 285 for (short j = 0; j < 10; j++) 286 { 287 list.add(j); 288 } 289 ShortList list2 = new ShortList(list); 290 291 assertTrue(list2.containsAll(list)); 292 assertTrue(list.containsAll(list2)); 293 list2.add(( short ) 10); 294 assertTrue(list2.containsAll(list)); 295 assertTrue(!list.containsAll(list2)); 296 list.add(( short ) 11); 297 assertTrue(!list2.containsAll(list)); 298 assertTrue(!list.containsAll(list2)); 299 } 300 301 304 305 public void testEquals() 306 { 307 ShortList list = new ShortList(); 308 309 assertEquals(list, list); 310 assertTrue(!list.equals(null)); 311 ShortList list2 = new ShortList(200); 312 313 assertEquals(list, list2); 314 assertEquals(list2, list); 315 assertEquals(list.hashCode(), list2.hashCode()); 316 list.add(( short ) 0); 317 list.add(( short ) 1); 318 list2.add(( short ) 1); 319 list2.add(( short ) 0); 320 assertTrue(!list.equals(list2)); 321 list2.removeValue(( short ) 1); 322 list2.add(( short ) 1); 323 assertEquals(list, list2); 324 assertEquals(list2, list); 325 list2.add(( short ) 2); 326 assertTrue(!list.equals(list2)); 327 assertTrue(!list2.equals(list)); 328 } 329 330 333 334 public void testGet() 335 { 336 ShortList list = new ShortList(); 337 338 for (short j = 0; j < 1000; j++) 339 { 340 list.add(j); 341 } 342 for (short j = 0; j < 1001; j++) 343 { 344 try 345 { 346 assertEquals(j, list.get(j)); 347 if (j == 1000) 348 { 349 fail("should have gotten exception"); 350 } 351 } 352 catch (IndexOutOfBoundsException e) 353 { 354 if (j != 1000) 355 { 356 fail("unexpected IndexOutOfBoundsException"); 357 } 358 } 359 } 360 } 361 362 365 366 public void testIndexOf() 367 { 368 ShortList list = new ShortList(); 369 370 for (short j = 0; j < 1000; j++) 371 { 372 list.add(( short ) (j / 2)); 373 } 374 for (short j = 0; j < 1000; j++) 375 { 376 if (j < 500) 377 { 378 assertEquals(j * 2, list.indexOf(j)); 379 } 380 else 381 { 382 assertEquals(-1, list.indexOf(j)); 383 } 384 } 385 } 386 387 390 391 public void testIsEmpty() 392 { 393 ShortList list1 = new ShortList(); 394 ShortList list2 = new ShortList(1000); 395 ShortList list3 = new ShortList(list1); 396 397 assertTrue(list1.isEmpty()); 398 assertTrue(list2.isEmpty()); 399 assertTrue(list3.isEmpty()); 400 list1.add(( short ) 1); 401 list2.add(( short ) 2); 402 list3 = new ShortList(list2); 403 assertTrue(!list1.isEmpty()); 404 assertTrue(!list2.isEmpty()); 405 assertTrue(!list3.isEmpty()); 406 list1.clear(); 407 list2.remove(0); 408 list3.removeValue(( short ) 2); 409 assertTrue(list1.isEmpty()); 410 assertTrue(list2.isEmpty()); 411 assertTrue(list3.isEmpty()); 412 } 413 414 417 418 public void testLastIndexOf() 419 { 420 ShortList list = new ShortList(); 421 422 for (short j = 0; j < 1000; j++) 423 { 424 list.add(( short ) (j / 2)); 425 } 426 for (short j = 0; j < 1000; j++) 427 { 428 if (j < 500) 429 { 430 assertEquals(1 + j * 2, list.lastIndexOf(j)); 431 } 432 else 433 { 434 assertEquals(-1, list.indexOf(j)); 435 } 436 } 437 } 438 439 442 443 public void testRemove() 444 { 445 ShortList list = new ShortList(); 446 447 for (short j = 0; j < 1000; j++) 448 { 449 list.add(j); 450 } 451 for (short j = 0; j < 1000; j++) 452 { 453 assertEquals(j, list.remove(0)); 454 assertEquals(( short ) (999 - j), list.size()); 455 } 456 for (short j = 0; j < 1000; j++) 457 { 458 list.add(j); 459 } 460 for (short j = 0; j < 1000; j++) 461 { 462 assertEquals(( short ) (999 - j), 463 list.remove(( short ) (999 - j))); 464 assertEquals(999 - j, list.size()); 465 } 466 try 467 { 468 list.remove(0); 469 fail("should have caught IndexOutOfBoundsException"); 470 } 471 catch (IndexOutOfBoundsException e) 472 { 473 474 } 476 } 477 478 481 482 public void testRemoveValue() 483 { 484 ShortList list = new ShortList(); 485 486 for (short j = 0; j < 1000; j++) 487 { 488 list.add(( short ) (j / 2)); 489 } 490 for (short j = 0; j < 1000; j++) 491 { 492 if (j < 500) 493 { 494 assertTrue(list.removeValue(j)); 495 assertTrue(list.removeValue(j)); 496 } 497 assertTrue(!list.removeValue(j)); 498 } 499 } 500 501 504 505 public void testRemoveAll() 506 { 507 ShortList list = new ShortList(); 508 509 for (short j = 0; j < 1000; j++) 510 { 511 list.add(j); 512 } 513 ShortList listCopy = new ShortList(list); 514 ShortList listOdd = new ShortList(); 515 ShortList listEven = new ShortList(); 516 517 for (short j = 0; j < 1000; j++) 518 { 519 if (j % 2 == 0) 520 { 521 listEven.add(j); 522 } 523 else 524 { 525 listOdd.add(j); 526 } 527 } 528 list.removeAll(listEven); 529 assertEquals(list, listOdd); 530 list.removeAll(listOdd); 531 assertTrue(list.isEmpty()); 532 listCopy.removeAll(listOdd); 533 assertEquals(listCopy, listEven); 534 listCopy.removeAll(listEven); 535 assertTrue(listCopy.isEmpty()); 536 } 537 538 541 542 public void testRetainAll() 543 { 544 ShortList list = new ShortList(); 545 546 for (short j = 0; j < 1000; j++) 547 { 548 list.add(j); 549 } 550 ShortList listCopy = new ShortList(list); 551 ShortList listOdd = new ShortList(); 552 ShortList listEven = new ShortList(); 553 554 for (short j = 0; j < 1000; j++) 555 { 556 if (j % 2 == 0) 557 { 558 listEven.add(j); 559 } 560 else 561 { 562 listOdd.add(j); 563 } 564 } 565 list.retainAll(listOdd); 566 assertEquals(list, listOdd); 567 list.retainAll(listEven); 568 assertTrue(list.isEmpty()); 569 listCopy.retainAll(listEven); 570 assertEquals(listCopy, listEven); 571 listCopy.retainAll(listOdd); 572 assertTrue(listCopy.isEmpty()); 573 } 574 575 578 579 public void testSet() 580 { 581 ShortList list = new ShortList(); 582 583 for (short j = 0; j < 1000; j++) 584 { 585 list.add(j); 586 } 587 for (short j = 0; j < 1001; j++) 588 { 589 try 590 { 591 list.set(j, ( short ) (j + 1)); 592 if (j == 1000) 593 { 594 fail("Should have gotten exception"); 595 } 596 assertEquals(j + 1, list.get(j)); 597 } 598 catch (IndexOutOfBoundsException e) 599 { 600 if (j != 1000) 601 { 602 fail("premature exception"); 603 } 604 } 605 } 606 } 607 608 611 612 public void testSize() 613 { 614 ShortList list = new ShortList(); 615 616 for (short j = 0; j < 1000; j++) 617 { 618 assertEquals(j, list.size()); 619 list.add(j); 620 assertEquals(j + 1, list.size()); 621 } 622 for (short j = 0; j < 1000; j++) 623 { 624 assertEquals(1000 - j, list.size()); 625 list.removeValue(j); 626 assertEquals(999 - j, list.size()); 627 } 628 } 629 630 633 634 public void testToArray() 635 { 636 ShortList list = new ShortList(); 637 638 for (short j = 0; j < 1000; j++) 639 { 640 list.add(j); 641 } 642 short[] a1 = list.toArray(); 643 644 assertEquals(a1.length, list.size()); 645 for (short j = 0; j < 1000; j++) 646 { 647 assertEquals(a1[ j ], list.get(j)); 648 } 649 short[] a2 = new short[ list.size() ]; 650 short[] a3 = list.toArray(a2); 651 652 assertSame(a2, a3); 653 for (short j = 0; j < 1000; j++) 654 { 655 assertEquals(a2[ j ], list.get(j)); 656 } 657 short[] aShort = new short[ list.size() - 1 ]; 658 short[] aLong = new short[ list.size() + 1 ]; 659 short[] a4 = list.toArray(aShort); 660 short[] a5 = list.toArray(aLong); 661 662 assertTrue(a4 != aShort); 663 assertTrue(a5 != aLong); 664 assertEquals(a4.length, list.size()); 665 for (short j = 0; j < 1000; j++) 666 { 667 assertEquals(a3[ j ], list.get(j)); 668 } 669 assertEquals(a5.length, list.size()); 670 for (short j = 0; j < 1000; j++) 671 { 672 assertEquals(a5[ j ], list.get(j)); 673 } 674 } 675 676 681 682 public static void main(String [] unused_args) 683 { 684 System.out.println("Testing util.ShortList functionality"); 685 junit.textui.TestRunner.run(TestShortList.class); 686 } 687 } 688 | Popular Tags |