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 BasicLong 37 extends Basic 38 { 39 40 private static final long[] VALUES = { 41 Long.MIN_VALUE, 42 (long) -1, 43 (long) 0, 44 (long) 1, 45 Long.MAX_VALUE, 46 47 48 49 50 51 52 53 54 55 56 57 58 }; 59 60 private static void relGet(LongBuffer b) { 61 int n = b.capacity(); 62 for (int i = 0; i < n; i++) 63 ck(b, (long)b.get(), (long)((long)ic(i))); 64 b.rewind(); 65 } 66 67 private static void relGet(LongBuffer b, int start) { 68 int n = b.remaining(); 69 for (int i = start; i < n; i++) 70 ck(b, (long)b.get(), (long)((long)ic(i))); 71 b.rewind(); 72 } 73 74 private static void absGet(LongBuffer b) { 75 int n = b.capacity(); 76 for (int i = 0; i < n; i++) 77 ck(b, (long)b.get(), (long)((long)ic(i))); 78 b.rewind(); 79 } 80 81 private static void bulkGet(LongBuffer b) { 82 int n = b.capacity(); 83 long[] a = new long[n + 7]; 84 b.get(a, 7, n); 85 for (int i = 0; i < n; i++) { 86 ck(b, (long)a[i + 7], (long)((long)ic(i))); 87 } 88 } 89 90 private static void absBulkGet(LongBuffer b) { 91 int n = b.capacity(); 92 int len = n - 7*2; 93 long[] a = new long[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)((long)ic(i))); 99 } 100 } 101 102 private static void relPut(LongBuffer b) { 103 int n = b.capacity(); 104 b.clear(); 105 for (int i = 0; i < n; i++) 106 b.put((long)ic(i)); 107 b.flip(); 108 } 109 110 private static void absPut(LongBuffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 for (int i = 0; i < n; i++) 114 b.put(i, (long)ic(i)); 115 b.limit(n); 116 b.position(0); 117 } 118 119 private static void bulkPutArray(LongBuffer b) { 120 int n = b.capacity(); 121 b.clear(); 122 long[] a = new long[n + 7]; 123 for (int i = 0; i < n; i++) 124 a[i + 7] = (long)ic(i); 125 b.put(a, 7, n); 126 b.flip(); 127 } 128 129 private static void bulkPutBuffer(LongBuffer b) { 130 int n = b.capacity(); 131 b.clear(); 132 LongBuffer c = LongBuffer.allocate(n + 7); 133 c.position(7); 134 for (int i = 0; i < n; i++) 135 c.put((long)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(LongBuffer b) { 152 int n = b.capacity(); 153 b.clear(); 154 int lim = n - 7; 155 int len = lim - 7; 156 b.limit(lim); 157 long[] a = new long[len + 7]; 158 for (int i = 0; i < len; i++) 159 a[i + 7] = (long)ic(i); 160 b.position(42); 161 b.put(7, a, 7, len); 162 ck(b, b.position() == 42); 163 } 164 165 private static void absBulkPutBuffer(LongBuffer b, boolean direct) { 166 int n = b.capacity(); 167 b.clear(); 168 int lim = n - 7; 169 int len = lim - 7; 170 b.limit(lim); 171 LongBuffer c = direct ? 172 173 174 175 ByteBuffer.allocateDirect(Long.BYTES*(len + 7)) 176 .asLongBuffer() 177 178 : LongBuffer.allocate(len + 7); 179 180 if (direct) 181 System.out.println("Direct buffer: " + c.getClass().getName()); 182 183 for (int i = 0; i < len; i++) 184 c.put(i + 7, (long)ic(i)); 185 b.position(42); 186 c.position(42); 187 b.put(7, c, 7, len); 188 ck(b, b.position() == 42); 189 ck(c, c.position() == 42); 190 } 191 192 //6231529 193 private static void callReset(LongBuffer b) { 194 b.position(0); 195 b.mark(); 196 197 b.duplicate().reset(); 198 b.asReadOnlyBuffer().reset(); 199 } 200 201 202 203 // 6221101-6234263 204 205 private static void putBuffer() { 206 final int cap = 10; 207 208 LongBuffer direct1 = ByteBuffer.allocateDirect(cap).asLongBuffer(); 209 LongBuffer nondirect1 = ByteBuffer.allocate(cap).asLongBuffer(); 210 direct1.put(nondirect1); 211 212 LongBuffer direct2 = ByteBuffer.allocateDirect(cap).asLongBuffer(); 213 LongBuffer nondirect2 = ByteBuffer.allocate(cap).asLongBuffer(); 214 nondirect2.put(direct2); 215 216 LongBuffer direct3 = ByteBuffer.allocateDirect(cap).asLongBuffer(); 217 LongBuffer direct4 = ByteBuffer.allocateDirect(cap).asLongBuffer(); 218 direct3.put(direct4); 219 220 LongBuffer nondirect3 = ByteBuffer.allocate(cap).asLongBuffer(); 221 LongBuffer nondirect4 = ByteBuffer.allocate(cap).asLongBuffer(); 222 nondirect3.put(nondirect4); 223 } 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 private static void checkSlice(LongBuffer b, LongBuffer slice) { 242 ck(slice, 0, slice.position()); 243 ck(slice, b.remaining(), slice.limit()); 244 ck(slice, b.remaining(), slice.capacity()); 245 if (b.isDirect() != slice.isDirect()) 246 fail("Lost direction", slice); 247 if (b.isReadOnly() != slice.isReadOnly()) 248 fail("Lost read-only", slice); 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 private static void fail(String problem, 503 LongBuffer xb, LongBuffer yb, 504 long x, long y) { 505 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); 506 } 507 508 private static void catchNullArgument(Buffer b, Runnable thunk) { 509 tryCatch(b, NullPointerException.class, thunk); 510 } 511 512 private static void catchIllegalArgument(Buffer b, Runnable thunk) { 513 tryCatch(b, IllegalArgumentException.class, thunk); 514 } 515 516 private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) { 517 tryCatch(b, ReadOnlyBufferException.class, thunk); 518 } 519 520 private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) { 521 tryCatch(b, IndexOutOfBoundsException.class, thunk); 522 } 523 524 private static void catchIndexOutOfBounds(long[] t, Runnable thunk) { 525 tryCatch(t, IndexOutOfBoundsException.class, thunk); 526 } 527 528 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { 529 boolean caught = false; 530 try { 531 thunk.run(); 532 } catch (Throwable x) { 533 if (ex.isAssignableFrom(x.getClass())) { 534 caught = true; 535 } else { 536 String s = x.getMessage(); 537 if (s == null) 538 s = x.getClass().getName(); 539 fail(s + " not expected"); 540 } 541 } 542 if (!caught) { 543 fail(ex.getName() + " not thrown", b); 544 } 545 } 546 547 private static void tryCatch(long[] t, Class<?> ex, Runnable thunk) { 548 tryCatch(LongBuffer.wrap(t), ex, thunk); 549 } 550 551 public static void test(int level, final LongBuffer b, boolean direct) { 552 553 show(level, b); 554 555 if (direct != b.isDirect()) 556 fail("Wrong direction", b); 557 558 // Gets and puts 559 560 relPut(b); 561 relGet(b); 562 absGet(b); 563 bulkGet(b); 564 565 absPut(b); 566 relGet(b); 567 absGet(b); 568 bulkGet(b); 569 570 bulkPutArray(b); 571 relGet(b); 572 573 bulkPutBuffer(b); 574 relGet(b); 575 576 absBulkPutArray(b); 577 absBulkGet(b); 578 579 absBulkPutBuffer(b, direct); 580 absBulkGet(b); 581 582 absBulkPutBuffer(b, !direct); 583 absBulkGet(b); 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 // Compact 622 623 relPut(b); 624 b.position(13); 625 b.compact(); 626 b.flip(); 627 relGet(b, 13); 628 629 // Exceptions 630 631 relPut(b); 632 b.limit(b.capacity() / 2); 633 b.position(b.limit()); 634 635 tryCatch(b, BufferUnderflowException.class, () -> b.get()); 636 tryCatch(b, BufferOverflowException.class, () -> b.put((long)42)); 637 // The index must be non-negative and less than the buffer's limit. 638 catchIndexOutOfBounds(b, () -> b.get(b.limit())); 639 catchIndexOutOfBounds(b, () -> b.get(-1)); 640 catchIndexOutOfBounds(b, () -> b.put(b.limit(), (long)42)); 641 tryCatch(b, InvalidMarkException.class, 642 () -> b.position(0).mark().compact().reset()); 643 644 try { 645 b.position(b.limit() + 1); 646 fail("IllegalArgumentException expected for position beyond limit"); 647 } catch (IllegalArgumentException e) { 648 if (e.getMessage() == null) { 649 fail("Non-null IllegalArgumentException message expected for" 650 + " position beyond limit"); 651 } 652 } 653 654 try { 655 b.position(-1); 656 fail("IllegalArgumentException expected for negative position"); 657 } catch (IllegalArgumentException e) { 658 if (e.getMessage() == null) { 659 fail("Non-null IllegalArgumentException message expected for" 660 + " negative position"); 661 } 662 } 663 664 try { 665 b.limit(b.capacity() + 1); 666 fail("IllegalArgumentException expected for limit beyond capacity"); 667 } catch (IllegalArgumentException e) { 668 if (e.getMessage() == null) { 669 fail("Non-null IllegalArgumentException message expected for" 670 + " limit beyond capacity"); 671 } 672 } 673 674 try { 675 b.limit(-1); 676 fail("IllegalArgumentException expected for negative limit"); 677 } catch (IllegalArgumentException e) { 678 if (e.getMessage() == null) { 679 fail("Non-null IllegalArgumentException message expected for" 680 + " negative limit"); 681 } 682 } 683 684 // Exceptions in absolute bulk operations 685 686 catchNullArgument(b, () -> b.get(7, null, 0, 42)); 687 catchNullArgument(b, () -> b.put(7, (long[])null, 0, 42)); 688 catchNullArgument(b, () -> b.put(7, (LongBuffer)null, 0, 42)); 689 690 long[] tmpa = new long[42]; 691 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42)); 692 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1)); 693 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1)); 694 catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1)); 695 catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1)); 696 catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42)); 697 698 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42)); 699 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1)); 700 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1)); 701 catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1)); 702 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1)); 703 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42)); 704 705 LongBuffer tmpb = LongBuffer.allocate(42); 706 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, -1, 42)); 707 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, 1)); 708 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, -1)); 709 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 41, 2)); 710 catchIndexOutOfBounds(b, () -> b.put(-1, tmpb, 0, 1)); 711 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpb, 0, 1)); 712 catchIndexOutOfBounds(b, () -> b.put(0, tmpb, 0, -1)); 713 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpb, 0, 42)); 714 715 // Values 716 717 b.clear(); 718 b.put((long)0); 719 b.put((long)-1); 720 b.put((long)1); 721 b.put(Long.MAX_VALUE); 722 b.put(Long.MIN_VALUE); 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 b.flip(); 741 ck(b, b.get(), 0); 742 ck(b, b.get(), (long)-1); 743 ck(b, b.get(), 1); 744 ck(b, b.get(), Long.MAX_VALUE); 745 ck(b, b.get(), Long.MIN_VALUE); 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 // Comparison 774 b.rewind(); 775 LongBuffer b2 = LongBuffer.allocate(b.capacity()); 776 b2.put(b); 777 b2.flip(); 778 b.position(2); 779 b2.position(2); 780 if (!b.equals(b2)) { 781 for (int i = 2; i < b.limit(); i++) { 782 long x = b.get(i); 783 long y = b2.get(i); 784 if (x != y 785 786 787 788 789 790 791 ) { 792 out.println("[" + i + "] " + x + " != " + y); 793 } 794 } 795 fail("Identical buffers not equal", b, b2); 796 } 797 if (b.compareTo(b2) != 0) { 798 fail("Comparison to identical buffer != 0", b, b2); 799 } 800 b.limit(b.limit() + 1); 801 b.position(b.limit() - 1); 802 b.put((long)99); 803 b.rewind(); 804 b2.rewind(); 805 if (b.equals(b2)) 806 fail("Non-identical buffers equal", b, b2); 807 if (b.compareTo(b2) <= 0) 808 fail("Comparison to shorter buffer <= 0", b, b2); 809 b.limit(b.limit() - 1); 810 811 b.put(2, (long)42); 812 if (b.equals(b2)) 813 fail("Non-identical buffers equal", b, b2); 814 if (b.compareTo(b2) <= 0) 815 fail("Comparison to lesser buffer <= 0", b, b2); 816 817 // Check equals and compareTo with interesting values 818 for (long x : VALUES) { 819 LongBuffer xb = LongBuffer.wrap(new long[] { x }); 820 if (xb.compareTo(xb) != 0) { 821 fail("compareTo not reflexive", xb, xb, x, x); 822 } 823 if (!xb.equals(xb)) { 824 fail("equals not reflexive", xb, xb, x, x); 825 } 826 for (long y : VALUES) { 827 LongBuffer yb = LongBuffer.wrap(new long[] { y }); 828 if (xb.compareTo(yb) != - yb.compareTo(xb)) { 829 fail("compareTo not anti-symmetric", 830 xb, yb, x, y); 831 } 832 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { 833 fail("compareTo inconsistent with equals", 834 xb, yb, x, y); 835 } 836 if (xb.compareTo(yb) != Long.compare(x, y)) { 837 838 839 840 841 842 843 fail("Incorrect results for LongBuffer.compareTo", 844 xb, yb, x, y); 845 } 846 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 847 fail("Incorrect results for LongBuffer.equals", 848 xb, yb, x, y); 849 } 850 } 851 } 852 853 // Sub, dup 854 855 relPut(b); 856 relGet(b.duplicate()); 857 b.position(13); 858 relGet(b.duplicate(), 13); 859 relGet(b.duplicate().slice(), 13); 860 relGet(b.slice(), 13); 861 relGet(b.slice().duplicate(), 13); 862 863 // Slice 864 865 b.position(5); 866 LongBuffer sb = b.slice(); 867 checkSlice(b, sb); 868 b.position(0); 869 LongBuffer sb2 = sb.slice(); 870 checkSlice(sb, sb2); 871 872 if (!sb.equals(sb2)) 873 fail("Sliced slices do not match", sb, sb2); 874 if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) { 875 fail("Array offsets do not match: " 876 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); 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 // Read-only views 911 912 b.rewind(); 913 final LongBuffer rb = b.asReadOnlyBuffer(); 914 if (!b.equals(rb)) 915 fail("Buffer not equal to read-only view", b, rb); 916 show(level + 1, rb); 917 918 catchReadOnlyBuffer(b, () -> relPut(rb)); 919 catchReadOnlyBuffer(b, () -> absPut(rb)); 920 catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); 921 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); 922 catchReadOnlyBuffer(b, () -> absBulkPutArray(rb)); 923 catchReadOnlyBuffer(b, () -> absBulkPutBuffer(rb, direct)); 924 925 // put(LongBuffer) should not change source position 926 final LongBuffer src = LongBuffer.allocate(1); 927 catchReadOnlyBuffer(b, () -> rb.put(src)); 928 ck(src, src.position(), 0); 929 930 catchReadOnlyBuffer(b, () -> rb.compact()); 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 if (rb.getClass().getName().startsWith("java.nio.Heap")) { 958 catchReadOnlyBuffer(b, () -> rb.array()); 959 catchReadOnlyBuffer(b, () -> rb.arrayOffset()); 960 if (rb.hasArray()) { 961 fail("Read-only heap buffer's backing array is accessible", rb); 962 } 963 } 964 965 // Bulk puts from read-only buffers 966 967 b.clear(); 968 rb.rewind(); 969 b.put(rb); 970 971 972 973 974 975 976 977 978 979 980 981 relPut(b); // Required by testViews 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 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 public static void test(final long [] ba) { 1034 int offset = 47; 1035 int length = 900; 1036 final LongBuffer b = LongBuffer.wrap(ba, offset, length); 1037 show(0, b); 1038 ck(b, b.capacity(), ba.length); 1039 ck(b, b.position(), offset); 1040 ck(b, b.limit(), offset + length); 1041 1042 // The offset must be non-negative and no larger than <array.length>. 1043 catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, -1, ba.length)); 1044 catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, ba.length + 1, ba.length)); 1045 catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, 0, -1)); 1046 catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, 0, ba.length + 1)); 1047 1048 // A NullPointerException will be thrown if the array is null. 1049 tryCatch(ba, NullPointerException.class, 1050 () -> LongBuffer.wrap((long []) null, 0, 5)); 1051 tryCatch(ba, NullPointerException.class, 1052 () -> LongBuffer.wrap((long []) null)); 1053 } 1054 1055 private static void testAllocate() { 1056 // An IllegalArgumentException will be thrown for negative capacities. 1057 catchIllegalArgument((Buffer) null, () -> LongBuffer.allocate(-1)); 1058 try { 1059 LongBuffer.allocate(-1); 1060 } catch (IllegalArgumentException e) { 1061 if (e.getMessage() == null) { 1062 fail("Non-null IllegalArgumentException message expected for" 1063 + " attempt to allocate negative capacity buffer"); 1064 } 1065 } 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 } 1078 1079 public static void test() { 1080 testAllocate(); 1081 test(0, LongBuffer.allocate(7 * 1024), false); 1082 test(0, LongBuffer.wrap(new long[7 * 1024], 0, 7 * 1024), false); 1083 test(new long[1024]); 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 callReset(LongBuffer.allocate(10)); 1095 1096 1097 1098 putBuffer(); 1099 1100 } 1101 1102 }