blob: 539af2c62d0b3e03087d94ea004a4d2a25513fa3 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
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// -- This file was mechanically generated: Do not edit! -- //
32
33import java.nio.*;
34
35
36public class BasicByte
37 extends Basic
38{
39
40 private static void relGet(ByteBuffer b) {
41 int n = b.capacity();
42 byte v;
43 for (int i = 0; i < n; i++)
44 ck(b, (long)b.get(), (long)((byte)ic(i)));
45 b.rewind();
46 }
47
48 private static void relGet(ByteBuffer b, int start) {
49 int n = b.remaining();
50 byte v;
51 for (int i = start; i < n; i++)
52 ck(b, (long)b.get(), (long)((byte)ic(i)));
53 b.rewind();
54 }
55
56 private static void absGet(ByteBuffer b) {
57 int n = b.capacity();
58 byte v;
59 for (int i = 0; i < n; i++)
60 ck(b, (long)b.get(), (long)((byte)ic(i)));
61 b.rewind();
62 }
63
64 private static void bulkGet(ByteBuffer b) {
65 int n = b.capacity();
66 byte[] a = new byte[n + 7];
67 b.get(a, 7, n);
68 for (int i = 0; i < n; i++)
69 ck(b, (long)a[i + 7], (long)((byte)ic(i)));
70 }
71
72 private static void relPut(ByteBuffer b) {
73 int n = b.capacity();
74 b.clear();
75 for (int i = 0; i < n; i++)
76 b.put((byte)ic(i));
77 b.flip();
78 }
79
80 private static void absPut(ByteBuffer b) {
81 int n = b.capacity();
82 b.clear();
83 for (int i = 0; i < n; i++)
84 b.put(i, (byte)ic(i));
85 b.limit(n);
86 b.position(0);
87 }
88
89 private static void bulkPutArray(ByteBuffer b) {
90 int n = b.capacity();
91 b.clear();
92 byte[] a = new byte[n + 7];
93 for (int i = 0; i < n; i++)
94 a[i + 7] = (byte)ic(i);
95 b.put(a, 7, n);
96 b.flip();
97 }
98
99 private static void bulkPutBuffer(ByteBuffer b) {
100 int n = b.capacity();
101 b.clear();
102 ByteBuffer c = ByteBuffer.allocate(n + 7);
103 c.position(7);
104 for (int i = 0; i < n; i++)
105 c.put((byte)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(ByteBuffer b) {
114 b.position(0);
115 b.mark();
116
117 b.duplicate().reset();
118 b.asReadOnlyBuffer().reset();
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 private static void checkSlice(ByteBuffer b, ByteBuffer 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
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
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(byte [] t, Class ex, Runnable thunk) {
299 tryCatch(ByteBuffer.wrap(t), ex, thunk);
300 }
301
302 public static void test(int level, final ByteBuffer 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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((byte)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(), (byte)42);
387 }});
388
389 // Values
390
391 b.clear();
392 b.put((byte)0);
393 b.put((byte)-1);
394 b.put((byte)1);
395 b.put(Byte.MAX_VALUE);
396 b.put(Byte.MIN_VALUE);
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 byte v;
415 b.flip();
416 ck(b, b.get(), 0);
417 ck(b, b.get(), (byte)-1);
418 ck(b, b.get(), 1);
419 ck(b, b.get(), Byte.MAX_VALUE);
420 ck(b, b.get(), Byte.MIN_VALUE);
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 // Comparison
444 b.rewind();
445 ByteBuffer b2 = ByteBuffer.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 byte x = b.get(i);
453 byte y = b2.get(i);
454 if (x != y
455
456
457
458
459
460
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((byte)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, (byte)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 ByteBuffer sb = b.slice();
500 checkSlice(b, sb);
501 b.position(0);
502 ByteBuffer 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
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
541
542 // Read-only views
543
544 b.rewind();
545 final ByteBuffer 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
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
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
658 // For byte buffers, test both the direct and non-direct cases
659 ByteBuffer ob
660 = (b.isDirect()
661 ? ByteBuffer.allocate(rb.capacity())
662 : ByteBuffer.allocateDirect(rb.capacity()));
663 rb.rewind();
664 ob.put(rb);
665
666
667 relPut(b); // Required by testViews
668
669 }
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753 public static void test(final byte [] ba) {
754 int offset = 47;
755 int length = 900;
756 final ByteBuffer b = ByteBuffer.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 ByteBuffer.wrap(ba, -1, ba.length);
766 }});
767 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
768 public void run() {
769 ByteBuffer.wrap(ba, ba.length + 1, ba.length);
770 }});
771 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
772 public void run() {
773 ByteBuffer.wrap(ba, 0, -1);
774 }});
775 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
776 public void run() {
777 ByteBuffer.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 ByteBuffer.wrap((byte []) null, 0, 5);
784 }});
785 tryCatch(ba, NullPointerException.class, new Runnable() {
786 public void run() {
787 ByteBuffer.wrap((byte []) 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 ByteBuffer.allocate(-1);
796 }});
797
798 tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
799 public void run() {
800 ByteBuffer.allocateDirect(-1);
801 }});
802
803 }
804
805 public static void test() {
806 testAllocate();
807 test(0, ByteBuffer.allocate(7 * 1024), false);
808 test(0, ByteBuffer.wrap(new byte[7 * 1024], 0, 7 * 1024), false);
809 test(new byte[1024]);
810
811 ByteBuffer b = ByteBuffer.allocateDirect(7 * 1024);
812 for (b.position(0); b.position() < b.limit(); )
813 ck(b, b.get(), 0);
814 test(0, b, true);
815
816
817
818
819
820 callReset(ByteBuffer.allocate(10));
821
822
823
824
825
826 }
827
828}