1 /*
2 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
70 ck(b, (long)b.get(), (long)((long)ic(i)));
71 b.rewind();
72 }
73
74 private static void absGet(LongBuffer b) {
75 int n = b.capacity();
76 for (int i = 0; i < n; i++)
77 ck(b, (long)b.get(), (long)((long)ic(i)));
78 b.rewind();
79 }
80
81 private static void bulkGet(LongBuffer b) {
82 int n = b.capacity();
83 long[] a = new long[n + 7];
84 b.get(a, 7, n);
85 for (int i = 0; i < n; i++) {
86 ck(b, (long)a[i + 7], (long)((long)ic(i)));
87 }
88 }
89
90 private static void relPut(LongBuffer b) {
91 int n = b.capacity();
92 b.clear();
93 for (int i = 0; i < n; i++)
94 b.put((long)ic(i));
95 b.flip();
96 }
97
98 private static void absPut(LongBuffer b) {
99 int n = b.capacity();
100 b.clear();
101 for (int i = 0; i < n; i++)
102 b.put(i, (long)ic(i));
103 b.limit(n);
104 b.position(0);
105 }
106
107 private static void bulkPutArray(LongBuffer b) {
108 int n = b.capacity();
109 b.clear();
119 b.clear();
120 LongBuffer c = LongBuffer.allocate(n + 7);
121 c.position(7);
122 for (int i = 0; i < n; i++)
123 c.put((long)ic(i));
124 c.flip();
125 c.position(7);
126 b.put(c);
127 b.flip();
128 try {
129 b.put(b);
130 fail("IllegalArgumentException expected for put into same buffer");
131 } catch (IllegalArgumentException e) {
132 if (e.getMessage() == null) {
133 fail("Non-null IllegalArgumentException message expected from"
134 + " put into same buffer");
135 }
136 }
137 }
138
139 //6231529
140 private static void callReset(LongBuffer b) {
141 b.position(0);
142 b.mark();
143
144 b.duplicate().reset();
145 b.asReadOnlyBuffer().reset();
146 }
147
148
149
150 // 6221101-6234263
151
152 private static void putBuffer() {
153 final int cap = 10;
154
155 LongBuffer direct1 = ByteBuffer.allocateDirect(cap).asLongBuffer();
156 LongBuffer nondirect1 = ByteBuffer.allocate(cap).asLongBuffer();
157 direct1.put(nondirect1);
158
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449 private static void fail(String problem,
450 LongBuffer xb, LongBuffer yb,
451 long x, long y) {
452 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
453 }
454
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(long[] t, Runnable thunk) {
468 tryCatch(t, IndexOutOfBoundsException.class, thunk);
469 }
470
471 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
472 boolean caught = false;
473 try {
474 thunk.run();
475 } catch (Throwable x) {
476 if (ex.isAssignableFrom(x.getClass())) {
477 caught = true;
478 } else {
479 fail(x.getMessage() + " not expected");
480 }
481 }
482 if (!caught) {
483 fail(ex.getName() + " not thrown", b);
484 }
485 }
486
487 private static void tryCatch(long[] t, Class<?> ex, Runnable thunk) {
488 tryCatch(LongBuffer.wrap(t), ex, thunk);
489 }
490
491 public static void test(int level, final LongBuffer 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
595 try {
596 b.limit(b.capacity() + 1);
597 fail("IllegalArgumentException expected for limit beyond capacity");
598 } catch (IllegalArgumentException e) {
599 if (e.getMessage() == null) {
600 fail("Non-null IllegalArgumentException message expected for"
601 + " limit beyond capacity");
602 }
603 }
604
605 try {
606 b.limit(-1);
607 fail("IllegalArgumentException expected for negative limit");
608 } catch (IllegalArgumentException e) {
609 if (e.getMessage() == null) {
610 fail("Non-null IllegalArgumentException message expected for"
611 + " negative limit");
612 }
613 }
614
615 // Values
616
617 b.clear();
618 b.put((long)0);
619 b.put((long)-1);
620 b.put((long)1);
621 b.put(Long.MAX_VALUE);
622 b.put(Long.MIN_VALUE);
623
624
625
626
627
628
629
630
631
632
633
634
802
803
804
805
806
807
808
809
810 // Read-only views
811
812 b.rewind();
813 final LongBuffer 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
818 catchReadOnlyBuffer(b, () -> relPut(rb));
819 catchReadOnlyBuffer(b, () -> absPut(rb));
820 catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
821 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
822
823 // put(LongBuffer) should not change source position
824 final LongBuffer src = LongBuffer.allocate(1);
825 catchReadOnlyBuffer(b, () -> rb.put(src));
826 ck(src, src.position(), 0);
827
828 catchReadOnlyBuffer(b, () -> rb.compact());
829
830
831
832
833
834
835
836
837
838
839
840
841
|
1 /*
2 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
70 ck(b, (long)b.get(), (long)((long)ic(i)));
71 b.rewind();
72 }
73
74 private static void absGet(LongBuffer b) {
75 int n = b.capacity();
76 for (int i = 0; i < n; i++)
77 ck(b, (long)b.get(), (long)((long)ic(i)));
78 b.rewind();
79 }
80
81 private static void bulkGet(LongBuffer b) {
82 int n = b.capacity();
83 long[] a = new long[n + 7];
84 b.get(a, 7, n);
85 for (int i = 0; i < n; i++) {
86 ck(b, (long)a[i + 7], (long)((long)ic(i)));
87 }
88 }
89
90 private static void absBulkGet(LongBuffer b) {
91 int n = b.capacity();
92 int len = n - 7*2;
93 long[] a = new long[n + 7];
94 b.position(42);
95 b.get(7, a, 7, len);
96 ck(b, b.position() == 42);
97 for (int i = 0; i < len; i++) {
98 ck(b, (long)a[i + 7], (long)((long)ic(i)));
99 }
100 }
101
102 private static void relPut(LongBuffer b) {
103 int n = b.capacity();
104 b.clear();
105 for (int i = 0; i < n; i++)
106 b.put((long)ic(i));
107 b.flip();
108 }
109
110 private static void absPut(LongBuffer b) {
111 int n = b.capacity();
112 b.clear();
113 for (int i = 0; i < n; i++)
114 b.put(i, (long)ic(i));
115 b.limit(n);
116 b.position(0);
117 }
118
119 private static void bulkPutArray(LongBuffer b) {
120 int n = b.capacity();
121 b.clear();
131 b.clear();
132 LongBuffer c = LongBuffer.allocate(n + 7);
133 c.position(7);
134 for (int i = 0; i < n; i++)
135 c.put((long)ic(i));
136 c.flip();
137 c.position(7);
138 b.put(c);
139 b.flip();
140 try {
141 b.put(b);
142 fail("IllegalArgumentException expected for put into same buffer");
143 } catch (IllegalArgumentException e) {
144 if (e.getMessage() == null) {
145 fail("Non-null IllegalArgumentException message expected from"
146 + " put into same buffer");
147 }
148 }
149 }
150
151 private static void absBulkPutArray(LongBuffer b) {
152 int n = b.capacity();
153 b.clear();
154 int lim = n - 7;
155 int len = lim - 7;
156 b.limit(lim);
157 long[] a = new long[len + 7];
158 for (int i = 0; i < len; i++)
159 a[i + 7] = (long)ic(i);
160 b.position(42);
161 b.put(7, a, 7, len);
162 ck(b, b.position() == 42);
163 }
164
165 private static void absBulkPutBuffer(LongBuffer b, boolean direct) {
166 int n = b.capacity();
167 b.clear();
168 int lim = n - 7;
169 int len = lim - 7;
170 b.limit(lim);
171 LongBuffer c = direct ?
172
173
174
175 ByteBuffer.allocateDirect(Long.BYTES*(len + 7))
176 .asLongBuffer()
177
178 : LongBuffer.allocate(len + 7);
179
180 if (direct)
181 System.out.println("Direct buffer: " + c.getClass().getName());
182
183 for (int i = 0; i < len; i++)
184 c.put(i + 7, (long)ic(i));
185 b.position(42);
186 c.position(42);
187 b.put(7, c, 7, len);
188 ck(b, b.position() == 42);
189 ck(c, c.position() == 42);
190 }
191
192 //6231529
193 private static void callReset(LongBuffer b) {
194 b.position(0);
195 b.mark();
196
197 b.duplicate().reset();
198 b.asReadOnlyBuffer().reset();
199 }
200
201
202
203 // 6221101-6234263
204
205 private static void putBuffer() {
206 final int cap = 10;
207
208 LongBuffer direct1 = ByteBuffer.allocateDirect(cap).asLongBuffer();
209 LongBuffer nondirect1 = ByteBuffer.allocate(cap).asLongBuffer();
210 direct1.put(nondirect1);
211
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502 private static void fail(String problem,
503 LongBuffer xb, LongBuffer yb,
504 long x, long y) {
505 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
506 }
507
508 private static void catchNullArgument(Buffer b, Runnable thunk) {
509 tryCatch(b, NullPointerException.class, thunk);
510 }
511
512 private static void catchIllegalArgument(Buffer b, Runnable thunk) {
513 tryCatch(b, IllegalArgumentException.class, thunk);
514 }
515
516 private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
517 tryCatch(b, ReadOnlyBufferException.class, thunk);
518 }
519
520 private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
521 tryCatch(b, IndexOutOfBoundsException.class, thunk);
522 }
523
524 private static void catchIndexOutOfBounds(long[] t, Runnable thunk) {
525 tryCatch(t, IndexOutOfBoundsException.class, thunk);
526 }
527
528 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
529 boolean caught = false;
530 try {
531 thunk.run();
532 } catch (Throwable x) {
533 if (ex.isAssignableFrom(x.getClass())) {
534 caught = true;
535 } else {
536 String s = x.getMessage();
537 if (s == null)
538 s = x.getClass().getName();
539 fail(s + " not expected");
540 }
541 }
542 if (!caught) {
543 fail(ex.getName() + " not thrown", b);
544 }
545 }
546
547 private static void tryCatch(long[] t, Class<?> ex, Runnable thunk) {
548 tryCatch(LongBuffer.wrap(t), ex, thunk);
549 }
550
551 public static void test(int level, final LongBuffer b, boolean direct) {
552
553 show(level, b);
554
555 if (direct != b.isDirect())
556 fail("Wrong direction", b);
557
558 // Gets and puts
559
560 relPut(b);
561 relGet(b);
562 absGet(b);
563 bulkGet(b);
564
565 absPut(b);
566 relGet(b);
567 absGet(b);
568 bulkGet(b);
569
570 bulkPutArray(b);
571 relGet(b);
572
573 bulkPutBuffer(b);
574 relGet(b);
575
576 absBulkPutArray(b);
577 absBulkGet(b);
578
579 absBulkPutBuffer(b, direct);
580 absBulkGet(b);
581
582 absBulkPutBuffer(b, !direct);
583 absBulkGet(b);
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
664 try {
665 b.limit(b.capacity() + 1);
666 fail("IllegalArgumentException expected for limit beyond capacity");
667 } catch (IllegalArgumentException e) {
668 if (e.getMessage() == null) {
669 fail("Non-null IllegalArgumentException message expected for"
670 + " limit beyond capacity");
671 }
672 }
673
674 try {
675 b.limit(-1);
676 fail("IllegalArgumentException expected for negative limit");
677 } catch (IllegalArgumentException e) {
678 if (e.getMessage() == null) {
679 fail("Non-null IllegalArgumentException message expected for"
680 + " negative limit");
681 }
682 }
683
684 // Exceptions in absolute bulk operations
685
686 catchNullArgument(b, () -> b.get(7, null, 0, 42));
687 catchNullArgument(b, () -> b.put(7, (long[])null, 0, 42));
688 catchNullArgument(b, () -> b.put(7, (LongBuffer)null, 0, 42));
689
690 long[] tmpa = new long[42];
691 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
692 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
693 catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));
694 catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
695 catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
696 catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
697
698 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
699 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
700 catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));
701 catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
702 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
703 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
704
705 LongBuffer tmpb = LongBuffer.allocate(42);
706 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, -1, 42));
707 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, 1));
708 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, -1));
709 catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 41, 2));
710 catchIndexOutOfBounds(b, () -> b.put(-1, tmpb, 0, 1));
711 catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpb, 0, 1));
712 catchIndexOutOfBounds(b, () -> b.put(0, tmpb, 0, -1));
713 catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpb, 0, 42));
714
715 // Values
716
717 b.clear();
718 b.put((long)0);
719 b.put((long)-1);
720 b.put((long)1);
721 b.put(Long.MAX_VALUE);
722 b.put(Long.MIN_VALUE);
723
724
725
726
727
728
729
730
731
732
733
734
902
903
904
905
906
907
908
909
910 // Read-only views
911
912 b.rewind();
913 final LongBuffer rb = b.asReadOnlyBuffer();
914 if (!b.equals(rb))
915 fail("Buffer not equal to read-only view", b, rb);
916 show(level + 1, rb);
917
918 catchReadOnlyBuffer(b, () -> relPut(rb));
919 catchReadOnlyBuffer(b, () -> absPut(rb));
920 catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
921 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
922 catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
923 catchReadOnlyBuffer(b, () -> absBulkPutBuffer(rb, direct));
924
925 // put(LongBuffer) should not change source position
926 final LongBuffer src = LongBuffer.allocate(1);
927 catchReadOnlyBuffer(b, () -> rb.put(src));
928 ck(src, src.position(), 0);
929
930 catchReadOnlyBuffer(b, () -> rb.compact());
931
932
933
934
935
936
937
938
939
940
941
942
943
|