1 /* 2 * Copyright (c) 2000, 2019, 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 35 36 public class BasicDouble 37 extends Basic 38 { 39 40 private static final double[] VALUES = { 41 Double.MIN_VALUE, 42 (double) -1, 43 (double) 0, 44 (double) 1, 45 Double.MAX_VALUE, 46 47 48 49 50 51 52 53 Double.NEGATIVE_INFINITY, 54 Double.POSITIVE_INFINITY, 55 Double.NaN, 56 (double) -0.0, 57 58 }; 59 60 private static void relGet(DoubleBuffer b) { 61 int n = b.capacity(); 62 for (int i = 0; i < n; i++) 63 ck(b, (long)b.get(), (long)((double)ic(i))); 64 b.rewind(); 65 } 66 67 private static void relGet(DoubleBuffer b, int start) { 68 int n = b.remaining(); 69 for (int i = start; i < n; i++) 70 ck(b, (long)b.get(), (long)((double)ic(i))); 71 b.rewind(); 72 } 73 74 private static void absGet(DoubleBuffer b) { 75 int n = b.capacity(); 76 for (int i = 0; i < n; i++) 77 ck(b, (long)b.get(), (long)((double)ic(i))); 78 b.rewind(); 79 } 80 81 private static void bulkGet(DoubleBuffer b) { 82 int n = b.capacity(); 83 double[] a = new double[n + 7]; 84 b.get(a, 7, n); 85 for (int i = 0; i < n; i++) { 86 ck(b, (long)a[i + 7], (long)((double)ic(i))); 87 } 88 } 89 90 private static void absBulkGet(DoubleBuffer b) { 91 int n = b.capacity(); 92 int len = n - 7*2; 93 double[] a = new double[n + 7]; 94 b.position(42); 95 b.get(7, a, 7, len); 96 ck(b, b.position() == 42); 97 for (int i = 0; i < len; i++) { 98 ck(b, (long)a[i + 7], (long)((double)ic(i))); 99 } 100 } 101 102 private static void relPut(DoubleBuffer b) { 103 int n = b.capacity(); 104 b.clear(); 105 for (int i = 0; i < n; i++) 106 b.put((double)ic(i)); 107 b.flip(); 108 } 109 110 private static void absPut(DoubleBuffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 for (int i = 0; i < n; i++) 114 b.put(i, (double)ic(i)); 115 b.limit(n); 116 b.position(0); 117 } 118 119 private static void bulkPutArray(DoubleBuffer b) { 120 int n = b.capacity(); 121 b.clear(); 122 double[] a = new double[n + 7]; 123 for (int i = 0; i < n; i++) 124 a[i + 7] = (double)ic(i); 125 b.put(a, 7, n); 126 b.flip(); 127 } 128 129 private static void bulkPutBuffer(DoubleBuffer b) { 130 int n = b.capacity(); 131 b.clear(); 132 DoubleBuffer c = DoubleBuffer.allocate(n + 7); 133 c.position(7); 134 for (int i = 0; i < n; i++) 135 c.put((double)ic(i)); 136 c.flip(); 137 c.position(7); 138 b.put(c); 139 b.flip(); 140 try { 141 b.put(b); 142 fail("IllegalArgumentException expected for put into same buffer"); 143 } catch (IllegalArgumentException e) { 144 if (e.getMessage() == null) { 145 fail("Non-null IllegalArgumentException message expected from" 146 + " put into same buffer"); 147 } 148 } 149 } 150 151 private static void absBulkPutArray(DoubleBuffer b) { 152 int n = b.capacity(); 153 b.clear(); 154 int lim = n - 7; 155 int len = lim - 7; 156 b.limit(lim); 157 double[] a = new double[len + 7]; 158 for (int i = 0; i < len; i++) 159 a[i + 7] = (double)ic(i); 160 b.position(42); 161 b.put(7, a, 7, len); 162 ck(b, b.position() == 42); 163 } 164 165 //6231529 166 private static void callReset(DoubleBuffer b) { 167 b.position(0); 168 b.mark(); 169 170 b.duplicate().reset(); 171 b.asReadOnlyBuffer().reset(); 172 } 173 174 175 176 // 6221101-6234263 177 178 private static void putBuffer() { 179 final int cap = 10; 180 181 DoubleBuffer direct1 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 182 DoubleBuffer nondirect1 = ByteBuffer.allocate(cap).asDoubleBuffer(); 183 direct1.put(nondirect1); 184 185 DoubleBuffer direct2 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 186 DoubleBuffer nondirect2 = ByteBuffer.allocate(cap).asDoubleBuffer(); 187 nondirect2.put(direct2); 188 189 DoubleBuffer direct3 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 190 DoubleBuffer direct4 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 191 direct3.put(direct4); 192 193 DoubleBuffer nondirect3 = ByteBuffer.allocate(cap).asDoubleBuffer(); 194 DoubleBuffer nondirect4 = ByteBuffer.allocate(cap).asDoubleBuffer(); 195 nondirect3.put(nondirect4); 196 } 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 private static void checkSlice(DoubleBuffer b, DoubleBuffer slice) { 215 ck(slice, 0, slice.position()); 216 ck(slice, b.remaining(), slice.limit()); 217 ck(slice, b.remaining(), slice.capacity()); 218 if (b.isDirect() != slice.isDirect()) 219 fail("Lost direction", slice); 220 if (b.isReadOnly() != slice.isReadOnly()) 221 fail("Lost read-only", slice); 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 private static void fail(String problem, 476 DoubleBuffer xb, DoubleBuffer yb, 477 double x, double y) { 478 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); 479 } 480 481 private static void catchNullArgument(Buffer b, Runnable thunk) { 482 tryCatch(b, NullPointerException.class, thunk); 483 } 484 485 private static void catchIllegalArgument(Buffer b, Runnable thunk) { 486 tryCatch(b, IllegalArgumentException.class, thunk); 487 } 488 489 private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) { 490 tryCatch(b, ReadOnlyBufferException.class, thunk); 491 } 492 493 private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) { 494 tryCatch(b, IndexOutOfBoundsException.class, thunk); 495 } 496 497 private static void catchIndexOutOfBounds(double[] t, Runnable thunk) { 498 tryCatch(t, IndexOutOfBoundsException.class, thunk); 499 } 500 501 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { 502 boolean caught = false; 503 try { 504 thunk.run(); 505 } catch (Throwable x) { 506 if (ex.isAssignableFrom(x.getClass())) { 507 caught = true; 508 } else { 509 String s = x.getMessage(); 510 if (s == null) 511 s = x.getClass().getName(); 512 fail(s + " not expected"); 513 } 514 } 515 if (!caught) { 516 fail(ex.getName() + " not thrown", b); 517 } 518 } 519 520 private static void tryCatch(double[] t, Class<?> ex, Runnable thunk) { 521 tryCatch(DoubleBuffer.wrap(t), ex, thunk); 522 } 523 524 public static void test(int level, final DoubleBuffer b, boolean direct) { 525 526 show(level, b); 527 528 if (direct != b.isDirect()) 529 fail("Wrong direction", b); 530 531 // Gets and puts 532 533 relPut(b); 534 relGet(b); 535 absGet(b); 536 bulkGet(b); 537 538 absPut(b); 539 relGet(b); 540 absGet(b); 541 bulkGet(b); 542 543 bulkPutArray(b); 544 relGet(b); 545 546 bulkPutBuffer(b); 547 relGet(b); 548 549 absBulkPutArray(b); 550 absBulkGet(b); 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 // Compact 589 590 relPut(b); 591 b.position(13); 592 b.compact(); 593 b.flip(); 594 relGet(b, 13); 595 596 // Exceptions 597 598 relPut(b); 599 b.limit(b.capacity() / 2); 600 b.position(b.limit()); 601 602 tryCatch(b, BufferUnderflowException.class, () -> b.get()); 603 tryCatch(b, BufferOverflowException.class, () -> b.put((double)42)); 604 // The index must be non-negative and less than the buffer's limit. 605 catchIndexOutOfBounds(b, () -> b.get(b.limit())); 606 catchIndexOutOfBounds(b, () -> b.get(-1)); 607 catchIndexOutOfBounds(b, () -> b.put(b.limit(), (double)42)); 608 tryCatch(b, InvalidMarkException.class, 609 () -> b.position(0).mark().compact().reset()); 610 611 try { 612 b.position(b.limit() + 1); 613 fail("IllegalArgumentException expected for position beyond limit"); 614 } catch (IllegalArgumentException e) { 615 if (e.getMessage() == null) { 616 fail("Non-null IllegalArgumentException message expected for" 617 + " position beyond limit"); 618 } 619 } 620 621 try { 622 b.position(-1); 623 fail("IllegalArgumentException expected for negative position"); 624 } catch (IllegalArgumentException e) { 625 if (e.getMessage() == null) { 626 fail("Non-null IllegalArgumentException message expected for" 627 + " negative position"); 628 } 629 } 630 631 try { 632 b.limit(b.capacity() + 1); 633 fail("IllegalArgumentException expected for limit beyond capacity"); 634 } catch (IllegalArgumentException e) { 635 if (e.getMessage() == null) { 636 fail("Non-null IllegalArgumentException message expected for" 637 + " limit beyond capacity"); 638 } 639 } 640 641 try { 642 b.limit(-1); 643 fail("IllegalArgumentException expected for negative limit"); 644 } catch (IllegalArgumentException e) { 645 if (e.getMessage() == null) { 646 fail("Non-null IllegalArgumentException message expected for" 647 + " negative limit"); 648 } 649 } 650 651 // Exceptions in absolute bulk operations 652 653 catchNullArgument(b, () -> b.get(7, null, 0, 42)); 654 catchNullArgument(b, () -> b.put(7, (double[])null, 0, 42)); 655 656 double[] tmpa = new double[42]; 657 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42)); 658 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1)); 659 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1)); 660 catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1)); 661 catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1)); 662 catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42)); 663 664 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42)); 665 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1)); 666 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1)); 667 catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1)); 668 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1)); 669 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42)); 670 671 // Values 672 673 b.clear(); 674 b.put((double)0); 675 b.put((double)-1); 676 b.put((double)1); 677 b.put(Double.MAX_VALUE); 678 b.put(Double.MIN_VALUE); 679 680 681 682 683 684 685 686 687 688 b.put(-Double.MAX_VALUE); 689 b.put(-Double.MIN_VALUE); 690 b.put(Double.NEGATIVE_INFINITY); 691 b.put(Double.POSITIVE_INFINITY); 692 b.put(Double.NaN); 693 b.put(0.5121609353879392); // Changes value if incorrectly swapped 694 695 696 b.flip(); 697 ck(b, b.get(), 0); 698 ck(b, b.get(), (double)-1); 699 ck(b, b.get(), 1); 700 ck(b, b.get(), Double.MAX_VALUE); 701 ck(b, b.get(), Double.MIN_VALUE); 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 double v; 717 ck(b, b.get(), -Double.MAX_VALUE); 718 ck(b, b.get(), -Double.MIN_VALUE); 719 ck(b, b.get(), Double.NEGATIVE_INFINITY); 720 ck(b, b.get(), Double.POSITIVE_INFINITY); 721 if (Double.doubleToRawLongBits(v = b.get()) 722 != Double.doubleToRawLongBits(Double.NaN)) { 723 fail(b, (long)Double.NaN, (long)v); 724 } 725 ck(b, b.get(), 0.5121609353879392); 726 727 728 729 // Comparison 730 b.rewind(); 731 DoubleBuffer b2 = DoubleBuffer.allocate(b.capacity()); 732 b2.put(b); 733 b2.flip(); 734 b.position(2); 735 b2.position(2); 736 if (!b.equals(b2)) { 737 for (int i = 2; i < b.limit(); i++) { 738 double x = b.get(i); 739 double y = b2.get(i); 740 if (x != y 741 742 || Double.compare(x, y) != 0 743 744 745 746 747 ) { 748 out.println("[" + i + "] " + x + " != " + y); 749 } 750 } 751 fail("Identical buffers not equal", b, b2); 752 } 753 if (b.compareTo(b2) != 0) { 754 fail("Comparison to identical buffer != 0", b, b2); 755 } 756 b.limit(b.limit() + 1); 757 b.position(b.limit() - 1); 758 b.put((double)99); 759 b.rewind(); 760 b2.rewind(); 761 if (b.equals(b2)) 762 fail("Non-identical buffers equal", b, b2); 763 if (b.compareTo(b2) <= 0) 764 fail("Comparison to shorter buffer <= 0", b, b2); 765 b.limit(b.limit() - 1); 766 767 b.put(2, (double)42); 768 if (b.equals(b2)) 769 fail("Non-identical buffers equal", b, b2); 770 if (b.compareTo(b2) <= 0) 771 fail("Comparison to lesser buffer <= 0", b, b2); 772 773 // Check equals and compareTo with interesting values 774 for (double x : VALUES) { 775 DoubleBuffer xb = DoubleBuffer.wrap(new double[] { x }); 776 if (xb.compareTo(xb) != 0) { 777 fail("compareTo not reflexive", xb, xb, x, x); 778 } 779 if (!xb.equals(xb)) { 780 fail("equals not reflexive", xb, xb, x, x); 781 } 782 for (double y : VALUES) { 783 DoubleBuffer yb = DoubleBuffer.wrap(new double[] { y }); 784 if (xb.compareTo(yb) != - yb.compareTo(xb)) { 785 fail("compareTo not anti-symmetric", 786 xb, yb, x, y); 787 } 788 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { 789 fail("compareTo inconsistent with equals", 790 xb, yb, x, y); 791 } 792 if (xb.compareTo(yb) != Double.compare(x, y)) { 793 794 795 796 797 if (x == 0.0 && y == 0.0) continue; 798 799 fail("Incorrect results for DoubleBuffer.compareTo", 800 xb, yb, x, y); 801 } 802 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 803 fail("Incorrect results for DoubleBuffer.equals", 804 xb, yb, x, y); 805 } 806 } 807 } 808 809 // Sub, dup 810 811 relPut(b); 812 relGet(b.duplicate()); 813 b.position(13); 814 relGet(b.duplicate(), 13); 815 relGet(b.duplicate().slice(), 13); 816 relGet(b.slice(), 13); 817 relGet(b.slice().duplicate(), 13); 818 819 // Slice 820 821 b.position(5); 822 DoubleBuffer sb = b.slice(); 823 checkSlice(b, sb); 824 b.position(0); 825 DoubleBuffer sb2 = sb.slice(); 826 checkSlice(sb, sb2); 827 828 if (!sb.equals(sb2)) 829 fail("Sliced slices do not match", sb, sb2); 830 if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) { 831 fail("Array offsets do not match: " 832 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); 833 } 834 835 int bPos = b.position(); 836 int bLim = b.limit(); 837 838 b.position(7); 839 b.limit(42); 840 DoubleBuffer rsb = b.slice(); 841 b.position(0); 842 b.limit(b.capacity()); 843 DoubleBuffer asb = b.slice(7, 35); 844 checkSlice(rsb, asb); 845 846 b.position(bPos); 847 b.limit(bLim); 848 849 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 // Read-only views 881 882 b.rewind(); 883 final DoubleBuffer rb = b.asReadOnlyBuffer(); 884 if (!b.equals(rb)) 885 fail("Buffer not equal to read-only view", b, rb); 886 show(level + 1, rb); 887 888 catchReadOnlyBuffer(b, () -> relPut(rb)); 889 catchReadOnlyBuffer(b, () -> absPut(rb)); 890 catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); 891 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); 892 catchReadOnlyBuffer(b, () -> absBulkPutArray(rb)); 893 894 // put(DoubleBuffer) should not change source position 895 final DoubleBuffer src = DoubleBuffer.allocate(1); 896 catchReadOnlyBuffer(b, () -> rb.put(src)); 897 ck(src, src.position(), 0); 898 899 catchReadOnlyBuffer(b, () -> rb.compact()); 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 if (rb.getClass().getName().startsWith("java.nio.Heap")) { 927 catchReadOnlyBuffer(b, () -> rb.array()); 928 catchReadOnlyBuffer(b, () -> rb.arrayOffset()); 929 if (rb.hasArray()) { 930 fail("Read-only heap buffer's backing array is accessible", rb); 931 } 932 } 933 934 // Bulk puts from read-only buffers 935 936 b.clear(); 937 rb.rewind(); 938 b.put(rb); 939 940 941 942 943 944 945 946 947 948 949 950 relPut(b); // Required by testViews 951 952 953 954 955 956 957 } 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 public static void test(final double [] ba) { 1010 int offset = 47; 1011 int length = 900; 1012 final DoubleBuffer b = DoubleBuffer.wrap(ba, offset, length); 1013 show(0, b); 1014 ck(b, b.capacity(), ba.length); 1015 ck(b, b.position(), offset); 1016 ck(b, b.limit(), offset + length); 1017 1018 // The offset must be non-negative and no larger than <array.length>. 1019 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, -1, ba.length)); 1020 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, ba.length + 1, ba.length)); 1021 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, 0, -1)); 1022 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, 0, ba.length + 1)); 1023 1024 // A NullPointerException will be thrown if the array is null. 1025 tryCatch(ba, NullPointerException.class, 1026 () -> DoubleBuffer.wrap((double []) null, 0, 5)); 1027 tryCatch(ba, NullPointerException.class, 1028 () -> DoubleBuffer.wrap((double []) null)); 1029 } 1030 1031 private static void testAllocate() { 1032 // An IllegalArgumentException will be thrown for negative capacities. 1033 catchIllegalArgument((Buffer) null, () -> DoubleBuffer.allocate(-1)); 1034 try { 1035 DoubleBuffer.allocate(-1); 1036 } catch (IllegalArgumentException e) { 1037 if (e.getMessage() == null) { 1038 fail("Non-null IllegalArgumentException message expected for" 1039 + " attempt to allocate negative capacity buffer"); 1040 } 1041 } 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 } 1054 1055 public static void test() { 1056 testAllocate(); 1057 test(0, DoubleBuffer.allocate(7 * 1024), false); 1058 test(0, DoubleBuffer.wrap(new double[7 * 1024], 0, 7 * 1024), false); 1059 test(new double[1024]); 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 callReset(DoubleBuffer.allocate(10)); 1071 1072 1073 1074 putBuffer(); 1075 1076 } 1077 1078 }