2
|
1 |
/*
|
|
2 |
* Copyright 2000-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
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 |
|
|
40 |
private static void relGet($Type$Buffer b) {
|
|
41 |
int n = b.capacity();
|
|
42 |
$type$ v;
|
|
43 |
for (int i = 0; i < n; i++)
|
|
44 |
ck(b, (long)b.get(), (long)(($type$)ic(i)));
|
|
45 |
b.rewind();
|
|
46 |
}
|
|
47 |
|
|
48 |
private static void relGet($Type$Buffer b, int start) {
|
|
49 |
int n = b.remaining();
|
|
50 |
$type$ v;
|
|
51 |
for (int i = start; i < n; i++)
|
|
52 |
ck(b, (long)b.get(), (long)(($type$)ic(i)));
|
|
53 |
b.rewind();
|
|
54 |
}
|
|
55 |
|
|
56 |
private static void absGet($Type$Buffer b) {
|
|
57 |
int n = b.capacity();
|
|
58 |
$type$ v;
|
|
59 |
for (int i = 0; i < n; i++)
|
|
60 |
ck(b, (long)b.get(), (long)(($type$)ic(i)));
|
|
61 |
b.rewind();
|
|
62 |
}
|
|
63 |
|
|
64 |
private static void bulkGet($Type$Buffer b) {
|
|
65 |
int n = b.capacity();
|
|
66 |
$type$[] a = new $type$[n + 7];
|
|
67 |
b.get(a, 7, n);
|
|
68 |
for (int i = 0; i < n; i++)
|
|
69 |
ck(b, (long)a[i + 7], (long)(($type$)ic(i)));
|
|
70 |
}
|
|
71 |
|
|
72 |
private static void relPut($Type$Buffer b) {
|
|
73 |
int n = b.capacity();
|
|
74 |
b.clear();
|
|
75 |
for (int i = 0; i < n; i++)
|
|
76 |
b.put(($type$)ic(i));
|
|
77 |
b.flip();
|
|
78 |
}
|
|
79 |
|
|
80 |
private static void absPut($Type$Buffer b) {
|
|
81 |
int n = b.capacity();
|
|
82 |
b.clear();
|
|
83 |
for (int i = 0; i < n; i++)
|
|
84 |
b.put(i, ($type$)ic(i));
|
|
85 |
b.limit(n);
|
|
86 |
b.position(0);
|
|
87 |
}
|
|
88 |
|
|
89 |
private static void bulkPutArray($Type$Buffer b) {
|
|
90 |
int n = b.capacity();
|
|
91 |
b.clear();
|
|
92 |
$type$[] a = new $type$[n + 7];
|
|
93 |
for (int i = 0; i < n; i++)
|
|
94 |
a[i + 7] = ($type$)ic(i);
|
|
95 |
b.put(a, 7, n);
|
|
96 |
b.flip();
|
|
97 |
}
|
|
98 |
|
|
99 |
private static void bulkPutBuffer($Type$Buffer b) {
|
|
100 |
int n = b.capacity();
|
|
101 |
b.clear();
|
|
102 |
$Type$Buffer c = $Type$Buffer.allocate(n + 7);
|
|
103 |
c.position(7);
|
|
104 |
for (int i = 0; i < n; i++)
|
|
105 |
c.put(($type$)ic(i));
|
|
106 |
c.flip();
|
|
107 |
c.position(7);
|
|
108 |
b.put(c);
|
|
109 |
b.flip();
|
|
110 |
}
|
|
111 |
|
|
112 |
//6231529
|
|
113 |
private static void callReset($Type$Buffer b) {
|
|
114 |
b.position(0);
|
|
115 |
b.mark();
|
|
116 |
|
|
117 |
b.duplicate().reset();
|
|
118 |
b.asReadOnlyBuffer().reset();
|
|
119 |
}
|
|
120 |
|
|
121 |
#if[byte]
|
|
122 |
#else[byte]
|
|
123 |
// 6221101-6234263
|
|
124 |
|
|
125 |
private static void putBuffer() {
|
|
126 |
final int cap = 10;
|
|
127 |
|
|
128 |
$Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
|
|
129 |
$Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer();
|
|
130 |
direct1.put(nondirect1);
|
|
131 |
|
|
132 |
$Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
|
|
133 |
$Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer();
|
|
134 |
nondirect2.put(direct2);
|
|
135 |
|
|
136 |
$Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
|
|
137 |
$Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
|
|
138 |
direct3.put(direct4);
|
|
139 |
|
|
140 |
$Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer();
|
|
141 |
$Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer();
|
|
142 |
nondirect3.put(nondirect4);
|
|
143 |
}
|
|
144 |
#end[byte]
|
|
145 |
|
|
146 |
#if[char]
|
|
147 |
|
|
148 |
private static void bulkPutString($Type$Buffer b) {
|
|
149 |
int n = b.capacity();
|
|
150 |
b.clear();
|
|
151 |
StringBuffer sb = new StringBuffer(n + 7);
|
|
152 |
sb.append("1234567");
|
|
153 |
for (int i = 0; i < n; i++)
|
|
154 |
sb.append((char)ic(i));
|
|
155 |
b.put(sb.toString(), 7, 7 + n);
|
|
156 |
b.flip();
|
|
157 |
}
|
|
158 |
|
|
159 |
#end[char]
|
|
160 |
|
|
161 |
private static void checkSlice($Type$Buffer b, $Type$Buffer slice) {
|
|
162 |
ck(slice, 0, slice.position());
|
|
163 |
ck(slice, b.remaining(), slice.limit());
|
|
164 |
ck(slice, b.remaining(), slice.capacity());
|
|
165 |
if (b.isDirect() != slice.isDirect())
|
|
166 |
fail("Lost direction", slice);
|
|
167 |
if (b.isReadOnly() != slice.isReadOnly())
|
|
168 |
fail("Lost read-only", slice);
|
|
169 |
}
|
|
170 |
|
|
171 |
#if[byte]
|
|
172 |
|
|
173 |
private static void checkBytes(ByteBuffer b, byte[] bs) {
|
|
174 |
int n = bs.length;
|
|
175 |
int p = b.position();
|
|
176 |
byte v;
|
|
177 |
if (b.order() == ByteOrder.BIG_ENDIAN) {
|
|
178 |
for (int i = 0; i < n; i++)
|
|
179 |
ck(b, b.get(), bs[i]);
|
|
180 |
} else {
|
|
181 |
for (int i = n - 1; i >= 0; i--)
|
|
182 |
ck(b, b.get(), bs[i]);
|
|
183 |
}
|
|
184 |
b.position(p);
|
|
185 |
}
|
|
186 |
|
|
187 |
private static void testViews(int level, ByteBuffer b, boolean direct) {
|
|
188 |
|
|
189 |
ShortBuffer sb = b.asShortBuffer();
|
|
190 |
BasicShort.test(level, sb, direct);
|
|
191 |
checkBytes(b, new byte[] { 0, (byte)ic(0) });
|
|
192 |
|
|
193 |
CharBuffer cb = b.asCharBuffer();
|
|
194 |
BasicChar.test(level, cb, direct);
|
|
195 |
checkBytes(b, new byte[] { 0, (byte)ic(0) });
|
|
196 |
|
|
197 |
IntBuffer ib = b.asIntBuffer();
|
|
198 |
BasicInt.test(level, ib, direct);
|
|
199 |
checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
|
|
200 |
|
|
201 |
LongBuffer lb = b.asLongBuffer();
|
|
202 |
BasicLong.test(level, lb, direct);
|
|
203 |
checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
|
|
204 |
|
|
205 |
FloatBuffer fb = b.asFloatBuffer();
|
|
206 |
BasicFloat.test(level, fb, direct);
|
|
207 |
checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
|
|
208 |
|
|
209 |
DoubleBuffer db = b.asDoubleBuffer();
|
|
210 |
BasicDouble.test(level, db, direct);
|
|
211 |
checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
|
|
212 |
|
|
213 |
}
|
|
214 |
|
|
215 |
private static void testHet(int level, ByteBuffer b) {
|
|
216 |
|
|
217 |
int p = b.position();
|
|
218 |
b.limit(b.capacity());
|
|
219 |
show(level, b);
|
|
220 |
out.print(" put:");
|
|
221 |
|
|
222 |
b.putChar((char)1);
|
|
223 |
b.putChar((char)Character.MAX_VALUE);
|
|
224 |
out.print(" char");
|
|
225 |
|
|
226 |
b.putShort((short)1);
|
|
227 |
b.putShort((short)Short.MAX_VALUE);
|
|
228 |
out.print(" short");
|
|
229 |
|
|
230 |
b.putInt(1);
|
|
231 |
b.putInt(Integer.MAX_VALUE);
|
|
232 |
out.print(" int");
|
|
233 |
|
|
234 |
b.putLong((long)1);
|
|
235 |
b.putLong((long)Long.MAX_VALUE);
|
|
236 |
out.print(" long");
|
|
237 |
|
|
238 |
b.putFloat((float)1);
|
|
239 |
b.putFloat((float)Float.MIN_VALUE);
|
|
240 |
b.putFloat((float)Float.MAX_VALUE);
|
|
241 |
out.print(" float");
|
|
242 |
|
|
243 |
b.putDouble((double)1);
|
|
244 |
b.putDouble((double)Double.MIN_VALUE);
|
|
245 |
b.putDouble((double)Double.MAX_VALUE);
|
|
246 |
out.print(" double");
|
|
247 |
|
|
248 |
out.println();
|
|
249 |
b.limit(b.position());
|
|
250 |
b.position(p);
|
|
251 |
show(level, b);
|
|
252 |
out.print(" get:");
|
|
253 |
|
|
254 |
ck(b, b.getChar(), 1);
|
|
255 |
ck(b, b.getChar(), Character.MAX_VALUE);
|
|
256 |
out.print(" char");
|
|
257 |
|
|
258 |
ck(b, b.getShort(), 1);
|
|
259 |
ck(b, b.getShort(), Short.MAX_VALUE);
|
|
260 |
out.print(" short");
|
|
261 |
|
|
262 |
ck(b, b.getInt(), 1);
|
|
263 |
ck(b, b.getInt(), Integer.MAX_VALUE);
|
|
264 |
out.print(" int");
|
|
265 |
|
|
266 |
ck(b, b.getLong(), 1);
|
|
267 |
ck(b, b.getLong(), Long.MAX_VALUE);
|
|
268 |
out.print(" long");
|
|
269 |
|
|
270 |
ck(b, (long)b.getFloat(), 1);
|
|
271 |
ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE);
|
|
272 |
ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE);
|
|
273 |
out.print(" float");
|
|
274 |
|
|
275 |
ck(b, (long)b.getDouble(), 1);
|
|
276 |
ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE);
|
|
277 |
ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE);
|
|
278 |
out.print(" double");
|
|
279 |
|
|
280 |
out.println();
|
|
281 |
|
|
282 |
}
|
|
283 |
|
|
284 |
#end[byte]
|
|
285 |
|
|
286 |
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
|
|
287 |
boolean caught = false;
|
|
288 |
try {
|
|
289 |
thunk.run();
|
|
290 |
} catch (Throwable x) {
|
|
291 |
if (ex.isAssignableFrom(x.getClass()))
|
|
292 |
caught = true;
|
|
293 |
}
|
|
294 |
if (!caught)
|
|
295 |
fail(ex.getName() + " not thrown", b);
|
|
296 |
}
|
|
297 |
|
|
298 |
private static void tryCatch($type$ [] t, Class ex, Runnable thunk) {
|
|
299 |
tryCatch($Type$Buffer.wrap(t), ex, thunk);
|
|
300 |
}
|
|
301 |
|
|
302 |
public static void test(int level, final $Type$Buffer b, boolean direct) {
|
|
303 |
|
|
304 |
show(level, b);
|
|
305 |
|
|
306 |
if (direct != b.isDirect())
|
|
307 |
fail("Wrong direction", b);
|
|
308 |
|
|
309 |
// Gets and puts
|
|
310 |
|
|
311 |
relPut(b);
|
|
312 |
relGet(b);
|
|
313 |
absGet(b);
|
|
314 |
bulkGet(b);
|
|
315 |
|
|
316 |
absPut(b);
|
|
317 |
relGet(b);
|
|
318 |
absGet(b);
|
|
319 |
bulkGet(b);
|
|
320 |
|
|
321 |
bulkPutArray(b);
|
|
322 |
relGet(b);
|
|
323 |
|
|
324 |
bulkPutBuffer(b);
|
|
325 |
relGet(b);
|
|
326 |
|
|
327 |
#if[char]
|
|
328 |
|
|
329 |
bulkPutString(b);
|
|
330 |
relGet(b);
|
|
331 |
b.position(1);
|
|
332 |
b.limit(7);
|
|
333 |
ck(b, b.toString().equals("bcdefg"));
|
|
334 |
|
|
335 |
// CharSequence ops
|
|
336 |
|
|
337 |
b.position(2);
|
|
338 |
ck(b, b.charAt(1), 'd');
|
|
339 |
CharBuffer c = (CharBuffer)b.subSequence(1, 4);
|
|
340 |
ck(b, b.subSequence(1, 4).toString().equals("def"));
|
|
341 |
|
|
342 |
// 4938424
|
|
343 |
b.position(4);
|
|
344 |
ck(b, b.charAt(1), 'f');
|
|
345 |
ck(b, b.subSequence(1, 3).toString().equals("fg"));
|
|
346 |
|
|
347 |
#end[char]
|
|
348 |
|
|
349 |
// Compact
|
|
350 |
|
|
351 |
relPut(b);
|
|
352 |
b.position(13);
|
|
353 |
b.compact();
|
|
354 |
b.flip();
|
|
355 |
relGet(b, 13);
|
|
356 |
|
|
357 |
// Exceptions
|
|
358 |
|
|
359 |
boolean caught = false;
|
|
360 |
relPut(b);
|
|
361 |
b.limit(b.capacity() / 2);
|
|
362 |
b.position(b.limit());
|
|
363 |
|
|
364 |
tryCatch(b, BufferUnderflowException.class, new Runnable() {
|
|
365 |
public void run() {
|
|
366 |
b.get();
|
|
367 |
}});
|
|
368 |
|
|
369 |
tryCatch(b, BufferOverflowException.class, new Runnable() {
|
|
370 |
public void run() {
|
|
371 |
b.put(($type$)42);
|
|
372 |
}});
|
|
373 |
|
|
374 |
// The index must be non-negative and lesss than the buffer's limit.
|
|
375 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
376 |
public void run() {
|
|
377 |
b.get(b.limit());
|
|
378 |
}});
|
|
379 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
380 |
public void run() {
|
|
381 |
b.get(-1);
|
|
382 |
}});
|
|
383 |
|
|
384 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
385 |
public void run() {
|
|
386 |
b.put(b.limit(), ($type$)42);
|
|
387 |
}});
|
|
388 |
|
|
389 |
// Values
|
|
390 |
|
|
391 |
b.clear();
|
|
392 |
b.put(($type$)0);
|
|
393 |
b.put(($type$)-1);
|
|
394 |
b.put(($type$)1);
|
|
395 |
b.put($Fulltype$.MAX_VALUE);
|
|
396 |
b.put($Fulltype$.MIN_VALUE);
|
|
397 |
#if[float]
|
|
398 |
b.put(-Float.MAX_VALUE);
|
|
399 |
b.put(-Float.MIN_VALUE);
|
|
400 |
b.put(Float.NEGATIVE_INFINITY);
|
|
401 |
b.put(Float.POSITIVE_INFINITY);
|
|
402 |
b.put(Float.NaN);
|
|
403 |
b.put(0.91697687f); // Changes value if incorrectly swapped
|
|
404 |
#end[float]
|
|
405 |
#if[double]
|
|
406 |
b.put(-Double.MAX_VALUE);
|
|
407 |
b.put(-Double.MIN_VALUE);
|
|
408 |
b.put(Double.NEGATIVE_INFINITY);
|
|
409 |
b.put(Double.POSITIVE_INFINITY);
|
|
410 |
b.put(Double.NaN);
|
|
411 |
b.put(0.5121609353879392); // Changes value if incorrectly swapped
|
|
412 |
#end[double]
|
|
413 |
|
|
414 |
$type$ v;
|
|
415 |
b.flip();
|
|
416 |
ck(b, b.get(), 0);
|
|
417 |
ck(b, b.get(), ($type$)-1);
|
|
418 |
ck(b, b.get(), 1);
|
|
419 |
ck(b, b.get(), $Fulltype$.MAX_VALUE);
|
|
420 |
ck(b, b.get(), $Fulltype$.MIN_VALUE);
|
|
421 |
|
|
422 |
#if[float]
|
|
423 |
ck(b, b.get(), -Float.MAX_VALUE);
|
|
424 |
ck(b, b.get(), -Float.MIN_VALUE);
|
|
425 |
ck(b, b.get(), Float.NEGATIVE_INFINITY);
|
|
426 |
ck(b, b.get(), Float.POSITIVE_INFINITY);
|
|
427 |
if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN))
|
|
428 |
fail(b, (long)Float.NaN, (long)v);
|
|
429 |
ck(b, b.get(), 0.91697687f);
|
|
430 |
#end[float]
|
|
431 |
#if[double]
|
|
432 |
ck(b, b.get(), -Double.MAX_VALUE);
|
|
433 |
ck(b, b.get(), -Double.MIN_VALUE);
|
|
434 |
ck(b, b.get(), Double.NEGATIVE_INFINITY);
|
|
435 |
ck(b, b.get(), Double.POSITIVE_INFINITY);
|
|
436 |
if (Double.doubleToRawLongBits(v = b.get())
|
|
437 |
!= Double.doubleToRawLongBits(Double.NaN))
|
|
438 |
fail(b, (long)Double.NaN, (long)v);
|
|
439 |
ck(b, b.get(), 0.5121609353879392);
|
|
440 |
#end[double]
|
|
441 |
|
|
442 |
|
|
443 |
// Comparison
|
|
444 |
b.rewind();
|
|
445 |
$Type$Buffer b2 = $Type$Buffer.allocate(b.capacity());
|
|
446 |
b2.put(b);
|
|
447 |
b2.flip();
|
|
448 |
b.position(2);
|
|
449 |
b2.position(2);
|
|
450 |
if (!b.equals(b2)) {
|
|
451 |
for (int i = 2; i < b.limit(); i++) {
|
|
452 |
$type$ x = b.get(i);
|
|
453 |
$type$ y = b2.get(i);
|
|
454 |
if (x != y
|
|
455 |
#if[double]
|
|
456 |
|| Double.compare(x, y) != 0
|
|
457 |
#end[double]
|
|
458 |
#if[float]
|
|
459 |
|| Float.compare(x, y) != 0
|
|
460 |
#end[float]
|
|
461 |
)
|
|
462 |
out.println("[" + i + "] " + x + " != " + y);
|
|
463 |
}
|
|
464 |
fail("Identical buffers not equal", b, b2);
|
|
465 |
}
|
|
466 |
if (b.compareTo(b2) != 0)
|
|
467 |
fail("Comparison to identical buffer != 0", b, b2);
|
|
468 |
|
|
469 |
b.limit(b.limit() + 1);
|
|
470 |
b.position(b.limit() - 1);
|
|
471 |
b.put(($type$)99);
|
|
472 |
b.rewind();
|
|
473 |
b2.rewind();
|
|
474 |
if (b.equals(b2))
|
|
475 |
fail("Non-identical buffers equal", b, b2);
|
|
476 |
if (b.compareTo(b2) <= 0)
|
|
477 |
fail("Comparison to shorter buffer <= 0", b, b2);
|
|
478 |
b.limit(b.limit() - 1);
|
|
479 |
|
|
480 |
b.put(2, ($type$)42);
|
|
481 |
if (b.equals(b2))
|
|
482 |
fail("Non-identical buffers equal", b, b2);
|
|
483 |
if (b.compareTo(b2) <= 0)
|
|
484 |
fail("Comparison to lesser buffer <= 0", b, b2);
|
|
485 |
|
|
486 |
// Sub, dup
|
|
487 |
|
|
488 |
relPut(b);
|
|
489 |
relGet(b.duplicate());
|
|
490 |
b.position(13);
|
|
491 |
relGet(b.duplicate(), 13);
|
|
492 |
relGet(b.duplicate().slice(), 13);
|
|
493 |
relGet(b.slice(), 13);
|
|
494 |
relGet(b.slice().duplicate(), 13);
|
|
495 |
|
|
496 |
// Slice
|
|
497 |
|
|
498 |
b.position(5);
|
|
499 |
$Type$Buffer sb = b.slice();
|
|
500 |
checkSlice(b, sb);
|
|
501 |
b.position(0);
|
|
502 |
$Type$Buffer sb2 = sb.slice();
|
|
503 |
checkSlice(sb, sb2);
|
|
504 |
|
|
505 |
if (!sb.equals(sb2))
|
|
506 |
fail("Sliced slices do not match", sb, sb2);
|
|
507 |
if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
|
|
508 |
fail("Array offsets do not match: "
|
|
509 |
+ sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
|
|
510 |
|
|
511 |
#if[byte]
|
|
512 |
|
|
513 |
// Views
|
|
514 |
|
|
515 |
b.clear();
|
|
516 |
b.order(ByteOrder.BIG_ENDIAN);
|
|
517 |
testViews(level + 1, b, direct);
|
|
518 |
|
|
519 |
for (int i = 1; i <= 9; i++) {
|
|
520 |
b.position(i);
|
|
521 |
show(level + 1, b);
|
|
522 |
testViews(level + 2, b, direct);
|
|
523 |
}
|
|
524 |
|
|
525 |
b.position(0);
|
|
526 |
b.order(ByteOrder.LITTLE_ENDIAN);
|
|
527 |
testViews(level + 1, b, direct);
|
|
528 |
|
|
529 |
// Heterogeneous accessors
|
|
530 |
|
|
531 |
b.order(ByteOrder.BIG_ENDIAN);
|
|
532 |
for (int i = 0; i <= 9; i++) {
|
|
533 |
b.position(i);
|
|
534 |
testHet(level + 1, b);
|
|
535 |
}
|
|
536 |
b.order(ByteOrder.LITTLE_ENDIAN);
|
|
537 |
b.position(3);
|
|
538 |
testHet(level + 1, b);
|
|
539 |
|
|
540 |
#end[byte]
|
|
541 |
|
|
542 |
// Read-only views
|
|
543 |
|
|
544 |
b.rewind();
|
|
545 |
final $Type$Buffer rb = b.asReadOnlyBuffer();
|
|
546 |
if (!b.equals(rb))
|
|
547 |
fail("Buffer not equal to read-only view", b, rb);
|
|
548 |
show(level + 1, rb);
|
|
549 |
|
|
550 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
551 |
public void run() {
|
|
552 |
relPut(rb);
|
|
553 |
}});
|
|
554 |
|
|
555 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
556 |
public void run() {
|
|
557 |
absPut(rb);
|
|
558 |
}});
|
|
559 |
|
|
560 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
561 |
public void run() {
|
|
562 |
bulkPutArray(rb);
|
|
563 |
}});
|
|
564 |
|
|
565 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
566 |
public void run() {
|
|
567 |
bulkPutBuffer(rb);
|
|
568 |
}});
|
|
569 |
|
|
570 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
571 |
public void run() {
|
|
572 |
rb.compact();
|
|
573 |
}});
|
|
574 |
|
|
575 |
#if[byte]
|
|
576 |
|
|
577 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
578 |
public void run() {
|
|
579 |
rb.putChar((char)1);
|
|
580 |
}});
|
|
581 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
582 |
public void run() {
|
|
583 |
rb.putChar(0, (char)1);
|
|
584 |
}});
|
|
585 |
|
|
586 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
587 |
public void run() {
|
|
588 |
rb.putShort((short)1);
|
|
589 |
}});
|
|
590 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
591 |
public void run() {
|
|
592 |
rb.putShort(0, (short)1);
|
|
593 |
}});
|
|
594 |
|
|
595 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
596 |
public void run() {
|
|
597 |
rb.putInt(1);
|
|
598 |
}});
|
|
599 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
600 |
public void run() {
|
|
601 |
rb.putInt(0, 1);
|
|
602 |
}});
|
|
603 |
|
|
604 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
605 |
public void run() {
|
|
606 |
rb.putLong((long)1);
|
|
607 |
}});
|
|
608 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
609 |
public void run() {
|
|
610 |
rb.putLong(0, (long)1);
|
|
611 |
}});
|
|
612 |
|
|
613 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
614 |
public void run() {
|
|
615 |
rb.putFloat((float)1);
|
|
616 |
}});
|
|
617 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
618 |
public void run() {
|
|
619 |
rb.putFloat(0, (float)1);
|
|
620 |
}});
|
|
621 |
|
|
622 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
623 |
public void run() {
|
|
624 |
rb.putDouble((double)1);
|
|
625 |
}});
|
|
626 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
627 |
public void run() {
|
|
628 |
rb.putDouble(0, (double)1);
|
|
629 |
}});
|
|
630 |
|
|
631 |
#end[byte]
|
|
632 |
|
|
633 |
if (rb.getClass().getName().startsWith("java.nio.Heap")) {
|
|
634 |
|
|
635 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
636 |
public void run() {
|
|
637 |
rb.array();
|
|
638 |
}});
|
|
639 |
|
|
640 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
641 |
public void run() {
|
|
642 |
rb.arrayOffset();
|
|
643 |
}});
|
|
644 |
|
|
645 |
if (rb.hasArray())
|
|
646 |
fail("Read-only heap buffer's backing array is accessible",
|
|
647 |
rb);
|
|
648 |
|
|
649 |
}
|
|
650 |
|
|
651 |
// Bulk puts from read-only buffers
|
|
652 |
|
|
653 |
b.clear();
|
|
654 |
rb.rewind();
|
|
655 |
b.put(rb);
|
|
656 |
|
|
657 |
#if[byte]
|
|
658 |
// For byte buffers, test both the direct and non-direct cases
|
|
659 |
$Type$Buffer ob
|
|
660 |
= (b.isDirect()
|
|
661 |
? $Type$Buffer.allocate(rb.capacity())
|
|
662 |
: $Type$Buffer.allocateDirect(rb.capacity()));
|
|
663 |
rb.rewind();
|
|
664 |
ob.put(rb);
|
|
665 |
#end[byte]
|
|
666 |
|
|
667 |
relPut(b); // Required by testViews
|
|
668 |
|
|
669 |
}
|
|
670 |
|
|
671 |
#if[char]
|
|
672 |
|
|
673 |
private static void testStr() {
|
|
674 |
final String s = "abcdefghijklm";
|
|
675 |
int start = 3;
|
|
676 |
int end = 9;
|
|
677 |
final CharBuffer b = CharBuffer.wrap(s, start, end);
|
|
678 |
show(0, b);
|
|
679 |
ck(b, b.toString().equals(s.substring(start, end)));
|
|
680 |
ck(b, b.toString().equals("defghi"));
|
|
681 |
ck(b, b.isReadOnly());
|
|
682 |
tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
|
|
683 |
public void run() {
|
|
684 |
b.put('x');
|
|
685 |
}});
|
|
686 |
ck(b, start, b.position());
|
|
687 |
ck(b, end, b.limit());
|
|
688 |
ck(b, s.length(), b.capacity());
|
|
689 |
|
|
690 |
// The index, relative to the position, must be non-negative and
|
|
691 |
// smaller than remaining().
|
|
692 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
693 |
public void run() {
|
|
694 |
b.charAt(-1);
|
|
695 |
}});
|
|
696 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
697 |
public void run() {
|
|
698 |
b.charAt(b.remaining());
|
|
699 |
}});
|
|
700 |
|
|
701 |
// The index must be non-negative and less than the buffer's limit.
|
|
702 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
703 |
public void run() {
|
|
704 |
b.get(b.limit());
|
|
705 |
}});
|
|
706 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
707 |
public void run() {
|
|
708 |
b.get(-1);
|
|
709 |
}});
|
|
710 |
|
|
711 |
// The start must be non-negative and no larger than remaining().
|
|
712 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
713 |
public void run() {
|
|
714 |
b.subSequence(-1, b.remaining());
|
|
715 |
}});
|
|
716 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
717 |
public void run() {
|
|
718 |
b.subSequence(b.remaining() + 1, b.remaining());
|
|
719 |
}});
|
|
720 |
|
|
721 |
// The end must be no smaller than start and no larger than
|
|
722 |
// remaining().
|
|
723 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
724 |
public void run() {
|
|
725 |
b.subSequence(2, 1);
|
|
726 |
}});
|
|
727 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
728 |
public void run() {
|
|
729 |
b.subSequence(0, b.remaining() + 1);
|
|
730 |
}});
|
|
731 |
|
|
732 |
// The offset must be non-negative and no larger than <array.length>.
|
|
733 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
734 |
public void run() {
|
|
735 |
$Type$Buffer.wrap(s, -1, s.length());
|
|
736 |
}});
|
|
737 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
738 |
public void run() {
|
|
739 |
$Type$Buffer.wrap(s, s.length() + 1, s.length());
|
|
740 |
}});
|
|
741 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
742 |
public void run() {
|
|
743 |
$Type$Buffer.wrap(s, 1, 0);
|
|
744 |
}});
|
|
745 |
tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
|
|
746 |
public void run() {
|
|
747 |
$Type$Buffer.wrap(s, 0, s.length() + 1);
|
|
748 |
}});
|
|
749 |
}
|
|
750 |
|
|
751 |
#end[char]
|
|
752 |
|
|
753 |
public static void test(final $type$ [] ba) {
|
|
754 |
int offset = 47;
|
|
755 |
int length = 900;
|
|
756 |
final $Type$Buffer b = $Type$Buffer.wrap(ba, offset, length);
|
|
757 |
show(0, b);
|
|
758 |
ck(b, b.capacity(), ba.length);
|
|
759 |
ck(b, b.position(), offset);
|
|
760 |
ck(b, b.limit(), offset + length);
|
|
761 |
|
|
762 |
// The offset must be non-negative and no larger than <array.length>.
|
|
763 |
tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
|
|
764 |
public void run() {
|
|
765 |
$Type$Buffer.wrap(ba, -1, ba.length);
|
|
766 |
}});
|
|
767 |
tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
|
|
768 |
public void run() {
|
|
769 |
$Type$Buffer.wrap(ba, ba.length + 1, ba.length);
|
|
770 |
}});
|
|
771 |
tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
|
|
772 |
public void run() {
|
|
773 |
$Type$Buffer.wrap(ba, 0, -1);
|
|
774 |
}});
|
|
775 |
tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
|
|
776 |
public void run() {
|
|
777 |
$Type$Buffer.wrap(ba, 0, ba.length + 1);
|
|
778 |
}});
|
|
779 |
|
|
780 |
// A NullPointerException will be thrown if the array is null.
|
|
781 |
tryCatch(ba, NullPointerException.class, new Runnable() {
|
|
782 |
public void run() {
|
|
783 |
$Type$Buffer.wrap(($type$ []) null, 0, 5);
|
|
784 |
}});
|
|
785 |
tryCatch(ba, NullPointerException.class, new Runnable() {
|
|
786 |
public void run() {
|
|
787 |
$Type$Buffer.wrap(($type$ []) null);
|
|
788 |
}});
|
|
789 |
}
|
|
790 |
|
|
791 |
private static void testAllocate() {
|
|
792 |
// An IllegalArgumentException will be thrown for negative capacities.
|
|
793 |
tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
|
|
794 |
public void run() {
|
|
795 |
$Type$Buffer.allocate(-1);
|
|
796 |
}});
|
|
797 |
#if[byte]
|
|
798 |
tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
|
|
799 |
public void run() {
|
|
800 |
$Type$Buffer.allocateDirect(-1);
|
|
801 |
}});
|
|
802 |
#end[byte]
|
|
803 |
}
|
|
804 |
|
|
805 |
public static void test() {
|
|
806 |
testAllocate();
|
|
807 |
test(0, $Type$Buffer.allocate(7 * 1024), false);
|
|
808 |
test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false);
|
|
809 |
test(new $type$[1024]);
|
|
810 |
#if[byte]
|
|
811 |
$Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024);
|
|
812 |
for (b.position(0); b.position() < b.limit(); )
|
|
813 |
ck(b, b.get(), 0);
|
|
814 |
test(0, b, true);
|
|
815 |
#end[byte]
|
|
816 |
#if[char]
|
|
817 |
testStr();
|
|
818 |
#end[char]
|
|
819 |
|
|
820 |
callReset($Type$Buffer.allocate(10));
|
|
821 |
|
|
822 |
#if[byte]
|
|
823 |
#else[byte]
|
|
824 |
putBuffer();
|
|
825 |
#end[byte]
|
|
826 |
}
|
|
827 |
|
|
828 |
}
|