1 /*
   2  * Copyright (c) 2000, 2012, 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     }
 132 
 133     //6231529
 134     private static void callReset($Type$Buffer b) {
 135         b.position(0);
 136         b.mark();
 137 
 138         b.duplicate().reset();
 139         b.asReadOnlyBuffer().reset();
 140     }
 141 
 142 #if[byte]
 143 #else[byte]
 144     // 6221101-6234263
 145 
 146     private static void putBuffer() {
 147         final int cap = 10;
 148 
 149         $Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
 150         $Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer();
 151         direct1.put(nondirect1);
 152 
 153         $Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
 154         $Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer();
 155         nondirect2.put(direct2);
 156 
 157         $Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
 158         $Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
 159         direct3.put(direct4);
 160 
 161         $Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer();
 162         $Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer();
 163         nondirect3.put(nondirect4);
 164     }
 165 #end[byte]
 166 
 167 #if[char]
 168 
 169     private static void bulkPutString($Type$Buffer b) {
 170         int n = b.capacity();
 171         b.clear();
 172         StringBuffer sb = new StringBuffer(n + 7);
 173         sb.append("1234567");
 174         for (int i = 0; i < n; i++)
 175             sb.append((char)ic(i));
 176         b.put(sb.toString(), 7, 7 + n);
 177         b.flip();
 178     }
 179 
 180 #end[char]
 181 
 182     private static void checkSlice($Type$Buffer b, $Type$Buffer slice) {
 183         ck(slice, 0, slice.position());
 184         ck(slice, b.remaining(), slice.limit());
 185         ck(slice, b.remaining(), slice.capacity());
 186         if (b.isDirect() != slice.isDirect())
 187             fail("Lost direction", slice);
 188         if (b.isReadOnly() != slice.isReadOnly())
 189             fail("Lost read-only", slice);
 190     }
 191 
 192 #if[byte]
 193 
 194     private static void checkBytes(ByteBuffer b, byte[] bs) {
 195         int n = bs.length;
 196         int p = b.position();
 197         byte v;
 198         if (b.order() == ByteOrder.BIG_ENDIAN) {
 199             for (int i = 0; i < n; i++)
 200                 ck(b, b.get(), bs[i]);
 201         } else {
 202             for (int i = n - 1; i >= 0; i--)
 203                 ck(b, b.get(), bs[i]);
 204         }
 205         b.position(p);
 206     }
 207 
 208     private static void compact(Buffer b) {
 209         try {
 210             Class<?> cl = b.getClass();
 211             Method m = cl.getDeclaredMethod("compact");
 212             m.setAccessible(true);
 213             m.invoke(b);
 214         } catch (Exception e) {
 215             fail(e.getMessage(), b);
 216         }
 217     }
 218 
 219     private static void checkInvalidMarkException(final Buffer b) {
 220         tryCatch(b, InvalidMarkException.class, new Runnable() {
 221             public void run() {
 222                 b.mark();
 223                 compact(b);
 224                 b.reset();
 225             }});
 226     }
 227 
 228     private static void testViews(int level, ByteBuffer b, boolean direct) {
 229 
 230         ShortBuffer sb = b.asShortBuffer();
 231         BasicShort.test(level, sb, direct);
 232         checkBytes(b, new byte[] { 0, (byte)ic(0) });
 233         checkInvalidMarkException(sb);
 234 
 235         CharBuffer cb = b.asCharBuffer();
 236         BasicChar.test(level, cb, direct);
 237         checkBytes(b, new byte[] { 0, (byte)ic(0) });
 238         checkInvalidMarkException(cb);
 239 
 240         IntBuffer ib = b.asIntBuffer();
 241         BasicInt.test(level, ib, direct);
 242         checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
 243         checkInvalidMarkException(ib);
 244 
 245         LongBuffer lb = b.asLongBuffer();
 246         BasicLong.test(level, lb, direct);
 247         checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
 248         checkInvalidMarkException(lb);
 249 
 250         FloatBuffer fb = b.asFloatBuffer();
 251         BasicFloat.test(level, fb, direct);
 252         checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
 253         checkInvalidMarkException(fb);
 254 
 255         DoubleBuffer db = b.asDoubleBuffer();
 256         BasicDouble.test(level, db, direct);
 257         checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
 258         checkInvalidMarkException(db);
 259     }
 260 
 261     private static void testHet(int level, ByteBuffer b) {
 262 
 263         int p = b.position();
 264         b.limit(b.capacity());
 265         show(level, b);
 266         out.print("    put:");
 267 
 268         b.putChar((char)1);
 269         b.putChar((char)Character.MAX_VALUE);
 270         out.print(" char");
 271 
 272         b.putShort((short)1);
 273         b.putShort((short)Short.MAX_VALUE);
 274         out.print(" short");
 275 
 276         b.putInt(1);
 277         b.putInt(Integer.MAX_VALUE);
 278         out.print(" int");
 279 
 280         b.putLong((long)1);
 281         b.putLong((long)Long.MAX_VALUE);
 282         out.print(" long");
 283 
 284         b.putFloat((float)1);
 285         b.putFloat((float)Float.MIN_VALUE);
 286         b.putFloat((float)Float.MAX_VALUE);
 287         out.print(" float");
 288 
 289         b.putDouble((double)1);
 290         b.putDouble((double)Double.MIN_VALUE);
 291         b.putDouble((double)Double.MAX_VALUE);
 292         out.print(" double");
 293 
 294         out.println();
 295         b.limit(b.position());
 296         b.position(p);
 297         show(level, b);
 298         out.print("    get:");
 299 
 300         ck(b, b.getChar(), 1);
 301         ck(b, b.getChar(), Character.MAX_VALUE);
 302         out.print(" char");
 303 
 304         ck(b, b.getShort(), 1);
 305         ck(b, b.getShort(), Short.MAX_VALUE);
 306         out.print(" short");
 307 
 308         ck(b, b.getInt(), 1);
 309         ck(b, b.getInt(), Integer.MAX_VALUE);
 310         out.print(" int");
 311 
 312         ck(b, b.getLong(), 1);
 313         ck(b, b.getLong(), Long.MAX_VALUE);
 314         out.print(" long");
 315 
 316         ck(b, (long)b.getFloat(), 1);
 317         ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE);
 318         ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE);
 319         out.print(" float");
 320 
 321         ck(b, (long)b.getDouble(), 1);
 322         ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE);
 323         ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE);
 324         out.print(" double");
 325 
 326         out.println();
 327 
 328     }
 329 
 330 #end[byte]
 331 
 332     private static void fail(String problem,
 333                              $Type$Buffer xb, $Type$Buffer yb,
 334                              $type$ x, $type$ y) {
 335         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 336     }
 337 
 338     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 339         boolean caught = false;
 340         try {
 341             thunk.run();
 342         } catch (Throwable x) {
 343             if (ex.isAssignableFrom(x.getClass())) {
 344                 caught = true;
 345             } else {
 346                 fail(x.getMessage() + " not expected");
 347             }
 348         }
 349         if (!caught)
 350             fail(ex.getName() + " not thrown", b);
 351     }
 352 
 353     private static void tryCatch($type$ [] t, Class<?> ex, Runnable thunk) {
 354         tryCatch($Type$Buffer.wrap(t), ex, thunk);
 355     }
 356 
 357     public static void test(int level, final $Type$Buffer b, boolean direct) {
 358 
 359         show(level, b);
 360 
 361         if (direct != b.isDirect())
 362             fail("Wrong direction", b);
 363 
 364         // Gets and puts
 365 
 366         relPut(b);
 367         relGet(b);
 368         absGet(b);
 369         bulkGet(b);
 370 
 371         absPut(b);
 372         relGet(b);
 373         absGet(b);
 374         bulkGet(b);
 375 
 376         bulkPutArray(b);
 377         relGet(b);
 378 
 379         bulkPutBuffer(b);
 380         relGet(b);
 381 
 382 #if[char]
 383 
 384         bulkPutString(b);
 385         relGet(b);
 386         b.position(1);
 387         b.limit(7);
 388         ck(b, b.toString().equals("bcdefg"));
 389 
 390         // CharSequence ops
 391 
 392         b.position(2);
 393         ck(b, b.charAt(1), 'd');
 394         CharBuffer c = b.subSequence(1, 4);
 395         ck(c, c.capacity(), b.capacity());
 396         ck(c, c.position(), b.position()+1);
 397         ck(c, c.limit(), b.position()+4);
 398         ck(c, b.subSequence(1, 4).toString().equals("def"));
 399 
 400         // 4938424
 401         b.position(4);
 402         ck(b, b.charAt(1), 'f');
 403         ck(b, b.subSequence(1, 3).toString().equals("fg"));
 404 
 405         // String ops
 406 
 407         // 7190219
 408         b.clear();
 409         int pos = b.position();
 410         tryCatch(b, BufferOverflowException.class, new Runnable() {
 411             public void run() {
 412                 b.put(String.valueOf(new char[b.capacity() + 1]), 0,
 413                         b.capacity() + 1);
 414             }});
 415         ck(b, b.position(), pos);
 416         relGet(b);
 417 
 418 #end[char]
 419 
 420         // Compact
 421 
 422         relPut(b);
 423         b.position(13);
 424         b.compact();
 425         b.flip();
 426         relGet(b, 13);
 427 
 428         // Exceptions
 429 
 430         relPut(b);
 431         b.limit(b.capacity() / 2);
 432         b.position(b.limit());
 433 
 434         tryCatch(b, BufferUnderflowException.class, new Runnable() {
 435                 public void run() {
 436                     b.get();
 437                 }});
 438 
 439         tryCatch(b, BufferOverflowException.class, new Runnable() {
 440                 public void run() {
 441                     b.put(($type$)42);
 442                 }});
 443 
 444         // The index must be non-negative and lesss than the buffer's limit.
 445         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 446                 public void run() {
 447                     b.get(b.limit());
 448                 }});
 449         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 450                 public void run() {
 451                     b.get(-1);
 452                 }});
 453 
 454         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 455                 public void run() {
 456                     b.put(b.limit(), ($type$)42);
 457                 }});
 458 
 459         tryCatch(b, InvalidMarkException.class, new Runnable() {
 460                 public void run() {
 461                     b.position(0);
 462                     b.mark();
 463                     b.compact();
 464                     b.reset();
 465                 }});
 466 
 467         // Values
 468 
 469         b.clear();
 470         b.put(($type$)0);
 471         b.put(($type$)-1);
 472         b.put(($type$)1);
 473         b.put($Fulltype$.MAX_VALUE);
 474         b.put($Fulltype$.MIN_VALUE);
 475 #if[float]
 476         b.put(-Float.MAX_VALUE);
 477         b.put(-Float.MIN_VALUE);
 478         b.put(Float.NEGATIVE_INFINITY);
 479         b.put(Float.POSITIVE_INFINITY);
 480         b.put(Float.NaN);
 481         b.put(0.91697687f);             // Changes value if incorrectly swapped
 482 #end[float]
 483 #if[double]
 484         b.put(-Double.MAX_VALUE);
 485         b.put(-Double.MIN_VALUE);
 486         b.put(Double.NEGATIVE_INFINITY);
 487         b.put(Double.POSITIVE_INFINITY);
 488         b.put(Double.NaN);
 489         b.put(0.5121609353879392);      // Changes value if incorrectly swapped
 490 #end[double]
 491 
 492         $type$ v;
 493         b.flip();
 494         ck(b, b.get(), 0);
 495         ck(b, b.get(), ($type$)-1);
 496         ck(b, b.get(), 1);
 497         ck(b, b.get(), $Fulltype$.MAX_VALUE);
 498         ck(b, b.get(), $Fulltype$.MIN_VALUE);
 499 
 500 #if[float]
 501         ck(b, b.get(), -Float.MAX_VALUE);
 502         ck(b, b.get(), -Float.MIN_VALUE);
 503         ck(b, b.get(), Float.NEGATIVE_INFINITY);
 504         ck(b, b.get(), Float.POSITIVE_INFINITY);
 505         if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN))
 506             fail(b, (long)Float.NaN, (long)v);
 507         ck(b, b.get(), 0.91697687f);
 508 #end[float]
 509 #if[double]
 510         ck(b, b.get(), -Double.MAX_VALUE);
 511         ck(b, b.get(), -Double.MIN_VALUE);
 512         ck(b, b.get(), Double.NEGATIVE_INFINITY);
 513         ck(b, b.get(), Double.POSITIVE_INFINITY);
 514         if (Double.doubleToRawLongBits(v = b.get())
 515             != Double.doubleToRawLongBits(Double.NaN))
 516             fail(b, (long)Double.NaN, (long)v);
 517         ck(b, b.get(), 0.5121609353879392);
 518 #end[double]
 519 
 520 
 521         // Comparison
 522         b.rewind();
 523         $Type$Buffer b2 = $Type$Buffer.allocate(b.capacity());
 524         b2.put(b);
 525         b2.flip();
 526         b.position(2);
 527         b2.position(2);
 528         if (!b.equals(b2)) {
 529             for (int i = 2; i < b.limit(); i++) {
 530                 $type$ x = b.get(i);
 531                 $type$ y = b2.get(i);
 532                 if (x != y
 533 #if[double]
 534                     || Double.compare(x, y) != 0
 535 #end[double]
 536 #if[float]
 537                     || Float.compare(x, y) != 0
 538 #end[float]
 539                     )
 540                     out.println("[" + i + "] " + x + " != " + y);
 541             }
 542             fail("Identical buffers not equal", b, b2);
 543         }
 544         if (b.compareTo(b2) != 0)
 545             fail("Comparison to identical buffer != 0", b, b2);
 546 
 547         b.limit(b.limit() + 1);
 548         b.position(b.limit() - 1);
 549         b.put(($type$)99);
 550         b.rewind();
 551         b2.rewind();
 552         if (b.equals(b2))
 553             fail("Non-identical buffers equal", b, b2);
 554         if (b.compareTo(b2) <= 0)
 555             fail("Comparison to shorter buffer <= 0", b, b2);
 556         b.limit(b.limit() - 1);
 557 
 558         b.put(2, ($type$)42);
 559         if (b.equals(b2))
 560             fail("Non-identical buffers equal", b, b2);
 561         if (b.compareTo(b2) <= 0)
 562             fail("Comparison to lesser buffer <= 0", b, b2);
 563 
 564         // Check equals and compareTo with interesting values
 565         for ($type$ x : VALUES) {
 566             $Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { x });
 567             if (xb.compareTo(xb) != 0) {
 568                 fail("compareTo not reflexive", xb, xb, x, x);
 569             }
 570             if (! xb.equals(xb)) {
 571                 fail("equals not reflexive", xb, xb, x, x);
 572             }
 573             for ($type$ y : VALUES) {
 574                 $Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { y });
 575                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
 576                     fail("compareTo not anti-symmetric",
 577                          xb, yb, x, y);
 578                 }
 579                 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
 580                     fail("compareTo inconsistent with equals",
 581                          xb, yb, x, y);
 582                 }
 583                 if (xb.compareTo(yb) != $Fulltype$.compare(x, y)) {
 584 #if[float]
 585                     if (x == 0.0 && y == 0.0) continue;
 586 #end[float]
 587 #if[double]
 588                     if (x == 0.0 && y == 0.0) continue;
 589 #end[double]
 590                     fail("Incorrect results for $Type$Buffer.compareTo",
 591                          xb, yb, x, y);
 592                 }
 593                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
 594                     fail("Incorrect results for $Type$Buffer.equals",
 595                          xb, yb, x, y);
 596                 }
 597             }
 598         }
 599 
 600         // Sub, dup
 601 
 602         relPut(b);
 603         relGet(b.duplicate());
 604         b.position(13);
 605         relGet(b.duplicate(), 13);
 606         relGet(b.duplicate().slice(), 13);
 607         relGet(b.slice(), 13);
 608         relGet(b.slice().duplicate(), 13);
 609 
 610         // Slice
 611 
 612         b.position(5);
 613         $Type$Buffer sb = b.slice();
 614         checkSlice(b, sb);
 615         b.position(0);
 616         $Type$Buffer sb2 = sb.slice();
 617         checkSlice(sb, sb2);
 618 
 619         if (!sb.equals(sb2))
 620             fail("Sliced slices do not match", sb, sb2);
 621         if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
 622             fail("Array offsets do not match: "
 623                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
 624 
 625 #if[byte]
 626 
 627         // Views
 628 
 629         b.clear();
 630         b.order(ByteOrder.BIG_ENDIAN);
 631         testViews(level + 1, b, direct);
 632 
 633         for (int i = 1; i <= 9; i++) {
 634             b.position(i);
 635             show(level + 1, b);
 636             testViews(level + 2, b, direct);
 637         }
 638 
 639         b.position(0);
 640         b.order(ByteOrder.LITTLE_ENDIAN);
 641         testViews(level + 1, b, direct);
 642 
 643         // Heterogeneous accessors
 644 
 645         b.order(ByteOrder.BIG_ENDIAN);
 646         for (int i = 0; i <= 9; i++) {
 647             b.position(i);
 648             testHet(level + 1, b);
 649         }
 650         b.order(ByteOrder.LITTLE_ENDIAN);
 651         b.position(3);
 652         testHet(level + 1, b);
 653 
 654 #end[byte]
 655 
 656         // Read-only views
 657 
 658         b.rewind();
 659         final $Type$Buffer rb = b.asReadOnlyBuffer();
 660         if (!b.equals(rb))
 661             fail("Buffer not equal to read-only view", b, rb);
 662         show(level + 1, rb);
 663 
 664         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 665                 public void run() {
 666                     relPut(rb);
 667                 }});
 668 
 669         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 670                 public void run() {
 671                     absPut(rb);
 672                 }});
 673 
 674         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 675                 public void run() {
 676                     bulkPutArray(rb);
 677                 }});
 678 
 679         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 680                 public void run() {
 681                     bulkPutBuffer(rb);
 682                 }});
 683 
 684         // put($Type$Buffer) should not change source position
 685         final $Type$Buffer src = $Type$Buffer.allocate(1);
 686         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 687                 public void run() {
 688                     rb.put(src);
 689                  }});
 690         ck(src, src.position(), 0);
 691 
 692         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 693                 public void run() {
 694                     rb.compact();
 695                 }});
 696 
 697 #if[byte]
 698 
 699         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 700                 public void run() {
 701                     rb.putChar((char)1);
 702                 }});
 703         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 704                 public void run() {
 705                     rb.putChar(0, (char)1);
 706                 }});
 707 
 708         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 709                 public void run() {
 710                     rb.putShort((short)1);
 711                 }});
 712         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 713                 public void run() {
 714                     rb.putShort(0, (short)1);
 715                 }});
 716 
 717         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 718                 public void run() {
 719                     rb.putInt(1);
 720                 }});
 721         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 722                 public void run() {
 723                     rb.putInt(0, 1);
 724                 }});
 725 
 726         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 727                 public void run() {
 728                     rb.putLong((long)1);
 729                 }});
 730         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 731                 public void run() {
 732                     rb.putLong(0, (long)1);
 733                 }});
 734 
 735         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 736                 public void run() {
 737                     rb.putFloat((float)1);
 738                 }});
 739         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 740                 public void run() {
 741                     rb.putFloat(0, (float)1);
 742                 }});
 743 
 744         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 745                 public void run() {
 746                     rb.putDouble((double)1);
 747                 }});
 748         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 749                 public void run() {
 750                     rb.putDouble(0, (double)1);
 751                 }});
 752 
 753 #end[byte]
 754 
 755 #if[char]
 756 
 757         // 7199551
 758         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 759             public void run() {
 760                 String s = new String(new char[rb.remaining() + 1]);
 761                 rb.put(s);
 762             }});
 763         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 764             public void run() {
 765                 String s = new String(new char[rb.remaining() + 1]);
 766                 rb.append(s);
 767             }});
 768 
 769 #end[char]
 770 
 771         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 772 
 773             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 774                     public void run() {
 775                         rb.array();
 776                     }});
 777 
 778             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 779                     public void run() {
 780                         rb.arrayOffset();
 781                     }});
 782 
 783             if (rb.hasArray())
 784                 fail("Read-only heap buffer's backing array is accessible",
 785                      rb);
 786 
 787         }
 788 
 789         // Bulk puts from read-only buffers
 790 
 791         b.clear();
 792         rb.rewind();
 793         b.put(rb);
 794 
 795 #if[byte]
 796         // For byte buffers, test both the direct and non-direct cases
 797         $Type$Buffer ob
 798             = (b.isDirect()
 799                ? $Type$Buffer.allocate(rb.capacity())
 800                : $Type$Buffer.allocateDirect(rb.capacity()));
 801         rb.rewind();
 802         ob.put(rb);
 803 #end[byte]
 804 
 805         relPut(b);                       // Required by testViews
 806 
 807     }
 808 
 809 #if[char]
 810 
 811     private static void testStr() {
 812         final String s = "abcdefghijklm";
 813         int start = 3;
 814         int end = 9;
 815         final CharBuffer b = CharBuffer.wrap(s, start, end);
 816         show(0, b);
 817         ck(b, b.toString().equals(s.substring(start, end)));
 818         ck(b, b.toString().equals("defghi"));
 819         ck(b, b.isReadOnly());
 820         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 821                 public void run() {
 822                     b.put('x');
 823                 }});
 824         ck(b, start, b.position());
 825         ck(b, end, b.limit());
 826         ck(b, s.length(), b.capacity());
 827         b.position(6);
 828         ck(b, b.subSequence(0,3).toString().equals("ghi"));
 829 
 830         // The index, relative to the position, must be non-negative and
 831         // smaller than remaining().
 832         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 833                 public void run() {
 834                     b.charAt(-1);
 835                 }});
 836         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 837                 public void run() {
 838                     b.charAt(b.remaining());
 839                 }});
 840 
 841         // The index must be non-negative and less than the buffer's limit.
 842         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 843                 public void run() {
 844                     b.get(b.limit());
 845                 }});
 846         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 847                 public void run() {
 848                     b.get(-1);
 849                 }});
 850 
 851         // The start must be non-negative and no larger than remaining().
 852         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 853                 public void run() {
 854                     b.subSequence(-1, b.remaining());
 855                 }});
 856         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 857                 public void run() {
 858                     b.subSequence(b.remaining() + 1, b.remaining());
 859                 }});
 860 
 861         // The end must be no smaller than start and no larger than
 862         // remaining().
 863         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 864                 public void run() {
 865                     b.subSequence(2, 1);
 866                 }});
 867         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 868                 public void run() {
 869                     b.subSequence(0, b.remaining() + 1);
 870                 }});
 871 
 872         // The offset must be non-negative and no larger than <array.length>.
 873         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 874                 public void run() {
 875                     $Type$Buffer.wrap(s, -1, s.length());
 876                 }});
 877         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 878                 public void run() {
 879                     $Type$Buffer.wrap(s, s.length() + 1, s.length());
 880                 }});
 881         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 882                 public void run() {
 883                     $Type$Buffer.wrap(s, 1, 0);
 884                 }});
 885         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 886                 public void run() {
 887                     $Type$Buffer.wrap(s, 0, s.length() + 1);
 888                 }});
 889     }
 890 
 891 #end[char]
 892 
 893     public static void test(final $type$ [] ba) {
 894         int offset = 47;
 895         int length = 900;
 896         final $Type$Buffer b = $Type$Buffer.wrap(ba, offset, length);
 897         show(0, b);
 898         ck(b, b.capacity(), ba.length);
 899         ck(b, b.position(), offset);
 900         ck(b, b.limit(), offset + length);
 901 
 902         // The offset must be non-negative and no larger than <array.length>.
 903         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 904                 public void run() {
 905                     $Type$Buffer.wrap(ba, -1, ba.length);
 906                 }});
 907         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 908                 public void run() {
 909                     $Type$Buffer.wrap(ba, ba.length + 1, ba.length);
 910                 }});
 911         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 912                 public void run() {
 913                     $Type$Buffer.wrap(ba, 0, -1);
 914                 }});
 915         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 916                 public void run() {
 917                     $Type$Buffer.wrap(ba, 0, ba.length + 1);
 918                 }});
 919 
 920         // A NullPointerException will be thrown if the array is null.
 921         tryCatch(ba, NullPointerException.class, new Runnable() {
 922                 public void run() {
 923                     $Type$Buffer.wrap(($type$ []) null, 0, 5);
 924                 }});
 925         tryCatch(ba, NullPointerException.class, new Runnable() {
 926                 public void run() {
 927                     $Type$Buffer.wrap(($type$ []) null);
 928                 }});
 929     }
 930 
 931     private static void testAllocate() {
 932         // An IllegalArgumentException will be thrown for negative capacities.
 933         tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
 934                 public void run() {
 935                     $Type$Buffer.allocate(-1);
 936                 }});
 937 #if[byte]
 938         tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
 939                 public void run() {
 940                     $Type$Buffer.allocateDirect(-1);
 941                 }});
 942 #end[byte]
 943     }
 944 
 945     public static void test() {
 946         testAllocate();
 947         test(0, $Type$Buffer.allocate(7 * 1024), false);
 948         test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false);
 949         test(new $type$[1024]);
 950 #if[byte]
 951         $Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024);
 952         for (b.position(0); b.position() < b.limit(); )
 953             ck(b, b.get(), 0);
 954         test(0, b, true);
 955 #end[byte]
 956 #if[char]
 957         testStr();
 958 #end[char]
 959 
 960         callReset($Type$Buffer.allocate(10));
 961 
 962 #if[byte]
 963 #else[byte]
 964         putBuffer();
 965 #end[byte]
 966     }
 967 
 968 }