1 /* 2 * Copyright (c) 2000, 2020, 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 package test.java.nio.Buffer; 31 32 // -- This file was mechanically generated: Do not edit! -- // 33 34 35 36 37 38 import java.nio.*; 39 import java.util.function.Supplier; 40 41 42 43 44 45 46 47 48 import static org.testng.Assert.assertEquals; 49 50 51 public class BasicDouble 52 extends Basic 53 { 54 55 private static final double[] VALUES = { 56 Double.MIN_VALUE, 57 (double) -1, 58 (double) 0, 59 (double) 1, 60 Double.MAX_VALUE, 61 62 63 64 65 66 67 68 Double.NEGATIVE_INFINITY, 69 Double.POSITIVE_INFINITY, 70 Double.NaN, 71 (double) -0.0, 72 73 }; 74 relGet(DoubleBuffer b)75 private static void relGet(DoubleBuffer b) { 76 int n = b.capacity(); 77 for (int i = 0; i < n; i++) 78 ck(b, (long)b.get(), (long)((double)ic(i))); 79 b.rewind(); 80 } 81 relGet(DoubleBuffer b, int start)82 private static void relGet(DoubleBuffer b, int start) { 83 int n = b.remaining(); 84 for (int i = start; i < n; i++) 85 ck(b, (long)b.get(), (long)((double)ic(i))); 86 b.rewind(); 87 } 88 absGet(DoubleBuffer b)89 private static void absGet(DoubleBuffer b) { 90 int n = b.capacity(); 91 for (int i = 0; i < n; i++) 92 ck(b, (long)b.get(), (long)((double)ic(i))); 93 b.rewind(); 94 } 95 bulkGet(DoubleBuffer b)96 private static void bulkGet(DoubleBuffer b) { 97 int n = b.capacity(); 98 double[] a = new double[n + 7]; 99 b.get(a, 7, n); 100 for (int i = 0; i < n; i++) { 101 ck(b, (long)a[i + 7], (long)((double)ic(i))); 102 } 103 } 104 absBulkGet(DoubleBuffer b)105 private static void absBulkGet(DoubleBuffer b) { 106 int n = b.capacity(); 107 int len = n - 7*2; 108 double[] a = new double[n + 7]; 109 b.position(42); 110 b.get(7, a, 7, len); 111 ck(b, b.position() == 42); 112 for (int i = 0; i < len; i++) { 113 ck(b, (long)a[i + 7], (long)((double)ic(i))); 114 } 115 } 116 relPut(DoubleBuffer b)117 private static void relPut(DoubleBuffer b) { 118 int n = b.capacity(); 119 b.clear(); 120 for (int i = 0; i < n; i++) 121 b.put((double)ic(i)); 122 b.flip(); 123 } 124 absPut(DoubleBuffer b)125 private static void absPut(DoubleBuffer b) { 126 int n = b.capacity(); 127 b.clear(); 128 for (int i = 0; i < n; i++) 129 b.put(i, (double)ic(i)); 130 b.limit(n); 131 b.position(0); 132 } 133 bulkPutArray(DoubleBuffer b)134 private static void bulkPutArray(DoubleBuffer b) { 135 int n = b.capacity(); 136 b.clear(); 137 double[] a = new double[n + 7]; 138 for (int i = 0; i < n; i++) 139 a[i + 7] = (double)ic(i); 140 b.put(a, 7, n); 141 b.flip(); 142 } 143 bulkPutBuffer(DoubleBuffer b)144 private static void bulkPutBuffer(DoubleBuffer b) { 145 int n = b.capacity(); 146 b.clear(); 147 DoubleBuffer c = DoubleBuffer.allocate(n + 7); 148 c.position(7); 149 for (int i = 0; i < n; i++) 150 c.put((double)ic(i)); 151 c.flip(); 152 c.position(7); 153 b.put(c); 154 b.flip(); 155 try { 156 b.put(b); 157 fail("IllegalArgumentException expected for put into same buffer"); 158 } catch (IllegalArgumentException e) { 159 if (e.getMessage() == null) { 160 fail("Non-null IllegalArgumentException message expected from" 161 + " put into same buffer"); 162 } 163 } 164 } 165 absBulkPutArray(DoubleBuffer b)166 private static void absBulkPutArray(DoubleBuffer b) { 167 int n = b.capacity(); 168 b.clear(); 169 int lim = n - 7; 170 int len = lim - 7; 171 b.limit(lim); 172 double[] a = new double[len + 7]; 173 for (int i = 0; i < len; i++) 174 a[i + 7] = (double)ic(i); 175 b.position(42); 176 b.put(7, a, 7, len); 177 ck(b, b.position() == 42); 178 } 179 180 //6231529 callReset(DoubleBuffer b)181 private static void callReset(DoubleBuffer b) { 182 b.position(0); 183 b.mark(); 184 185 b.duplicate().reset(); 186 b.asReadOnlyBuffer().reset(); 187 } 188 189 190 191 // 6221101-6234263 192 putBuffer()193 private static void putBuffer() { 194 final int cap = 10; 195 196 DoubleBuffer direct1 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 197 DoubleBuffer nondirect1 = ByteBuffer.allocate(cap).asDoubleBuffer(); 198 direct1.put(nondirect1); 199 200 DoubleBuffer direct2 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 201 DoubleBuffer nondirect2 = ByteBuffer.allocate(cap).asDoubleBuffer(); 202 nondirect2.put(direct2); 203 204 DoubleBuffer direct3 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 205 DoubleBuffer direct4 = ByteBuffer.allocateDirect(cap).asDoubleBuffer(); 206 direct3.put(direct4); 207 208 DoubleBuffer nondirect3 = ByteBuffer.allocate(cap).asDoubleBuffer(); 209 DoubleBuffer nondirect4 = ByteBuffer.allocate(cap).asDoubleBuffer(); 210 nondirect3.put(nondirect4); 211 } 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 checkSlice(DoubleBuffer b, DoubleBuffer slice)229 private static void checkSlice(DoubleBuffer b, DoubleBuffer slice) { 230 ck(slice, 0, slice.position()); 231 ck(slice, b.remaining(), slice.limit()); 232 ck(slice, b.remaining(), slice.capacity()); 233 if (b.isDirect() != slice.isDirect()) 234 fail("Lost direction", slice); 235 if (b.isReadOnly() != slice.isReadOnly()) 236 fail("Lost read-only", slice); 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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 fail(String problem, DoubleBuffer xb, DoubleBuffer yb, double x, double y)559 private static void fail(String problem, 560 DoubleBuffer xb, DoubleBuffer yb, 561 double x, double y) { 562 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); 563 } 564 catchNullArgument(Buffer b, Runnable thunk)565 private static void catchNullArgument(Buffer b, Runnable thunk) { 566 tryCatch(b, NullPointerException.class, thunk); 567 } 568 catchIllegalArgument(Buffer b, Runnable thunk)569 private static void catchIllegalArgument(Buffer b, Runnable thunk) { 570 tryCatch(b, IllegalArgumentException.class, thunk); 571 } 572 catchReadOnlyBuffer(Buffer b, Runnable thunk)573 private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) { 574 tryCatch(b, ReadOnlyBufferException.class, thunk); 575 } 576 catchIndexOutOfBounds(Buffer b, Runnable thunk)577 private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) { 578 tryCatch(b, IndexOutOfBoundsException.class, thunk); 579 } 580 catchIndexOutOfBounds(double[] t, Runnable thunk)581 private static void catchIndexOutOfBounds(double[] t, Runnable thunk) { 582 tryCatch(t, IndexOutOfBoundsException.class, thunk); 583 } 584 tryCatch(Buffer b, Class<?> ex, Runnable thunk)585 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { 586 boolean caught = false; 587 try { 588 thunk.run(); 589 } catch (Throwable x) { 590 if (ex.isAssignableFrom(x.getClass())) { 591 caught = true; 592 } else { 593 String s = x.getMessage(); 594 if (s == null) 595 s = x.getClass().getName(); 596 fail(s + " not expected"); 597 } 598 } 599 if (!caught) { 600 fail(ex.getName() + " not thrown", b); 601 } 602 } 603 tryCatch(double[] t, Class<?> ex, Runnable thunk)604 private static void tryCatch(double[] t, Class<?> ex, Runnable thunk) { 605 tryCatch(DoubleBuffer.wrap(t), ex, thunk); 606 } 607 608 // Android-changed: Add @SuppressWarnings as the operation is tested to be reflexive. 609 @SuppressWarnings({"SelfComparison", "SelfEquals"}) test(int level, final DoubleBuffer b, boolean direct)610 public static void test(int level, final DoubleBuffer b, boolean direct) { 611 612 show(level, b); 613 614 if (direct != b.isDirect()) 615 fail("Wrong direction", b); 616 617 // Gets and puts 618 619 relPut(b); 620 relGet(b); 621 absGet(b); 622 bulkGet(b); 623 624 absPut(b); 625 relGet(b); 626 absGet(b); 627 bulkGet(b); 628 629 bulkPutArray(b); 630 relGet(b); 631 632 bulkPutBuffer(b); 633 relGet(b); 634 635 absBulkPutArray(b); 636 absBulkGet(b); 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 // Compact 675 676 relPut(b); 677 b.position(13); 678 b.compact(); 679 b.flip(); 680 relGet(b, 13); 681 682 // Exceptions 683 684 relPut(b); 685 b.limit(b.capacity() / 2); 686 b.position(b.limit()); 687 688 tryCatch(b, BufferUnderflowException.class, () -> b.get()); 689 tryCatch(b, BufferOverflowException.class, () -> b.put((double)42)); 690 // The index must be non-negative and less than the buffer's limit. 691 catchIndexOutOfBounds(b, () -> b.get(b.limit())); 692 catchIndexOutOfBounds(b, () -> b.get(-1)); 693 catchIndexOutOfBounds(b, () -> b.put(b.limit(), (double)42)); 694 tryCatch(b, InvalidMarkException.class, 695 // Android-changed: Explicit casting due to @CovariantReturnType methods. 696 // () -> b.position(0).mark().compact().reset()); 697 () -> ((DoubleBuffer) b.position(0).mark()).compact().reset()); 698 699 try { 700 b.position(b.limit() + 1); 701 fail("IllegalArgumentException expected for position beyond limit"); 702 } catch (IllegalArgumentException e) { 703 if (e.getMessage() == null) { 704 fail("Non-null IllegalArgumentException message expected for" 705 + " position beyond limit"); 706 } 707 } 708 709 try { 710 b.position(-1); 711 fail("IllegalArgumentException expected for negative position"); 712 } catch (IllegalArgumentException e) { 713 if (e.getMessage() == null) { 714 fail("Non-null IllegalArgumentException message expected for" 715 + " negative position"); 716 } 717 } 718 719 try { 720 b.limit(b.capacity() + 1); 721 fail("IllegalArgumentException expected for limit beyond capacity"); 722 } catch (IllegalArgumentException e) { 723 if (e.getMessage() == null) { 724 fail("Non-null IllegalArgumentException message expected for" 725 + " limit beyond capacity"); 726 } 727 } 728 729 try { 730 b.limit(-1); 731 fail("IllegalArgumentException expected for negative limit"); 732 } catch (IllegalArgumentException e) { 733 if (e.getMessage() == null) { 734 fail("Non-null IllegalArgumentException message expected for" 735 + " negative limit"); 736 } 737 } 738 739 // Exceptions in absolute bulk and slice operations 740 741 catchNullArgument(b, () -> b.get(7, null, 0, 42)); 742 catchNullArgument(b, () -> b.put(7, (double[])null, 0, 42)); 743 744 double[] tmpa = new double[42]; 745 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42)); 746 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1)); 747 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1)); 748 catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1)); 749 catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1)); 750 catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42)); 751 752 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42)); 753 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1)); 754 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1)); 755 catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1)); 756 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1)); 757 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42)); 758 759 catchIndexOutOfBounds(b, () -> b.slice(-1, 7)); 760 catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7)); 761 catchIndexOutOfBounds(b, () -> b.slice(0, -1)); 762 catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1)); 763 764 // Values 765 766 b.clear(); 767 b.put((double)0); 768 b.put((double)-1); 769 b.put((double)1); 770 b.put(Double.MAX_VALUE); 771 b.put(Double.MIN_VALUE); 772 773 774 775 776 777 778 779 780 781 b.put(-Double.MAX_VALUE); 782 b.put(-Double.MIN_VALUE); 783 b.put(Double.NEGATIVE_INFINITY); 784 b.put(Double.POSITIVE_INFINITY); 785 b.put(Double.NaN); 786 b.put(0.5121609353879392); // Changes value if incorrectly swapped 787 788 789 b.flip(); 790 ck(b, b.get(), 0); 791 ck(b, b.get(), (double)-1); 792 ck(b, b.get(), 1); 793 ck(b, b.get(), Double.MAX_VALUE); 794 ck(b, b.get(), Double.MIN_VALUE); 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 double v; 810 ck(b, b.get(), -Double.MAX_VALUE); 811 ck(b, b.get(), -Double.MIN_VALUE); 812 ck(b, b.get(), Double.NEGATIVE_INFINITY); 813 ck(b, b.get(), Double.POSITIVE_INFINITY); 814 if (Double.doubleToRawLongBits(v = b.get()) 815 != Double.doubleToRawLongBits(Double.NaN)) { 816 fail(b, (long)Double.NaN, (long)v); 817 } 818 ck(b, b.get(), 0.5121609353879392); 819 820 821 822 // Comparison 823 b.rewind(); 824 DoubleBuffer b2 = DoubleBuffer.allocate(b.capacity()); 825 b2.put(b); 826 b2.flip(); 827 b.position(2); 828 b2.position(2); 829 if (!b.equals(b2)) { 830 for (int i = 2; i < b.limit(); i++) { 831 double x = b.get(i); 832 double y = b2.get(i); 833 if (x != y 834 835 || Double.compare(x, y) != 0 836 837 838 839 840 ) { 841 out.println("[" + i + "] " + x + " != " + y); 842 } 843 } 844 fail("Identical buffers not equal", b, b2); 845 } 846 if (b.compareTo(b2) != 0) { 847 fail("Comparison to identical buffer != 0", b, b2); 848 } 849 b.limit(b.limit() + 1); 850 b.position(b.limit() - 1); 851 b.put((double)99); 852 b.rewind(); 853 b2.rewind(); 854 if (b.equals(b2)) 855 fail("Non-identical buffers equal", b, b2); 856 if (b.compareTo(b2) <= 0) 857 fail("Comparison to shorter buffer <= 0", b, b2); 858 b.limit(b.limit() - 1); 859 860 b.put(2, (double)42); 861 if (b.equals(b2)) 862 fail("Non-identical buffers equal", b, b2); 863 if (b.compareTo(b2) <= 0) 864 fail("Comparison to lesser buffer <= 0", b, b2); 865 866 // Check equals and compareTo with interesting values 867 for (double x : VALUES) { 868 DoubleBuffer xb = DoubleBuffer.wrap(new double[] { x }); 869 if (xb.compareTo(xb) != 0) { 870 fail("compareTo not reflexive", xb, xb, x, x); 871 } 872 if (!xb.equals(xb)) { 873 fail("equals not reflexive", xb, xb, x, x); 874 } 875 for (double y : VALUES) { 876 DoubleBuffer yb = DoubleBuffer.wrap(new double[] { y }); 877 if (xb.compareTo(yb) != - yb.compareTo(xb)) { 878 fail("compareTo not anti-symmetric", 879 xb, yb, x, y); 880 } 881 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { 882 fail("compareTo inconsistent with equals", 883 xb, yb, x, y); 884 } 885 if (xb.compareTo(yb) != Double.compare(x, y)) { 886 887 888 889 890 if (x == 0.0 && y == 0.0) continue; 891 892 fail("Incorrect results for DoubleBuffer.compareTo", 893 xb, yb, x, y); 894 } 895 896 897 898 899 900 901 // Android-changed: fix issue flagged by error prone (x != x) instead of isNan(x). 902 if (!(Double.isNaN(x) && Double.isNaN(y))) { 903 904 if (xb.equals(yb) != (x == y)) { 905 fail("Incorrect results for DoubleBuffer.equals", 906 xb, yb, x, y); 907 } 908 909 910 911 912 913 } 914 915 } 916 } 917 918 // Sub, dup 919 920 relPut(b); 921 relGet(b.duplicate()); 922 b.position(13); 923 relGet(b.duplicate(), 13); 924 relGet(b.duplicate().slice(), 13); 925 relGet(b.slice(), 13); 926 relGet(b.slice().duplicate(), 13); 927 928 // Slice 929 930 b.position(5); 931 DoubleBuffer sb = b.slice(); 932 checkSlice(b, sb); 933 b.position(0); 934 DoubleBuffer sb2 = sb.slice(); 935 checkSlice(sb, sb2); 936 937 if (!sb.equals(sb2)) 938 fail("Sliced slices do not match", sb, sb2); 939 if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) { 940 fail("Array offsets do not match: " 941 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); 942 } 943 944 int bPos = b.position(); 945 int bLim = b.limit(); 946 947 b.position(7); 948 b.limit(42); 949 DoubleBuffer rsb = b.slice(); 950 b.position(0); 951 b.limit(b.capacity()); 952 DoubleBuffer asb = b.slice(7, 35); 953 checkSlice(rsb, asb); 954 955 b.position(bPos); 956 b.limit(bLim); 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 // Read-only views 990 991 b.rewind(); 992 final DoubleBuffer rb = b.asReadOnlyBuffer(); 993 if (!b.equals(rb)) 994 fail("Buffer not equal to read-only view", b, rb); 995 show(level + 1, rb); 996 997 catchReadOnlyBuffer(b, () -> relPut(rb)); 998 catchReadOnlyBuffer(b, () -> absPut(rb)); 999 catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); 1000 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); 1001 catchReadOnlyBuffer(b, () -> absBulkPutArray(rb)); 1002 1003 // put(DoubleBuffer) should not change source position 1004 final DoubleBuffer src = DoubleBuffer.allocate(1); 1005 catchReadOnlyBuffer(b, () -> rb.put(src)); 1006 ck(src, src.position(), 0); 1007 1008 catchReadOnlyBuffer(b, () -> rb.compact()); 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 if (rb.getClass().getName().startsWith("java.nio.Heap")) { 1036 catchReadOnlyBuffer(b, () -> rb.array()); 1037 catchReadOnlyBuffer(b, () -> rb.arrayOffset()); 1038 if (rb.hasArray()) { 1039 fail("Read-only heap buffer's backing array is accessible", rb); 1040 } 1041 } 1042 1043 // Bulk puts from read-only buffers 1044 1045 b.clear(); 1046 rb.rewind(); 1047 b.put(rb); 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 relPut(b); // Required by testViews 1060 1061 1062 1063 1064 1065 1066 } 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 test(final double [] ba)1118 public static void test(final double [] ba) { 1119 int offset = 47; 1120 int length = 900; 1121 final DoubleBuffer b = DoubleBuffer.wrap(ba, offset, length); 1122 show(0, b); 1123 ck(b, b.capacity(), ba.length); 1124 ck(b, b.position(), offset); 1125 ck(b, b.limit(), offset + length); 1126 1127 // The offset must be non-negative and no larger than <array.length>. 1128 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, -1, ba.length)); 1129 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, ba.length + 1, ba.length)); 1130 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, 0, -1)); 1131 catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, 0, ba.length + 1)); 1132 1133 // A NullPointerException will be thrown if the array is null. 1134 tryCatch(ba, NullPointerException.class, 1135 () -> DoubleBuffer.wrap((double []) null, 0, 5)); 1136 tryCatch(ba, NullPointerException.class, 1137 () -> DoubleBuffer.wrap((double []) null)); 1138 } 1139 testAllocate()1140 private static void testAllocate() { 1141 // An IllegalArgumentException will be thrown for negative capacities. 1142 catchIllegalArgument((Buffer) null, () -> DoubleBuffer.allocate(-1)); 1143 try { 1144 DoubleBuffer.allocate(-1); 1145 } catch (IllegalArgumentException e) { 1146 if (e.getMessage() == null) { 1147 fail("Non-null IllegalArgumentException message expected for" 1148 + " attempt to allocate negative capacity buffer"); 1149 } 1150 } 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 } 1163 testToString()1164 public static void testToString() { 1165 final int cap = 10; 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 DoubleBuffer nondirect1 = DoubleBuffer.allocate(cap); 1177 if (!nondirect1.toString().equals(Basic.toString(nondirect1))) { 1178 fail("Heap buffer toString is incorrect: " 1179 + nondirect1.toString() + " vs " + Basic.toString(nondirect1)); 1180 } 1181 1182 } 1183 test()1184 public static void test() { 1185 testAllocate(); 1186 test(0, DoubleBuffer.allocate(7 * 1024), false); 1187 test(0, DoubleBuffer.wrap(new double[7 * 1024], 0, 7 * 1024), false); 1188 test(new double[1024]); 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 callReset(DoubleBuffer.allocate(10)); 1200 1201 1202 1203 putBuffer(); 1204 1205 1206 testToString(); 1207 1208 // Android-added: Add API coverage for get(), put(). 1209 testGetPutArrayWithIndex(); 1210 1211 testPutBuffer(); 1212 1213 } 1214 1215 // BEGIN Android-added: Add API coverage for get(), put(). testGetPutArrayWithIndex()1216 private static void testGetPutArrayWithIndex() { 1217 DoubleBuffer buf = DoubleBuffer.allocate(16); 1218 double firstElement = 11, secondElement = 12; 1219 buf.put(firstElement); 1220 buf.put(secondElement); 1221 buf.position(0); 1222 double[] arr = new double[] { 4, 3, 2, 1 }; 1223 buf.put(2, arr); 1224 double[] actual = new double[4]; 1225 buf.get(2, actual); 1226 assertEquals(actual, arr); 1227 buf.get(0, actual); 1228 assertEquals(actual, new double[] {firstElement, secondElement, 4, 3}); 1229 } 1230 testPutBuffer()1231 private static void testPutBuffer() { 1232 Supplier<DoubleBuffer>[] newBuffers = new Supplier[] { 1233 () -> DoubleBuffer.allocate(512), 1234 () -> DoubleBuffer.allocate(512).slice(100, 412), 1235 () -> ByteBuffer.allocate(512 * Double.BYTES).order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer(), 1236 () -> ByteBuffer.allocate(512 * Double.BYTES).order(ByteOrder.BIG_ENDIAN).asDoubleBuffer(), 1237 () -> ByteBuffer.allocateDirect(512 * Double.BYTES).order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer(), 1238 () -> ByteBuffer.allocateDirect(512 * Double.BYTES).order(ByteOrder.BIG_ENDIAN).asDoubleBuffer(), 1239 () -> ByteBuffer.allocateDirect(512 * Double.BYTES).asDoubleBuffer().slice(100, 412), 1240 () -> ((ByteBuffer) ByteBuffer.allocateDirect(512 * Double.BYTES) 1241 .order(ByteOrder.LITTLE_ENDIAN).position(100)).asDoubleBuffer(), 1242 () -> ((ByteBuffer) ByteBuffer.allocateDirect(512 * Double.BYTES) 1243 .order(ByteOrder.BIG_ENDIAN).position(100)).asDoubleBuffer(), 1244 }; 1245 1246 double[] samples = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 1247 for (var newSrc : newBuffers) { 1248 for (var newDst : newBuffers) { 1249 double[] out = new double[10]; 1250 DoubleBuffer src = newSrc.get(); 1251 src.put(samples); 1252 src.get(0, out); 1253 assertEquals(out, samples); 1254 assertEquals(src.get(10), (double) 0); 1255 src.limit(10); 1256 src.rewind(); 1257 1258 out = new double[10]; 1259 DoubleBuffer dst = newDst.get(); 1260 dst.put(src); 1261 dst.get(0, out); 1262 assertEquals(out, samples); 1263 dst.rewind(); 1264 1265 dst.put(6, src, 1, 2); 1266 assertEquals(dst.get(6), (double) 1); 1267 assertEquals(dst.get(7), (double) 2); 1268 assertEquals(dst.get(8), (double) 8); 1269 assertEquals(dst.get(10), (double) 0); 1270 1271 dst.put(12, src, 2, 2); 1272 out = new double[5]; 1273 dst.get(10, out); 1274 assertEquals(out, new double[] {0, 0, 2, 3, 0}); 1275 1276 } 1277 } 1278 } 1279 1280 // END Android-added: Add API coverage for get(), put(). 1281 } 1282