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 #warn This file is preprocessed before being compiled 32 33 import java.nio.*; 34 35 36 public class Basic$Type$ 37 extends Basic 38 { 39 40 private static final $type$[] VALUES = { 41 $Fulltype$.MIN_VALUE, 42 ($type$) -1, 43 ($type$) 0, 44 ($type$) 1, 45 $Fulltype$.MAX_VALUE, 46 #if[float] 47 $Fulltype$.NEGATIVE_INFINITY, 48 $Fulltype$.POSITIVE_INFINITY, 49 $Fulltype$.NaN, 50 ($type$) -0.0, 51 #end[float] 52 #if[double] 53 $Fulltype$.NEGATIVE_INFINITY, 54 $Fulltype$.POSITIVE_INFINITY, 55 $Fulltype$.NaN, 56 ($type$) -0.0, 57 #end[double] 58 }; 59 60 private static void relGet($Type$Buffer b) { 61 int n = b.capacity(); 62 for (int i = 0; i < n; i++) 63 ck(b, (long)b.get(), (long)(($type$)ic(i))); 64 b.rewind(); 65 } 66 67 private static void relGet($Type$Buffer b, int start) { 68 int n = b.remaining(); 69 for (int i = start; i < n; i++) 70 ck(b, (long)b.get(), (long)(($type$)ic(i))); 71 b.rewind(); 72 } 73 74 private static void absGet($Type$Buffer b) { 75 int n = b.capacity(); 76 for (int i = 0; i < n; i++) 77 ck(b, (long)b.get(), (long)(($type$)ic(i))); 78 b.rewind(); 79 } 80 81 private static void bulkGet($Type$Buffer b) { 82 int n = b.capacity(); 83 $type$[] a = new $type$[n + 7]; 84 b.get(a, 7, n); 85 for (int i = 0; i < n; i++) { 86 ck(b, (long)a[i + 7], (long)(($type$)ic(i))); 87 } 88 } 89 90 private static void absBulkGet($Type$Buffer b) { 91 int n = b.capacity(); 92 int len = n - 7*2; 93 $type$[] a = new $type$[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)(($type$)ic(i))); 99 } 100 } 101 102 private static void relPut($Type$Buffer b) { 103 int n = b.capacity(); 104 b.clear(); 105 for (int i = 0; i < n; i++) 106 b.put(($type$)ic(i)); 107 b.flip(); 108 } 109 110 private static void absPut($Type$Buffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 for (int i = 0; i < n; i++) 114 b.put(i, ($type$)ic(i)); 115 b.limit(n); 116 b.position(0); 117 } 118 119 private static void bulkPutArray($Type$Buffer b) { 120 int n = b.capacity(); 121 b.clear(); 122 $type$[] a = new $type$[n + 7]; 123 for (int i = 0; i < n; i++) 124 a[i + 7] = ($type$)ic(i); 125 b.put(a, 7, n); 126 b.flip(); 127 } 128 129 private static void bulkPutBuffer($Type$Buffer b) { 130 int n = b.capacity(); 131 b.clear(); 132 $Type$Buffer c = $Type$Buffer.allocate(n + 7); 133 c.position(7); 134 for (int i = 0; i < n; i++) 135 c.put(($type$)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($Type$Buffer b) { 152 int n = b.capacity(); 153 b.clear(); 154 int lim = n - 7; 155 int len = lim - 7; 156 b.limit(lim); 157 $type$[] a = new $type$[len + 7]; 158 for (int i = 0; i < len; i++) 159 a[i + 7] = ($type$)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($Type$Buffer 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 $Type$Buffer c = direct ? 172 #if[byte] 173 ByteBuffer.allocateDirect(len + 7) 174 #else[byte] 175 ByteBuffer.allocateDirect($Fulltype$.BYTES*(len + 7)) 176 .as$Type$Buffer() 177 #end[byte] 178 : $Type$Buffer.allocate(len + 7); 179 #if[!byte] 180 if (direct) 181 System.out.println("Direct buffer: " + c.getClass().getName()); 182 #end[!byte] 183 for (int i = 0; i < len; i++) 184 c.put(i + 7, ($type$)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($Type$Buffer b) { 194 b.position(0); 195 b.mark(); 196 197 b.duplicate().reset(); 198 b.asReadOnlyBuffer().reset(); 199 } 200 201 #if[byte] 202 #else[byte] 203 // 6221101-6234263 204 205 private static void putBuffer() { 206 final int cap = 10; 207 208 $Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 209 $Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer(); 210 direct1.put(nondirect1); 211 212 $Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 213 $Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer(); 214 nondirect2.put(direct2); 215 216 $Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 217 $Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 218 direct3.put(direct4); 219 220 $Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer(); 221 $Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer(); 222 nondirect3.put(nondirect4); 223 } 224 #end[byte] 225 226 #if[char] 227 228 private static void bulkPutString($Type$Buffer b) { 229 int n = b.capacity(); 230 b.clear(); 231 StringBuilder sb = new StringBuilder(n + 7); 232 sb.append("1234567"); 233 for (int i = 0; i < n; i++) 234 sb.append((char)ic(i)); 235 b.put(sb.toString(), 7, 7 + n); 236 b.flip(); 237 } 238 239 #end[char] 240 241 private static void checkSlice($Type$Buffer b, $Type$Buffer 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 #if[byte] 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 #end[byte] 501 502 private static void fail(String problem, 503 $Type$Buffer xb, $Type$Buffer yb, 504 $type$ x, $type$ 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($type$[] 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 fail(x.getMessage() + " not expected"); 537 } 538 } 539 if (!caught) { 540 fail(ex.getName() + " not thrown", b); 541 } 542 } 543 544 private static void tryCatch($type$[] t, Class<?> ex, Runnable thunk) { 545 tryCatch($Type$Buffer.wrap(t), ex, thunk); 546 } 547 548 public static void test(int level, final $Type$Buffer b, boolean direct) { 549 550 show(level, b); 551 552 if (direct != b.isDirect()) 553 fail("Wrong direction", b); 554 555 // Gets and puts 556 557 relPut(b); 558 relGet(b); 559 absGet(b); 560 bulkGet(b); 561 562 absPut(b); 563 relGet(b); 564 absGet(b); 565 bulkGet(b); 566 567 bulkPutArray(b); 568 relGet(b); 569 570 bulkPutBuffer(b); 571 relGet(b); 572 573 absBulkPutArray(b); 574 absBulkGet(b); 575 576 absBulkPutBuffer(b, direct); 577 absBulkGet(b); 578 579 absBulkPutBuffer(b, !direct); 580 absBulkGet(b); 581 582 #if[char] 583 584 bulkPutString(b); 585 relGet(b); 586 b.position(1); 587 b.limit(7); 588 ck(b, b.toString().equals("bcdefg")); 589 590 // CharSequence ops 591 592 b.position(2); 593 ck(b, b.charAt(1), 'd'); 594 CharBuffer c = b.subSequence(1, 4); 595 ck(c, c.capacity(), b.capacity()); 596 ck(c, c.position(), b.position()+1); 597 ck(c, c.limit(), b.position()+4); 598 ck(c, b.subSequence(1, 4).toString().equals("def")); 599 600 // 4938424 601 b.position(4); 602 ck(b, b.charAt(1), 'f'); 603 ck(b, b.subSequence(1, 3).toString().equals("fg")); 604 605 // String ops 606 607 // 7190219 608 b.clear(); 609 int pos = b.position(); 610 tryCatch(b, BufferOverflowException.class, () -> 611 b.put(String.valueOf(new char[b.capacity() + 1]), 0, b.capacity() + 1) 612 ); 613 ck(b, b.position(), pos); 614 relGet(b); 615 616 #end[char] 617 618 // Compact 619 620 relPut(b); 621 b.position(13); 622 b.compact(); 623 b.flip(); 624 relGet(b, 13); 625 626 // Exceptions 627 628 relPut(b); 629 b.limit(b.capacity() / 2); 630 b.position(b.limit()); 631 632 tryCatch(b, BufferUnderflowException.class, () -> b.get()); 633 tryCatch(b, BufferOverflowException.class, () -> b.put(($type$)42)); 634 // The index must be non-negative and less than the buffer's limit. 635 catchIndexOutOfBounds(b, () -> b.get(b.limit())); 636 catchIndexOutOfBounds(b, () -> b.get(-1)); 637 catchIndexOutOfBounds(b, () -> b.put(b.limit(), ($type$)42)); 638 tryCatch(b, InvalidMarkException.class, 639 () -> b.position(0).mark().compact().reset()); 640 641 try { 642 b.position(b.limit() + 1); 643 fail("IllegalArgumentException expected for position beyond limit"); 644 } catch (IllegalArgumentException e) { 645 if (e.getMessage() == null) { 646 fail("Non-null IllegalArgumentException message expected for" 647 + " position beyond limit"); 648 } 649 } 650 651 try { 652 b.position(-1); 653 fail("IllegalArgumentException expected for negative position"); 654 } catch (IllegalArgumentException e) { 655 if (e.getMessage() == null) { 656 fail("Non-null IllegalArgumentException message expected for" 657 + " negative position"); 658 } 659 } 660 661 try { 662 b.limit(b.capacity() + 1); 663 fail("IllegalArgumentException expected for limit beyond capacity"); 664 } catch (IllegalArgumentException e) { 665 if (e.getMessage() == null) { 666 fail("Non-null IllegalArgumentException message expected for" 667 + " limit beyond capacity"); 668 } 669 } 670 671 try { 672 b.limit(-1); 673 fail("IllegalArgumentException expected for negative limit"); 674 } catch (IllegalArgumentException e) { 675 if (e.getMessage() == null) { 676 fail("Non-null IllegalArgumentException message expected for" 677 + " negative limit"); 678 } 679 } 680 681 // Exceptions in absolute bulk operations 682 683 catchNullArgument(b, () -> b.get(7, null, 0, 42)); 684 catchNullArgument(b, () -> b.put(7, ($type$[])null, 0, 42)); 685 catchNullArgument(b, () -> b.put(7, ($Type$Buffer)null, 0, 42)); 686 687 $type$[] tmpa = new $type$[42]; 688 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42)); 689 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1)); 690 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, 2)); 691 catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1)); 692 catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1)); 693 catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42)); 694 695 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42)); 696 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1)); 697 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, 2)); 698 catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1)); 699 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1)); 700 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42)); 701 702 $Type$Buffer tmpb = $Type$Buffer.allocate(42); 703 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, -1, 42)); 704 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, 1)); 705 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 41, 2)); 706 catchIndexOutOfBounds(b, () -> b.put(-1, tmpb, 0, 1)); 707 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpb, 0, 1)); 708 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpb, 0, 42)); 709 710 // Values 711 712 b.clear(); 713 b.put(($type$)0); 714 b.put(($type$)-1); 715 b.put(($type$)1); 716 b.put($Fulltype$.MAX_VALUE); 717 b.put($Fulltype$.MIN_VALUE); 718 #if[float] 719 b.put(-Float.MAX_VALUE); 720 b.put(-Float.MIN_VALUE); 721 b.put(Float.NEGATIVE_INFINITY); 722 b.put(Float.POSITIVE_INFINITY); 723 b.put(Float.NaN); 724 b.put(0.91697687f); // Changes value if incorrectly swapped 725 #end[float] 726 #if[double] 727 b.put(-Double.MAX_VALUE); 728 b.put(-Double.MIN_VALUE); 729 b.put(Double.NEGATIVE_INFINITY); 730 b.put(Double.POSITIVE_INFINITY); 731 b.put(Double.NaN); 732 b.put(0.5121609353879392); // Changes value if incorrectly swapped 733 #end[double] 734 735 b.flip(); 736 ck(b, b.get(), 0); 737 ck(b, b.get(), ($type$)-1); 738 ck(b, b.get(), 1); 739 ck(b, b.get(), $Fulltype$.MAX_VALUE); 740 ck(b, b.get(), $Fulltype$.MIN_VALUE); 741 742 #if[float] 743 $type$ v; 744 ck(b, b.get(), -Float.MAX_VALUE); 745 ck(b, b.get(), -Float.MIN_VALUE); 746 ck(b, b.get(), Float.NEGATIVE_INFINITY); 747 ck(b, b.get(), Float.POSITIVE_INFINITY); 748 if (Float.floatToRawIntBits(v = b.get()) != 749 Float.floatToRawIntBits(Float.NaN)) { 750 fail(b, (long)Float.NaN, (long)v); 751 } 752 ck(b, b.get(), 0.91697687f); 753 #end[float] 754 #if[double] 755 $type$ v; 756 ck(b, b.get(), -Double.MAX_VALUE); 757 ck(b, b.get(), -Double.MIN_VALUE); 758 ck(b, b.get(), Double.NEGATIVE_INFINITY); 759 ck(b, b.get(), Double.POSITIVE_INFINITY); 760 if (Double.doubleToRawLongBits(v = b.get()) 761 != Double.doubleToRawLongBits(Double.NaN)) { 762 fail(b, (long)Double.NaN, (long)v); 763 } 764 ck(b, b.get(), 0.5121609353879392); 765 #end[double] 766 767 768 // Comparison 769 b.rewind(); 770 $Type$Buffer b2 = $Type$Buffer.allocate(b.capacity()); 771 b2.put(b); 772 b2.flip(); 773 b.position(2); 774 b2.position(2); 775 if (!b.equals(b2)) { 776 for (int i = 2; i < b.limit(); i++) { 777 $type$ x = b.get(i); 778 $type$ y = b2.get(i); 779 if (x != y 780 #if[double] 781 || Double.compare(x, y) != 0 782 #end[double] 783 #if[float] 784 || Float.compare(x, y) != 0 785 #end[float] 786 ) { 787 out.println("[" + i + "] " + x + " != " + y); 788 } 789 } 790 fail("Identical buffers not equal", b, b2); 791 } 792 if (b.compareTo(b2) != 0) { 793 fail("Comparison to identical buffer != 0", b, b2); 794 } 795 b.limit(b.limit() + 1); 796 b.position(b.limit() - 1); 797 b.put(($type$)99); 798 b.rewind(); 799 b2.rewind(); 800 if (b.equals(b2)) 801 fail("Non-identical buffers equal", b, b2); 802 if (b.compareTo(b2) <= 0) 803 fail("Comparison to shorter buffer <= 0", b, b2); 804 b.limit(b.limit() - 1); 805 806 b.put(2, ($type$)42); 807 if (b.equals(b2)) 808 fail("Non-identical buffers equal", b, b2); 809 if (b.compareTo(b2) <= 0) 810 fail("Comparison to lesser buffer <= 0", b, b2); 811 812 // Check equals and compareTo with interesting values 813 for ($type$ x : VALUES) { 814 $Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { x }); 815 if (xb.compareTo(xb) != 0) { 816 fail("compareTo not reflexive", xb, xb, x, x); 817 } 818 if (!xb.equals(xb)) { 819 fail("equals not reflexive", xb, xb, x, x); 820 } 821 for ($type$ y : VALUES) { 822 $Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { y }); 823 if (xb.compareTo(yb) != - yb.compareTo(xb)) { 824 fail("compareTo not anti-symmetric", 825 xb, yb, x, y); 826 } 827 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { 828 fail("compareTo inconsistent with equals", 829 xb, yb, x, y); 830 } 831 if (xb.compareTo(yb) != $Fulltype$.compare(x, y)) { 832 #if[float] 833 if (x == 0.0 && y == 0.0) continue; 834 #end[float] 835 #if[double] 836 if (x == 0.0 && y == 0.0) continue; 837 #end[double] 838 fail("Incorrect results for $Type$Buffer.compareTo", 839 xb, yb, x, y); 840 } 841 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 842 fail("Incorrect results for $Type$Buffer.equals", 843 xb, yb, x, y); 844 } 845 } 846 } 847 848 // Sub, dup 849 850 relPut(b); 851 relGet(b.duplicate()); 852 b.position(13); 853 relGet(b.duplicate(), 13); 854 relGet(b.duplicate().slice(), 13); 855 relGet(b.slice(), 13); 856 relGet(b.slice().duplicate(), 13); 857 858 // Slice 859 860 b.position(5); 861 $Type$Buffer sb = b.slice(); 862 checkSlice(b, sb); 863 b.position(0); 864 $Type$Buffer sb2 = sb.slice(); 865 checkSlice(sb, sb2); 866 867 if (!sb.equals(sb2)) 868 fail("Sliced slices do not match", sb, sb2); 869 if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) { 870 fail("Array offsets do not match: " 871 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); 872 } 873 874 #if[byte] 875 876 // Views 877 878 b.clear(); 879 b.order(ByteOrder.BIG_ENDIAN); 880 testViews(level + 1, b, direct); 881 882 for (int i = 1; i <= 9; i++) { 883 b.position(i); 884 show(level + 1, b); 885 testViews(level + 2, b, direct); 886 } 887 888 b.position(0); 889 b.order(ByteOrder.LITTLE_ENDIAN); 890 testViews(level + 1, b, direct); 891 892 // Heterogeneous accessors 893 894 b.order(ByteOrder.BIG_ENDIAN); 895 for (int i = 0; i <= 9; i++) { 896 b.position(i); 897 testHet(level + 1, b); 898 } 899 b.order(ByteOrder.LITTLE_ENDIAN); 900 b.position(3); 901 testHet(level + 1, b); 902 903 #end[byte] 904 905 // Read-only views 906 907 b.rewind(); 908 final $Type$Buffer rb = b.asReadOnlyBuffer(); 909 if (!b.equals(rb)) 910 fail("Buffer not equal to read-only view", b, rb); 911 show(level + 1, rb); 912 913 catchReadOnlyBuffer(b, () -> relPut(rb)); 914 catchReadOnlyBuffer(b, () -> absPut(rb)); 915 catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); 916 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); 917 catchReadOnlyBuffer(b, () -> absBulkPutArray(rb)); 918 catchReadOnlyBuffer(b, () -> absBulkPutBuffer(rb, direct)); 919 920 // put($Type$Buffer) should not change source position 921 final $Type$Buffer src = $Type$Buffer.allocate(1); 922 catchReadOnlyBuffer(b, () -> rb.put(src)); 923 ck(src, src.position(), 0); 924 925 catchReadOnlyBuffer(b, () -> rb.compact()); 926 927 #if[byte] 928 929 catchReadOnlyBuffer(b, () -> rb.putChar((char)1)); 930 catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1)); 931 catchReadOnlyBuffer(b, () -> rb.putShort((short)1)); 932 catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1)); 933 catchReadOnlyBuffer(b, () -> rb.putInt(1)); 934 catchReadOnlyBuffer(b, () -> rb.putInt(0, 1)); 935 catchReadOnlyBuffer(b, () -> rb.putLong((long)1)); 936 catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1)); 937 catchReadOnlyBuffer(b, () -> rb.putFloat((float)1)); 938 catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1)); 939 catchReadOnlyBuffer(b, () -> rb.putDouble((double)1)); 940 catchReadOnlyBuffer(b, () -> rb.putDouble(0, (double)1)); 941 942 #end[byte] 943 944 #if[char] 945 946 // 7199551 947 catchReadOnlyBuffer(b, () -> rb.put(new String(new char[rb.remaining() + 1]))); 948 catchReadOnlyBuffer(b, () -> rb.append(new String(new char[rb.remaining() + 1]))); 949 950 #end[char] 951 952 if (rb.getClass().getName().startsWith("java.nio.Heap")) { 953 catchReadOnlyBuffer(b, () -> rb.array()); 954 catchReadOnlyBuffer(b, () -> rb.arrayOffset()); 955 if (rb.hasArray()) { 956 fail("Read-only heap buffer's backing array is accessible", rb); 957 } 958 } 959 960 // Bulk puts from read-only buffers 961 962 b.clear(); 963 rb.rewind(); 964 b.put(rb); 965 966 #if[byte] 967 // For byte buffers, test both the direct and non-direct cases 968 $Type$Buffer ob 969 = (b.isDirect() 970 ? $Type$Buffer.allocate(rb.capacity()) 971 : $Type$Buffer.allocateDirect(rb.capacity())); 972 rb.rewind(); 973 ob.put(rb); 974 #end[byte] 975 976 relPut(b); // Required by testViews 977 978 #if[byte] 979 // Test alignment 980 981 testAlign(b, direct); 982 #end[byte] 983 } 984 985 #if[char] 986 987 private static void testStr() { 988 final String s = "abcdefghijklm"; 989 int start = 3; 990 int end = 9; 991 final CharBuffer b = CharBuffer.wrap(s, start, end); 992 show(0, b); 993 ck(b, b.toString().equals(s.substring(start, end))); 994 ck(b, b.toString().equals("defghi")); 995 ck(b, b.isReadOnly()); 996 catchReadOnlyBuffer(b, () -> b.put('x')); 997 ck(b, start, b.position()); 998 ck(b, end, b.limit()); 999 ck(b, s.length(), b.capacity()); 1000 b.position(6); 1001 ck(b, b.subSequence(0,3).toString().equals("ghi")); 1002 1003 // The index, relative to the position, must be non-negative and 1004 // smaller than remaining(). 1005 catchIndexOutOfBounds(b, () -> b.charAt(-1)); 1006 catchIndexOutOfBounds(b, () -> b.charAt(b.remaining())); 1007 // The index must be non-negative and less than the buffer's limit. 1008 catchIndexOutOfBounds(b, () -> b.get(b.limit())); 1009 catchIndexOutOfBounds(b, () -> b.get(-1)); 1010 // The start must be non-negative and no larger than remaining(). 1011 catchIndexOutOfBounds(b, () -> b.subSequence(-1, b.remaining())); 1012 catchIndexOutOfBounds(b, () -> b.subSequence(b.remaining() + 1, b.remaining())); 1013 1014 // The end must be no smaller than start and no larger than 1015 // remaining(). 1016 catchIndexOutOfBounds(b, () -> b.subSequence(2, 1)); 1017 catchIndexOutOfBounds(b, () -> b.subSequence(0, b.remaining() + 1)); 1018 1019 // The offset must be non-negative and no larger than <array.length>. 1020 catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, -1, s.length())); 1021 catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, s.length() + 1, s.length())); 1022 catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, 1, 0)); 1023 catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, 0, s.length() + 1)); 1024 } 1025 1026 #end[char] 1027 1028 public static void test(final $type$ [] ba) { 1029 int offset = 47; 1030 int length = 900; 1031 final $Type$Buffer b = $Type$Buffer.wrap(ba, offset, length); 1032 show(0, b); 1033 ck(b, b.capacity(), ba.length); 1034 ck(b, b.position(), offset); 1035 ck(b, b.limit(), offset + length); 1036 1037 // The offset must be non-negative and no larger than <array.length>. 1038 catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, -1, ba.length)); 1039 catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, ba.length + 1, ba.length)); 1040 catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, 0, -1)); 1041 catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, 0, ba.length + 1)); 1042 1043 // A NullPointerException will be thrown if the array is null. 1044 tryCatch(ba, NullPointerException.class, 1045 () -> $Type$Buffer.wrap(($type$ []) null, 0, 5)); 1046 tryCatch(ba, NullPointerException.class, 1047 () -> $Type$Buffer.wrap(($type$ []) null)); 1048 } 1049 1050 private static void testAllocate() { 1051 // An IllegalArgumentException will be thrown for negative capacities. 1052 catchIllegalArgument((Buffer) null, () -> $Type$Buffer.allocate(-1)); 1053 try { 1054 $Type$Buffer.allocate(-1); 1055 } catch (IllegalArgumentException e) { 1056 if (e.getMessage() == null) { 1057 fail("Non-null IllegalArgumentException message expected for" 1058 + " attempt to allocate negative capacity buffer"); 1059 } 1060 } 1061 #if[byte] 1062 catchIllegalArgument((Buffer) null, () -> $Type$Buffer.allocateDirect(-1)); 1063 try { 1064 $Type$Buffer.allocateDirect(-1); 1065 } catch (IllegalArgumentException e) { 1066 if (e.getMessage() == null) { 1067 fail("Non-null IllegalArgumentException message expected for" 1068 + " attempt to allocate negative capacity direct buffer"); 1069 } 1070 } 1071 #end[byte] 1072 } 1073 1074 public static void test() { 1075 testAllocate(); 1076 test(0, $Type$Buffer.allocate(7 * 1024), false); 1077 test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false); 1078 test(new $type$[1024]); 1079 #if[byte] 1080 $Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024); 1081 for (b.position(0); b.position() < b.limit(); ) 1082 ck(b, b.get(), 0); 1083 test(0, b, true); 1084 #end[byte] 1085 #if[char] 1086 testStr(); 1087 #end[char] 1088 1089 callReset($Type$Buffer.allocate(10)); 1090 1091 #if[byte] 1092 #else[byte] 1093 putBuffer(); 1094 #end[byte] 1095 } 1096 1097 }