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