< prev index next >

test/jdk/java/nio/Buffer/Basic-X.java.template

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)(($type$)ic(i)));
  71         b.rewind();
  72     }
  73 
  74     private static void absGet($Type$Buffer b) {
  75         int n = b.capacity();
  76         for (int i = 0; i < n; i++)
  77             ck(b, (long)b.get(), (long)(($type$)ic(i)));
  78         b.rewind();
  79     }
  80 
  81     private static void bulkGet($Type$Buffer b) {
  82         int n = b.capacity();
  83         $type$[] a = new $type$[n + 7];
  84         b.get(a, 7, n);
  85         for (int i = 0; i < n; i++) {
  86             ck(b, (long)a[i + 7], (long)(($type$)ic(i)));
  87         }
  88     }
  89 












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


 119         b.clear();
 120         $Type$Buffer c = $Type$Buffer.allocate(n + 7);
 121         c.position(7);
 122         for (int i = 0; i < n; i++)
 123             c.put(($type$)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($Type$Buffer b) {
 141         b.position(0);
 142         b.mark();
 143 
 144         b.duplicate().reset();
 145         b.asReadOnlyBuffer().reset();
 146     }
 147 
 148 #if[byte]
 149 #else[byte]
 150     // 6221101-6234263
 151 
 152     private static void putBuffer() {
 153         final int cap = 10;
 154 
 155         $Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
 156         $Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer();
 157         direct1.put(nondirect1);
 158 


 435                 // Round up position
 436                 p = (p_mod > 0) ? p + (us - p_mod) : p;
 437                 // Round down limit
 438                 l = l - l_mod;
 439 
 440                 int ec = l - p;
 441                 if (as.limit() != ec) {
 442                     fail("Buffer capacity incorrect, expected: " + ec, as);
 443                 }
 444             }
 445         }
 446     }
 447 #end[byte]
 448 
 449     private static void fail(String problem,
 450                              $Type$Buffer xb, $Type$Buffer yb,
 451                              $type$ x, $type$ 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($type$[] 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();


 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 #if[char]
 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(($type$)0);
 619         b.put(($type$)-1);
 620         b.put(($type$)1);
 621         b.put($Fulltype$.MAX_VALUE);
 622         b.put($Fulltype$.MIN_VALUE);
 623 #if[float]
 624         b.put(-Float.MAX_VALUE);
 625         b.put(-Float.MIN_VALUE);
 626         b.put(Float.NEGATIVE_INFINITY);
 627         b.put(Float.POSITIVE_INFINITY);
 628         b.put(Float.NaN);
 629         b.put(0.91697687f);             // Changes value if incorrectly swapped
 630 #end[float]
 631 #if[double]
 632         b.put(-Double.MAX_VALUE);
 633         b.put(-Double.MIN_VALUE);
 634         b.put(Double.NEGATIVE_INFINITY);


 802             testHet(level + 1, b);
 803         }
 804         b.order(ByteOrder.LITTLE_ENDIAN);
 805         b.position(3);
 806         testHet(level + 1, b);
 807 
 808 #end[byte]
 809 
 810         // Read-only views
 811 
 812         b.rewind();
 813         final $Type$Buffer 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($Type$Buffer) should not change source position
 824         final $Type$Buffer src = $Type$Buffer.allocate(1);
 825         catchReadOnlyBuffer(b, () -> rb.put(src));
 826         ck(src, src.position(), 0);
 827 
 828         catchReadOnlyBuffer(b, () -> rb.compact());
 829 
 830 #if[byte]
 831 
 832         catchReadOnlyBuffer(b, () -> rb.putChar((char)1));
 833         catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1));
 834         catchReadOnlyBuffer(b, () -> rb.putShort((short)1));
 835         catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1));
 836         catchReadOnlyBuffer(b, () -> rb.putInt(1));
 837         catchReadOnlyBuffer(b, () -> rb.putInt(0, 1));
 838         catchReadOnlyBuffer(b, () -> rb.putLong((long)1));
 839         catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1));
 840         catchReadOnlyBuffer(b, () -> rb.putFloat((float)1));
 841         catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1));


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


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


 488                 // Round up position
 489                 p = (p_mod > 0) ? p + (us - p_mod) : p;
 490                 // Round down limit
 491                 l = l - l_mod;
 492 
 493                 int ec = l - p;
 494                 if (as.limit() != ec) {
 495                     fail("Buffer capacity incorrect, expected: " + ec, as);
 496                 }
 497             }
 498         }
 499     }
 500 #end[byte]
 501 
 502     private static void fail(String problem,
 503                              $Type$Buffer xb, $Type$Buffer yb,
 504                              $type$ x, $type$ y) {
 505         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 506     }
 507 
 508     private static void catchNullArgument(Buffer b, Runnable thunk) {
 509         tryCatch(b, NullPointerException.class, thunk);
 510     }
 511 
 512     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
 513         tryCatch(b, IllegalArgumentException.class, thunk);
 514     }
 515 
 516     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
 517         tryCatch(b, ReadOnlyBufferException.class, thunk);
 518     }
 519 
 520     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
 521         tryCatch(b, IndexOutOfBoundsException.class, thunk);
 522     }
 523 
 524     private static void catchIndexOutOfBounds($type$[] t, Runnable thunk) {
 525         tryCatch(t, IndexOutOfBoundsException.class, thunk);
 526     }
 527 
 528     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 529         boolean caught = false;
 530         try {
 531             thunk.run();


 553             fail("Wrong direction", b);
 554 
 555         // Gets and puts
 556 
 557         relPut(b);
 558         relGet(b);
 559         absGet(b);
 560         bulkGet(b);
 561 
 562         absPut(b);
 563         relGet(b);
 564         absGet(b);
 565         bulkGet(b);
 566 
 567         bulkPutArray(b);
 568         relGet(b);
 569 
 570         bulkPutBuffer(b);
 571         relGet(b);
 572 
 573         absBulkPutArray(b);
 574         absBulkGet(b);
 575 
 576         absBulkPutBuffer(b, direct);
 577         absBulkGet(b);
 578 
 579         absBulkPutBuffer(b, !direct);
 580         absBulkGet(b);
 581 
 582 #if[char]
 583 
 584         bulkPutString(b);
 585         relGet(b);
 586         b.position(1);
 587         b.limit(7);
 588         ck(b, b.toString().equals("bcdefg"));
 589 
 590         // CharSequence ops
 591 
 592         b.position(2);
 593         ck(b, b.charAt(1), 'd');
 594         CharBuffer c = b.subSequence(1, 4);
 595         ck(c, c.capacity(), b.capacity());
 596         ck(c, c.position(), b.position()+1);
 597         ck(c, c.limit(), b.position()+4);
 598         ck(c, b.subSequence(1, 4).toString().equals("def"));
 599 
 600         // 4938424
 601         b.position(4);


 661         try {
 662             b.limit(b.capacity() + 1);
 663             fail("IllegalArgumentException expected for limit beyond capacity");
 664         } catch (IllegalArgumentException e) {
 665             if (e.getMessage() == null) {
 666                 fail("Non-null IllegalArgumentException message expected for"
 667                      + " limit beyond capacity");
 668             }
 669         }
 670 
 671         try {
 672             b.limit(-1);
 673             fail("IllegalArgumentException expected for negative limit");
 674         } catch (IllegalArgumentException e) {
 675             if (e.getMessage() == null) {
 676                 fail("Non-null IllegalArgumentException message expected for"
 677                      + " negative limit");
 678             }
 679         }
 680 
 681         // Exceptions in absolute bulk operations
 682 
 683         catchNullArgument(b, () -> b.get(7, null, 0, 42));
 684         catchNullArgument(b, () -> b.put(7, ($type$[])null, 0, 42));
 685         catchNullArgument(b, () -> b.put(7, ($Type$Buffer)null, 0, 42));
 686 
 687         $type$[] tmpa = new $type$[42];
 688         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
 689         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
 690         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, 2));
 691         catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
 692         catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
 693         catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
 694 
 695         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
 696         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
 697         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, 2));
 698         catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
 699         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
 700         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 701 
 702         $Type$Buffer tmpb = $Type$Buffer.allocate(42);
 703         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, -1, 42));
 704         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, 1));
 705         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 41, 2));
 706         catchIndexOutOfBounds(b, () -> b.put(-1, tmpb, 0, 1));
 707         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpb, 0, 1));
 708         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpb, 0, 42));
 709 
 710         // Values
 711 
 712         b.clear();
 713         b.put(($type$)0);
 714         b.put(($type$)-1);
 715         b.put(($type$)1);
 716         b.put($Fulltype$.MAX_VALUE);
 717         b.put($Fulltype$.MIN_VALUE);
 718 #if[float]
 719         b.put(-Float.MAX_VALUE);
 720         b.put(-Float.MIN_VALUE);
 721         b.put(Float.NEGATIVE_INFINITY);
 722         b.put(Float.POSITIVE_INFINITY);
 723         b.put(Float.NaN);
 724         b.put(0.91697687f);             // Changes value if incorrectly swapped
 725 #end[float]
 726 #if[double]
 727         b.put(-Double.MAX_VALUE);
 728         b.put(-Double.MIN_VALUE);
 729         b.put(Double.NEGATIVE_INFINITY);


 897             testHet(level + 1, b);
 898         }
 899         b.order(ByteOrder.LITTLE_ENDIAN);
 900         b.position(3);
 901         testHet(level + 1, b);
 902 
 903 #end[byte]
 904 
 905         // Read-only views
 906 
 907         b.rewind();
 908         final $Type$Buffer rb = b.asReadOnlyBuffer();
 909         if (!b.equals(rb))
 910             fail("Buffer not equal to read-only view", b, rb);
 911         show(level + 1, rb);
 912 
 913         catchReadOnlyBuffer(b, () -> relPut(rb));
 914         catchReadOnlyBuffer(b, () -> absPut(rb));
 915         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
 916         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
 917         catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
 918         catchReadOnlyBuffer(b, () -> absBulkPutBuffer(rb, direct));
 919 
 920         // put($Type$Buffer) should not change source position
 921         final $Type$Buffer src = $Type$Buffer.allocate(1);
 922         catchReadOnlyBuffer(b, () -> rb.put(src));
 923         ck(src, src.position(), 0);
 924 
 925         catchReadOnlyBuffer(b, () -> rb.compact());
 926 
 927 #if[byte]
 928 
 929         catchReadOnlyBuffer(b, () -> rb.putChar((char)1));
 930         catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1));
 931         catchReadOnlyBuffer(b, () -> rb.putShort((short)1));
 932         catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1));
 933         catchReadOnlyBuffer(b, () -> rb.putInt(1));
 934         catchReadOnlyBuffer(b, () -> rb.putInt(0, 1));
 935         catchReadOnlyBuffer(b, () -> rb.putLong((long)1));
 936         catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1));
 937         catchReadOnlyBuffer(b, () -> rb.putFloat((float)1));
 938         catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1));


< prev index next >