author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 37913 | jdk/test/java/nio/Buffer/Basic-X.java.template@3cc95b690353 |
child 53855 | 7c362992527a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
2 |
* Copyright (c) 2000, 2016, 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 |
/* Type-specific source code for unit test |
|
25 |
* |
|
26 |
* Regenerate the BasicX classes via genBasic.sh whenever this file changes. |
|
27 |
* We check in the generated source files so that the test tree can be used |
|
28 |
* independently of the rest of the source tree. |
|
29 |
*/ |
|
30 |
||
31 |
#warn This file is preprocessed before being compiled |
|
32 |
||
33 |
import java.nio.*; |
|
34 |
||
35 |
||
36 |
public class Basic$Type$ |
|
37 |
extends Basic |
|
38 |
{ |
|
39 |
||
4502
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
40 |
private static final $type$[] VALUES = { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
41 |
$Fulltype$.MIN_VALUE, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
42 |
($type$) -1, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
43 |
($type$) 0, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
44 |
($type$) 1, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
45 |
$Fulltype$.MAX_VALUE, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
46 |
#if[float] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
47 |
$Fulltype$.NEGATIVE_INFINITY, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
48 |
$Fulltype$.POSITIVE_INFINITY, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
49 |
$Fulltype$.NaN, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
50 |
($type$) -0.0, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
51 |
#end[float] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
52 |
#if[double] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
53 |
$Fulltype$.NEGATIVE_INFINITY, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
54 |
$Fulltype$.POSITIVE_INFINITY, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
55 |
$Fulltype$.NaN, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
56 |
($type$) -0.0, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
57 |
#end[double] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
58 |
}; |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
59 |
|
2 | 60 |
private static void relGet($Type$Buffer b) { |
61 |
int n = b.capacity(); |
|
62 |
for (int i = 0; i < n; i++) |
|
63 |
ck(b, (long)b.get(), (long)(($type$)ic(i))); |
|
64 |
b.rewind(); |
|
65 |
} |
|
66 |
||
67 |
private static void relGet($Type$Buffer b, int start) { |
|
68 |
int n = b.remaining(); |
|
69 |
for (int i = start; i < n; i++) |
|
70 |
ck(b, (long)b.get(), (long)(($type$)ic(i))); |
|
71 |
b.rewind(); |
|
72 |
} |
|
73 |
||
74 |
private static void absGet($Type$Buffer b) { |
|
75 |
int n = b.capacity(); |
|
76 |
for (int i = 0; i < n; i++) |
|
77 |
ck(b, (long)b.get(), (long)(($type$)ic(i))); |
|
78 |
b.rewind(); |
|
79 |
} |
|
80 |
||
81 |
private static void bulkGet($Type$Buffer b) { |
|
82 |
int n = b.capacity(); |
|
83 |
$type$[] a = new $type$[n + 7]; |
|
84 |
b.get(a, 7, n); |
|
37913 | 85 |
for (int i = 0; i < n; i++) { |
2 | 86 |
ck(b, (long)a[i + 7], (long)(($type$)ic(i))); |
37913 | 87 |
} |
2 | 88 |
} |
89 |
||
90 |
private static void relPut($Type$Buffer b) { |
|
91 |
int n = b.capacity(); |
|
92 |
b.clear(); |
|
93 |
for (int i = 0; i < n; i++) |
|
94 |
b.put(($type$)ic(i)); |
|
95 |
b.flip(); |
|
96 |
} |
|
97 |
||
98 |
private static void absPut($Type$Buffer b) { |
|
99 |
int n = b.capacity(); |
|
100 |
b.clear(); |
|
101 |
for (int i = 0; i < n; i++) |
|
102 |
b.put(i, ($type$)ic(i)); |
|
103 |
b.limit(n); |
|
104 |
b.position(0); |
|
105 |
} |
|
106 |
||
107 |
private static void bulkPutArray($Type$Buffer b) { |
|
108 |
int n = b.capacity(); |
|
109 |
b.clear(); |
|
110 |
$type$[] a = new $type$[n + 7]; |
|
111 |
for (int i = 0; i < n; i++) |
|
112 |
a[i + 7] = ($type$)ic(i); |
|
113 |
b.put(a, 7, n); |
|
114 |
b.flip(); |
|
115 |
} |
|
116 |
||
117 |
private static void bulkPutBuffer($Type$Buffer b) { |
|
118 |
int n = b.capacity(); |
|
119 |
b.clear(); |
|
120 |
$Type$Buffer c = $Type$Buffer.allocate(n + 7); |
|
121 |
c.position(7); |
|
122 |
for (int i = 0; i < n; i++) |
|
123 |
c.put(($type$)ic(i)); |
|
124 |
c.flip(); |
|
125 |
c.position(7); |
|
126 |
b.put(c); |
|
127 |
b.flip(); |
|
31873
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
128 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
129 |
b.put(b); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
130 |
fail("IllegalArgumentException expected for put into same buffer"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
131 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
132 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
133 |
fail("Non-null IllegalArgumentException message expected from" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
134 |
+ " put into same buffer"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
135 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
136 |
} |
2 | 137 |
} |
138 |
||
139 |
//6231529 |
|
140 |
private static void callReset($Type$Buffer b) { |
|
141 |
b.position(0); |
|
142 |
b.mark(); |
|
143 |
||
144 |
b.duplicate().reset(); |
|
145 |
b.asReadOnlyBuffer().reset(); |
|
146 |
} |
|
147 |
||
148 |
#if[byte] |
|
149 |
#else[byte] |
|
150 |
// 6221101-6234263 |
|
151 |
||
152 |
private static void putBuffer() { |
|
153 |
final int cap = 10; |
|
154 |
||
155 |
$Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); |
|
156 |
$Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer(); |
|
157 |
direct1.put(nondirect1); |
|
158 |
||
159 |
$Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); |
|
160 |
$Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer(); |
|
161 |
nondirect2.put(direct2); |
|
162 |
||
163 |
$Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); |
|
164 |
$Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer(); |
|
165 |
direct3.put(direct4); |
|
166 |
||
167 |
$Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer(); |
|
168 |
$Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer(); |
|
169 |
nondirect3.put(nondirect4); |
|
170 |
} |
|
171 |
#end[byte] |
|
172 |
||
173 |
#if[char] |
|
174 |
||
175 |
private static void bulkPutString($Type$Buffer b) { |
|
176 |
int n = b.capacity(); |
|
177 |
b.clear(); |
|
37913 | 178 |
StringBuilder sb = new StringBuilder(n + 7); |
2 | 179 |
sb.append("1234567"); |
180 |
for (int i = 0; i < n; i++) |
|
181 |
sb.append((char)ic(i)); |
|
182 |
b.put(sb.toString(), 7, 7 + n); |
|
183 |
b.flip(); |
|
184 |
} |
|
185 |
||
186 |
#end[char] |
|
187 |
||
188 |
private static void checkSlice($Type$Buffer b, $Type$Buffer slice) { |
|
189 |
ck(slice, 0, slice.position()); |
|
190 |
ck(slice, b.remaining(), slice.limit()); |
|
191 |
ck(slice, b.remaining(), slice.capacity()); |
|
192 |
if (b.isDirect() != slice.isDirect()) |
|
193 |
fail("Lost direction", slice); |
|
194 |
if (b.isReadOnly() != slice.isReadOnly()) |
|
195 |
fail("Lost read-only", slice); |
|
196 |
} |
|
197 |
||
198 |
#if[byte] |
|
199 |
||
200 |
private static void checkBytes(ByteBuffer b, byte[] bs) { |
|
201 |
int n = bs.length; |
|
202 |
int p = b.position(); |
|
203 |
if (b.order() == ByteOrder.BIG_ENDIAN) { |
|
37913 | 204 |
for (int i = 0; i < n; i++) { |
2 | 205 |
ck(b, b.get(), bs[i]); |
37913 | 206 |
} |
2 | 207 |
} else { |
37913 | 208 |
for (int i = n - 1; i >= 0; i--) { |
2 | 209 |
ck(b, b.get(), bs[i]); |
37913 | 210 |
} |
2 | 211 |
} |
212 |
b.position(p); |
|
213 |
} |
|
214 |
||
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
215 |
private static void compact(Buffer b) { |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
216 |
try { |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
217 |
Class<?> cl = b.getClass(); |
37913 | 218 |
java.lang.reflect.Method m = cl.getDeclaredMethod("compact"); |
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
219 |
m.setAccessible(true); |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
220 |
m.invoke(b); |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
221 |
} catch (Exception e) { |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
222 |
fail(e.getMessage(), b); |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
223 |
} |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
224 |
} |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
225 |
|
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
226 |
private static void checkInvalidMarkException(final Buffer b) { |
37913 | 227 |
tryCatch(b, InvalidMarkException.class, () -> { |
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
228 |
b.mark(); |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
229 |
compact(b); |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
230 |
b.reset(); |
37913 | 231 |
}); |
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
232 |
} |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
233 |
|
2 | 234 |
private static void testViews(int level, ByteBuffer b, boolean direct) { |
235 |
||
236 |
ShortBuffer sb = b.asShortBuffer(); |
|
237 |
BasicShort.test(level, sb, direct); |
|
238 |
checkBytes(b, new byte[] { 0, (byte)ic(0) }); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
239 |
checkInvalidMarkException(sb); |
2 | 240 |
|
241 |
CharBuffer cb = b.asCharBuffer(); |
|
242 |
BasicChar.test(level, cb, direct); |
|
243 |
checkBytes(b, new byte[] { 0, (byte)ic(0) }); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
244 |
checkInvalidMarkException(cb); |
2 | 245 |
|
246 |
IntBuffer ib = b.asIntBuffer(); |
|
247 |
BasicInt.test(level, ib, direct); |
|
248 |
checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) }); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
249 |
checkInvalidMarkException(ib); |
2 | 250 |
|
251 |
LongBuffer lb = b.asLongBuffer(); |
|
252 |
BasicLong.test(level, lb, direct); |
|
253 |
checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) }); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
254 |
checkInvalidMarkException(lb); |
2 | 255 |
|
256 |
FloatBuffer fb = b.asFloatBuffer(); |
|
257 |
BasicFloat.test(level, fb, direct); |
|
258 |
checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 }); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
259 |
checkInvalidMarkException(fb); |
2 | 260 |
|
261 |
DoubleBuffer db = b.asDoubleBuffer(); |
|
262 |
BasicDouble.test(level, db, direct); |
|
263 |
checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 }); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
264 |
checkInvalidMarkException(db); |
2 | 265 |
} |
266 |
||
267 |
private static void testHet(int level, ByteBuffer b) { |
|
268 |
||
269 |
int p = b.position(); |
|
270 |
b.limit(b.capacity()); |
|
271 |
show(level, b); |
|
272 |
out.print(" put:"); |
|
273 |
||
274 |
b.putChar((char)1); |
|
275 |
b.putChar((char)Character.MAX_VALUE); |
|
276 |
out.print(" char"); |
|
277 |
||
278 |
b.putShort((short)1); |
|
279 |
b.putShort((short)Short.MAX_VALUE); |
|
280 |
out.print(" short"); |
|
281 |
||
282 |
b.putInt(1); |
|
283 |
b.putInt(Integer.MAX_VALUE); |
|
284 |
out.print(" int"); |
|
285 |
||
286 |
b.putLong((long)1); |
|
287 |
b.putLong((long)Long.MAX_VALUE); |
|
288 |
out.print(" long"); |
|
289 |
||
290 |
b.putFloat((float)1); |
|
291 |
b.putFloat((float)Float.MIN_VALUE); |
|
292 |
b.putFloat((float)Float.MAX_VALUE); |
|
293 |
out.print(" float"); |
|
294 |
||
295 |
b.putDouble((double)1); |
|
296 |
b.putDouble((double)Double.MIN_VALUE); |
|
297 |
b.putDouble((double)Double.MAX_VALUE); |
|
298 |
out.print(" double"); |
|
299 |
||
300 |
out.println(); |
|
301 |
b.limit(b.position()); |
|
302 |
b.position(p); |
|
303 |
show(level, b); |
|
304 |
out.print(" get:"); |
|
305 |
||
306 |
ck(b, b.getChar(), 1); |
|
307 |
ck(b, b.getChar(), Character.MAX_VALUE); |
|
308 |
out.print(" char"); |
|
309 |
||
310 |
ck(b, b.getShort(), 1); |
|
311 |
ck(b, b.getShort(), Short.MAX_VALUE); |
|
312 |
out.print(" short"); |
|
313 |
||
314 |
ck(b, b.getInt(), 1); |
|
315 |
ck(b, b.getInt(), Integer.MAX_VALUE); |
|
316 |
out.print(" int"); |
|
317 |
||
318 |
ck(b, b.getLong(), 1); |
|
319 |
ck(b, b.getLong(), Long.MAX_VALUE); |
|
320 |
out.print(" long"); |
|
321 |
||
322 |
ck(b, (long)b.getFloat(), 1); |
|
323 |
ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE); |
|
324 |
ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE); |
|
325 |
out.print(" float"); |
|
326 |
||
327 |
ck(b, (long)b.getDouble(), 1); |
|
328 |
ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE); |
|
329 |
ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE); |
|
330 |
out.print(" double"); |
|
331 |
||
332 |
out.println(); |
|
333 |
||
334 |
} |
|
335 |
||
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
336 |
private static void testAlign(final ByteBuffer b, boolean direct) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
337 |
// index out-of bounds |
37913 | 338 |
catchIllegalArgument(b, () -> b.alignmentOffset(-1, (short) 1)); |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
339 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
340 |
// unit size values |
37913 | 341 |
catchIllegalArgument(b, () -> b.alignmentOffset(0, (short) 0)); |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
342 |
for (int us = 1; us < 65; us++) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
343 |
int _us = us; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
344 |
if ((us & (us - 1)) != 0) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
345 |
// unit size not a power of two |
37913 | 346 |
catchIllegalArgument(b, () -> b.alignmentOffset(0, _us)); |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
347 |
} else { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
348 |
if (direct || us <= 8) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
349 |
b.alignmentOffset(0, us); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
350 |
} else { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
351 |
// unit size > 8 with non-direct buffer |
37913 | 352 |
tryCatch(b, UnsupportedOperationException.class, |
353 |
() -> b.alignmentOffset(0, _us)); |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
354 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
355 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
356 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
357 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
358 |
// Probe for long misalignment at index zero for a newly created buffer |
37913 | 359 |
ByteBuffer empty = |
360 |
direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0); |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
361 |
int longMisalignmentAtZero = empty.alignmentOffset(0, 8); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
362 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
363 |
if (direct) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
364 |
// Freshly created direct byte buffers should be aligned at index 0 |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
365 |
// for ref and primitive values (see Unsafe.allocateMemory) |
37913 | 366 |
if (longMisalignmentAtZero != 0) { |
367 |
fail("Direct byte buffer misaligned at index 0" |
|
368 |
+ " for ref and primitive values " |
|
369 |
+ longMisalignmentAtZero); |
|
370 |
} |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
371 |
} else { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
372 |
// For heap byte buffers misalignment may occur on 32-bit systems |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
373 |
// where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0 |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
374 |
// Note the GC will preserve alignment of the base address of the |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
375 |
// array |
37913 | 376 |
if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 |
377 |
!= longMisalignmentAtZero) { |
|
378 |
fail("Heap byte buffer misaligned at index 0" |
|
379 |
+ " for ref and primitive values " |
|
380 |
+ longMisalignmentAtZero); |
|
381 |
} |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
382 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
383 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
384 |
// Ensure test buffer is correctly aligned at index 0 |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
385 |
if (b.alignmentOffset(0, 8) != longMisalignmentAtZero) |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
386 |
fail("Test input buffer not correctly aligned at index 0", b); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
387 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
388 |
// Test misalignment values |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
389 |
for (int us : new int[]{1, 2, 4, 8}) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
390 |
for (int i = 0; i < us * 2; i++) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
391 |
int am = b.alignmentOffset(i, us); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
392 |
int expectedAm = (longMisalignmentAtZero + i) % us; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
393 |
|
37913 | 394 |
if (am != expectedAm) { |
395 |
String f = "b.alignmentOffset(%d, %d) == %d incorrect, expected %d"; |
|
396 |
fail(String.format(f, i, us, am, expectedAm)); |
|
397 |
} |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
398 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
399 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
400 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
401 |
// Created aligned slice to test against |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
402 |
int ap = 8 - longMisalignmentAtZero; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
403 |
int al = b.limit() - b.alignmentOffset(b.limit(), 8); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
404 |
ByteBuffer ab = b.position(ap).limit(al). |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
405 |
slice(); |
37913 | 406 |
if (ab.limit() == 0) { |
407 |
fail("Test input buffer not sufficiently sized to cover" + |
|
408 |
" an aligned region for all values", b); |
|
409 |
} |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
410 |
if (ab.alignmentOffset(0, 8) != 0) |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
411 |
fail("Aligned test input buffer not correctly aligned at index 0", ab); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
412 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
413 |
for (int us : new int[]{1, 2, 4, 8}) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
414 |
for (int p = 1; p < 16; p++) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
415 |
int l = ab.limit() - p; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
416 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
417 |
ByteBuffer as = ab.slice().position(p).limit(l). |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
418 |
alignedSlice(us); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
419 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
420 |
ck(as, 0, as.position()); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
421 |
ck(as, as.capacity(), as.limit()); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
422 |
if (b.isDirect() != as.isDirect()) |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
423 |
fail("Lost direction", as); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
424 |
if (b.isReadOnly() != as.isReadOnly()) |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
425 |
fail("Lost read-only", as); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
426 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
427 |
if (as.alignmentOffset(0, us) != 0) |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
428 |
fail("Buffer not correctly aligned at index 0", as); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
429 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
430 |
if (as.alignmentOffset(as.limit(), us) != 0) |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
431 |
fail("Buffer not correctly aligned at limit", as); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
432 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
433 |
int p_mod = ab.alignmentOffset(p, us); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
434 |
int l_mod = ab.alignmentOffset(l, us); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
435 |
// Round up position |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
436 |
p = (p_mod > 0) ? p + (us - p_mod) : p; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
437 |
// Round down limit |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
438 |
l = l - l_mod; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
439 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
440 |
int ec = l - p; |
37913 | 441 |
if (as.limit() != ec) { |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
442 |
fail("Buffer capacity incorrect, expected: " + ec, as); |
37913 | 443 |
} |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
444 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
445 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
446 |
} |
2 | 447 |
#end[byte] |
448 |
||
4502
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
449 |
private static void fail(String problem, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
450 |
$Type$Buffer xb, $Type$Buffer yb, |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
451 |
$type$ x, $type$ y) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
452 |
fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
453 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
454 |
|
37913 | 455 |
private static void catchIllegalArgument(Buffer b, Runnable thunk) { |
456 |
tryCatch(b, IllegalArgumentException.class, thunk); |
|
457 |
} |
|
458 |
||
459 |
private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) { |
|
460 |
tryCatch(b, ReadOnlyBufferException.class, thunk); |
|
461 |
} |
|
462 |
||
463 |
private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) { |
|
464 |
tryCatch(b, IndexOutOfBoundsException.class, thunk); |
|
465 |
} |
|
466 |
||
467 |
private static void catchIndexOutOfBounds($type$[] t, Runnable thunk) { |
|
468 |
tryCatch(t, IndexOutOfBoundsException.class, thunk); |
|
469 |
} |
|
470 |
||
14001
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
471 |
private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { |
2 | 472 |
boolean caught = false; |
473 |
try { |
|
474 |
thunk.run(); |
|
475 |
} catch (Throwable x) { |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
476 |
if (ex.isAssignableFrom(x.getClass())) { |
2 | 477 |
caught = true; |
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
478 |
} else { |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
479 |
fail(x.getMessage() + " not expected"); |
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
480 |
} |
2 | 481 |
} |
37913 | 482 |
if (!caught) { |
2 | 483 |
fail(ex.getName() + " not thrown", b); |
37913 | 484 |
} |
2 | 485 |
} |
486 |
||
37913 | 487 |
private static void tryCatch($type$[] t, Class<?> ex, Runnable thunk) { |
2 | 488 |
tryCatch($Type$Buffer.wrap(t), ex, thunk); |
489 |
} |
|
490 |
||
491 |
public static void test(int level, final $Type$Buffer b, boolean direct) { |
|
492 |
||
493 |
show(level, b); |
|
494 |
||
495 |
if (direct != b.isDirect()) |
|
496 |
fail("Wrong direction", b); |
|
497 |
||
498 |
// Gets and puts |
|
499 |
||
500 |
relPut(b); |
|
501 |
relGet(b); |
|
502 |
absGet(b); |
|
503 |
bulkGet(b); |
|
504 |
||
505 |
absPut(b); |
|
506 |
relGet(b); |
|
507 |
absGet(b); |
|
508 |
bulkGet(b); |
|
509 |
||
510 |
bulkPutArray(b); |
|
511 |
relGet(b); |
|
512 |
||
513 |
bulkPutBuffer(b); |
|
514 |
relGet(b); |
|
515 |
||
516 |
#if[char] |
|
517 |
||
518 |
bulkPutString(b); |
|
519 |
relGet(b); |
|
520 |
b.position(1); |
|
521 |
b.limit(7); |
|
522 |
ck(b, b.toString().equals("bcdefg")); |
|
523 |
||
524 |
// CharSequence ops |
|
525 |
||
526 |
b.position(2); |
|
527 |
ck(b, b.charAt(1), 'd'); |
|
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
528 |
CharBuffer c = b.subSequence(1, 4); |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
529 |
ck(c, c.capacity(), b.capacity()); |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
530 |
ck(c, c.position(), b.position()+1); |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
531 |
ck(c, c.limit(), b.position()+4); |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
532 |
ck(c, b.subSequence(1, 4).toString().equals("def")); |
2 | 533 |
|
534 |
// 4938424 |
|
535 |
b.position(4); |
|
536 |
ck(b, b.charAt(1), 'f'); |
|
537 |
ck(b, b.subSequence(1, 3).toString().equals("fg")); |
|
538 |
||
13563
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
539 |
// String ops |
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
540 |
|
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
541 |
// 7190219 |
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
542 |
b.clear(); |
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
543 |
int pos = b.position(); |
37913 | 544 |
tryCatch(b, BufferOverflowException.class, () -> |
545 |
b.put(String.valueOf(new char[b.capacity() + 1]), 0, b.capacity() + 1) |
|
546 |
); |
|
13563
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
547 |
ck(b, b.position(), pos); |
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
548 |
relGet(b); |
13c8e8f0302b
7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents:
5506
diff
changeset
|
549 |
|
2 | 550 |
#end[char] |
551 |
||
552 |
// Compact |
|
553 |
||
554 |
relPut(b); |
|
555 |
b.position(13); |
|
556 |
b.compact(); |
|
557 |
b.flip(); |
|
558 |
relGet(b, 13); |
|
559 |
||
560 |
// Exceptions |
|
561 |
||
562 |
relPut(b); |
|
563 |
b.limit(b.capacity() / 2); |
|
564 |
b.position(b.limit()); |
|
565 |
||
37913 | 566 |
tryCatch(b, BufferUnderflowException.class, () -> b.get()); |
567 |
tryCatch(b, BufferOverflowException.class, () -> b.put(($type$)42)); |
|
568 |
// The index must be non-negative and less than the buffer's limit. |
|
569 |
catchIndexOutOfBounds(b, () -> b.get(b.limit())); |
|
570 |
catchIndexOutOfBounds(b, () -> b.get(-1)); |
|
571 |
catchIndexOutOfBounds(b, () -> b.put(b.limit(), ($type$)42)); |
|
572 |
tryCatch(b, InvalidMarkException.class, |
|
573 |
() -> b.position(0).mark().compact().reset()); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
2
diff
changeset
|
574 |
|
31873
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
575 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
576 |
b.position(b.limit() + 1); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
577 |
fail("IllegalArgumentException expected for position beyond limit"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
578 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
579 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
580 |
fail("Non-null IllegalArgumentException message expected for" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
581 |
+ " position beyond limit"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
582 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
583 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
584 |
|
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
585 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
586 |
b.position(-1); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
587 |
fail("IllegalArgumentException expected for negative position"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
588 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
589 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
590 |
fail("Non-null IllegalArgumentException message expected for" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
591 |
+ " negative position"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
592 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
593 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
594 |
|
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
595 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
596 |
b.limit(b.capacity() + 1); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
597 |
fail("IllegalArgumentException expected for limit beyond capacity"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
598 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
599 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
600 |
fail("Non-null IllegalArgumentException message expected for" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
601 |
+ " limit beyond capacity"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
602 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
603 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
604 |
|
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
605 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
606 |
b.limit(-1); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
607 |
fail("IllegalArgumentException expected for negative limit"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
608 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
609 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
610 |
fail("Non-null IllegalArgumentException message expected for" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
611 |
+ " negative limit"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
612 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
613 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
614 |
|
2 | 615 |
// Values |
616 |
||
617 |
b.clear(); |
|
618 |
b.put(($type$)0); |
|
619 |
b.put(($type$)-1); |
|
620 |
b.put(($type$)1); |
|
621 |
b.put($Fulltype$.MAX_VALUE); |
|
622 |
b.put($Fulltype$.MIN_VALUE); |
|
623 |
#if[float] |
|
624 |
b.put(-Float.MAX_VALUE); |
|
625 |
b.put(-Float.MIN_VALUE); |
|
626 |
b.put(Float.NEGATIVE_INFINITY); |
|
627 |
b.put(Float.POSITIVE_INFINITY); |
|
628 |
b.put(Float.NaN); |
|
629 |
b.put(0.91697687f); // Changes value if incorrectly swapped |
|
630 |
#end[float] |
|
631 |
#if[double] |
|
632 |
b.put(-Double.MAX_VALUE); |
|
633 |
b.put(-Double.MIN_VALUE); |
|
634 |
b.put(Double.NEGATIVE_INFINITY); |
|
635 |
b.put(Double.POSITIVE_INFINITY); |
|
636 |
b.put(Double.NaN); |
|
637 |
b.put(0.5121609353879392); // Changes value if incorrectly swapped |
|
638 |
#end[double] |
|
639 |
||
640 |
b.flip(); |
|
641 |
ck(b, b.get(), 0); |
|
642 |
ck(b, b.get(), ($type$)-1); |
|
643 |
ck(b, b.get(), 1); |
|
644 |
ck(b, b.get(), $Fulltype$.MAX_VALUE); |
|
645 |
ck(b, b.get(), $Fulltype$.MIN_VALUE); |
|
646 |
||
647 |
#if[float] |
|
37913 | 648 |
$type$ v; |
2 | 649 |
ck(b, b.get(), -Float.MAX_VALUE); |
650 |
ck(b, b.get(), -Float.MIN_VALUE); |
|
651 |
ck(b, b.get(), Float.NEGATIVE_INFINITY); |
|
652 |
ck(b, b.get(), Float.POSITIVE_INFINITY); |
|
31873
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
653 |
if (Float.floatToRawIntBits(v = b.get()) != |
37913 | 654 |
Float.floatToRawIntBits(Float.NaN)) { |
2 | 655 |
fail(b, (long)Float.NaN, (long)v); |
37913 | 656 |
} |
2 | 657 |
ck(b, b.get(), 0.91697687f); |
658 |
#end[float] |
|
659 |
#if[double] |
|
37913 | 660 |
$type$ v; |
2 | 661 |
ck(b, b.get(), -Double.MAX_VALUE); |
662 |
ck(b, b.get(), -Double.MIN_VALUE); |
|
663 |
ck(b, b.get(), Double.NEGATIVE_INFINITY); |
|
664 |
ck(b, b.get(), Double.POSITIVE_INFINITY); |
|
665 |
if (Double.doubleToRawLongBits(v = b.get()) |
|
37913 | 666 |
!= Double.doubleToRawLongBits(Double.NaN)) { |
2 | 667 |
fail(b, (long)Double.NaN, (long)v); |
37913 | 668 |
} |
2 | 669 |
ck(b, b.get(), 0.5121609353879392); |
670 |
#end[double] |
|
671 |
||
672 |
||
673 |
// Comparison |
|
674 |
b.rewind(); |
|
675 |
$Type$Buffer b2 = $Type$Buffer.allocate(b.capacity()); |
|
676 |
b2.put(b); |
|
677 |
b2.flip(); |
|
678 |
b.position(2); |
|
679 |
b2.position(2); |
|
680 |
if (!b.equals(b2)) { |
|
681 |
for (int i = 2; i < b.limit(); i++) { |
|
682 |
$type$ x = b.get(i); |
|
683 |
$type$ y = b2.get(i); |
|
684 |
if (x != y |
|
685 |
#if[double] |
|
686 |
|| Double.compare(x, y) != 0 |
|
687 |
#end[double] |
|
688 |
#if[float] |
|
689 |
|| Float.compare(x, y) != 0 |
|
690 |
#end[float] |
|
37913 | 691 |
) { |
2 | 692 |
out.println("[" + i + "] " + x + " != " + y); |
37913 | 693 |
} |
2 | 694 |
} |
695 |
fail("Identical buffers not equal", b, b2); |
|
696 |
} |
|
37913 | 697 |
if (b.compareTo(b2) != 0) { |
2 | 698 |
fail("Comparison to identical buffer != 0", b, b2); |
37913 | 699 |
} |
2 | 700 |
b.limit(b.limit() + 1); |
701 |
b.position(b.limit() - 1); |
|
702 |
b.put(($type$)99); |
|
703 |
b.rewind(); |
|
704 |
b2.rewind(); |
|
705 |
if (b.equals(b2)) |
|
706 |
fail("Non-identical buffers equal", b, b2); |
|
707 |
if (b.compareTo(b2) <= 0) |
|
708 |
fail("Comparison to shorter buffer <= 0", b, b2); |
|
709 |
b.limit(b.limit() - 1); |
|
710 |
||
711 |
b.put(2, ($type$)42); |
|
712 |
if (b.equals(b2)) |
|
713 |
fail("Non-identical buffers equal", b, b2); |
|
714 |
if (b.compareTo(b2) <= 0) |
|
715 |
fail("Comparison to lesser buffer <= 0", b, b2); |
|
716 |
||
4502
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
717 |
// Check equals and compareTo with interesting values |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
718 |
for ($type$ x : VALUES) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
719 |
$Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { x }); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
720 |
if (xb.compareTo(xb) != 0) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
721 |
fail("compareTo not reflexive", xb, xb, x, x); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
722 |
} |
37913 | 723 |
if (!xb.equals(xb)) { |
4502
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
724 |
fail("equals not reflexive", xb, xb, x, x); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
725 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
726 |
for ($type$ y : VALUES) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
727 |
$Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { y }); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
728 |
if (xb.compareTo(yb) != - yb.compareTo(xb)) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
729 |
fail("compareTo not anti-symmetric", |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
730 |
xb, yb, x, y); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
731 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
732 |
if ((xb.compareTo(yb) == 0) != xb.equals(yb)) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
733 |
fail("compareTo inconsistent with equals", |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
734 |
xb, yb, x, y); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
735 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
736 |
if (xb.compareTo(yb) != $Fulltype$.compare(x, y)) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
737 |
#if[float] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
738 |
if (x == 0.0 && y == 0.0) continue; |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
739 |
#end[float] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
740 |
#if[double] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
741 |
if (x == 0.0 && y == 0.0) continue; |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
742 |
#end[double] |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
743 |
fail("Incorrect results for $Type$Buffer.compareTo", |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
744 |
xb, yb, x, y); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
745 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
746 |
if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) { |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
747 |
fail("Incorrect results for $Type$Buffer.equals", |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
748 |
xb, yb, x, y); |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
749 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
750 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
751 |
} |
18f387917b89
6903754: (bf) Improve floating-point buffer comparison
martin
parents:
4115
diff
changeset
|
752 |
|
2 | 753 |
// Sub, dup |
754 |
||
755 |
relPut(b); |
|
756 |
relGet(b.duplicate()); |
|
757 |
b.position(13); |
|
758 |
relGet(b.duplicate(), 13); |
|
759 |
relGet(b.duplicate().slice(), 13); |
|
760 |
relGet(b.slice(), 13); |
|
761 |
relGet(b.slice().duplicate(), 13); |
|
762 |
||
763 |
// Slice |
|
764 |
||
765 |
b.position(5); |
|
766 |
$Type$Buffer sb = b.slice(); |
|
767 |
checkSlice(b, sb); |
|
768 |
b.position(0); |
|
769 |
$Type$Buffer sb2 = sb.slice(); |
|
770 |
checkSlice(sb, sb2); |
|
771 |
||
772 |
if (!sb.equals(sb2)) |
|
773 |
fail("Sliced slices do not match", sb, sb2); |
|
37913 | 774 |
if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) { |
2 | 775 |
fail("Array offsets do not match: " |
776 |
+ sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2); |
|
37913 | 777 |
} |
2 | 778 |
|
779 |
#if[byte] |
|
780 |
||
781 |
// Views |
|
782 |
||
783 |
b.clear(); |
|
784 |
b.order(ByteOrder.BIG_ENDIAN); |
|
785 |
testViews(level + 1, b, direct); |
|
786 |
||
787 |
for (int i = 1; i <= 9; i++) { |
|
788 |
b.position(i); |
|
789 |
show(level + 1, b); |
|
790 |
testViews(level + 2, b, direct); |
|
791 |
} |
|
792 |
||
793 |
b.position(0); |
|
794 |
b.order(ByteOrder.LITTLE_ENDIAN); |
|
795 |
testViews(level + 1, b, direct); |
|
796 |
||
797 |
// Heterogeneous accessors |
|
798 |
||
799 |
b.order(ByteOrder.BIG_ENDIAN); |
|
800 |
for (int i = 0; i <= 9; i++) { |
|
801 |
b.position(i); |
|
802 |
testHet(level + 1, b); |
|
803 |
} |
|
804 |
b.order(ByteOrder.LITTLE_ENDIAN); |
|
805 |
b.position(3); |
|
806 |
testHet(level + 1, b); |
|
807 |
||
808 |
#end[byte] |
|
809 |
||
810 |
// Read-only views |
|
811 |
||
812 |
b.rewind(); |
|
813 |
final $Type$Buffer rb = b.asReadOnlyBuffer(); |
|
814 |
if (!b.equals(rb)) |
|
815 |
fail("Buffer not equal to read-only view", b, rb); |
|
816 |
show(level + 1, rb); |
|
817 |
||
37913 | 818 |
catchReadOnlyBuffer(b, () -> relPut(rb)); |
819 |
catchReadOnlyBuffer(b, () -> absPut(rb)); |
|
820 |
catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); |
|
821 |
catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); |
|
2 | 822 |
|
14001
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
823 |
// put($Type$Buffer) should not change source position |
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
824 |
final $Type$Buffer src = $Type$Buffer.allocate(1); |
37913 | 825 |
catchReadOnlyBuffer(b, () -> rb.put(src)); |
14001
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
826 |
ck(src, src.position(), 0); |
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
827 |
|
37913 | 828 |
catchReadOnlyBuffer(b, () -> rb.compact()); |
2 | 829 |
|
830 |
#if[byte] |
|
831 |
||
37913 | 832 |
catchReadOnlyBuffer(b, () -> rb.putChar((char)1)); |
833 |
catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1)); |
|
834 |
catchReadOnlyBuffer(b, () -> rb.putShort((short)1)); |
|
835 |
catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1)); |
|
836 |
catchReadOnlyBuffer(b, () -> rb.putInt(1)); |
|
837 |
catchReadOnlyBuffer(b, () -> rb.putInt(0, 1)); |
|
838 |
catchReadOnlyBuffer(b, () -> rb.putLong((long)1)); |
|
839 |
catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1)); |
|
840 |
catchReadOnlyBuffer(b, () -> rb.putFloat((float)1)); |
|
841 |
catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1)); |
|
842 |
catchReadOnlyBuffer(b, () -> rb.putDouble((double)1)); |
|
843 |
catchReadOnlyBuffer(b, () -> rb.putDouble(0, (double)1)); |
|
2 | 844 |
|
845 |
#end[byte] |
|
846 |
||
14001
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
847 |
#if[char] |
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
848 |
|
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
849 |
// 7199551 |
37913 | 850 |
catchReadOnlyBuffer(b, () -> rb.put(new String(new char[rb.remaining() + 1]))); |
851 |
catchReadOnlyBuffer(b, () -> rb.append(new String(new char[rb.remaining() + 1]))); |
|
14001
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
852 |
|
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
853 |
#end[char] |
e8e8413ad49a
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents:
13563
diff
changeset
|
854 |
|
2 | 855 |
if (rb.getClass().getName().startsWith("java.nio.Heap")) { |
37913 | 856 |
catchReadOnlyBuffer(b, () -> rb.array()); |
857 |
catchReadOnlyBuffer(b, () -> rb.arrayOffset()); |
|
858 |
if (rb.hasArray()) { |
|
859 |
fail("Read-only heap buffer's backing array is accessible", rb); |
|
860 |
} |
|
2 | 861 |
} |
862 |
||
863 |
// Bulk puts from read-only buffers |
|
864 |
||
865 |
b.clear(); |
|
866 |
rb.rewind(); |
|
867 |
b.put(rb); |
|
868 |
||
869 |
#if[byte] |
|
870 |
// For byte buffers, test both the direct and non-direct cases |
|
871 |
$Type$Buffer ob |
|
872 |
= (b.isDirect() |
|
873 |
? $Type$Buffer.allocate(rb.capacity()) |
|
874 |
: $Type$Buffer.allocateDirect(rb.capacity())); |
|
875 |
rb.rewind(); |
|
876 |
ob.put(rb); |
|
877 |
#end[byte] |
|
878 |
||
879 |
relPut(b); // Required by testViews |
|
880 |
||
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
881 |
#if[byte] |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
882 |
// Test alignment |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
883 |
|
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
884 |
testAlign(b, direct); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
31873
diff
changeset
|
885 |
#end[byte] |
2 | 886 |
} |
887 |
||
888 |
#if[char] |
|
889 |
||
890 |
private static void testStr() { |
|
891 |
final String s = "abcdefghijklm"; |
|
892 |
int start = 3; |
|
893 |
int end = 9; |
|
894 |
final CharBuffer b = CharBuffer.wrap(s, start, end); |
|
895 |
show(0, b); |
|
896 |
ck(b, b.toString().equals(s.substring(start, end))); |
|
897 |
ck(b, b.toString().equals("defghi")); |
|
898 |
ck(b, b.isReadOnly()); |
|
37913 | 899 |
catchReadOnlyBuffer(b, () -> b.put('x')); |
2 | 900 |
ck(b, start, b.position()); |
901 |
ck(b, end, b.limit()); |
|
902 |
ck(b, s.length(), b.capacity()); |
|
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
903 |
b.position(6); |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1639
diff
changeset
|
904 |
ck(b, b.subSequence(0,3).toString().equals("ghi")); |
2 | 905 |
|
906 |
// The index, relative to the position, must be non-negative and |
|
907 |
// smaller than remaining(). |
|
37913 | 908 |
catchIndexOutOfBounds(b, () -> b.charAt(-1)); |
909 |
catchIndexOutOfBounds(b, () -> b.charAt(b.remaining())); |
|
2 | 910 |
// The index must be non-negative and less than the buffer's limit. |
37913 | 911 |
catchIndexOutOfBounds(b, () -> b.get(b.limit())); |
912 |
catchIndexOutOfBounds(b, () -> b.get(-1)); |
|
2 | 913 |
// The start must be non-negative and no larger than remaining(). |
37913 | 914 |
catchIndexOutOfBounds(b, () -> b.subSequence(-1, b.remaining())); |
915 |
catchIndexOutOfBounds(b, () -> b.subSequence(b.remaining() + 1, b.remaining())); |
|
2 | 916 |
|
917 |
// The end must be no smaller than start and no larger than |
|
918 |
// remaining(). |
|
37913 | 919 |
catchIndexOutOfBounds(b, () -> b.subSequence(2, 1)); |
920 |
catchIndexOutOfBounds(b, () -> b.subSequence(0, b.remaining() + 1)); |
|
2 | 921 |
|
922 |
// The offset must be non-negative and no larger than <array.length>. |
|
37913 | 923 |
catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, -1, s.length())); |
924 |
catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, s.length() + 1, s.length())); |
|
925 |
catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, 1, 0)); |
|
926 |
catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, 0, s.length() + 1)); |
|
2 | 927 |
} |
928 |
||
929 |
#end[char] |
|
930 |
||
931 |
public static void test(final $type$ [] ba) { |
|
932 |
int offset = 47; |
|
933 |
int length = 900; |
|
934 |
final $Type$Buffer b = $Type$Buffer.wrap(ba, offset, length); |
|
935 |
show(0, b); |
|
936 |
ck(b, b.capacity(), ba.length); |
|
937 |
ck(b, b.position(), offset); |
|
938 |
ck(b, b.limit(), offset + length); |
|
939 |
||
940 |
// The offset must be non-negative and no larger than <array.length>. |
|
37913 | 941 |
catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, -1, ba.length)); |
942 |
catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, ba.length + 1, ba.length)); |
|
943 |
catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, 0, -1)); |
|
944 |
catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, 0, ba.length + 1)); |
|
2 | 945 |
|
946 |
// A NullPointerException will be thrown if the array is null. |
|
37913 | 947 |
tryCatch(ba, NullPointerException.class, |
948 |
() -> $Type$Buffer.wrap(($type$ []) null, 0, 5)); |
|
949 |
tryCatch(ba, NullPointerException.class, |
|
950 |
() -> $Type$Buffer.wrap(($type$ []) null)); |
|
2 | 951 |
} |
952 |
||
953 |
private static void testAllocate() { |
|
954 |
// An IllegalArgumentException will be thrown for negative capacities. |
|
37913 | 955 |
catchIllegalArgument((Buffer) null, () -> $Type$Buffer.allocate(-1)); |
31873
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
956 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
957 |
$Type$Buffer.allocate(-1); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
958 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
959 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
960 |
fail("Non-null IllegalArgumentException message expected for" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
961 |
+ " attempt to allocate negative capacity buffer"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
962 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
963 |
} |
2 | 964 |
#if[byte] |
37913 | 965 |
catchIllegalArgument((Buffer) null, () -> $Type$Buffer.allocateDirect(-1)); |
31873
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
966 |
try { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
967 |
$Type$Buffer.allocateDirect(-1); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
968 |
} catch (IllegalArgumentException e) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
969 |
if (e.getMessage() == null) { |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
970 |
fail("Non-null IllegalArgumentException message expected for" |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
971 |
+ " attempt to allocate negative capacity direct buffer"); |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
972 |
} |
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
14342
diff
changeset
|
973 |
} |
2 | 974 |
#end[byte] |
975 |
} |
|
976 |
||
977 |
public static void test() { |
|
978 |
testAllocate(); |
|
979 |
test(0, $Type$Buffer.allocate(7 * 1024), false); |
|
980 |
test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false); |
|
981 |
test(new $type$[1024]); |
|
982 |
#if[byte] |
|
983 |
$Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024); |
|
984 |
for (b.position(0); b.position() < b.limit(); ) |
|
985 |
ck(b, b.get(), 0); |
|
986 |
test(0, b, true); |
|
987 |
#end[byte] |
|
988 |
#if[char] |
|
989 |
testStr(); |
|
990 |
#end[char] |
|
991 |
||
992 |
callReset($Type$Buffer.allocate(10)); |
|
993 |
||
994 |
#if[byte] |
|
995 |
#else[byte] |
|
996 |
putBuffer(); |
|
997 |
#end[byte] |
|
998 |
} |
|
999 |
||
1000 |
} |