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 #warn This file is preprocessed before being compiled 32 33 import java.nio.*; 34 import java.lang.reflect.Method; 35 36 37 public class Basic$Type$ 38 extends Basic 39 { 40 41 private static final $type$[] VALUES = { 42 $Fulltype$.MIN_VALUE, 43 ($type$) -1, 44 ($type$) 0, 45 ($type$) 1, 46 $Fulltype$.MAX_VALUE, 47 #if[float] 48 $Fulltype$.NEGATIVE_INFINITY, 49 $Fulltype$.POSITIVE_INFINITY, 50 $Fulltype$.NaN, 51 ($type$) -0.0, 52 #end[float] 53 #if[double] 54 $Fulltype$.NEGATIVE_INFINITY, 55 $Fulltype$.POSITIVE_INFINITY, 56 $Fulltype$.NaN, 57 ($type$) -0.0, 58 #end[double] 59 }; 60 61 private static void relGet($Type$Buffer b) { 62 int n = b.capacity(); 63 $type$ v; 64 for (int i = 0; i < n; i++) 65 ck(b, (long)b.get(), (long)(($type$)ic(i))); 66 b.rewind(); 67 } 68 69 private static void relGet($Type$Buffer b, int start) { 70 int n = b.remaining(); 71 $type$ v; 72 for (int i = start; i < n; i++) 73 ck(b, (long)b.get(), (long)(($type$)ic(i))); 74 b.rewind(); 75 } 76 77 private static void absGet($Type$Buffer b) { 78 int n = b.capacity(); 79 $type$ v; 80 for (int i = 0; i < n; i++) 81 ck(b, (long)b.get(), (long)(($type$)ic(i))); 82 b.rewind(); 83 } 84 85 private static void bulkGet($Type$Buffer b) { 86 int n = b.capacity(); 87 $type$[] a = new $type$[n + 7]; 88 b.get(a, 7, n); 89 for (int i = 0; i < n; i++) 90 ck(b, (long)a[i + 7], (long)(($type$)ic(i))); 91 } 92 93 private static void relPut($Type$Buffer b) { 94 int n = b.capacity(); 95 b.clear(); 96 for (int i = 0; i < n; i++) 97 b.put(($type$)ic(i)); 98 b.flip(); 99 } 100 101 private static void absPut($Type$Buffer b) { 102 int n = b.capacity(); 103 b.clear(); 104 for (int i = 0; i < n; i++) 105 b.put(i, ($type$)ic(i)); 106 b.limit(n); 107 b.position(0); 108 } 109 110 private static void bulkPutArray($Type$Buffer b) { 111 int n = b.capacity(); 112 b.clear(); 113 $type$[] a = new $type$[n + 7]; 114 for (int i = 0; i < n; i++) 115 a[i + 7] = ($type$)ic(i); 116 b.put(a, 7, n); 117 b.flip(); 118 } 119 120 private static void bulkPutBuffer($Type$Buffer b) { 121 int n = b.capacity(); 122 b.clear(); 123 $Type$Buffer c = $Type$Buffer.allocate(n + 7); 124 c.position(7); 125 for (int i = 0; i < n; i++) 126 c.put(($type$)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($Type$Buffer b) { 144 b.position(0); 145 b.mark(); 146 147 b.duplicate().reset(); 148 b.asReadOnlyBuffer().reset(); 149 } 150 151 #if[byte] 152 #else[byte] 153 // 6221101-6234263 154 155 private static void putBuffer() { 156 final int cap = 10; 157 158 $Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 159 $Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer(); 160 direct1.put(nondirect1); 161 162 $Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 163 $Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer(); 164 nondirect2.put(direct2); 165 166 $Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 167 $Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); 168 direct3.put(direct4); 169 170 $Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer(); 171 $Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer(); 172 nondirect3.put(nondirect4); 173 } 174 #end[byte] 175 176 #if[char] 177 178 private static void bulkPutString($Type$Buffer b) { 179 int n = b.capacity(); 180 b.clear(); 181 StringBuffer sb = new StringBuffer(n + 7); 182 sb.append("1234567"); 183 for (int i = 0; i < n; i++) 184 sb.append((char)ic(i)); 185 b.put(sb.toString(), 7, 7 + n); 186 b.flip(); 187 } 188 189 #end[char] 190 191 private static void checkSlice($Type$Buffer b, $Type$Buffer 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 #if[byte] 202 203 private static void checkBytes(ByteBuffer b, byte[] bs) { 204 int n = bs.length; 205 int p = b.position(); 206 byte v; 207 if (b.order() == ByteOrder.BIG_ENDIAN) { 208 for (int i = 0; i < n; i++) 209 ck(b, b.get(), bs[i]); 210 } else { 211 for (int i = n - 1; i >= 0; i--) 212 ck(b, b.get(), bs[i]); 213 } 214 b.position(p); 215 } 216 217 private static void compact(Buffer b) { 218 try { 219 Class<?> cl = b.getClass(); 220 Method m = cl.getDeclaredMethod("compact"); 221 m.setAccessible(true); 222 m.invoke(b); 223 } catch (Exception e) { 224 fail(e.getMessage(), b); 225 } 226 } 227 228 private static void checkInvalidMarkException(final Buffer b) { 229 tryCatch(b, InvalidMarkException.class, new Runnable() { 230 public void run() { 231 b.mark(); 232 compact(b); 233 b.reset(); 234 }}); 235 } 236 237 private static void testViews(int level, ByteBuffer b, boolean direct) { 238 239 ShortBuffer sb = b.asShortBuffer(); 240 BasicShort.test(level, sb, direct); 241 checkBytes(b, new byte[] { 0, (byte)ic(0) }); 242 checkInvalidMarkException(sb); 243 244 CharBuffer cb = b.asCharBuffer(); 245 BasicChar.test(level, cb, direct); 246 checkBytes(b, new byte[] { 0, (byte)ic(0) }); 247 checkInvalidMarkException(cb); 248 249 IntBuffer ib = b.asIntBuffer(); 250 BasicInt.test(level, ib, direct); 251 checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) }); 252 checkInvalidMarkException(ib); 253 254 LongBuffer lb = b.asLongBuffer(); 255 BasicLong.test(level, lb, direct); 256 checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) }); 257 checkInvalidMarkException(lb); 258 259 FloatBuffer fb = b.asFloatBuffer(); 260 BasicFloat.test(level, fb, direct); 261 checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 }); 262 checkInvalidMarkException(fb); 263 264 DoubleBuffer db = b.asDoubleBuffer(); 265 BasicDouble.test(level, db, direct); 266 checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 }); 267 checkInvalidMarkException(db); 268 } 269 270 private static void testHet(int level, ByteBuffer b) { 271 272 int p = b.position(); 273 b.limit(b.capacity()); 274 show(level, b); 275 out.print(" put:"); 276 277 b.putChar((char)1); 278 b.putChar((char)Character.MAX_VALUE); 279 out.print(" char"); 280 281 b.putShort((short)1); 282 b.putShort((short)Short.MAX_VALUE); 283 out.print(" short"); 284 285 b.putInt(1); 286 b.putInt(Integer.MAX_VALUE); 287 out.print(" int"); 288 289 b.putLong((long)1); 290 b.putLong((long)Long.MAX_VALUE); 291 out.print(" long"); 292 293 b.putFloat((float)1); 294 b.putFloat((float)Float.MIN_VALUE); 295 b.putFloat((float)Float.MAX_VALUE); 296 out.print(" float"); 297 298 b.putDouble((double)1); 299 b.putDouble((double)Double.MIN_VALUE); 300 b.putDouble((double)Double.MAX_VALUE); 301 out.print(" double"); 302 303 out.println(); 304 b.limit(b.position()); 305 b.position(p); 306 show(level, b); 307 out.print(" get:"); 308 309 ck(b, b.getChar(), 1); 310 ck(b, b.getChar(), Character.MAX_VALUE); 311 out.print(" char"); 312 313 ck(b, b.getShort(), 1); 314 ck(b, b.getShort(), Short.MAX_VALUE); 315 out.print(" short"); 316 317 ck(b, b.getInt(), 1); 318 ck(b, b.getInt(), Integer.MAX_VALUE); 319 out.print(" int"); 320 321 ck(b, b.getLong(), 1); 322 ck(b, b.getLong(), Long.MAX_VALUE); 323 out.print(" long"); 324 325 ck(b, (long)b.getFloat(), 1); 326 ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE); 327 ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE); 328 out.print(" float"); 329 330 ck(b, (long)b.getDouble(), 1); 331 ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE); 332 ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE); 333 out.print(" double"); 334 335 out.println(); 336 337 } 338 339 private static void testAlign(final ByteBuffer b, boolean direct) { 340 // index out-of bounds 341 tryCatch(b, IllegalArgumentException.class, () -> b.alignmentOffset(-1, (short) 1)); 342 343 // unit size values 344 tryCatch(b, IllegalArgumentException.class, () -> b.alignmentOffset(0, (short) 0)); 345 for (int us = 1; us < 65; us++) { 346 int _us = us; 347 if ((us & (us - 1)) != 0) { 348 // unit size not a power of two 349 tryCatch(b, IllegalArgumentException.class, () -> b.alignmentOffset(0, _us)); 350 } else { 351 if (direct || us <= 8) { 352 b.alignmentOffset(0, us); 353 } else { 354 // unit size > 8 with non-direct buffer 355 tryCatch(b, UnsupportedOperationException.class, () -> b.alignmentOffset(0, _us)); 356 } 357 } 358 } 359 360 // Probe for long misalignment at index zero for a newly created buffer 361 ByteBuffer empty = direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0); 362 int longMisalignmentAtZero = empty.alignmentOffset(0, 8); 363 364 if (direct) { 365 // Freshly created direct byte buffers should be aligned at index 0 366 // for ref and primitive values (see Unsafe.allocateMemory) 367 if (longMisalignmentAtZero != 0) 368 fail("Direct byte buffer misalligned at index 0 for ref and primitive values " + longMisalignmentAtZero); 369 } else { 370 // For heap byte buffers misalignment may occur on 32-bit systems 371 // where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0 372 // Note the GC will preserve alignment of the base address of the 373 // array 374 if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 != longMisalignmentAtZero) 375 fail("Heap byte buffer misalligned at index 0 for ref and primitive values " + longMisalignmentAtZero); 376 } 377 378 // Ensure test buffer is correctly aligned at index 0 379 if (b.alignmentOffset(0, 8) != longMisalignmentAtZero) 380 fail("Test input buffer not correctly aligned at index 0", b); 381 382 // Test misalignment values 383 for (int us : new int[]{1, 2, 4, 8}) { 384 for (int i = 0; i < us * 2; i++) { 385 int am = b.alignmentOffset(i, us); 386 int expectedAm = (longMisalignmentAtZero + i) % us; 387 388 if (am != expectedAm) 389 fail(String.format("b.alignmentOffset(%d, %d) == %d incorrect, expected %d", i, us, am, expectedAm)); 390 } 391 } 392 393 // Created aligned slice to test against 394 int ap = 8 - longMisalignmentAtZero; 395 int al = b.limit() - b.alignmentOffset(b.limit(), 8); 396 ByteBuffer ab = b.position(ap).limit(al). 397 slice(); 398 if (ab.limit() == 0) 399 fail("Test input buffer not sufficiently sized to cover an aligned region for all values", b); 400 if (ab.alignmentOffset(0, 8) != 0) 401 fail("Aligned test input buffer not correctly aligned at index 0", ab); 402 403 for (int us : new int[]{1, 2, 4, 8}) { 404 for (int p = 1; p < 16; p++) { 405 int l = ab.limit() - p; 406 407 ByteBuffer as = ab.slice().position(p).limit(l). 408 alignedSlice(us); 409 410 ck(as, 0, as.position()); 411 ck(as, as.capacity(), as.limit()); 412 if (b.isDirect() != as.isDirect()) 413 fail("Lost direction", as); 414 if (b.isReadOnly() != as.isReadOnly()) 415 fail("Lost read-only", as); 416 417 if (as.alignmentOffset(0, us) != 0) 418 fail("Buffer not correctly aligned at index 0", as); 419 420 if (as.alignmentOffset(as.limit(), us) != 0) 421 fail("Buffer not correctly aligned at limit", as); 422 423 int p_mod = ab.alignmentOffset(p, us); 424 int l_mod = ab.alignmentOffset(l, us); 425 // Round up position 426 p = (p_mod > 0) ? p + (us - p_mod) : p; 427 // Round down limit 428 l = l - l_mod; 429 430 int ec = l - p; 431 if (as.limit() != ec) 432 fail("Buffer capacity incorrect, expected: " + ec, as); 433 } 434 } 435 } 436 #end[byte] 437 438 private static void fail(String problem, 439 $Type$Buffer xb, $Type$Buffer yb, 440 $type$ x, $type$ 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($type$ [] t, Class<?> ex, Runnable thunk) { 460 tryCatch($Type$Buffer.wrap(t), ex, thunk); 461 } 462 463 public static void test(int level, final $Type$Buffer 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 #if[char] 489 490 bulkPutString(b); 491 relGet(b); 492 b.position(1); 493 b.limit(7); 494 ck(b, b.toString().equals("bcdefg")); 495 496 // CharSequence ops 497 498 b.position(2); 499 ck(b, b.charAt(1), 'd'); 500 CharBuffer c = b.subSequence(1, 4); 501 ck(c, c.capacity(), b.capacity()); 502 ck(c, c.position(), b.position()+1); 503 ck(c, c.limit(), b.position()+4); 504 ck(c, b.subSequence(1, 4).toString().equals("def")); 505 506 // 4938424 507 b.position(4); 508 ck(b, b.charAt(1), 'f'); 509 ck(b, b.subSequence(1, 3).toString().equals("fg")); 510 511 // String ops 512 513 // 7190219 514 b.clear(); 515 int pos = b.position(); 516 tryCatch(b, BufferOverflowException.class, new Runnable() { 517 public void run() { 518 b.put(String.valueOf(new char[b.capacity() + 1]), 0, 519 b.capacity() + 1); 520 }}); 521 ck(b, b.position(), pos); 522 relGet(b); 523 524 #end[char] 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(($type$)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(), ($type$)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(($type$)0); 617 b.put(($type$)-1); 618 b.put(($type$)1); 619 b.put($Fulltype$.MAX_VALUE); 620 b.put($Fulltype$.MIN_VALUE); 621 #if[float] 622 b.put(-Float.MAX_VALUE); 623 b.put(-Float.MIN_VALUE); 624 b.put(Float.NEGATIVE_INFINITY); 625 b.put(Float.POSITIVE_INFINITY); 626 b.put(Float.NaN); 627 b.put(0.91697687f); // Changes value if incorrectly swapped 628 #end[float] 629 #if[double] 630 b.put(-Double.MAX_VALUE); 631 b.put(-Double.MIN_VALUE); 632 b.put(Double.NEGATIVE_INFINITY); 633 b.put(Double.POSITIVE_INFINITY); 634 b.put(Double.NaN); 635 b.put(0.5121609353879392); // Changes value if incorrectly swapped 636 #end[double] 637 638 $type$ v; 639 b.flip(); 640 ck(b, b.get(), 0); 641 ck(b, b.get(), ($type$)-1); 642 ck(b, b.get(), 1); 643 ck(b, b.get(), $Fulltype$.MAX_VALUE); 644 ck(b, b.get(), $Fulltype$.MIN_VALUE); 645 646 #if[float] 647 ck(b, b.get(), -Float.MAX_VALUE); 648 ck(b, b.get(), -Float.MIN_VALUE); 649 ck(b, b.get(), Float.NEGATIVE_INFINITY); 650 ck(b, b.get(), Float.POSITIVE_INFINITY); 651 if (Float.floatToRawIntBits(v = b.get()) != 652 Float.floatToRawIntBits(Float.NaN)) 653 fail(b, (long)Float.NaN, (long)v); 654 ck(b, b.get(), 0.91697687f); 655 #end[float] 656 #if[double] 657 ck(b, b.get(), -Double.MAX_VALUE); 658 ck(b, b.get(), -Double.MIN_VALUE); 659 ck(b, b.get(), Double.NEGATIVE_INFINITY); 660 ck(b, b.get(), Double.POSITIVE_INFINITY); 661 if (Double.doubleToRawLongBits(v = b.get()) 662 != Double.doubleToRawLongBits(Double.NaN)) 663 fail(b, (long)Double.NaN, (long)v); 664 ck(b, b.get(), 0.5121609353879392); 665 #end[double] 666 667 668 // Comparison 669 b.rewind(); 670 $Type$Buffer b2 = $Type$Buffer.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 $type$ x = b.get(i); 678 $type$ y = b2.get(i); 679 if (x != y 680 #if[double] 681 || Double.compare(x, y) != 0 682 #end[double] 683 #if[float] 684 || Float.compare(x, y) != 0 685 #end[float] 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(($type$)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, ($type$)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 ($type$ x : VALUES) { 713 $Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { 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 ($type$ y : VALUES) { 721 $Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { 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) != $Fulltype$.compare(x, y)) { 731 #if[float] 732 if (x == 0.0 && y == 0.0) continue; 733 #end[float] 734 #if[double] 735 if (x == 0.0 && y == 0.0) continue; 736 #end[double] 737 fail("Incorrect results for $Type$Buffer.compareTo", 738 xb, yb, x, y); 739 } 740 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { 741 fail("Incorrect results for $Type$Buffer.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 $Type$Buffer sb = b.slice(); 761 checkSlice(b, sb); 762 b.position(0); 763 $Type$Buffer 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 #if[byte] 773 774 // Views 775 776 b.clear(); 777 b.order(ByteOrder.BIG_ENDIAN); 778 testViews(level + 1, b, direct); 779 780 for (int i = 1; i <= 9; i++) { 781 b.position(i); 782 show(level + 1, b); 783 testViews(level + 2, b, direct); 784 } 785 786 b.position(0); 787 b.order(ByteOrder.LITTLE_ENDIAN); 788 testViews(level + 1, b, direct); 789 790 // Heterogeneous accessors 791 792 b.order(ByteOrder.BIG_ENDIAN); 793 for (int i = 0; i <= 9; i++) { 794 b.position(i); 795 testHet(level + 1, b); 796 } 797 b.order(ByteOrder.LITTLE_ENDIAN); 798 b.position(3); 799 testHet(level + 1, b); 800 801 #end[byte] 802 803 // Read-only views 804 805 b.rewind(); 806 final $Type$Buffer 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($Type$Buffer) should not change source position 832 final $Type$Buffer src = $Type$Buffer.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 #if[byte] 845 846 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 847 public void run() { 848 rb.putChar((char)1); 849 }}); 850 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 851 public void run() { 852 rb.putChar(0, (char)1); 853 }}); 854 855 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 856 public void run() { 857 rb.putShort((short)1); 858 }}); 859 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 860 public void run() { 861 rb.putShort(0, (short)1); 862 }}); 863 864 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 865 public void run() { 866 rb.putInt(1); 867 }}); 868 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 869 public void run() { 870 rb.putInt(0, 1); 871 }}); 872 873 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 874 public void run() { 875 rb.putLong((long)1); 876 }}); 877 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 878 public void run() { 879 rb.putLong(0, (long)1); 880 }}); 881 882 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 883 public void run() { 884 rb.putFloat((float)1); 885 }}); 886 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 887 public void run() { 888 rb.putFloat(0, (float)1); 889 }}); 890 891 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 892 public void run() { 893 rb.putDouble((double)1); 894 }}); 895 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 896 public void run() { 897 rb.putDouble(0, (double)1); 898 }}); 899 900 #end[byte] 901 902 #if[char] 903 904 // 7199551 905 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 906 public void run() { 907 String s = new String(new char[rb.remaining() + 1]); 908 rb.put(s); 909 }}); 910 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 911 public void run() { 912 String s = new String(new char[rb.remaining() + 1]); 913 rb.append(s); 914 }}); 915 916 #end[char] 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 #if[byte] 943 // For byte buffers, test both the direct and non-direct cases 944 $Type$Buffer ob 945 = (b.isDirect() 946 ? $Type$Buffer.allocate(rb.capacity()) 947 : $Type$Buffer.allocateDirect(rb.capacity())); 948 rb.rewind(); 949 ob.put(rb); 950 #end[byte] 951 952 relPut(b); // Required by testViews 953 954 #if[byte] 955 // Test alignment 956 957 testAlign(b, direct); 958 #end[byte] 959 } 960 961 #if[char] 962 963 private static void testStr() { 964 final String s = "abcdefghijklm"; 965 int start = 3; 966 int end = 9; 967 final CharBuffer b = CharBuffer.wrap(s, start, end); 968 show(0, b); 969 ck(b, b.toString().equals(s.substring(start, end))); 970 ck(b, b.toString().equals("defghi")); 971 ck(b, b.isReadOnly()); 972 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { 973 public void run() { 974 b.put('x'); 975 }}); 976 ck(b, start, b.position()); 977 ck(b, end, b.limit()); 978 ck(b, s.length(), b.capacity()); 979 b.position(6); 980 ck(b, b.subSequence(0,3).toString().equals("ghi")); 981 982 // The index, relative to the position, must be non-negative and 983 // smaller than remaining(). 984 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 985 public void run() { 986 b.charAt(-1); 987 }}); 988 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 989 public void run() { 990 b.charAt(b.remaining()); 991 }}); 992 993 // The index must be non-negative and less than the buffer's limit. 994 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 995 public void run() { 996 b.get(b.limit()); 997 }}); 998 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 999 public void run() { 1000 b.get(-1); 1001 }}); 1002 1003 // The start must be non-negative and no larger than remaining(). 1004 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1005 public void run() { 1006 b.subSequence(-1, b.remaining()); 1007 }}); 1008 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1009 public void run() { 1010 b.subSequence(b.remaining() + 1, b.remaining()); 1011 }}); 1012 1013 // The end must be no smaller than start and no larger than 1014 // remaining(). 1015 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1016 public void run() { 1017 b.subSequence(2, 1); 1018 }}); 1019 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1020 public void run() { 1021 b.subSequence(0, b.remaining() + 1); 1022 }}); 1023 1024 // The offset must be non-negative and no larger than <array.length>. 1025 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1026 public void run() { 1027 $Type$Buffer.wrap(s, -1, s.length()); 1028 }}); 1029 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1030 public void run() { 1031 $Type$Buffer.wrap(s, s.length() + 1, s.length()); 1032 }}); 1033 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1034 public void run() { 1035 $Type$Buffer.wrap(s, 1, 0); 1036 }}); 1037 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { 1038 public void run() { 1039 $Type$Buffer.wrap(s, 0, s.length() + 1); 1040 }}); 1041 } 1042 1043 #end[char] 1044 1045 public static void test(final $type$ [] ba) { 1046 int offset = 47; 1047 int length = 900; 1048 final $Type$Buffer b = $Type$Buffer.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 $Type$Buffer.wrap(ba, -1, ba.length); 1058 }}); 1059 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1060 public void run() { 1061 $Type$Buffer.wrap(ba, ba.length + 1, ba.length); 1062 }}); 1063 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1064 public void run() { 1065 $Type$Buffer.wrap(ba, 0, -1); 1066 }}); 1067 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { 1068 public void run() { 1069 $Type$Buffer.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 $Type$Buffer.wrap(($type$ []) null, 0, 5); 1076 }}); 1077 tryCatch(ba, NullPointerException.class, new Runnable() { 1078 public void run() { 1079 $Type$Buffer.wrap(($type$ []) 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 $Type$Buffer.allocate(-1); 1088 }}); 1089 try { 1090 $Type$Buffer.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 #if[byte] 1098 tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() { 1099 public void run() { 1100 $Type$Buffer.allocateDirect(-1); 1101 }}); 1102 try { 1103 $Type$Buffer.allocateDirect(-1); 1104 } catch (IllegalArgumentException e) { 1105 if (e.getMessage() == null) { 1106 fail("Non-null IllegalArgumentException message expected for" 1107 + " attempt to allocate negative capacity direct buffer"); 1108 } 1109 } 1110 #end[byte] 1111 } 1112 1113 public static void test() { 1114 testAllocate(); 1115 test(0, $Type$Buffer.allocate(7 * 1024), false); 1116 test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false); 1117 test(new $type$[1024]); 1118 #if[byte] 1119 $Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024); 1120 for (b.position(0); b.position() < b.limit(); ) 1121 ck(b, b.get(), 0); 1122 test(0, b, true); 1123 #end[byte] 1124 #if[char] 1125 testStr(); 1126 #end[char] 1127 1128 callReset($Type$Buffer.allocate(10)); 1129 1130 #if[byte] 1131 #else[byte] 1132 putBuffer(); 1133 #end[byte] 1134 } 1135 1136 }