1 /* 2 * Copyright (c) 2000, 2016, 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 BasicInt 38 extends Basic 39 { 40 41 private static final int[] VALUES = { 42 Integer.MIN_VALUE, 43 (int) -1, 44 (int) 0, 45 (int) 1, 46 Integer.MAX_VALUE, 47 48 49 50 51 52 53 54 55 56 57 58 59 }; 60 61 private static void relGet(IntBuffer b) { 62 int n = b.capacity(); 63 int v; 64 for (int i = 0; i < n; i++) 65 ck(b, (long)b.get(), (long)((int)ic(i))); 66 b.rewind(); 67 } 68 69 private static void relGet(IntBuffer b, int start) { 70 int n = b.remaining(); 71 int v; 72 for (int i = start; i < n; i++) 73 ck(b, (long)b.get(), (long)((int)ic(i))); 74 b.rewind(); 75 } 76 77 private static void absGet(IntBuffer b) { 78 int n = b.capacity(); 79 int v; 80 for (int i = 0; i < n; i++) 81 ck(b, (long)b.get(), (long)((int)ic(i))); 82 b.rewind(); 83 } 84 85 private static void bulkGet(IntBuffer b) { 86 int n = b.capacity(); 87 int[] a = new int[n + 7]; 88 b.get(a, 7, n); 89 for (int i = 0; i < n; i++) 90 ck(b, (long)a[i + 7], (long)((int)ic(i))); 91 } 92 93 private static void relPut(IntBuffer b) { 94 int n = b.capacity(); 95 b.clear(); 96 for (int i = 0; i < n; i++) 97 b.put((int)ic(i)); 98 b.flip(); 99 } 100 101 private static void absPut(IntBuffer b) { 102 int n = b.capacity(); 103 b.clear(); 104 for (int i = 0; i < n; i++) 105 b.put(i, (int)ic(i)); 106 b.limit(n); 107 b.position(0); 108 } 109 110 private static void bulkPutArray(IntBuffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 int[] a = new int[n + 7]; 114 for (int i = 0; i < n; i++) 115 a[i + 7] = (int)ic(i); 116 b.put(a, 7, n); 117 b.flip(); 118 } 119 120 private static void bulkPutBuffer(IntBuffer b) { 121 int n = b.capacity(); 122 b.clear(); 123 IntBuffer c = IntBuffer.allocate(n + 7); 124 c.position(7); 125 for (int i = 0; i < n; i++) 126 c.put((int)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 put into same buffer"); 134 } catch (IllegalArgumentException e) { 135 if (e.getMessage() == null) { 136 fail("Non-null IllegalArgumentException message expected from" 137 + " put into same buffer"); 138 } 139 } 140 } 141 142 //6231529 143 private static void callReset(IntBuffer b) { 144 b.position(0); 145 b.mark(); 146 147 b.duplicate().reset(); 148 b.asReadOnlyBuffer().reset(); 149 } 150 151 152 153 // 6221101-6234263 154 155 private static void putBuffer() { 156 final int cap = 10; 157 158 IntBuffer direct1 = ByteBuffer.allocateDirect(cap).asIntBuffer(); 159 IntBuffer nondirect1 = ByteBuffer.allocate(cap).asIntBuffer(); 160 direct1.put(nondirect1); 161 162 IntBuffer direct2 = ByteBuffer.allocateDirect(cap).asIntBuffer(); 163 IntBuffer nondirect2 = ByteBuffer.allocate(cap).asIntBuffer(); 164 nondirect2.put(direct2); 165 166 IntBuffer direct3 = ByteBuffer.allocateDirect(cap).asIntBuffer(); 167 IntBuffer direct4 = ByteBuffer.allocateDirect(cap).asIntBuffer(); 168 direct3.put(direct4); 169 170 IntBuffer nondirect3 = ByteBuffer.allocate(cap).asIntBuffer(); 171 IntBuffer nondirect4 = ByteBuffer.allocate(cap).asIntBuffer(); 172 nondirect3.put(nondirect4); 173 } 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 private static void checkSlice(IntBuffer b, IntBuffer slice) { 192 ck(slice, 0, slice.position()); 193 ck(slice, b.remaining(), slice.limit()); 194 ck(slice, b.remaining(), slice.capacity()); 195 if (b.isDirect() != slice.isDirect()) 196 fail("Lost direction", slice); 197 if (b.isReadOnly() != slice.isReadOnly()) 198 fail("Lost read-only", slice); 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 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 private static void fail(String problem, 439 IntBuffer xb, IntBuffer yb, 440 int x, int y) { 441 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); 442 } 443 444 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { 445 boolean caught = false; 446 try { 447 thunk.run(); 448 } catch (Throwable x) { 449 if (ex.isAssignableFrom(x.getClass())) { 450 caught = true; 451 } else { 452 fail(x.getMessage() + " not expected"); 453 } 454 } 455 if (!caught) 456 fail(ex.getName() + " not thrown", b); 457 } 458 459 private static void tryCatch(int [] t, Class<?> ex, Runnable thunk) { 460 tryCatch(IntBuffer.wrap(t), ex, thunk); 461 } 462 463 public static void test(int level, final IntBuffer b, boolean direct) { 464 465 show(level, b); 466 467 if (direct != b.isDirect()) 468 fail("Wrong direction", b); 469 470 // Gets and puts 471 472 relPut(b); 473 relGet(b); 474 absGet(b); 475 bulkGet(b); 476 477 absPut(b); 478 relGet(b); 479 absGet(b); 480 bulkGet(b); 481 482 bulkPutArray(b); 483 relGet(b); 484 485 bulkPutBuffer(b); 486 relGet(b); 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 // Compact 527 528 relPut(b); 529 b.position(13); 530 b.compact(); 531 b.flip(); 532 relGet(b, 13); 533 534 // Exceptions 535 536 relPut(b); 537 b.limit(b.capacity() / 2); 538 b.position(b.limit()); 539 540 tryCatch(b, BufferUnderflowException.class, new Runnable() { 541 public void run() { 542 b.get(); 543 }}); 544 545 tryCatch(b, BufferOverflowException.class, new Runnable() { 546 public void run() { 547 b.put((int)42); 548 }}); 549 550 // The index must be non-negative and lesss than the buffer's limit. 551 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 552 public void run() { 553 b.get(b.limit()); 554 }}); 555 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 556 public void run() { 557 b.get(-1); 558 }}); 559 560 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 561 public void run() { 562 b.put(b.limit(), (int)42); 563 }}); 564 565 tryCatch(b, InvalidMarkException.class, new Runnable() { 566 public void run() { 567 b.position(0); 568 b.mark(); 569 b.compact(); 570 b.reset(); 571 }}); 572 573 try { 574 b.position(b.limit() + 1); 575 fail("IllegalArgumentException expected for position beyond limit"); 576 } catch (IllegalArgumentException e) { 577 if (e.getMessage() == null) { 578 fail("Non-null IllegalArgumentException message expected for" 579 + " position beyond limit"); 580 } 581 } 582 583 try { 584 b.position(-1); 585 fail("IllegalArgumentException expected for negative position"); 586 } catch (IllegalArgumentException e) { 587 if (e.getMessage() == null) { 588 fail("Non-null IllegalArgumentException message expected for" 589 + " negative position"); 590 } 591 } 592 593 try { 594 b.limit(b.capacity() + 1); 595 fail("IllegalArgumentException expected for limit beyond capacity"); 596 } catch (IllegalArgumentException e) { 597 if (e.getMessage() == null) { 598 fail("Non-null IllegalArgumentException message expected for" 599 + " limit beyond capacity"); 600 } 601 } 602 603 try { 604 b.limit(-1); 605 fail("IllegalArgumentException expected for negative limit"); 606 } catch (IllegalArgumentException e) { 607 if (e.getMessage() == null) { 608 fail("Non-null IllegalArgumentException message expected for" 609 + " negative limit"); 610 } 611 } 612 613 // Values 614 615 b.clear(); 616 b.put((int)0); 617 b.put((int)-1); 618 b.put((int)1); 619 b.put(Integer.MAX_VALUE); 620 b.put(Integer.MIN_VALUE); 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 int v; 639 b.flip(); 640 ck(b, b.get(), 0); 641 ck(b, b.get(), (int)-1); 642 ck(b, b.get(), 1); 643 ck(b, b.get(), Integer.MAX_VALUE); 644 ck(b, b.get(), Integer.MIN_VALUE); 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 // Comparison 669 b.rewind(); 670 IntBuffer b2 = IntBuffer.allocate(b.capacity()); 671 b2.put(b); 672 b2.flip(); 673 b.position(2); 674 b2.position(2); 675 if (!b.equals(b2)) { 676 for (int i = 2; i < b.limit(); i++) { 677 int x = b.get(i); 678 int y = b2.get(i); 679 if (x != y 680 681 682 683 684 685 686 ) 687 out.println("[" + i + "] " + x + " != " + y); 688 } 689 fail("Identical buffers not equal", b, b2); 690 } 691 if (b.compareTo(b2) != 0) 692 fail("Comparison to identical buffer != 0", b, b2); 693 694 b.limit(b.limit() + 1); 695 b.position(b.limit() - 1); 696 b.put((int)99); 697 b.rewind(); 698 b2.rewind(); 699 if (b.equals(b2)) 700 fail("Non-identical buffers equal", b, b2); 701 if (b.compareTo(b2) <= 0) 702 fail("Comparison to shorter buffer <= 0", b, b2); 703 b.limit(b.limit() - 1); 704 705 b.put(2, (int)42); 706 if (b.equals(b2)) 707 fail("Non-identical buffers equal", b, b2); 708 if (b.compareTo(b2) <= 0) 709 fail("Comparison to lesser buffer <= 0", b, b2); 710 711 // Check equals and compareTo with interesting values 712 for (int x : VALUES) { 713 IntBuffer xb = IntBuffer.wrap(new int[] { x }); 714 if (xb.compareTo(xb) != 0) { 715 fail("compareTo not reflexive", xb, xb, x, x); 716 } 717 if (! xb.equals(xb)) { 718 fail("equals not reflexive", xb, xb, x, x); 719 } 720 for (int y : VALUES) { 721 IntBuffer yb = IntBuffer.wrap(new int[] { y }); 722 if (xb.compareTo(yb) != - yb.compareTo(xb)) { 723 fail("compareTo not anti-symmetric", 724 xb, yb, x, y); 725 } 726 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { 727 fail("compareTo inconsistent with equals", 728 xb, yb, x, y); 729 } 730 if (xb.compareTo(yb) != Integer.compare(x, y)) { 731 732 733 734 735 736 737 fail("Incorrect results for IntBuffer.compareTo", 738 xb, yb, x, y); 739 } 740 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 741 fail("Incorrect results for IntBuffer.equals", 742 xb, yb, x, y); 743 } 744 } 745 } 746 747 // Sub, dup 748 749 relPut(b); 750 relGet(b.duplicate()); 751 b.position(13); 752 relGet(b.duplicate(), 13); 753 relGet(b.duplicate().slice(), 13); 754 relGet(b.slice(), 13); 755 relGet(b.slice().duplicate(), 13); 756 757 // Slice 758 759 b.position(5); 760 IntBuffer sb = b.slice(); 761 checkSlice(b, sb); 762 b.position(0); 763 IntBuffer sb2 = sb.slice(); 764 checkSlice(sb, sb2); 765 766 if (!sb.equals(sb2)) 767 fail("Sliced slices do not match", sb, sb2); 768 if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) 769 fail("Array offsets do not match: " 770 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); 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 // Read-only views 804 805 b.rewind(); 806 final IntBuffer rb = b.asReadOnlyBuffer(); 807 if (!b.equals(rb)) 808 fail("Buffer not equal to read-only view", b, rb); 809 show(level + 1, rb); 810 811 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 812 public void run() { 813 relPut(rb); 814 }}); 815 816 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 817 public void run() { 818 absPut(rb); 819 }}); 820 821 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 822 public void run() { 823 bulkPutArray(rb); 824 }}); 825 826 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 827 public void run() { 828 bulkPutBuffer(rb); 829 }}); 830 831 // put(IntBuffer) should not change source position 832 final IntBuffer src = IntBuffer.allocate(1); 833 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 834 public void run() { 835 rb.put(src); 836 }}); 837 ck(src, src.position(), 0); 838 839 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 840 public void run() { 841 rb.compact(); 842 }}); 843 844 845 846 847 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 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 if (rb.getClass().getName().startsWith("java.nio.Heap")) { 919 920 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 921 public void run() { 922 rb.array(); 923 }}); 924 925 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 926 public void run() { 927 rb.arrayOffset(); 928 }}); 929 930 if (rb.hasArray()) 931 fail("Read-only heap buffer's backing array is accessible", 932 rb); 933 934 } 935 936 // Bulk puts from read-only buffers 937 938 b.clear(); 939 rb.rewind(); 940 b.put(rb); 941 942 943 944 945 946 947 948 949 950 951 952 relPut(b); // Required by testViews 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 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 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 public static void test(final int [] ba) { 1046 int offset = 47; 1047 int length = 900; 1048 final IntBuffer b = IntBuffer.wrap(ba, offset, length); 1049 show(0, b); 1050 ck(b, b.capacity(), ba.length); 1051 ck(b, b.position(), offset); 1052 ck(b, b.limit(), offset + length); 1053 1054 // The offset must be non-negative and no larger than <array.length>. 1055 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1056 public void run() { 1057 IntBuffer.wrap(ba, -1, ba.length); 1058 }}); 1059 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1060 public void run() { 1061 IntBuffer.wrap(ba, ba.length + 1, ba.length); 1062 }}); 1063 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1064 public void run() { 1065 IntBuffer.wrap(ba, 0, -1); 1066 }}); 1067 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1068 public void run() { 1069 IntBuffer.wrap(ba, 0, ba.length + 1); 1070 }}); 1071 1072 // A NullPointerException will be thrown if the array is null. 1073 tryCatch(ba, NullPointerException.class, new Runnable() { 1074 public void run() { 1075 IntBuffer.wrap((int []) null, 0, 5); 1076 }}); 1077 tryCatch(ba, NullPointerException.class, new Runnable() { 1078 public void run() { 1079 IntBuffer.wrap((int []) null); 1080 }}); 1081 } 1082 1083 private static void testAllocate() { 1084 // An IllegalArgumentException will be thrown for negative capacities. 1085 tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() { 1086 public void run() { 1087 IntBuffer.allocate(-1); 1088 }}); 1089 try { 1090 IntBuffer.allocate(-1); 1091 } catch (IllegalArgumentException e) { 1092 if (e.getMessage() == null) { 1093 fail("Non-null IllegalArgumentException message expected for" 1094 + " attempt to allocate negative capacity buffer"); 1095 } 1096 } 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 } 1112 1113 public static void test() { 1114 testAllocate(); 1115 test(0, IntBuffer.allocate(7 * 1024), false); 1116 test(0, IntBuffer.wrap(new int[7 * 1024], 0, 7 * 1024), false); 1117 test(new int[1024]); 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 callReset(IntBuffer.allocate(10)); 1129 1130 1131 1132 putBuffer(); 1133 1134 } 1135 1136 }