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 BasicByte 37 extends Basic 38 { 39 40 private static final byte[] VALUES = { 41 Byte.MIN_VALUE, 42 (byte) -1, 43 (byte) 0, 44 (byte) 1, 45 Byte.MAX_VALUE, 46 47 48 49 50 51 52 53 54 55 56 57 58 }; 59 60 private static void relGet(ByteBuffer b) { 61 int n = b.capacity(); 62 for (int i = 0; i < n; i++) 63 ck(b, (long)b.get(), (long)((byte)ic(i))); 64 b.rewind(); 65 } 66 67 private static void relGet(ByteBuffer b, int start) { 68 int n = b.remaining(); 69 for (int i = start; i < n; i++) 70 ck(b, (long)b.get(), (long)((byte)ic(i))); 71 b.rewind(); 72 } 73 74 private static void absGet(ByteBuffer b) { 75 int n = b.capacity(); 76 for (int i = 0; i < n; i++) 77 ck(b, (long)b.get(), (long)((byte)ic(i))); 78 b.rewind(); 79 } 80 81 private static void bulkGet(ByteBuffer b) { 82 int n = b.capacity(); 83 byte[] a = new byte[n + 7]; 84 b.get(a, 7, n); 85 for (int i = 0; i < n; i++) { 86 ck(b, (long)a[i + 7], (long)((byte)ic(i))); 87 } 88 } 89 90 private static void absBulkGet(ByteBuffer b) { 91 int n = b.capacity(); 92 int len = n - 7*2; 93 byte[] a = new byte[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)((byte)ic(i))); 99 } 100 } 101 102 private static void relPut(ByteBuffer b) { 103 int n = b.capacity(); 104 b.clear(); 105 for (int i = 0; i < n; i++) 106 b.put((byte)ic(i)); 107 b.flip(); 108 } 109 110 private static void absPut(ByteBuffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 for (int i = 0; i < n; i++) 114 b.put(i, (byte)ic(i)); 115 b.limit(n); 116 b.position(0); 117 } 118 119 private static void bulkPutArray(ByteBuffer b) { 120 int n = b.capacity(); 121 b.clear(); 122 byte[] a = new byte[n + 7]; 123 for (int i = 0; i < n; i++) 124 a[i + 7] = (byte)ic(i); 125 b.put(a, 7, n); 126 b.flip(); 127 } 128 129 private static void bulkPutBuffer(ByteBuffer b) { 130 int n = b.capacity(); 131 b.clear(); 132 ByteBuffer c = ByteBuffer.allocate(n + 7); 133 c.position(7); 134 for (int i = 0; i < n; i++) 135 c.put((byte)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(ByteBuffer b) { 152 int n = b.capacity(); 153 b.clear(); 154 int lim = n - 7; 155 int len = lim - 7; 156 b.limit(lim); 157 byte[] a = new byte[len + 7]; 158 for (int i = 0; i < len; i++) 159 a[i + 7] = (byte)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(ByteBuffer 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 ByteBuffer c = direct ? 172 173 ByteBuffer.allocateDirect(len + 7) 174 175 176 177 178 : ByteBuffer.allocate(len + 7); 179 180 181 182 183 for (int i = 0; i < len; i++) 184 c.put(i + 7, (byte)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(ByteBuffer b) { 194 b.position(0); 195 b.mark(); 196 197 b.duplicate().reset(); 198 b.asReadOnlyBuffer().reset(); 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 private static void checkSlice(ByteBuffer b, ByteBuffer 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 private static void checkBytes(ByteBuffer b, byte[] bs) { 254 int n = bs.length; 255 int p = b.position(); 256 if (b.order() == ByteOrder.BIG_ENDIAN) { 257 for (int i = 0; i < n; i++) { 258 ck(b, b.get(), bs[i]); 259 } 260 } else { 261 for (int i = n - 1; i >= 0; i--) { 262 ck(b, b.get(), bs[i]); 263 } 264 } 265 b.position(p); 266 } 267 268 private static void compact(Buffer b) { 269 try { 270 Class<?> cl = b.getClass(); 271 java.lang.reflect.Method m = cl.getDeclaredMethod("compact"); 272 m.setAccessible(true); 273 m.invoke(b); 274 } catch (Exception e) { 275 fail(e.getMessage(), b); 276 } 277 } 278 279 private static void checkInvalidMarkException(final Buffer b) { 280 tryCatch(b, InvalidMarkException.class, () -> { 281 b.mark(); 282 compact(b); 283 b.reset(); 284 }); 285 } 286 287 private static void testViews(int level, ByteBuffer b, boolean direct) { 288 289 ShortBuffer sb = b.asShortBuffer(); 290 BasicShort.test(level, sb, direct); 291 checkBytes(b, new byte[] { 0, (byte)ic(0) }); 292 checkInvalidMarkException(sb); 293 294 CharBuffer cb = b.asCharBuffer(); 295 BasicChar.test(level, cb, direct); 296 checkBytes(b, new byte[] { 0, (byte)ic(0) }); 297 checkInvalidMarkException(cb); 298 299 IntBuffer ib = b.asIntBuffer(); 300 BasicInt.test(level, ib, direct); 301 checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) }); 302 checkInvalidMarkException(ib); 303 304 LongBuffer lb = b.asLongBuffer(); 305 BasicLong.test(level, lb, direct); 306 checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) }); 307 checkInvalidMarkException(lb); 308 309 FloatBuffer fb = b.asFloatBuffer(); 310 BasicFloat.test(level, fb, direct); 311 checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 }); 312 checkInvalidMarkException(fb); 313 314 DoubleBuffer db = b.asDoubleBuffer(); 315 BasicDouble.test(level, db, direct); 316 checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 }); 317 checkInvalidMarkException(db); 318 } 319 320 private static void testHet(int level, ByteBuffer b) { 321 322 int p = b.position(); 323 b.limit(b.capacity()); 324 show(level, b); 325 out.print(" put:"); 326 327 b.putChar((char)1); 328 b.putChar((char)Character.MAX_VALUE); 329 out.print(" char"); 330 331 b.putShort((short)1); 332 b.putShort((short)Short.MAX_VALUE); 333 out.print(" short"); 334 335 b.putInt(1); 336 b.putInt(Integer.MAX_VALUE); 337 out.print(" int"); 338 339 b.putLong((long)1); 340 b.putLong((long)Long.MAX_VALUE); 341 out.print(" long"); 342 343 b.putFloat((float)1); 344 b.putFloat((float)Float.MIN_VALUE); 345 b.putFloat((float)Float.MAX_VALUE); 346 out.print(" float"); 347 348 b.putDouble((double)1); 349 b.putDouble((double)Double.MIN_VALUE); 350 b.putDouble((double)Double.MAX_VALUE); 351 out.print(" double"); 352 353 out.println(); 354 b.limit(b.position()); 355 b.position(p); 356 show(level, b); 357 out.print(" get:"); 358 359 ck(b, b.getChar(), 1); 360 ck(b, b.getChar(), Character.MAX_VALUE); 361 out.print(" char"); 362 363 ck(b, b.getShort(), 1); 364 ck(b, b.getShort(), Short.MAX_VALUE); 365 out.print(" short"); 366 367 ck(b, b.getInt(), 1); 368 ck(b, b.getInt(), Integer.MAX_VALUE); 369 out.print(" int"); 370 371 ck(b, b.getLong(), 1); 372 ck(b, b.getLong(), Long.MAX_VALUE); 373 out.print(" long"); 374 375 ck(b, (long)b.getFloat(), 1); 376 ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE); 377 ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE); 378 out.print(" float"); 379 380 ck(b, (long)b.getDouble(), 1); 381 ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE); 382 ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE); 383 out.print(" double"); 384 385 out.println(); 386 387 } 388 389 private static void testAlign(final ByteBuffer b, boolean direct) { 390 // index out-of bounds 391 catchIllegalArgument(b, () -> b.alignmentOffset(-1, (short) 1)); 392 393 // unit size values 394 catchIllegalArgument(b, () -> b.alignmentOffset(0, (short) 0)); 395 for (int us = 1; us < 65; us++) { 396 int _us = us; 397 if ((us & (us - 1)) != 0) { 398 // unit size not a power of two 399 catchIllegalArgument(b, () -> b.alignmentOffset(0, _us)); 400 } else { 401 if (direct || us <= 8) { 402 b.alignmentOffset(0, us); 403 } else { 404 // unit size > 8 with non-direct buffer 405 tryCatch(b, UnsupportedOperationException.class, 406 () -> b.alignmentOffset(0, _us)); 407 } 408 } 409 } 410 411 // Probe for long misalignment at index zero for a newly created buffer 412 ByteBuffer empty = 413 direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0); 414 int longMisalignmentAtZero = empty.alignmentOffset(0, 8); 415 416 if (direct) { 417 // Freshly created direct byte buffers should be aligned at index 0 418 // for ref and primitive values (see Unsafe.allocateMemory) 419 if (longMisalignmentAtZero != 0) { 420 fail("Direct byte buffer misaligned at index 0" 421 + " for ref and primitive values " 422 + longMisalignmentAtZero); 423 } 424 } else { 425 // For heap byte buffers misalignment may occur on 32-bit systems 426 // where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0 427 // Note the GC will preserve alignment of the base address of the 428 // array 429 if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 430 != longMisalignmentAtZero) { 431 fail("Heap byte buffer misaligned at index 0" 432 + " for ref and primitive values " 433 + longMisalignmentAtZero); 434 } 435 } 436 437 // Ensure test buffer is correctly aligned at index 0 438 if (b.alignmentOffset(0, 8) != longMisalignmentAtZero) 439 fail("Test input buffer not correctly aligned at index 0", b); 440 441 // Test misalignment values 442 for (int us : new int[]{1, 2, 4, 8}) { 443 for (int i = 0; i < us * 2; i++) { 444 int am = b.alignmentOffset(i, us); 445 int expectedAm = (longMisalignmentAtZero + i) % us; 446 447 if (am != expectedAm) { 448 String f = "b.alignmentOffset(%d, %d) == %d incorrect, expected %d"; 449 fail(String.format(f, i, us, am, expectedAm)); 450 } 451 } 452 } 453 454 // Created aligned slice to test against 455 int ap = 8 - longMisalignmentAtZero; 456 int al = b.limit() - b.alignmentOffset(b.limit(), 8); 457 ByteBuffer ab = b.position(ap).limit(al). 458 slice(); 459 if (ab.limit() == 0) { 460 fail("Test input buffer not sufficiently sized to cover" + 461 " an aligned region for all values", b); 462 } 463 if (ab.alignmentOffset(0, 8) != 0) 464 fail("Aligned test input buffer not correctly aligned at index 0", ab); 465 466 for (int us : new int[]{1, 2, 4, 8}) { 467 for (int p = 1; p < 16; p++) { 468 int l = ab.limit() - p; 469 470 ByteBuffer as = ab.slice().position(p).limit(l). 471 alignedSlice(us); 472 473 ck(as, 0, as.position()); 474 ck(as, as.capacity(), as.limit()); 475 if (b.isDirect() != as.isDirect()) 476 fail("Lost direction", as); 477 if (b.isReadOnly() != as.isReadOnly()) 478 fail("Lost read-only", as); 479 480 if (as.alignmentOffset(0, us) != 0) 481 fail("Buffer not correctly aligned at index 0", as); 482 483 if (as.alignmentOffset(as.limit(), us) != 0) 484 fail("Buffer not correctly aligned at limit", as); 485 486 int p_mod = ab.alignmentOffset(p, us); 487 int l_mod = ab.alignmentOffset(l, us); 488 // Round up position 489 p = (p_mod > 0) ? p + (us - p_mod) : p; 490 // Round down limit 491 l = l - l_mod; 492 493 int ec = l - p; 494 if (as.limit() != ec) { 495 fail("Buffer capacity incorrect, expected: " + ec, as); 496 } 497 } 498 } 499 } 500 501 502 private static void fail(String problem, 503 ByteBuffer xb, ByteBuffer yb, 504 byte x, byte 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(byte[] 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(byte[] t, Class<?> ex, Runnable thunk) { 548 tryCatch(ByteBuffer.wrap(t), ex, thunk); 549 } 550 551 public static void test(int level, final ByteBuffer 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((byte)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(), (byte)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, (byte[])null, 0, 42)); 688 catchNullArgument(b, () -> b.put(7, (ByteBuffer)null, 0, 42)); 689 690 byte[] tmpa = new byte[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 ByteBuffer tmpb = ByteBuffer.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((byte)0); 719 b.put((byte)-1); 720 b.put((byte)1); 721 b.put(Byte.MAX_VALUE); 722 b.put(Byte.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(), (byte)-1); 743 ck(b, b.get(), 1); 744 ck(b, b.get(), Byte.MAX_VALUE); 745 ck(b, b.get(), Byte.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 ByteBuffer b2 = ByteBuffer.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 byte x = b.get(i); 783 byte 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((byte)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, (byte)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 (byte x : VALUES) { 819 ByteBuffer xb = ByteBuffer.wrap(new byte[] { 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 (byte y : VALUES) { 827 ByteBuffer yb = ByteBuffer.wrap(new byte[] { 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) != Byte.compare(x, y)) { 837 838 839 840 841 842 843 fail("Incorrect results for ByteBuffer.compareTo", 844 xb, yb, x, y); 845 } 846 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 847 fail("Incorrect results for ByteBuffer.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 ByteBuffer sb = b.slice(); 867 checkSlice(b, sb); 868 b.position(0); 869 ByteBuffer 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 // Views 882 883 b.clear(); 884 b.order(ByteOrder.BIG_ENDIAN); 885 testViews(level + 1, b, direct); 886 887 for (int i = 1; i <= 9; i++) { 888 b.position(i); 889 show(level + 1, b); 890 testViews(level + 2, b, direct); 891 } 892 893 b.position(0); 894 b.order(ByteOrder.LITTLE_ENDIAN); 895 testViews(level + 1, b, direct); 896 897 // Heterogeneous accessors 898 899 b.order(ByteOrder.BIG_ENDIAN); 900 for (int i = 0; i <= 9; i++) { 901 b.position(i); 902 testHet(level + 1, b); 903 } 904 b.order(ByteOrder.LITTLE_ENDIAN); 905 b.position(3); 906 testHet(level + 1, b); 907 908 909 910 // Read-only views 911 912 b.rewind(); 913 final ByteBuffer 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(ByteBuffer) should not change source position 926 final ByteBuffer src = ByteBuffer.allocate(1); 927 catchReadOnlyBuffer(b, () -> rb.put(src)); 928 ck(src, src.position(), 0); 929 930 catchReadOnlyBuffer(b, () -> rb.compact()); 931 932 933 934 catchReadOnlyBuffer(b, () -> rb.putChar((char)1)); 935 catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1)); 936 catchReadOnlyBuffer(b, () -> rb.putShort((short)1)); 937 catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1)); 938 catchReadOnlyBuffer(b, () -> rb.putInt(1)); 939 catchReadOnlyBuffer(b, () -> rb.putInt(0, 1)); 940 catchReadOnlyBuffer(b, () -> rb.putLong((long)1)); 941 catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1)); 942 catchReadOnlyBuffer(b, () -> rb.putFloat((float)1)); 943 catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1)); 944 catchReadOnlyBuffer(b, () -> rb.putDouble((double)1)); 945 catchReadOnlyBuffer(b, () -> rb.putDouble(0, (double)1)); 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 // For byte buffers, test both the direct and non-direct cases 973 ByteBuffer ob 974 = (b.isDirect() 975 ? ByteBuffer.allocate(rb.capacity()) 976 : ByteBuffer.allocateDirect(rb.capacity())); 977 rb.rewind(); 978 ob.put(rb); 979 980 981 relPut(b); // Required by testViews 982 983 984 // Test alignment 985 986 testAlign(b, direct); 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 byte [] ba) { 1034 int offset = 47; 1035 int length = 900; 1036 final ByteBuffer b = ByteBuffer.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, () -> ByteBuffer.wrap(ba, -1, ba.length)); 1044 catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, ba.length + 1, ba.length)); 1045 catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, -1)); 1046 catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, ba.length + 1)); 1047 1048 // A NullPointerException will be thrown if the array is null. 1049 tryCatch(ba, NullPointerException.class, 1050 () -> ByteBuffer.wrap((byte []) null, 0, 5)); 1051 tryCatch(ba, NullPointerException.class, 1052 () -> ByteBuffer.wrap((byte []) null)); 1053 } 1054 1055 private static void testAllocate() { 1056 // An IllegalArgumentException will be thrown for negative capacities. 1057 catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocate(-1)); 1058 try { 1059 ByteBuffer.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 catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocateDirect(-1)); 1068 try { 1069 ByteBuffer.allocateDirect(-1); 1070 } catch (IllegalArgumentException e) { 1071 if (e.getMessage() == null) { 1072 fail("Non-null IllegalArgumentException message expected for" 1073 + " attempt to allocate negative capacity direct buffer"); 1074 } 1075 } 1076 1077 } 1078 1079 public static void test() { 1080 testAllocate(); 1081 test(0, ByteBuffer.allocate(7 * 1024), false); 1082 test(0, ByteBuffer.wrap(new byte[7 * 1024], 0, 7 * 1024), false); 1083 test(new byte[1024]); 1084 1085 ByteBuffer b = ByteBuffer.allocateDirect(7 * 1024); 1086 for (b.position(0); b.position() < b.limit(); ) 1087 ck(b, b.get(), 0); 1088 test(0, b, true); 1089 1090 1091 1092 1093 1094 callReset(ByteBuffer.allocate(10)); 1095 1096 1097 1098 1099 1100 } 1101 1102 }