1 /* 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* Type-specific source code for unit test 25 * 26 * Regenerate the BasicX classes via genBasic.sh whenever this file changes. 27 * We check in the generated source files so that the test tree can be used 28 * independently of the rest of the source tree. 29 */ 30 31 // -- This file was mechanically generated: Do not edit! -- // 32 33 import java.nio.*; 34 import java.lang.reflect.Method; 35 36 37 public class BasicFloat 38 extends Basic 39 { 40 41 private static final float[] VALUES = { 42 Float.MIN_VALUE, 43 (float) -1, 44 (float) 0, 45 (float) 1, 46 Float.MAX_VALUE, 47 48 Float.NEGATIVE_INFINITY, 49 Float.POSITIVE_INFINITY, 50 Float.NaN, 51 (float) -0.0, 52 53 54 55 56 57 58 59 }; 60 61 private static void relGet(FloatBuffer b) { 62 int n = b.capacity(); 63 float v; 64 for (int i = 0; i < n; i++) 65 ck(b, (long)b.get(), (long)((float)ic(i))); 66 b.rewind(); 67 } 68 69 private static void relGet(FloatBuffer b, int start) { 70 int n = b.remaining(); 71 float v; 72 for (int i = start; i < n; i++) 73 ck(b, (long)b.get(), (long)((float)ic(i))); 74 b.rewind(); 75 } 76 77 private static void absGet(FloatBuffer b) { 78 int n = b.capacity(); 79 float v; 80 for (int i = 0; i < n; i++) 81 ck(b, (long)b.get(), (long)((float)ic(i))); 82 b.rewind(); 83 } 84 85 private static void bulkGet(FloatBuffer b) { 86 int n = b.capacity(); 87 float[] a = new float[n + 7]; 88 b.get(a, 7, n); 89 for (int i = 0; i < n; i++) 90 ck(b, (long)a[i + 7], (long)((float)ic(i))); 91 } 92 93 private static void relPut(FloatBuffer b) { 94 int n = b.capacity(); 95 b.clear(); 96 for (int i = 0; i < n; i++) 97 b.put((float)ic(i)); 98 b.flip(); 99 } 100 101 private static void absPut(FloatBuffer b) { 102 int n = b.capacity(); 103 b.clear(); 104 for (int i = 0; i < n; i++) 105 b.put(i, (float)ic(i)); 106 b.limit(n); 107 b.position(0); 108 } 109 110 private static void bulkPutArray(FloatBuffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 float[] a = new float[n + 7]; 114 for (int i = 0; i < n; i++) 115 a[i + 7] = (float)ic(i); 116 b.put(a, 7, n); 117 b.flip(); 118 } 119 120 private static void bulkPutBuffer(FloatBuffer b) { 121 int n = b.capacity(); 122 b.clear(); 123 FloatBuffer c = FloatBuffer.allocate(n + 7); 124 c.position(7); 125 for (int i = 0; i < n; i++) 126 c.put((float)ic(i)); 127 c.flip(); 128 c.position(7); 129 b.put(c); 130 b.flip(); 131 try { 132 b.put(b); 133 fail("IllegalArgumentException expected for putting into same buffer"); 134 } catch (IllegalArgumentException e) { 135 if (e.getMessage() == null) { 136 fail("Non-null IllegalArgumentException message expected from putting into same buffer"); 137 } 138 } 139 } 140 141 //6231529 142 private static void callReset(FloatBuffer b) { 143 b.position(0); 144 b.mark(); 145 146 b.duplicate().reset(); 147 b.asReadOnlyBuffer().reset(); 148 } 149 150 151 152 // 6221101-6234263 153 154 private static void putBuffer() { 155 final int cap = 10; 156 157 FloatBuffer direct1 = ByteBuffer.allocateDirect(cap).asFloatBuffer(); 158 FloatBuffer nondirect1 = ByteBuffer.allocate(cap).asFloatBuffer(); 159 direct1.put(nondirect1); 160 161 FloatBuffer direct2 = ByteBuffer.allocateDirect(cap).asFloatBuffer(); 162 FloatBuffer nondirect2 = ByteBuffer.allocate(cap).asFloatBuffer(); 163 nondirect2.put(direct2); 164 165 FloatBuffer direct3 = ByteBuffer.allocateDirect(cap).asFloatBuffer(); 166 FloatBuffer direct4 = ByteBuffer.allocateDirect(cap).asFloatBuffer(); 167 direct3.put(direct4); 168 169 FloatBuffer nondirect3 = ByteBuffer.allocate(cap).asFloatBuffer(); 170 FloatBuffer nondirect4 = ByteBuffer.allocate(cap).asFloatBuffer(); 171 nondirect3.put(nondirect4); 172 } 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 private static void checkSlice(FloatBuffer b, FloatBuffer slice) { 191 ck(slice, 0, slice.position()); 192 ck(slice, b.remaining(), slice.limit()); 193 ck(slice, b.remaining(), slice.capacity()); 194 if (b.isDirect() != slice.isDirect()) 195 fail("Lost direction", slice); 196 if (b.isReadOnly() != slice.isReadOnly()) 197 fail("Lost read-only", slice); 198 } 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 private static void fail(String problem, 341 FloatBuffer xb, FloatBuffer yb, 342 float x, float y) { 343 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); 344 } 345 346 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { 347 boolean caught = false; 348 try { 349 thunk.run(); 350 } catch (Throwable x) { 351 if (ex.isAssignableFrom(x.getClass())) { 352 caught = true; 353 } else { 354 fail(x.getMessage() + " not expected"); 355 } 356 } 357 if (!caught) 358 fail(ex.getName() + " not thrown", b); 359 } 360 361 private static void tryCatch(float [] t, Class<?> ex, Runnable thunk) { 362 tryCatch(FloatBuffer.wrap(t), ex, thunk); 363 } 364 365 public static void test(int level, final FloatBuffer b, boolean direct) { 366 367 show(level, b); 368 369 if (direct != b.isDirect()) 370 fail("Wrong direction", b); 371 372 // Gets and puts 373 374 relPut(b); 375 relGet(b); 376 absGet(b); 377 bulkGet(b); 378 379 absPut(b); 380 relGet(b); 381 absGet(b); 382 bulkGet(b); 383 384 bulkPutArray(b); 385 relGet(b); 386 387 bulkPutBuffer(b); 388 relGet(b); 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 // Compact 429 430 relPut(b); 431 b.position(13); 432 b.compact(); 433 b.flip(); 434 relGet(b, 13); 435 436 // Exceptions 437 438 relPut(b); 439 b.limit(b.capacity() / 2); 440 b.position(b.limit()); 441 442 tryCatch(b, BufferUnderflowException.class, new Runnable() { 443 public void run() { 444 b.get(); 445 }}); 446 447 tryCatch(b, BufferOverflowException.class, new Runnable() { 448 public void run() { 449 b.put((float)42); 450 }}); 451 452 // The index must be non-negative and lesss than the buffer's limit. 453 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 454 public void run() { 455 b.get(b.limit()); 456 }}); 457 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 458 public void run() { 459 b.get(-1); 460 }}); 461 462 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 463 public void run() { 464 b.put(b.limit(), (float)42); 465 }}); 466 467 tryCatch(b, InvalidMarkException.class, new Runnable() { 468 public void run() { 469 b.position(0); 470 b.mark(); 471 b.compact(); 472 b.reset(); 473 }}); 474 475 try { 476 b.position(b.limit() + 1); 477 fail("IllegalArgumentException expected for setting position beyond limit"); 478 } catch (IllegalArgumentException e) { 479 if (e.getMessage() == null) { 480 fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); 481 } 482 } 483 484 try { 485 b.position(-1); 486 fail("IllegalArgumentException expected for setting negative position"); 487 } catch (IllegalArgumentException e) { 488 if (e.getMessage() == null) { 489 fail("Non-null IllegalArgumentException message expected for setting negative position"); 490 } 491 } 492 493 try { 494 b.limit(b.capacity() + 1); 495 fail("IllegalArgumentException expected for setting limit beyond capacity"); 496 } catch (IllegalArgumentException e) { 497 if (e.getMessage() == null) { 498 fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); 499 } 500 } 501 502 try { 503 b.limit(-1); 504 fail("IllegalArgumentException expected for setting negative limit"); 505 } catch (IllegalArgumentException e) { 506 if (e.getMessage() == null) { 507 fail("Non-null IllegalArgumentException message expected for setting negative limit"); 508 } 509 } 510 511 // Values 512 513 b.clear(); 514 b.put((float)0); 515 b.put((float)-1); 516 b.put((float)1); 517 b.put(Float.MAX_VALUE); 518 b.put(Float.MIN_VALUE); 519 520 b.put(-Float.MAX_VALUE); 521 b.put(-Float.MIN_VALUE); 522 b.put(Float.NEGATIVE_INFINITY); 523 b.put(Float.POSITIVE_INFINITY); 524 b.put(Float.NaN); 525 b.put(0.91697687f); // Changes value if incorrectly swapped 526 527 528 529 530 531 532 533 534 535 536 float v; 537 b.flip(); 538 ck(b, b.get(), 0); 539 ck(b, b.get(), (float)-1); 540 ck(b, b.get(), 1); 541 ck(b, b.get(), Float.MAX_VALUE); 542 ck(b, b.get(), Float.MIN_VALUE); 543 544 545 ck(b, b.get(), -Float.MAX_VALUE); 546 ck(b, b.get(), -Float.MIN_VALUE); 547 ck(b, b.get(), Float.NEGATIVE_INFINITY); 548 ck(b, b.get(), Float.POSITIVE_INFINITY); 549 if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN)) 550 fail(b, (long)Float.NaN, (long)v); 551 ck(b, b.get(), 0.91697687f); 552 553 554 555 556 557 558 559 560 561 562 563 564 565 // Comparison 566 b.rewind(); 567 FloatBuffer b2 = FloatBuffer.allocate(b.capacity()); 568 b2.put(b); 569 b2.flip(); 570 b.position(2); 571 b2.position(2); 572 if (!b.equals(b2)) { 573 for (int i = 2; i < b.limit(); i++) { 574 float x = b.get(i); 575 float y = b2.get(i); 576 if (x != y 577 578 579 580 581 || Float.compare(x, y) != 0 582 583 ) 584 out.println("[" + i + "] " + x + " != " + y); 585 } 586 fail("Identical buffers not equal", b, b2); 587 } 588 if (b.compareTo(b2) != 0) 589 fail("Comparison to identical buffer != 0", b, b2); 590 591 b.limit(b.limit() + 1); 592 b.position(b.limit() - 1); 593 b.put((float)99); 594 b.rewind(); 595 b2.rewind(); 596 if (b.equals(b2)) 597 fail("Non-identical buffers equal", b, b2); 598 if (b.compareTo(b2) <= 0) 599 fail("Comparison to shorter buffer <= 0", b, b2); 600 b.limit(b.limit() - 1); 601 602 b.put(2, (float)42); 603 if (b.equals(b2)) 604 fail("Non-identical buffers equal", b, b2); 605 if (b.compareTo(b2) <= 0) 606 fail("Comparison to lesser buffer <= 0", b, b2); 607 608 // Check equals and compareTo with interesting values 609 for (float x : VALUES) { 610 FloatBuffer xb = FloatBuffer.wrap(new float[] { x }); 611 if (xb.compareTo(xb) != 0) { 612 fail("compareTo not reflexive", xb, xb, x, x); 613 } 614 if (! xb.equals(xb)) { 615 fail("equals not reflexive", xb, xb, x, x); 616 } 617 for (float y : VALUES) { 618 FloatBuffer yb = FloatBuffer.wrap(new float[] { y }); 619 if (xb.compareTo(yb) != - yb.compareTo(xb)) { 620 fail("compareTo not anti-symmetric", 621 xb, yb, x, y); 622 } 623 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { 624 fail("compareTo inconsistent with equals", 625 xb, yb, x, y); 626 } 627 if (xb.compareTo(yb) != Float.compare(x, y)) { 628 629 if (x == 0.0 && y == 0.0) continue; 630 631 632 633 634 fail("Incorrect results for FloatBuffer.compareTo", 635 xb, yb, x, y); 636 } 637 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 638 fail("Incorrect results for FloatBuffer.equals", 639 xb, yb, x, y); 640 } 641 } 642 } 643 644 // Sub, dup 645 646 relPut(b); 647 relGet(b.duplicate()); 648 b.position(13); 649 relGet(b.duplicate(), 13); 650 relGet(b.duplicate().slice(), 13); 651 relGet(b.slice(), 13); 652 relGet(b.slice().duplicate(), 13); 653 654 // Slice 655 656 b.position(5); 657 FloatBuffer sb = b.slice(); 658 checkSlice(b, sb); 659 b.position(0); 660 FloatBuffer sb2 = sb.slice(); 661 checkSlice(sb, sb2); 662 663 if (!sb.equals(sb2)) 664 fail("Sliced slices do not match", sb, sb2); 665 if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) 666 fail("Array offsets do not match: " 667 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 // Read-only views 701 702 b.rewind(); 703 final FloatBuffer rb = b.asReadOnlyBuffer(); 704 if (!b.equals(rb)) 705 fail("Buffer not equal to read-only view", b, rb); 706 show(level + 1, rb); 707 708 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 709 public void run() { 710 relPut(rb); 711 }}); 712 713 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 714 public void run() { 715 absPut(rb); 716 }}); 717 718 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 719 public void run() { 720 bulkPutArray(rb); 721 }}); 722 723 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 724 public void run() { 725 bulkPutBuffer(rb); 726 }}); 727 728 // put(FloatBuffer) should not change source position 729 final FloatBuffer src = FloatBuffer.allocate(1); 730 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 731 public void run() { 732 rb.put(src); 733 }}); 734 ck(src, src.position(), 0); 735 736 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 737 public void run() { 738 rb.compact(); 739 }}); 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 if (rb.getClass().getName().startsWith("java.nio.Heap")) { 816 817 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 818 public void run() { 819 rb.array(); 820 }}); 821 822 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 823 public void run() { 824 rb.arrayOffset(); 825 }}); 826 827 if (rb.hasArray()) 828 fail("Read-only heap buffer's backing array is accessible", 829 rb); 830 831 } 832 833 // Bulk puts from read-only buffers 834 835 b.clear(); 836 rb.rewind(); 837 b.put(rb); 838 839 840 841 842 843 844 845 846 847 848 849 relPut(b); // Required by testViews 850 851 } 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 public static void test(final float [] ba) { 938 int offset = 47; 939 int length = 900; 940 final FloatBuffer b = FloatBuffer.wrap(ba, offset, length); 941 show(0, b); 942 ck(b, b.capacity(), ba.length); 943 ck(b, b.position(), offset); 944 ck(b, b.limit(), offset + length); 945 946 // The offset must be non-negative and no larger than <array.length>. 947 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 948 public void run() { 949 FloatBuffer.wrap(ba, -1, ba.length); 950 }}); 951 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 952 public void run() { 953 FloatBuffer.wrap(ba, ba.length + 1, ba.length); 954 }}); 955 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 956 public void run() { 957 FloatBuffer.wrap(ba, 0, -1); 958 }}); 959 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 960 public void run() { 961 FloatBuffer.wrap(ba, 0, ba.length + 1); 962 }}); 963 964 // A NullPointerException will be thrown if the array is null. 965 tryCatch(ba, NullPointerException.class, new Runnable() { 966 public void run() { 967 FloatBuffer.wrap((float []) null, 0, 5); 968 }}); 969 tryCatch(ba, NullPointerException.class, new Runnable() { 970 public void run() { 971 FloatBuffer.wrap((float []) null); 972 }}); 973 } 974 975 private static void testAllocate() { 976 // An IllegalArgumentException will be thrown for negative capacities. 977 tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() { 978 public void run() { 979 FloatBuffer.allocate(-1); 980 }}); 981 try { 982 FloatBuffer.allocate(-1); 983 } catch (IllegalArgumentException e) { 984 if (e.getMessage() == null) { 985 fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); 986 } 987 } 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 } 1002 1003 public static void test() { 1004 testAllocate(); 1005 test(0, FloatBuffer.allocate(7 * 1024), false); 1006 test(0, FloatBuffer.wrap(new float[7 * 1024], 0, 7 * 1024), false); 1007 test(new float[1024]); 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 callReset(FloatBuffer.allocate(10)); 1019 1020 1021 1022 putBuffer(); 1023 1024 } 1025 1026 }