1 /* 2 * Copyright (c) 2005, 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 */ 23 24 /* @test 25 * @bug 4997655 5071718 7000913 26 * @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position 27 */ 28 package test.java.nio.Buffer; 29 30 import java.nio.CharBuffer; 31 import java.nio.InvalidMarkException; 32 import java.util.function.BiConsumer; 33 import java.util.function.Consumer; 34 import org.testng.annotations.Test; 35 36 public class StringCharBufferSliceTest { 37 38 // Android-changed: Use TestNg. 39 // public static void main( String[] args) throws Exception { 40 @Test testSlice()41 public void testSlice() throws Exception { 42 // Android-changed: Remove System.out to avoid spamming logcat. 43 // System.out.println( 44 // ">>> StringCharBufferSliceTest-main: testing the slice method..."); 45 46 final String in = "for testing"; 47 48 System.out.println( 49 ">>> StringCharBufferSliceTest-main: testing with the position 0."); 50 51 CharBuffer buff = CharBuffer.wrap(in); 52 helper(buff, buff.slice()); 53 helper(buff, buff.slice(0, buff.remaining())); 54 55 // Android-changed: Remove System.out to avoid spamming logcat. 56 // System.out.println( 57 // ">>> StringCharBufferSliceTest-main: testing with new position."); 58 59 buff.position(2); 60 helper(buff, buff.slice()); 61 helper(buff, buff.slice(2, buff.remaining())); 62 63 // Android-changed: Remove System.out to avoid spamming logcat. 64 // System.out.println( 65 // ">>> StringCharBufferSliceTest-main: testing with non zero initial position."); 66 67 buff = CharBuffer.wrap(in, 3, in.length()); 68 helper(buff, buff.slice()); 69 helper(buff, buff.slice(0, buff.remaining())); 70 71 // Android-changed: Remove System.out to avoid spamming logcat. 72 // System.out.println( 73 // ">>> StringCharBufferSliceTest-main: testing slice result with get()"); 74 buff.position(4); 75 buff.limit(7); 76 BiConsumer<CharBuffer,CharBuffer> bitest = (b, s) -> { 77 for (int i = 0; i < 3; i++) { 78 if (s.get() != b.get()) { 79 throw new RuntimeException 80 ("Wrong characters in slice result."); 81 } 82 } 83 }; 84 bitest.accept(buff, buff.slice()); 85 buff.position(4); 86 bitest.accept(buff, buff.slice(4, 3)); 87 88 89 // Android-changed: Remove System.out to avoid spamming logcat. 90 // System.out.println( 91 // ">>> StringCharBufferSliceTest-main: testing slice result with get(int)"); 92 buff.position(4); 93 buff.limit(7); 94 bitest = (b, s) -> { 95 for (int i = 0; i < 3; i++) { 96 if (s.get(i) != b.get(4 + i)) { 97 throw new RuntimeException 98 ("Wrong characters in slice result."); 99 } 100 } 101 }; 102 bitest.accept(buff, buff.slice()); 103 buff.position(4); 104 bitest.accept(buff, buff.slice(4, 3)); 105 106 // Android-changed: Remove System.out to avoid spamming logcat. 107 // System.out.println( 108 // ">>> StringCharBufferSliceTest-main: testing slice with result of slice"); 109 buff.position(0); 110 buff.limit(buff.capacity()); 111 Consumer<CharBuffer> test = (s) -> { 112 for (int i=0; i<4; i++) { 113 s.position(i); 114 CharBuffer nextSlice = s.slice(); 115 if (nextSlice.position() != 0) 116 throw new RuntimeException 117 ("New buffer's position should be zero"); 118 if (!nextSlice.equals(s)) 119 throw new RuntimeException("New buffer should be equal"); 120 s = nextSlice; 121 } 122 }; 123 test.accept(buff.slice()); 124 test.accept(buff.slice(0, buff.capacity())); 125 126 // Android-changed: Remove System.out to avoid spamming logcat. 127 // System.out.println( 128 // ">>> StringCharBufferSliceTest-main: testing toString."); 129 buff.position(4); 130 buff.limit(7); 131 test = (s) -> { 132 if (!s.toString().equals("tes")) { 133 throw new RuntimeException 134 ("bad toString() after slice(): " + s.toString()); 135 } 136 }; 137 test.accept(buff.slice()); 138 test.accept(buff.slice(4, 3)); 139 140 // Android-changed: Remove System.out to avoid spamming logcat. 141 // System.out.println( 142 // ">>> StringCharBufferSliceTest-main: testing subSequence."); 143 buff.position(4); 144 buff.limit(8); 145 test = (s) -> { 146 CharSequence subSeq = s.subSequence(1, 3); 147 if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') { 148 throw new RuntimeException 149 ("bad subSequence() after slice(): '" + subSeq + "'"); 150 } 151 }; 152 test.accept(buff.slice()); 153 test.accept(buff.slice(4, 4)); 154 155 // Android-changed: Remove System.out to avoid spamming logcat. 156 // System.out.println( 157 // ">>> StringCharBufferSliceTest-main: testing duplicate."); 158 buff.position(4); 159 buff.limit(8); 160 test = (s) -> { 161 CharBuffer dupe = s.duplicate(); 162 if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e' 163 || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') { 164 throw new RuntimeException 165 ("bad duplicate() after slice(): '" + dupe + "'"); 166 } 167 }; 168 test.accept(buff.slice()); 169 test.accept(buff.slice(4, 4)); 170 171 // Android-changed: Remove System.out to avoid spamming logcat. 172 // System.out.println(">>> StringCharBufferSliceTest-main: done!"); 173 } 174 175 // Android-changed: Rename the method to avoid confusing JUnit / TestNg. 176 // public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException { helper(CharBuffer buff, CharBuffer slice)177 public static void helper(CharBuffer buff, CharBuffer slice) throws RuntimeException { 178 boolean marked = false; 179 180 try { 181 slice.reset(); 182 183 marked = true; 184 } catch (InvalidMarkException ime) { 185 // expected 186 } 187 188 if (marked || 189 slice.position() != 0 || 190 buff.remaining() != slice.limit() || 191 buff.remaining() != slice.capacity()) { 192 193 throw new RuntimeException( 194 "Calling the CharBuffer.slice method failed."); 195 } 196 } 197 }