Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/Buffer/StringCharBufferSliceTest.java
38813 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4997655 700091325* @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position26*/2728import java.nio.*;2930public class StringCharBufferSliceTest {31public static void main( String[] args) throws Exception {32System.out.println(33">>> StringCharBufferSliceTest-main: testing the slice method...");3435final String in = "for testing";3637System.out.println(38">>> StringCharBufferSliceTest-main: testing with the position 0.");3940CharBuffer buff = CharBuffer.wrap(in);41test(buff, buff.slice());4243System.out.println(44">>> StringCharBufferSliceTest-main: testing with new position.");4546buff.position(2);47test(buff, buff.slice());4849System.out.println(50">>> StringCharBufferSliceTest-main: testing with non zero initial position.");5152buff = CharBuffer.wrap(in, 3, in.length());53test(buff, buff.slice());5455System.out.println(56">>> StringCharBufferSliceTest-main: testing slice result with get()");57buff.position(4);58buff.limit(7);59CharBuffer slice = buff.slice();60for (int i = 0; i < 3; i++) {61if (slice.get() != buff.get()) {62throw new RuntimeException("Wrong characters in slice result.");63}64}6566System.out.println(67">>> StringCharBufferSliceTest-main: testing slice result with get(int)");68buff.position(4);69buff.limit(7);70slice = buff.slice();71for (int i = 0; i < 3; i++) {72if (slice.get(i) != buff.get(4 + i)) {73throw new RuntimeException("Wrong characters in slice result.");74}75}7677System.out.println(78">>> StringCharBufferSliceTest-main: testing slice with result of slice");79buff.position(0);80buff.limit(buff.capacity());81slice = buff.slice();82for (int i=0; i<4; i++) {83slice.position(i);84CharBuffer nextSlice = slice.slice();85if (nextSlice.position() != 0)86throw new RuntimeException("New buffer's position should be zero");87if (!nextSlice.equals(slice))88throw new RuntimeException("New buffer should be equal");89slice = nextSlice;90}9192System.out.println(93">>> StringCharBufferSliceTest-main: testing toString.");94buff.position(4);95buff.limit(7);96slice = buff.slice();97if (!slice.toString().equals("tes")) {98throw new RuntimeException("bad toString() after slice(): " + slice.toString());99}100101System.out.println(102">>> StringCharBufferSliceTest-main: testing subSequence.");103buff.position(4);104buff.limit(8);105slice = buff.slice();106CharSequence subSeq = slice.subSequence(1, 3);107if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {108throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");109}110111System.out.println(112">>> StringCharBufferSliceTest-main: testing duplicate.");113buff.position(4);114buff.limit(8);115slice = buff.slice();116CharBuffer dupe = slice.duplicate();117if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'118|| dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {119throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");120}121122System.out.println(">>> StringCharBufferSliceTest-main: done!");123}124125public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {126boolean marked = false;127128try {129slice.reset();130131marked = true;132} catch (InvalidMarkException ime) {133// expected134}135136if (marked ||137slice.position() != 0 ||138buff.remaining() != slice.limit() ||139buff.remaining() != slice.capacity()) {140141throw new RuntimeException(142"Calling the CharBuffer.slice method failed.");143}144}145}146147148