1 8 9 package com.sleepycat.bind.tuple.test; 10 11 import java.util.Arrays ; 12 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 import com.sleepycat.bind.tuple.TupleBinding; 18 import com.sleepycat.bind.tuple.TupleInput; 19 import com.sleepycat.bind.tuple.TupleOutput; 20 import com.sleepycat.collections.test.DbTestUtil; 21 import com.sleepycat.je.DatabaseEntry; 22 23 26 public class TupleFormatTest extends TestCase { 27 28 private TupleInput in; 29 private TupleOutput out; 30 private DatabaseEntry buffer; 31 32 public static void main(String [] args) 33 throws Exception { 34 35 junit.framework.TestResult tr = 36 junit.textui.TestRunner.run(suite()); 37 if (tr.errorCount() > 0 || 38 tr.failureCount() > 0) { 39 System.exit(1); 40 } else { 41 System.exit(0); 42 } 43 } 44 45 public static Test suite() 46 throws Exception { 47 48 TestSuite suite = new TestSuite(TupleFormatTest.class); 49 return suite; 50 } 51 52 public TupleFormatTest(String name) { 53 54 super(name); 55 } 56 57 public void setUp() { 58 59 DbTestUtil.printTestName("TupleFormatTest." + getName()); 60 buffer = new DatabaseEntry(); 61 out = new TupleOutput(); 62 } 63 64 public void tearDown() { 65 66 67 in = null; 68 out = null; 69 buffer = null; 70 } 71 72 private void copyOutputToInput() { 73 74 TupleBinding.outputToEntry(out, buffer); 75 assertEquals(out.size(), buffer.getSize()); 76 in = TupleBinding.entryToInput(buffer); 77 assertEquals(in.available(), buffer.getSize()); 78 assertEquals(in.getBufferLength(), buffer.getSize()); 79 } 80 81 private void stringTest(String val) { 82 83 out.reset(); 84 out.writeString(val); 85 assertEquals(val.length() + 1, out.size()); copyOutputToInput(); 87 assertEquals(val, in.readString()); 88 assertEquals(0, in.available()); 89 } 90 91 public void testString() { 92 93 stringTest(""); 94 stringTest("a"); 95 stringTest("abc"); 96 97 out.reset(); 98 out.writeString("abc"); 99 out.writeString("defg"); 100 assertEquals(9, out.size()); 101 copyOutputToInput(); 102 assertEquals("abc", in.readString()); 103 assertEquals("defg", in.readString()); 104 assertEquals(0, in.available()); 105 106 out.reset(); 107 out.writeString("abc"); 108 out.writeString("defg"); 109 out.writeString("hijkl"); 110 assertEquals(15, out.size()); 111 copyOutputToInput(); 112 assertEquals("abc", in.readString()); 113 assertEquals("defg", in.readString()); 114 assertEquals("hijkl", in.readString()); 115 assertEquals(0, in.available()); 116 } 117 118 private void fixedStringTest(char[] val) { 119 120 out.reset(); 121 out.writeString(val); 122 assertEquals(val.length, out.size()); copyOutputToInput(); 124 char[] val2 = new char[val.length]; 125 in.readString(val2); 126 assertTrue(Arrays.equals(val, val2)); 127 assertEquals(0, in.available()); 128 in.reset(); 129 String val3 = in.readString(val.length); 130 assertTrue(Arrays.equals(val, val3.toCharArray())); 131 assertEquals(0, in.available()); 132 } 133 134 public void testFixedString() { 135 136 fixedStringTest(new char[0]); 137 fixedStringTest(new char[] {'a'}); 138 fixedStringTest(new char[] {'a', 'b', 'c'}); 139 140 out.reset(); 141 out.writeString(new char[] {'a', 'b', 'c'}); 142 out.writeString(new char[] {'d', 'e', 'f', 'g'}); 143 assertEquals(7, out.size()); 144 copyOutputToInput(); 145 assertEquals("abc", in.readString(3)); 146 assertEquals("defg", in.readString(4)); 147 assertEquals(0, in.available()); 148 149 out.reset(); 150 out.writeString(new char[] {'a', 'b', 'c'}); 151 out.writeString(new char[] {'d', 'e', 'f', 'g'}); 152 out.writeString(new char[] {'h', 'i', 'j', 'k', 'l'}); 153 assertEquals(12, out.size()); 154 copyOutputToInput(); 155 assertEquals("abc", in.readString(3)); 156 assertEquals("defg", in.readString(4)); 157 assertEquals("hijkl", in.readString(5)); 158 assertEquals(0, in.available()); 159 } 160 161 public void testNullString() { 162 163 out.reset(); 164 out.writeString((String ) null); 165 assertEquals(2, out.size()); 166 copyOutputToInput(); 167 assertEquals(null, in.readString()); 168 assertEquals(0, in.available()); 169 170 out.reset(); 171 out.writeString((String ) null); 172 out.writeString("x"); 173 assertEquals(4, out.size()); 174 copyOutputToInput(); 175 assertEquals(null, in.readString()); 176 assertEquals(2, in.available()); 177 assertEquals("x", in.readString()); 178 assertEquals(0, in.available()); 179 180 out.reset(); 181 out.writeString("x"); 182 out.writeString((String ) null); 183 assertEquals(4, out.size()); 184 copyOutputToInput(); 185 assertEquals("x", in.readString()); 186 assertEquals(2, in.available()); 187 assertEquals(null, in.readString()); 188 assertEquals(0, in.available()); 189 190 out.reset(); 191 out.writeString((String ) null); 192 out.writeInt(123); 193 assertEquals(6, out.size()); 194 copyOutputToInput(); 195 assertEquals(null, in.readString()); 196 assertEquals(4, in.available()); 197 assertEquals(123, in.readInt()); 198 assertEquals(0, in.available()); 199 200 out.reset(); 201 out.writeInt(123); 202 out.writeString((String ) null); 203 assertEquals(6, out.size()); 204 copyOutputToInput(); 205 assertEquals(123, in.readInt()); 206 assertEquals(2, in.available()); 207 assertEquals(null, in.readString()); 208 assertEquals(0, in.available()); 209 } 210 211 private void charsTest(char[] val) { 212 213 for (int mode = 0; mode < 2; mode += 1) { 214 out.reset(); 215 switch (mode) { 216 case 0: out.writeChars(val); break; 217 case 1: out.writeChars(new String (val)); break; 218 default: throw new IllegalStateException (); 219 } 220 assertEquals(val.length * 2, out.size()); 221 copyOutputToInput(); 222 char[] val2 = new char[val.length]; 223 in.readChars(val2); 224 assertTrue(Arrays.equals(val, val2)); 225 assertEquals(0, in.available()); 226 in.reset(); 227 String val3 = in.readChars(val.length); 228 assertTrue(Arrays.equals(val, val3.toCharArray())); 229 assertEquals(0, in.available()); 230 } 231 } 232 233 public void testChars() { 234 235 charsTest(new char[0]); 236 charsTest(new char[] {'a'}); 237 charsTest(new char[] {'a', 'b', 'c'}); 238 239 out.reset(); 240 out.writeChars("abc"); 241 out.writeChars("defg"); 242 assertEquals(7 * 2, out.size()); 243 copyOutputToInput(); 244 assertEquals("abc", in.readChars(3)); 245 assertEquals("defg", in.readChars(4)); 246 assertEquals(0, in.available()); 247 248 out.reset(); 249 out.writeChars("abc"); 250 out.writeChars("defg"); 251 out.writeChars("hijkl"); 252 assertEquals(12 * 2, out.size()); 253 copyOutputToInput(); 254 assertEquals("abc", in.readChars(3)); 255 assertEquals("defg", in.readChars(4)); 256 assertEquals("hijkl", in.readChars(5)); 257 assertEquals(0, in.available()); 258 } 259 260 private void bytesTest(char[] val) { 261 262 char[] valBytes = new char[val.length]; 263 for (int i = 0; i < val.length; i += 1) 264 valBytes[i] = (char) (val[i] & 0xFF); 265 266 for (int mode = 0; mode < 2; mode += 1) { 267 out.reset(); 268 switch (mode) { 269 case 0: out.writeBytes(val); break; 270 case 1: out.writeBytes(new String (val)); break; 271 default: throw new IllegalStateException (); 272 } 273 assertEquals(val.length, out.size()); 274 copyOutputToInput(); 275 char[] val2 = new char[val.length]; 276 in.readBytes(val2); 277 assertTrue(Arrays.equals(valBytes, val2)); 278 assertEquals(0, in.available()); 279 in.reset(); 280 String val3 = in.readBytes(val.length); 281 assertTrue(Arrays.equals(valBytes, val3.toCharArray())); 282 assertEquals(0, in.available()); 283 } 284 } 285 286 public void testBytes() { 287 288 bytesTest(new char[0]); 289 bytesTest(new char[] {'a'}); 290 bytesTest(new char[] {'a', 'b', 'c'}); 291 bytesTest(new char[] {0x7F00, 0x7FFF, 0xFF00, 0xFFFF}); 292 293 out.reset(); 294 out.writeBytes("abc"); 295 out.writeBytes("defg"); 296 assertEquals(7, out.size()); 297 copyOutputToInput(); 298 assertEquals("abc", in.readBytes(3)); 299 assertEquals("defg", in.readBytes(4)); 300 assertEquals(0, in.available()); 301 302 out.reset(); 303 out.writeBytes("abc"); 304 out.writeBytes("defg"); 305 out.writeBytes("hijkl"); 306 assertEquals(12, out.size()); 307 copyOutputToInput(); 308 assertEquals("abc", in.readBytes(3)); 309 assertEquals("defg", in.readBytes(4)); 310 assertEquals("hijkl", in.readBytes(5)); 311 assertEquals(0, in.available()); 312 } 313 314 private void booleanTest(boolean val) { 315 316 out.reset(); 317 out.writeBoolean(val); 318 assertEquals(1, out.size()); 319 copyOutputToInput(); 320 assertEquals(val, in.readBoolean()); 321 assertEquals(0, in.available()); 322 } 323 324 public void testBoolean() { 325 326 booleanTest(true); 327 booleanTest(false); 328 329 out.reset(); 330 out.writeBoolean(true); 331 out.writeBoolean(false); 332 assertEquals(2, out.size()); 333 copyOutputToInput(); 334 assertEquals(true, in.readBoolean()); 335 assertEquals(false, in.readBoolean()); 336 assertEquals(0, in.available()); 337 338 out.reset(); 339 out.writeBoolean(true); 340 out.writeBoolean(false); 341 out.writeBoolean(true); 342 assertEquals(3, out.size()); 343 copyOutputToInput(); 344 assertEquals(true, in.readBoolean()); 345 assertEquals(false, in.readBoolean()); 346 assertEquals(true, in.readBoolean()); 347 assertEquals(0, in.available()); 348 } 349 350 private void unsignedByteTest(int val) { 351 352 unsignedByteTest(val, val); 353 } 354 355 private void unsignedByteTest(int val, int expected) { 356 357 out.reset(); 358 out.writeUnsignedByte(val); 359 assertEquals(1, out.size()); 360 copyOutputToInput(); 361 assertEquals(expected, in.readUnsignedByte()); 362 } 363 364 public void testUnsignedByte() { 365 366 unsignedByteTest(0); 367 unsignedByteTest(1); 368 unsignedByteTest(254); 369 unsignedByteTest(255); 370 unsignedByteTest(256, 0); 371 unsignedByteTest(-1, 255); 372 unsignedByteTest(-2, 254); 373 unsignedByteTest(-255, 1); 374 375 out.reset(); 376 out.writeUnsignedByte(0); 377 out.writeUnsignedByte(1); 378 out.writeUnsignedByte(255); 379 assertEquals(3, out.size()); 380 copyOutputToInput(); 381 assertEquals(0, in.readUnsignedByte()); 382 assertEquals(1, in.readUnsignedByte()); 383 assertEquals(255, in.readUnsignedByte()); 384 assertEquals(0, in.available()); 385 } 386 387 private void unsignedShortTest(int val) { 388 389 unsignedShortTest(val, val); 390 } 391 392 private void unsignedShortTest(int val, int expected) { 393 394 out.reset(); 395 out.writeUnsignedShort(val); 396 assertEquals(2, out.size()); 397 copyOutputToInput(); 398 assertEquals(expected, in.readUnsignedShort()); 399 } 400 401 public void testUnsignedShort() { 402 403 unsignedShortTest(0); 404 unsignedShortTest(1); 405 unsignedShortTest(255); 406 unsignedShortTest(256); 407 unsignedShortTest(257); 408 unsignedShortTest(Short.MAX_VALUE - 1); 409 unsignedShortTest(Short.MAX_VALUE); 410 unsignedShortTest(Short.MAX_VALUE + 1); 411 unsignedShortTest(0xFFFF - 1); 412 unsignedShortTest(0xFFFF); 413 unsignedShortTest(0xFFFF + 1, 0); 414 unsignedShortTest(0x7FFF0000, 0); 415 unsignedShortTest(0xFFFF0000, 0); 416 unsignedShortTest(-1, 0xFFFF); 417 unsignedShortTest(-2, 0xFFFF - 1); 418 unsignedShortTest(-0xFFFF, 1); 419 420 out.reset(); 421 out.writeUnsignedShort(0); 422 out.writeUnsignedShort(1); 423 out.writeUnsignedShort(0xFFFF); 424 assertEquals(6, out.size()); 425 copyOutputToInput(); 426 assertEquals(0, in.readUnsignedShort()); 427 assertEquals(1, in.readUnsignedShort()); 428 assertEquals(0xFFFF, in.readUnsignedShort()); 429 assertEquals(0, in.available()); 430 } 431 432 private void unsignedIntTest(long val) { 433 434 unsignedIntTest(val, val); 435 } 436 437 private void unsignedIntTest(long val, long expected) { 438 439 out.reset(); 440 out.writeUnsignedInt(val); 441 assertEquals(4, out.size()); 442 copyOutputToInput(); 443 assertEquals(expected, in.readUnsignedInt()); 444 } 445 446 public void testUnsignedInt() { 447 448 unsignedIntTest(0L); 449 unsignedIntTest(1L); 450 unsignedIntTest(255L); 451 unsignedIntTest(256L); 452 unsignedIntTest(257L); 453 unsignedIntTest(Short.MAX_VALUE - 1L); 454 unsignedIntTest(Short.MAX_VALUE); 455 unsignedIntTest(Short.MAX_VALUE + 1L); 456 unsignedIntTest(Integer.MAX_VALUE - 1L); 457 unsignedIntTest(Integer.MAX_VALUE); 458 unsignedIntTest(Integer.MAX_VALUE + 1L); 459 unsignedIntTest(0xFFFFFFFFL - 1L); 460 unsignedIntTest(0xFFFFFFFFL); 461 unsignedIntTest(0xFFFFFFFFL + 1L, 0L); 462 unsignedIntTest(0x7FFFFFFF00000000L, 0L); 463 unsignedIntTest(0xFFFFFFFF00000000L, 0L); 464 unsignedIntTest(-1, 0xFFFFFFFFL); 465 unsignedIntTest(-2, 0xFFFFFFFFL - 1L); 466 unsignedIntTest(-0xFFFFFFFFL, 1L); 467 468 out.reset(); 469 out.writeUnsignedInt(0L); 470 out.writeUnsignedInt(1L); 471 out.writeUnsignedInt(0xFFFFFFFFL); 472 assertEquals(12, out.size()); 473 copyOutputToInput(); 474 assertEquals(0L, in.readUnsignedInt()); 475 assertEquals(1L, in.readUnsignedInt()); 476 assertEquals(0xFFFFFFFFL, in.readUnsignedInt()); 477 assertEquals(0L, in.available()); 478 } 479 480 private void byteTest(int val) { 481 482 out.reset(); 483 out.writeByte(val); 484 assertEquals(1, out.size()); 485 copyOutputToInput(); 486 assertEquals((byte) val, in.readByte()); 487 } 488 489 public void testByte() { 490 491 byteTest(0); 492 byteTest(1); 493 byteTest(-1); 494 byteTest(Byte.MAX_VALUE - 1); 495 byteTest(Byte.MAX_VALUE); 496 byteTest(Byte.MAX_VALUE + 1); 497 byteTest(Byte.MIN_VALUE + 1); 498 byteTest(Byte.MIN_VALUE); 499 byteTest(Byte.MIN_VALUE - 1); 500 byteTest(0x7F); 501 byteTest(0xFF); 502 byteTest(0x7FFF); 503 byteTest(0xFFFF); 504 byteTest(0x7FFFFFFF); 505 byteTest(0xFFFFFFFF); 506 507 out.reset(); 508 out.writeByte(0); 509 out.writeByte(1); 510 out.writeByte(-1); 511 assertEquals(3, out.size()); 512 copyOutputToInput(); 513 assertEquals(0, in.readByte()); 514 assertEquals(1, in.readByte()); 515 assertEquals(-1, in.readByte()); 516 assertEquals(0, in.available()); 517 } 518 519 private void shortTest(int val) { 520 521 out.reset(); 522 out.writeShort(val); 523 assertEquals(2, out.size()); 524 copyOutputToInput(); 525 assertEquals((short) val, in.readShort()); 526 } 527 528 public void testShort() { 529 530 shortTest(0); 531 shortTest(1); 532 shortTest(-1); 533 shortTest(Short.MAX_VALUE - 1); 534 shortTest(Short.MAX_VALUE); 535 shortTest(Short.MAX_VALUE + 1); 536 shortTest(Short.MIN_VALUE + 1); 537 shortTest(Short.MIN_VALUE); 538 shortTest(Short.MIN_VALUE - 1); 539 shortTest(0x7F); 540 shortTest(0xFF); 541 shortTest(0x7FFF); 542 shortTest(0xFFFF); 543 shortTest(0x7FFFFFFF); 544 shortTest(0xFFFFFFFF); 545 546 out.reset(); 547 out.writeShort(0); 548 out.writeShort(1); 549 out.writeShort(-1); 550 assertEquals(3 * 2, out.size()); 551 copyOutputToInput(); 552 assertEquals(0, in.readShort()); 553 assertEquals(1, in.readShort()); 554 assertEquals(-1, in.readShort()); 555 assertEquals(0, in.available()); 556 } 557 558 private void intTest(int val) { 559 560 out.reset(); 561 out.writeInt(val); 562 assertEquals(4, out.size()); 563 copyOutputToInput(); 564 assertEquals((int) val, in.readInt()); 565 } 566 567 public void testInt() { 568 569 intTest(0); 570 intTest(1); 571 intTest(-1); 572 intTest(Integer.MAX_VALUE - 1); 573 intTest(Integer.MAX_VALUE); 574 intTest(Integer.MAX_VALUE + 1); 575 intTest(Integer.MIN_VALUE + 1); 576 intTest(Integer.MIN_VALUE); 577 intTest(Integer.MIN_VALUE - 1); 578 intTest(0x7F); 579 intTest(0xFF); 580 intTest(0x7FFF); 581 intTest(0xFFFF); 582 intTest(0x7FFFFFFF); 583 intTest(0xFFFFFFFF); 584 585 out.reset(); 586 out.writeInt(0); 587 out.writeInt(1); 588 out.writeInt(-1); 589 assertEquals(3 * 4, out.size()); 590 copyOutputToInput(); 591 assertEquals(0, in.readInt()); 592 assertEquals(1, in.readInt()); 593 assertEquals(-1, in.readInt()); 594 assertEquals(0, in.available()); 595 } 596 597 private void longTest(long val) { 598 599 out.reset(); 600 out.writeLong(val); 601 assertEquals(8, out.size()); 602 copyOutputToInput(); 603 assertEquals((long) val, in.readLong()); 604 } 605 606 public void testLong() { 607 608 longTest(0); 609 longTest(1); 610 longTest(-1); 611 longTest(Long.MAX_VALUE - 1); 612 longTest(Long.MAX_VALUE); 613 longTest(Long.MAX_VALUE + 1); 614 longTest(Long.MIN_VALUE + 1); 615 longTest(Long.MIN_VALUE); 616 longTest(Long.MIN_VALUE - 1); 617 longTest(0x7F); 618 longTest(0xFF); 619 longTest(0x7FFF); 620 longTest(0xFFFF); 621 longTest(0x7FFFFFFF); 622 longTest(0xFFFFFFFF); 623 longTest(0x7FFFFFFFFFFFFFFFL); 624 longTest(0xFFFFFFFFFFFFFFFFL); 625 626 out.reset(); 627 out.writeLong(0); 628 out.writeLong(1); 629 out.writeLong(-1); 630 assertEquals(3 * 8, out.size()); 631 copyOutputToInput(); 632 assertEquals(0, in.readLong()); 633 assertEquals(1, in.readLong()); 634 assertEquals(-1, in.readLong()); 635 assertEquals(0, in.available()); 636 } 637 638 private void floatTest(double val) { 639 640 out.reset(); 641 out.writeFloat((float) val); 642 assertEquals(4, out.size()); 643 copyOutputToInput(); 644 if (Double.isNaN(val)) { 645 assertTrue(Float.isNaN(in.readFloat())); 646 } else { 647 assertEquals((float) val, in.readFloat(), 0); 648 } 649 } 650 651 public void testFloat() { 652 653 floatTest(0); 654 floatTest(1); 655 floatTest(-1); 656 floatTest(1.0); 657 floatTest(0.1); 658 floatTest(-1.0); 659 floatTest(-0.1); 660 floatTest(Float.NaN); 661 floatTest(Float.NEGATIVE_INFINITY); 662 floatTest(Float.POSITIVE_INFINITY); 663 floatTest(Short.MAX_VALUE); 664 floatTest(Short.MIN_VALUE); 665 floatTest(Integer.MAX_VALUE); 666 floatTest(Integer.MIN_VALUE); 667 floatTest(Long.MAX_VALUE); 668 floatTest(Long.MIN_VALUE); 669 floatTest(Float.MAX_VALUE); 670 floatTest(Float.MAX_VALUE + 1); 671 floatTest(Float.MIN_VALUE + 1); 672 floatTest(Float.MIN_VALUE); 673 floatTest(Float.MIN_VALUE - 1); 674 floatTest(0x7F); 675 floatTest(0xFF); 676 floatTest(0x7FFF); 677 floatTest(0xFFFF); 678 floatTest(0x7FFFFFFF); 679 floatTest(0xFFFFFFFF); 680 floatTest(0x7FFFFFFFFFFFFFFFL); 681 floatTest(0xFFFFFFFFFFFFFFFFL); 682 683 out.reset(); 684 out.writeFloat(0); 685 out.writeFloat(1); 686 out.writeFloat(-1); 687 assertEquals(3 * 4, out.size()); 688 copyOutputToInput(); 689 assertEquals(0, in.readFloat(), 0); 690 assertEquals(1, in.readFloat(), 0); 691 assertEquals(-1, in.readFloat(), 0); 692 assertEquals(0, in.available(), 0); 693 } 694 695 private void doubleTest(double val) { 696 697 out.reset(); 698 out.writeDouble((double) val); 699 assertEquals(8, out.size()); 700 copyOutputToInput(); 701 if (Double.isNaN(val)) { 702 assertTrue(Double.isNaN(in.readDouble())); 703 } else { 704 assertEquals((double) val, in.readDouble(), 0); 705 } 706 } 707 708 public void testDouble() { 709 710 doubleTest(0); 711 doubleTest(1); 712 doubleTest(-1); 713 doubleTest(1.0); 714 doubleTest(0.1); 715 doubleTest(-1.0); 716 doubleTest(-0.1); 717 doubleTest(Double.NaN); 718 doubleTest(Double.NEGATIVE_INFINITY); 719 doubleTest(Double.POSITIVE_INFINITY); 720 doubleTest(Short.MAX_VALUE); 721 doubleTest(Short.MIN_VALUE); 722 doubleTest(Integer.MAX_VALUE); 723 doubleTest(Integer.MIN_VALUE); 724 doubleTest(Long.MAX_VALUE); 725 doubleTest(Long.MIN_VALUE); 726 doubleTest(Float.MAX_VALUE); 727 doubleTest(Float.MIN_VALUE); 728 doubleTest(Double.MAX_VALUE - 1); 729 doubleTest(Double.MAX_VALUE); 730 doubleTest(Double.MAX_VALUE + 1); 731 doubleTest(Double.MIN_VALUE + 1); 732 doubleTest(Double.MIN_VALUE); 733 doubleTest(Double.MIN_VALUE - 1); 734 doubleTest(0x7F); 735 doubleTest(0xFF); 736 doubleTest(0x7FFF); 737 doubleTest(0xFFFF); 738 doubleTest(0x7FFFFFFF); 739 doubleTest(0xFFFFFFFF); 740 doubleTest(0x7FFFFFFFFFFFFFFFL); 741 doubleTest(0xFFFFFFFFFFFFFFFFL); 742 743 out.reset(); 744 out.writeDouble(0); 745 out.writeDouble(1); 746 out.writeDouble(-1); 747 assertEquals(3 * 8, out.size()); 748 copyOutputToInput(); 749 assertEquals(0, in.readDouble(), 0); 750 assertEquals(1, in.readDouble(), 0); 751 assertEquals(-1, in.readDouble(), 0); 752 assertEquals(0, in.available(), 0); 753 } 754 755 private void sortedFloatTest(double val) { 756 757 out.reset(); 758 out.writeSortedFloat((float) val); 759 assertEquals(4, out.size()); 760 copyOutputToInput(); 761 if (Double.isNaN(val)) { 762 assertTrue(Float.isNaN(in.readSortedFloat())); 763 } else { 764 assertEquals((float) val, in.readSortedFloat(), 0); 765 } 766 } 767 768 public void testSortedFloat() { 769 770 sortedFloatTest(0); 771 sortedFloatTest(1); 772 sortedFloatTest(-1); 773 sortedFloatTest(1.0); 774 sortedFloatTest(0.1); 775 sortedFloatTest(-1.0); 776 sortedFloatTest(-0.1); 777 sortedFloatTest(Float.NaN); 778 sortedFloatTest(Float.NEGATIVE_INFINITY); 779 sortedFloatTest(Float.POSITIVE_INFINITY); 780 sortedFloatTest(Short.MAX_VALUE); 781 sortedFloatTest(Short.MIN_VALUE); 782 sortedFloatTest(Integer.MAX_VALUE); 783 sortedFloatTest(Integer.MIN_VALUE); 784 sortedFloatTest(Long.MAX_VALUE); 785 sortedFloatTest(Long.MIN_VALUE); 786 sortedFloatTest(Float.MAX_VALUE); 787 sortedFloatTest(Float.MAX_VALUE + 1); 788 sortedFloatTest(Float.MIN_VALUE + 1); 789 sortedFloatTest(Float.MIN_VALUE); 790 sortedFloatTest(Float.MIN_VALUE - 1); 791 sortedFloatTest(0x7F); 792 sortedFloatTest(0xFF); 793 sortedFloatTest(0x7FFF); 794 sortedFloatTest(0xFFFF); 795 sortedFloatTest(0x7FFFFFFF); 796 sortedFloatTest(0xFFFFFFFF); 797 sortedFloatTest(0x7FFFFFFFFFFFFFFFL); 798 sortedFloatTest(0xFFFFFFFFFFFFFFFFL); 799 800 out.reset(); 801 out.writeSortedFloat(0); 802 out.writeSortedFloat(1); 803 out.writeSortedFloat(-1); 804 assertEquals(3 * 4, out.size()); 805 copyOutputToInput(); 806 assertEquals(0, in.readSortedFloat(), 0); 807 assertEquals(1, in.readSortedFloat(), 0); 808 assertEquals(-1, in.readSortedFloat(), 0); 809 assertEquals(0, in.available(), 0); 810 } 811 812 private void sortedDoubleTest(double val) { 813 814 out.reset(); 815 out.writeSortedDouble((double) val); 816 assertEquals(8, out.size()); 817 copyOutputToInput(); 818 if (Double.isNaN(val)) { 819 assertTrue(Double.isNaN(in.readSortedDouble())); 820 } else { 821 assertEquals((double) val, in.readSortedDouble(), 0); 822 } 823 } 824 825 public void testSortedDouble() { 826 827 sortedDoubleTest(0); 828 sortedDoubleTest(1); 829 sortedDoubleTest(-1); 830 sortedDoubleTest(1.0); 831 sortedDoubleTest(0.1); 832 sortedDoubleTest(-1.0); 833 sortedDoubleTest(-0.1); 834 sortedDoubleTest(Double.NaN); 835 sortedDoubleTest(Double.NEGATIVE_INFINITY); 836 sortedDoubleTest(Double.POSITIVE_INFINITY); 837 sortedDoubleTest(Short.MAX_VALUE); 838 sortedDoubleTest(Short.MIN_VALUE); 839 sortedDoubleTest(Integer.MAX_VALUE); 840 sortedDoubleTest(Integer.MIN_VALUE); 841 sortedDoubleTest(Long.MAX_VALUE); 842 sortedDoubleTest(Long.MIN_VALUE); 843 sortedDoubleTest(Float.MAX_VALUE); 844 sortedDoubleTest(Float.MIN_VALUE); 845 sortedDoubleTest(Double.MAX_VALUE - 1); 846 sortedDoubleTest(Double.MAX_VALUE); 847 sortedDoubleTest(Double.MAX_VALUE + 1); 848 sortedDoubleTest(Double.MIN_VALUE + 1); 849 sortedDoubleTest(Double.MIN_VALUE); 850 sortedDoubleTest(Double.MIN_VALUE - 1); 851 sortedDoubleTest(0x7F); 852 sortedDoubleTest(0xFF); 853 sortedDoubleTest(0x7FFF); 854 sortedDoubleTest(0xFFFF); 855 sortedDoubleTest(0x7FFFFFFF); 856 sortedDoubleTest(0xFFFFFFFF); 857 sortedDoubleTest(0x7FFFFFFFFFFFFFFFL); 858 sortedDoubleTest(0xFFFFFFFFFFFFFFFFL); 859 860 out.reset(); 861 out.writeSortedDouble(0); 862 out.writeSortedDouble(1); 863 out.writeSortedDouble(-1); 864 assertEquals(3 * 8, out.size()); 865 copyOutputToInput(); 866 assertEquals(0, in.readSortedDouble(), 0); 867 assertEquals(1, in.readSortedDouble(), 0); 868 assertEquals(-1, in.readSortedDouble(), 0); 869 assertEquals(0, in.available(), 0); 870 } 871 } 872 | Popular Tags |