Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/Buffer/StringCharBufferSliceTest.java
41149 views
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
29
import java.nio.CharBuffer;
30
import java.nio.InvalidMarkException;
31
import java.util.function.BiConsumer;
32
import java.util.function.Consumer;
33
34
public class StringCharBufferSliceTest {
35
public static void main( String[] args) throws Exception {
36
System.out.println(
37
">>> StringCharBufferSliceTest-main: testing the slice method...");
38
39
final String in = "for testing";
40
41
System.out.println(
42
">>> StringCharBufferSliceTest-main: testing with the position 0.");
43
44
CharBuffer buff = CharBuffer.wrap(in);
45
test(buff, buff.slice());
46
test(buff, buff.slice(0, buff.remaining()));
47
48
System.out.println(
49
">>> StringCharBufferSliceTest-main: testing with new position.");
50
51
buff.position(2);
52
test(buff, buff.slice());
53
test(buff, buff.slice(2, buff.remaining()));
54
55
System.out.println(
56
">>> StringCharBufferSliceTest-main: testing with non zero initial position.");
57
58
buff = CharBuffer.wrap(in, 3, in.length());
59
test(buff, buff.slice());
60
test(buff, buff.slice(0, buff.remaining()));
61
62
System.out.println(
63
">>> StringCharBufferSliceTest-main: testing slice result with get()");
64
buff.position(4);
65
buff.limit(7);
66
BiConsumer<CharBuffer,CharBuffer> bitest = (b, s) -> {
67
for (int i = 0; i < 3; i++) {
68
if (s.get() != b.get()) {
69
throw new RuntimeException
70
("Wrong characters in slice result.");
71
}
72
}
73
};
74
bitest.accept(buff, buff.slice());
75
buff.position(4);
76
bitest.accept(buff, buff.slice(4, 3));
77
78
System.out.println(
79
">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
80
buff.position(4);
81
buff.limit(7);
82
bitest = (b, s) -> {
83
for (int i = 0; i < 3; i++) {
84
if (s.get(i) != b.get(4 + i)) {
85
throw new RuntimeException
86
("Wrong characters in slice result.");
87
}
88
}
89
};
90
bitest.accept(buff, buff.slice());
91
buff.position(4);
92
bitest.accept(buff, buff.slice(4, 3));
93
94
System.out.println(
95
">>> StringCharBufferSliceTest-main: testing slice with result of slice");
96
buff.position(0);
97
buff.limit(buff.capacity());
98
Consumer<CharBuffer> test = (s) -> {
99
for (int i=0; i<4; i++) {
100
s.position(i);
101
CharBuffer nextSlice = s.slice();
102
if (nextSlice.position() != 0)
103
throw new RuntimeException
104
("New buffer's position should be zero");
105
if (!nextSlice.equals(s))
106
throw new RuntimeException("New buffer should be equal");
107
s = nextSlice;
108
}
109
};
110
test.accept(buff.slice());
111
test.accept(buff.slice(0, buff.capacity()));
112
113
System.out.println(
114
">>> StringCharBufferSliceTest-main: testing toString.");
115
buff.position(4);
116
buff.limit(7);
117
test = (s) -> {
118
if (!s.toString().equals("tes")) {
119
throw new RuntimeException
120
("bad toString() after slice(): " + s.toString());
121
}
122
};
123
test.accept(buff.slice());
124
test.accept(buff.slice(4, 3));
125
126
System.out.println(
127
">>> StringCharBufferSliceTest-main: testing subSequence.");
128
buff.position(4);
129
buff.limit(8);
130
test = (s) -> {
131
CharSequence subSeq = s.subSequence(1, 3);
132
if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
133
throw new RuntimeException
134
("bad subSequence() after slice(): '" + subSeq + "'");
135
}
136
};
137
test.accept(buff.slice());
138
test.accept(buff.slice(4, 4));
139
140
System.out.println(
141
">>> StringCharBufferSliceTest-main: testing duplicate.");
142
buff.position(4);
143
buff.limit(8);
144
test = (s) -> {
145
CharBuffer dupe = s.duplicate();
146
if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
147
|| dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
148
throw new RuntimeException
149
("bad duplicate() after slice(): '" + dupe + "'");
150
}
151
};
152
test.accept(buff.slice());
153
test.accept(buff.slice(4, 4));
154
155
System.out.println(">>> StringCharBufferSliceTest-main: done!");
156
}
157
158
public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException {
159
boolean marked = false;
160
161
try {
162
slice.reset();
163
164
marked = true;
165
} catch (InvalidMarkException ime) {
166
// expected
167
}
168
169
if (marked ||
170
slice.position() != 0 ||
171
buff.remaining() != slice.limit() ||
172
buff.remaining() != slice.capacity()) {
173
174
throw new RuntimeException(
175
"Calling the CharBuffer.slice method failed.");
176
}
177
}
178
}
179
180