Path: blob/master/test/jdk/java/nio/Buffer/StringCharBufferSliceTest.java
41149 views
/*1* Copyright (c) 2005, 2019, 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 5071718 700091325* @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position26*/2728import java.nio.CharBuffer;29import java.nio.InvalidMarkException;30import java.util.function.BiConsumer;31import java.util.function.Consumer;3233public class StringCharBufferSliceTest {34public static void main( String[] args) throws Exception {35System.out.println(36">>> StringCharBufferSliceTest-main: testing the slice method...");3738final String in = "for testing";3940System.out.println(41">>> StringCharBufferSliceTest-main: testing with the position 0.");4243CharBuffer buff = CharBuffer.wrap(in);44test(buff, buff.slice());45test(buff, buff.slice(0, buff.remaining()));4647System.out.println(48">>> StringCharBufferSliceTest-main: testing with new position.");4950buff.position(2);51test(buff, buff.slice());52test(buff, buff.slice(2, buff.remaining()));5354System.out.println(55">>> StringCharBufferSliceTest-main: testing with non zero initial position.");5657buff = CharBuffer.wrap(in, 3, in.length());58test(buff, buff.slice());59test(buff, buff.slice(0, buff.remaining()));6061System.out.println(62">>> StringCharBufferSliceTest-main: testing slice result with get()");63buff.position(4);64buff.limit(7);65BiConsumer<CharBuffer,CharBuffer> bitest = (b, s) -> {66for (int i = 0; i < 3; i++) {67if (s.get() != b.get()) {68throw new RuntimeException69("Wrong characters in slice result.");70}71}72};73bitest.accept(buff, buff.slice());74buff.position(4);75bitest.accept(buff, buff.slice(4, 3));7677System.out.println(78">>> StringCharBufferSliceTest-main: testing slice result with get(int)");79buff.position(4);80buff.limit(7);81bitest = (b, s) -> {82for (int i = 0; i < 3; i++) {83if (s.get(i) != b.get(4 + i)) {84throw new RuntimeException85("Wrong characters in slice result.");86}87}88};89bitest.accept(buff, buff.slice());90buff.position(4);91bitest.accept(buff, buff.slice(4, 3));9293System.out.println(94">>> StringCharBufferSliceTest-main: testing slice with result of slice");95buff.position(0);96buff.limit(buff.capacity());97Consumer<CharBuffer> test = (s) -> {98for (int i=0; i<4; i++) {99s.position(i);100CharBuffer nextSlice = s.slice();101if (nextSlice.position() != 0)102throw new RuntimeException103("New buffer's position should be zero");104if (!nextSlice.equals(s))105throw new RuntimeException("New buffer should be equal");106s = nextSlice;107}108};109test.accept(buff.slice());110test.accept(buff.slice(0, buff.capacity()));111112System.out.println(113">>> StringCharBufferSliceTest-main: testing toString.");114buff.position(4);115buff.limit(7);116test = (s) -> {117if (!s.toString().equals("tes")) {118throw new RuntimeException119("bad toString() after slice(): " + s.toString());120}121};122test.accept(buff.slice());123test.accept(buff.slice(4, 3));124125System.out.println(126">>> StringCharBufferSliceTest-main: testing subSequence.");127buff.position(4);128buff.limit(8);129test = (s) -> {130CharSequence subSeq = s.subSequence(1, 3);131if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {132throw new RuntimeException133("bad subSequence() after slice(): '" + subSeq + "'");134}135};136test.accept(buff.slice());137test.accept(buff.slice(4, 4));138139System.out.println(140">>> StringCharBufferSliceTest-main: testing duplicate.");141buff.position(4);142buff.limit(8);143test = (s) -> {144CharBuffer dupe = s.duplicate();145if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'146|| dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {147throw new RuntimeException148("bad duplicate() after slice(): '" + dupe + "'");149}150};151test.accept(buff.slice());152test.accept(buff.slice(4, 4));153154System.out.println(">>> StringCharBufferSliceTest-main: done!");155}156157public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {158boolean marked = false;159160try {161slice.reset();162163marked = true;164} catch (InvalidMarkException ime) {165// expected166}167168if (marked ||169slice.position() != 0 ||170buff.remaining() != slice.limit() ||171buff.remaining() != slice.capacity()) {172173throw new RuntimeException(174"Calling the CharBuffer.slice method failed.");175}176}177}178179180