author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 7668 | jdk/test/java/nio/Buffer/StringCharBufferSliceTest.java@d4a77089c587 |
child 53959 | 1542e63eb537 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
7275
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
25 |
* @bug 4997655 7000913 |
2 | 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 |
||
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
56 |
System.out.println( |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
57 |
">>> StringCharBufferSliceTest-main: testing slice result with get()"); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
58 |
buff.position(4); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
59 |
buff.limit(7); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
60 |
CharBuffer slice = buff.slice(); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
61 |
for (int i = 0; i < 3; i++) { |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
62 |
if (slice.get() != buff.get()) { |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
63 |
throw new RuntimeException("Wrong characters in slice result."); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
64 |
} |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
65 |
} |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
66 |
|
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
67 |
System.out.println( |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
68 |
">>> StringCharBufferSliceTest-main: testing slice result with get(int)"); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
69 |
buff.position(4); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
70 |
buff.limit(7); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
71 |
slice = buff.slice(); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
72 |
for (int i = 0; i < 3; i++) { |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
73 |
if (slice.get(i) != buff.get(4 + i)) { |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
74 |
throw new RuntimeException("Wrong characters in slice result."); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
75 |
} |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
76 |
} |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
77 |
|
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
78 |
System.out.println( |
7275
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
79 |
">>> StringCharBufferSliceTest-main: testing slice with result of slice"); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
80 |
buff.position(0); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
81 |
buff.limit(buff.capacity()); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
82 |
slice = buff.slice(); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
83 |
for (int i=0; i<4; i++) { |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
84 |
slice.position(i); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
85 |
CharBuffer nextSlice = slice.slice(); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
86 |
if (nextSlice.position() != 0) |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
87 |
throw new RuntimeException("New buffer's position should be zero"); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
88 |
if (!nextSlice.equals(slice)) |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
89 |
throw new RuntimeException("New buffer should be equal"); |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
90 |
slice = nextSlice; |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
91 |
} |
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
92 |
|
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
93 |
System.out.println( |
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
94 |
">>> StringCharBufferSliceTest-main: testing toString."); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
95 |
buff.position(4); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
96 |
buff.limit(7); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
97 |
slice = buff.slice(); |
7275
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
98 |
if (!slice.toString().equals("tes")) { |
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
99 |
throw new RuntimeException("bad toString() after slice(): " + slice.toString()); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
100 |
} |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
101 |
|
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
102 |
System.out.println( |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
103 |
">>> StringCharBufferSliceTest-main: testing subSequence."); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
104 |
buff.position(4); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
105 |
buff.limit(8); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
106 |
slice = buff.slice(); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
107 |
CharSequence subSeq = slice.subSequence(1, 3); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
108 |
if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') { |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
109 |
throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'"); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
110 |
} |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
111 |
|
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
112 |
System.out.println( |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
113 |
">>> StringCharBufferSliceTest-main: testing duplicate."); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
114 |
buff.position(4); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
115 |
buff.limit(8); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
116 |
slice = buff.slice(); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
117 |
CharBuffer dupe = slice.duplicate(); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
118 |
if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e' |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
119 |
|| dupe.charAt(2) != 's' || dupe.charAt(3) != 't') { |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
120 |
throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'"); |
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
121 |
} |
7275
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
122 |
|
2 | 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 |
} |