test/jdk/java/nio/Buffer/StringCharBufferSliceTest.java
changeset 47216 71c04702a3d5
parent 7668 d4a77089c587
child 53959 1542e63eb537
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2005, 2010, 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 /* @test
       
    25  * @bug 4997655 7000913
       
    26  * @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position
       
    27  */
       
    28 
       
    29 import java.nio.*;
       
    30 
       
    31 public class StringCharBufferSliceTest {
       
    32     public static void main( String[] args) throws Exception {
       
    33         System.out.println(
       
    34             ">>> StringCharBufferSliceTest-main: testing the slice method...");
       
    35 
       
    36         final String in = "for testing";
       
    37 
       
    38         System.out.println(
       
    39             ">>> StringCharBufferSliceTest-main: testing with the position 0.");
       
    40 
       
    41         CharBuffer buff = CharBuffer.wrap(in);
       
    42         test(buff, buff.slice());
       
    43 
       
    44         System.out.println(
       
    45             ">>> StringCharBufferSliceTest-main: testing with new position.");
       
    46 
       
    47         buff.position(2);
       
    48         test(buff, buff.slice());
       
    49 
       
    50         System.out.println(
       
    51           ">>> StringCharBufferSliceTest-main: testing with non zero initial position.");
       
    52 
       
    53         buff = CharBuffer.wrap(in, 3, in.length());
       
    54         test(buff, buff.slice());
       
    55 
       
    56         System.out.println(
       
    57             ">>> StringCharBufferSliceTest-main: testing slice result with get()");
       
    58         buff.position(4);
       
    59         buff.limit(7);
       
    60         CharBuffer slice = buff.slice();
       
    61         for (int i = 0; i < 3; i++) {
       
    62             if (slice.get() != buff.get()) {
       
    63                 throw new RuntimeException("Wrong characters in slice result.");
       
    64             }
       
    65         }
       
    66 
       
    67         System.out.println(
       
    68             ">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
       
    69         buff.position(4);
       
    70         buff.limit(7);
       
    71         slice = buff.slice();
       
    72         for (int i = 0; i < 3; i++) {
       
    73             if (slice.get(i) != buff.get(4 + i)) {
       
    74                 throw new RuntimeException("Wrong characters in slice result.");
       
    75             }
       
    76         }
       
    77 
       
    78         System.out.println(
       
    79           ">>> StringCharBufferSliceTest-main: testing slice with result of slice");
       
    80         buff.position(0);
       
    81         buff.limit(buff.capacity());
       
    82         slice = buff.slice();
       
    83         for (int i=0; i<4; i++) {
       
    84             slice.position(i);
       
    85             CharBuffer nextSlice = slice.slice();
       
    86             if (nextSlice.position() != 0)
       
    87                 throw new RuntimeException("New buffer's position should be zero");
       
    88             if (!nextSlice.equals(slice))
       
    89                 throw new RuntimeException("New buffer should be equal");
       
    90             slice = nextSlice;
       
    91         }
       
    92 
       
    93         System.out.println(
       
    94           ">>> StringCharBufferSliceTest-main: testing toString.");
       
    95         buff.position(4);
       
    96         buff.limit(7);
       
    97         slice = buff.slice();
       
    98         if (!slice.toString().equals("tes")) {
       
    99             throw new RuntimeException("bad toString() after slice(): " + slice.toString());
       
   100         }
       
   101 
       
   102         System.out.println(
       
   103           ">>> StringCharBufferSliceTest-main: testing subSequence.");
       
   104         buff.position(4);
       
   105         buff.limit(8);
       
   106         slice = buff.slice();
       
   107         CharSequence subSeq = slice.subSequence(1, 3);
       
   108         if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
       
   109             throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
       
   110         }
       
   111 
       
   112         System.out.println(
       
   113           ">>> StringCharBufferSliceTest-main: testing duplicate.");
       
   114         buff.position(4);
       
   115         buff.limit(8);
       
   116         slice = buff.slice();
       
   117         CharBuffer dupe = slice.duplicate();
       
   118         if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
       
   119             || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
       
   120             throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
       
   121         }
       
   122 
       
   123         System.out.println(">>> StringCharBufferSliceTest-main: done!");
       
   124     }
       
   125 
       
   126     public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {
       
   127         boolean marked = false;
       
   128 
       
   129         try {
       
   130             slice.reset();
       
   131 
       
   132             marked = true;
       
   133         } catch (InvalidMarkException ime) {
       
   134             // expected
       
   135         }
       
   136 
       
   137         if (marked ||
       
   138             slice.position() != 0 ||
       
   139             buff.remaining() != slice.limit() ||
       
   140             buff.remaining() != slice.capacity()) {
       
   141 
       
   142             throw new RuntimeException(
       
   143                  "Calling the CharBuffer.slice method failed.");
       
   144         }
       
   145     }
       
   146 }