< prev index next >

test/jdk/java/nio/Buffer/BasicChar.java

Print this page


   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  */


  70             ck(b, (long)b.get(), (long)((char)ic(i)));
  71         b.rewind();
  72     }
  73 
  74     private static void absGet(CharBuffer b) {
  75         int n = b.capacity();
  76         for (int i = 0; i < n; i++)
  77             ck(b, (long)b.get(), (long)((char)ic(i)));
  78         b.rewind();
  79     }
  80 
  81     private static void bulkGet(CharBuffer b) {
  82         int n = b.capacity();
  83         char[] a = new char[n + 7];
  84         b.get(a, 7, n);
  85         for (int i = 0; i < n; i++) {
  86             ck(b, (long)a[i + 7], (long)((char)ic(i)));
  87         }
  88     }
  89 












  90     private static void relPut(CharBuffer b) {
  91         int n = b.capacity();
  92         b.clear();
  93         for (int i = 0; i < n; i++)
  94             b.put((char)ic(i));
  95         b.flip();
  96     }
  97 
  98     private static void absPut(CharBuffer b) {
  99         int n = b.capacity();
 100         b.clear();
 101         for (int i = 0; i < n; i++)
 102             b.put(i, (char)ic(i));
 103         b.limit(n);
 104         b.position(0);
 105     }
 106 
 107     private static void bulkPutArray(CharBuffer b) {
 108         int n = b.capacity();
 109         b.clear();


 119         b.clear();
 120         CharBuffer c = CharBuffer.allocate(n + 7);
 121         c.position(7);
 122         for (int i = 0; i < n; i++)
 123             c.put((char)ic(i));
 124         c.flip();
 125         c.position(7);
 126         b.put(c);
 127         b.flip();
 128         try {
 129             b.put(b);
 130             fail("IllegalArgumentException expected for put into same buffer");
 131         } catch (IllegalArgumentException e) {
 132             if (e.getMessage() == null) {
 133                 fail("Non-null IllegalArgumentException message expected from"
 134                      + " put into same buffer");
 135             }
 136         }
 137     }
 138 









































 139     //6231529
 140     private static void callReset(CharBuffer b) {
 141         b.position(0);
 142         b.mark();
 143 
 144         b.duplicate().reset();
 145         b.asReadOnlyBuffer().reset();
 146     }
 147 
 148 
 149 
 150     // 6221101-6234263
 151 
 152     private static void putBuffer() {
 153         final int cap = 10;
 154 
 155         CharBuffer direct1 = ByteBuffer.allocateDirect(cap).asCharBuffer();
 156         CharBuffer nondirect1 = ByteBuffer.allocate(cap).asCharBuffer();
 157         direct1.put(nondirect1);
 158 


 435 
 436 
 437 
 438 
 439 
 440 
 441 
 442 
 443 
 444 
 445 
 446 
 447 
 448 
 449     private static void fail(String problem,
 450                              CharBuffer xb, CharBuffer yb,
 451                              char x, char y) {
 452         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 453     }
 454 




 455     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
 456         tryCatch(b, IllegalArgumentException.class, thunk);
 457     }
 458 
 459     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
 460         tryCatch(b, ReadOnlyBufferException.class, thunk);
 461     }
 462 
 463     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
 464         tryCatch(b, IndexOutOfBoundsException.class, thunk);
 465     }
 466 
 467     private static void catchIndexOutOfBounds(char[] t, Runnable thunk) {
 468         tryCatch(t, IndexOutOfBoundsException.class, thunk);
 469     }
 470 
 471     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 472         boolean caught = false;
 473         try {
 474             thunk.run();
 475         } catch (Throwable x) {
 476             if (ex.isAssignableFrom(x.getClass())) {
 477                 caught = true;
 478             } else {
 479                 fail(x.getMessage() + " not expected");



 480             }
 481         }
 482         if (!caught) {
 483             fail(ex.getName() + " not thrown", b);
 484         }
 485     }
 486 
 487     private static void tryCatch(char[] t, Class<?> ex, Runnable thunk) {
 488         tryCatch(CharBuffer.wrap(t), ex, thunk);
 489     }
 490 
 491     public static void test(int level, final CharBuffer b, boolean direct) {
 492 
 493         show(level, b);
 494 
 495         if (direct != b.isDirect())
 496             fail("Wrong direction", b);
 497 
 498         // Gets and puts
 499 
 500         relPut(b);
 501         relGet(b);
 502         absGet(b);
 503         bulkGet(b);
 504 
 505         absPut(b);
 506         relGet(b);
 507         absGet(b);
 508         bulkGet(b);
 509 
 510         bulkPutArray(b);
 511         relGet(b);
 512 
 513         bulkPutBuffer(b);
 514         relGet(b);
 515 









 516 
 517 
 518         bulkPutString(b);
 519         relGet(b);
 520         b.position(1);
 521         b.limit(7);
 522         ck(b, b.toString().equals("bcdefg"));
 523 
 524         // CharSequence ops
 525 
 526         b.position(2);
 527         ck(b, b.charAt(1), 'd');
 528         CharBuffer c = b.subSequence(1, 4);
 529         ck(c, c.capacity(), b.capacity());
 530         ck(c, c.position(), b.position()+1);
 531         ck(c, c.limit(), b.position()+4);
 532         ck(c, b.subSequence(1, 4).toString().equals("def"));
 533 
 534         // 4938424
 535         b.position(4);


 595         try {
 596             b.limit(b.capacity() + 1);
 597             fail("IllegalArgumentException expected for limit beyond capacity");
 598         } catch (IllegalArgumentException e) {
 599             if (e.getMessage() == null) {
 600                 fail("Non-null IllegalArgumentException message expected for"
 601                      + " limit beyond capacity");
 602             }
 603         }
 604 
 605         try {
 606             b.limit(-1);
 607             fail("IllegalArgumentException expected for negative limit");
 608         } catch (IllegalArgumentException e) {
 609             if (e.getMessage() == null) {
 610                 fail("Non-null IllegalArgumentException message expected for"
 611                      + " negative limit");
 612             }
 613         }
 614 































 615         // Values
 616 
 617         b.clear();
 618         b.put((char)0);
 619         b.put((char)-1);
 620         b.put((char)1);
 621         b.put(Character.MAX_VALUE);
 622         b.put(Character.MIN_VALUE);
 623 
 624 
 625 
 626 
 627 
 628 
 629 
 630 
 631 
 632 
 633 
 634 


 802 
 803 
 804 
 805 
 806 
 807 
 808 
 809 
 810         // Read-only views
 811 
 812         b.rewind();
 813         final CharBuffer rb = b.asReadOnlyBuffer();
 814         if (!b.equals(rb))
 815             fail("Buffer not equal to read-only view", b, rb);
 816         show(level + 1, rb);
 817 
 818         catchReadOnlyBuffer(b, () -> relPut(rb));
 819         catchReadOnlyBuffer(b, () -> absPut(rb));
 820         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
 821         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));


 822 
 823         // put(CharBuffer) should not change source position
 824         final CharBuffer src = CharBuffer.allocate(1);
 825         catchReadOnlyBuffer(b, () -> rb.put(src));
 826         ck(src, src.position(), 0);
 827 
 828         catchReadOnlyBuffer(b, () -> rb.compact());
 829 
 830 
 831 
 832 
 833 
 834 
 835 
 836 
 837 
 838 
 839 
 840 
 841 


   1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  70             ck(b, (long)b.get(), (long)((char)ic(i)));
  71         b.rewind();
  72     }
  73 
  74     private static void absGet(CharBuffer b) {
  75         int n = b.capacity();
  76         for (int i = 0; i < n; i++)
  77             ck(b, (long)b.get(), (long)((char)ic(i)));
  78         b.rewind();
  79     }
  80 
  81     private static void bulkGet(CharBuffer b) {
  82         int n = b.capacity();
  83         char[] a = new char[n + 7];
  84         b.get(a, 7, n);
  85         for (int i = 0; i < n; i++) {
  86             ck(b, (long)a[i + 7], (long)((char)ic(i)));
  87         }
  88     }
  89 
  90     private static void absBulkGet(CharBuffer b) {
  91         int n = b.capacity();
  92         int len = n - 7*2;
  93         char[] a = new char[n + 7];
  94         b.position(42);
  95         b.get(7, a, 7, len);
  96         ck(b, b.position() == 42);
  97         for (int i = 0; i < len; i++) {
  98             ck(b, (long)a[i + 7], (long)((char)ic(i)));
  99         }
 100     }
 101 
 102     private static void relPut(CharBuffer b) {
 103         int n = b.capacity();
 104         b.clear();
 105         for (int i = 0; i < n; i++)
 106             b.put((char)ic(i));
 107         b.flip();
 108     }
 109 
 110     private static void absPut(CharBuffer b) {
 111         int n = b.capacity();
 112         b.clear();
 113         for (int i = 0; i < n; i++)
 114             b.put(i, (char)ic(i));
 115         b.limit(n);
 116         b.position(0);
 117     }
 118 
 119     private static void bulkPutArray(CharBuffer b) {
 120         int n = b.capacity();
 121         b.clear();


 131         b.clear();
 132         CharBuffer c = CharBuffer.allocate(n + 7);
 133         c.position(7);
 134         for (int i = 0; i < n; i++)
 135             c.put((char)ic(i));
 136         c.flip();
 137         c.position(7);
 138         b.put(c);
 139         b.flip();
 140         try {
 141             b.put(b);
 142             fail("IllegalArgumentException expected for put into same buffer");
 143         } catch (IllegalArgumentException e) {
 144             if (e.getMessage() == null) {
 145                 fail("Non-null IllegalArgumentException message expected from"
 146                      + " put into same buffer");
 147             }
 148         }
 149     }
 150 
 151     private static void absBulkPutArray(CharBuffer b) {
 152         int n = b.capacity();
 153         b.clear();
 154         int lim = n - 7;
 155         int len = lim - 7;
 156         b.limit(lim);
 157         char[] a = new char[len + 7];
 158         for (int i = 0; i < len; i++)
 159             a[i + 7] = (char)ic(i);
 160         b.position(42);
 161         b.put(7, a, 7, len);
 162         ck(b, b.position() == 42);
 163     }
 164 
 165     private static void absBulkPutBuffer(CharBuffer b, boolean direct) {
 166         int n = b.capacity();
 167         b.clear();
 168         int lim = n - 7;
 169         int len = lim - 7;
 170         b.limit(lim);
 171         CharBuffer c = direct ?
 172 
 173 
 174 
 175             ByteBuffer.allocateDirect(Character.BYTES*(len + 7))
 176                 .asCharBuffer()
 177 
 178             : CharBuffer.allocate(len + 7);
 179 
 180         if (direct)
 181             System.out.println("Direct buffer: " + c.getClass().getName());
 182 
 183         for (int i = 0; i < len; i++)
 184             c.put(i + 7, (char)ic(i));
 185         b.position(42);
 186         c.position(42);
 187         b.put(7, c, 7, len);
 188         ck(b, b.position() == 42);
 189         ck(c, c.position() == 42);
 190     }
 191 
 192     //6231529
 193     private static void callReset(CharBuffer b) {
 194         b.position(0);
 195         b.mark();
 196 
 197         b.duplicate().reset();
 198         b.asReadOnlyBuffer().reset();
 199     }
 200 
 201 
 202 
 203     // 6221101-6234263
 204 
 205     private static void putBuffer() {
 206         final int cap = 10;
 207 
 208         CharBuffer direct1 = ByteBuffer.allocateDirect(cap).asCharBuffer();
 209         CharBuffer nondirect1 = ByteBuffer.allocate(cap).asCharBuffer();
 210         direct1.put(nondirect1);
 211 


 488 
 489 
 490 
 491 
 492 
 493 
 494 
 495 
 496 
 497 
 498 
 499 
 500 
 501 
 502     private static void fail(String problem,
 503                              CharBuffer xb, CharBuffer yb,
 504                              char x, char y) {
 505         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 506     }
 507 
 508     private static void catchNullArgument(Buffer b, Runnable thunk) {
 509         tryCatch(b, NullPointerException.class, thunk);
 510     }
 511 
 512     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
 513         tryCatch(b, IllegalArgumentException.class, thunk);
 514     }
 515 
 516     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
 517         tryCatch(b, ReadOnlyBufferException.class, thunk);
 518     }
 519 
 520     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
 521         tryCatch(b, IndexOutOfBoundsException.class, thunk);
 522     }
 523 
 524     private static void catchIndexOutOfBounds(char[] t, Runnable thunk) {
 525         tryCatch(t, IndexOutOfBoundsException.class, thunk);
 526     }
 527 
 528     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 529         boolean caught = false;
 530         try {
 531             thunk.run();
 532         } catch (Throwable x) {
 533             if (ex.isAssignableFrom(x.getClass())) {
 534                 caught = true;
 535             } else {
 536                 String s = x.getMessage();
 537                 if (s == null)
 538                     s = x.getClass().getName();
 539                 fail(s + " not expected");
 540             }
 541         }
 542         if (!caught) {
 543             fail(ex.getName() + " not thrown", b);
 544         }
 545     }
 546 
 547     private static void tryCatch(char[] t, Class<?> ex, Runnable thunk) {
 548         tryCatch(CharBuffer.wrap(t), ex, thunk);
 549     }
 550 
 551     public static void test(int level, final CharBuffer b, boolean direct) {
 552 
 553         show(level, b);
 554 
 555         if (direct != b.isDirect())
 556             fail("Wrong direction", b);
 557 
 558         // Gets and puts
 559 
 560         relPut(b);
 561         relGet(b);
 562         absGet(b);
 563         bulkGet(b);
 564 
 565         absPut(b);
 566         relGet(b);
 567         absGet(b);
 568         bulkGet(b);
 569 
 570         bulkPutArray(b);
 571         relGet(b);
 572 
 573         bulkPutBuffer(b);
 574         relGet(b);
 575 
 576         absBulkPutArray(b);
 577         absBulkGet(b);
 578 
 579         absBulkPutBuffer(b, direct);
 580         absBulkGet(b);
 581 
 582         absBulkPutBuffer(b, !direct);
 583         absBulkGet(b);
 584 
 585 
 586 
 587         bulkPutString(b);
 588         relGet(b);
 589         b.position(1);
 590         b.limit(7);
 591         ck(b, b.toString().equals("bcdefg"));
 592 
 593         // CharSequence ops
 594 
 595         b.position(2);
 596         ck(b, b.charAt(1), 'd');
 597         CharBuffer c = b.subSequence(1, 4);
 598         ck(c, c.capacity(), b.capacity());
 599         ck(c, c.position(), b.position()+1);
 600         ck(c, c.limit(), b.position()+4);
 601         ck(c, b.subSequence(1, 4).toString().equals("def"));
 602 
 603         // 4938424
 604         b.position(4);


 664         try {
 665             b.limit(b.capacity() + 1);
 666             fail("IllegalArgumentException expected for limit beyond capacity");
 667         } catch (IllegalArgumentException e) {
 668             if (e.getMessage() == null) {
 669                 fail("Non-null IllegalArgumentException message expected for"
 670                      + " limit beyond capacity");
 671             }
 672         }
 673 
 674         try {
 675             b.limit(-1);
 676             fail("IllegalArgumentException expected for negative limit");
 677         } catch (IllegalArgumentException e) {
 678             if (e.getMessage() == null) {
 679                 fail("Non-null IllegalArgumentException message expected for"
 680                      + " negative limit");
 681             }
 682         }
 683 
 684         // Exceptions in absolute bulk operations
 685 
 686         catchNullArgument(b, () -> b.get(7, null, 0, 42));
 687         catchNullArgument(b, () -> b.put(7, (char[])null, 0, 42));
 688         catchNullArgument(b, () -> b.put(7, (CharBuffer)null, 0, 42));
 689 
 690         char[] tmpa = new char[42];
 691         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
 692         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
 693         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));
 694         catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
 695         catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
 696         catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
 697 
 698         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
 699         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
 700         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));
 701         catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
 702         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
 703         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 704 
 705         CharBuffer tmpb = CharBuffer.allocate(42);
 706         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, -1, 42));
 707         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, 1));
 708         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, -1));
 709         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 41, 2));
 710         catchIndexOutOfBounds(b, () -> b.put(-1, tmpb, 0, 1));
 711         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpb, 0, 1));
 712         catchIndexOutOfBounds(b, () -> b.put(0, tmpb, 0, -1));
 713         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpb, 0, 42));
 714 
 715         // Values
 716 
 717         b.clear();
 718         b.put((char)0);
 719         b.put((char)-1);
 720         b.put((char)1);
 721         b.put(Character.MAX_VALUE);
 722         b.put(Character.MIN_VALUE);
 723 
 724 
 725 
 726 
 727 
 728 
 729 
 730 
 731 
 732 
 733 
 734 


 902 
 903 
 904 
 905 
 906 
 907 
 908 
 909 
 910         // Read-only views
 911 
 912         b.rewind();
 913         final CharBuffer rb = b.asReadOnlyBuffer();
 914         if (!b.equals(rb))
 915             fail("Buffer not equal to read-only view", b, rb);
 916         show(level + 1, rb);
 917 
 918         catchReadOnlyBuffer(b, () -> relPut(rb));
 919         catchReadOnlyBuffer(b, () -> absPut(rb));
 920         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
 921         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
 922         catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
 923         catchReadOnlyBuffer(b, () -> absBulkPutBuffer(rb, direct));
 924 
 925         // put(CharBuffer) should not change source position
 926         final CharBuffer src = CharBuffer.allocate(1);
 927         catchReadOnlyBuffer(b, () -> rb.put(src));
 928         ck(src, src.position(), 0);
 929 
 930         catchReadOnlyBuffer(b, () -> rb.compact());
 931 
 932 
 933 
 934 
 935 
 936 
 937 
 938 
 939 
 940 
 941 
 942 
 943 


< prev index next >