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