• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000, 2020, 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  */
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 package test.java.nio.Buffer;
31 
32 // -- This file was mechanically generated: Do not edit! -- //
33 
34 
35 
36 
37 
38 import java.nio.*;
39 import java.util.function.Supplier;
40 
41 
42 
43 
44 
45 
46 
47 
48 import static org.testng.Assert.assertEquals;
49 
50 
51 public class BasicChar
52     extends Basic
53 {
54 
55     private static final char[] VALUES = {
56         Character.MIN_VALUE,
57         (char) -1,
58         (char) 0,
59         (char) 1,
60         Character.MAX_VALUE,
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73     };
74 
relGet(CharBuffer b)75     private static void relGet(CharBuffer b) {
76         int n = b.capacity();
77         for (int i = 0; i < n; i++)
78             ck(b, (long)b.get(), (long)((char)ic(i)));
79         b.rewind();
80     }
81 
relGet(CharBuffer b, int start)82     private static void relGet(CharBuffer b, int start) {
83         int n = b.remaining();
84         for (int i = start; i < n; i++)
85             ck(b, (long)b.get(), (long)((char)ic(i)));
86         b.rewind();
87     }
88 
absGet(CharBuffer b)89     private static void absGet(CharBuffer b) {
90         int n = b.capacity();
91         for (int i = 0; i < n; i++)
92             ck(b, (long)b.get(), (long)((char)ic(i)));
93         b.rewind();
94     }
95 
bulkGet(CharBuffer b)96     private static void bulkGet(CharBuffer b) {
97         int n = b.capacity();
98         char[] a = new char[n + 7];
99         b.get(a, 7, n);
100         for (int i = 0; i < n; i++) {
101             ck(b, (long)a[i + 7], (long)((char)ic(i)));
102         }
103     }
104 
absBulkGet(CharBuffer b)105     private static void absBulkGet(CharBuffer b) {
106         int n = b.capacity();
107         int len = n - 7*2;
108         char[] a = new char[n + 7];
109         b.position(42);
110         b.get(7, a, 7, len);
111         ck(b, b.position() == 42);
112         for (int i = 0; i < len; i++) {
113             ck(b, (long)a[i + 7], (long)((char)ic(i)));
114         }
115     }
116 
relPut(CharBuffer b)117     private static void relPut(CharBuffer b) {
118         int n = b.capacity();
119         b.clear();
120         for (int i = 0; i < n; i++)
121             b.put((char)ic(i));
122         b.flip();
123     }
124 
absPut(CharBuffer b)125     private static void absPut(CharBuffer b) {
126         int n = b.capacity();
127         b.clear();
128         for (int i = 0; i < n; i++)
129             b.put(i, (char)ic(i));
130         b.limit(n);
131         b.position(0);
132     }
133 
bulkPutArray(CharBuffer b)134     private static void bulkPutArray(CharBuffer b) {
135         int n = b.capacity();
136         b.clear();
137         char[] a = new char[n + 7];
138         for (int i = 0; i < n; i++)
139             a[i + 7] = (char)ic(i);
140         b.put(a, 7, n);
141         b.flip();
142     }
143 
bulkPutBuffer(CharBuffer b)144     private static void bulkPutBuffer(CharBuffer b) {
145         int n = b.capacity();
146         b.clear();
147         CharBuffer c = CharBuffer.allocate(n + 7);
148         c.position(7);
149         for (int i = 0; i < n; i++)
150             c.put((char)ic(i));
151         c.flip();
152         c.position(7);
153         b.put(c);
154         b.flip();
155         try {
156             b.put(b);
157             fail("IllegalArgumentException expected for put into same buffer");
158         } catch (IllegalArgumentException e) {
159             if (e.getMessage() == null) {
160                 fail("Non-null IllegalArgumentException message expected from"
161                      + " put into same buffer");
162             }
163         }
164     }
165 
absBulkPutArray(CharBuffer b)166     private static void absBulkPutArray(CharBuffer b) {
167         int n = b.capacity();
168         b.clear();
169         int lim = n - 7;
170         int len = lim - 7;
171         b.limit(lim);
172         char[] a = new char[len + 7];
173         for (int i = 0; i < len; i++)
174             a[i + 7] = (char)ic(i);
175         b.position(42);
176         b.put(7, a, 7, len);
177         ck(b, b.position() == 42);
178     }
179 
180     //6231529
callReset(CharBuffer b)181     private static void callReset(CharBuffer b) {
182         b.position(0);
183         b.mark();
184 
185         b.duplicate().reset();
186         b.asReadOnlyBuffer().reset();
187     }
188 
189 
190 
191     // 6221101-6234263
192 
putBuffer()193     private static void putBuffer() {
194         final int cap = 10;
195 
196         CharBuffer direct1 = ByteBuffer.allocateDirect(cap).asCharBuffer();
197         CharBuffer nondirect1 = ByteBuffer.allocate(cap).asCharBuffer();
198         direct1.put(nondirect1);
199 
200         CharBuffer direct2 = ByteBuffer.allocateDirect(cap).asCharBuffer();
201         CharBuffer nondirect2 = ByteBuffer.allocate(cap).asCharBuffer();
202         nondirect2.put(direct2);
203 
204         CharBuffer direct3 = ByteBuffer.allocateDirect(cap).asCharBuffer();
205         CharBuffer direct4 = ByteBuffer.allocateDirect(cap).asCharBuffer();
206         direct3.put(direct4);
207 
208         CharBuffer nondirect3 = ByteBuffer.allocate(cap).asCharBuffer();
209         CharBuffer nondirect4 = ByteBuffer.allocate(cap).asCharBuffer();
210         nondirect3.put(nondirect4);
211     }
212 
213 
214 
215 
bulkPutString(CharBuffer b)216     private static void bulkPutString(CharBuffer b) {
217         int n = b.capacity();
218         b.clear();
219         StringBuilder sb = new StringBuilder(n + 7);
220         sb.append("1234567");
221         for (int i = 0; i < n; i++)
222             sb.append((char)ic(i));
223         b.put(sb.toString(), 7, 7 + n);
224         b.flip();
225     }
226 
227 
228 
checkSlice(CharBuffer b, CharBuffer slice)229     private static void checkSlice(CharBuffer b, CharBuffer slice) {
230         ck(slice, 0, slice.position());
231         ck(slice, b.remaining(), slice.limit());
232         ck(slice, b.remaining(), slice.capacity());
233         if (b.isDirect() != slice.isDirect())
234             fail("Lost direction", slice);
235         if (b.isReadOnly() != slice.isReadOnly())
236             fail("Lost read-only", slice);
237     }
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
363 
364 
365 
366 
367 
368 
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387 
388 
389 
390 
391 
392 
393 
394 
395 
396 
397 
398 
399 
400 
401 
402 
403 
404 
405 
406 
407 
408 
409 
410 
411 
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425 
426 
427 
428 
429 
430 
431 
432 
433 
434 
435 
436 
437 
438 
439 
440 
441 
442 
443 
444 
445 
446 
447 
448 
449 
450 
451 
452 
453 
454 
455 
456 
457 
458 
459 
460 
461 
462 
463 
464 
465 
466 
467 
468 
469 
470 
471 
472 
473 
474 
475 
476 
477 
478 
479 
480 
481 
482 
483 
484 
485 
486 
487 
488 
489 
490 
491 
492 
493 
494 
495 
496 
497 
498 
499 
500 
501 
502 
503 
504 
505 
506 
507 
508 
509 
510 
511 
512 
513 
514 
515 
516 
517 
518 
519 
520 
521 
522 
523 
524 
525 
526 
527 
528 
529 
530 
531 
532 
533 
534 
535 
536 
537 
538 
539 
540 
541 
542 
543 
544 
545 
546 
547 
548 
549 
550 
551 
552 
553 
554 
555 
556 
557 
558 
fail(String problem, CharBuffer xb, CharBuffer yb, char x, char y)559     private static void fail(String problem,
560                              CharBuffer xb, CharBuffer yb,
561                              char x, char y) {
562         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
563     }
564 
catchNullArgument(Buffer b, Runnable thunk)565     private static void catchNullArgument(Buffer b, Runnable thunk) {
566         tryCatch(b, NullPointerException.class, thunk);
567     }
568 
catchIllegalArgument(Buffer b, Runnable thunk)569     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
570         tryCatch(b, IllegalArgumentException.class, thunk);
571     }
572 
catchReadOnlyBuffer(Buffer b, Runnable thunk)573     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
574         tryCatch(b, ReadOnlyBufferException.class, thunk);
575     }
576 
catchIndexOutOfBounds(Buffer b, Runnable thunk)577     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
578         tryCatch(b, IndexOutOfBoundsException.class, thunk);
579     }
580 
catchIndexOutOfBounds(char[] t, Runnable thunk)581     private static void catchIndexOutOfBounds(char[] t, Runnable thunk) {
582         tryCatch(t, IndexOutOfBoundsException.class, thunk);
583     }
584 
tryCatch(Buffer b, Class<?> ex, Runnable thunk)585     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
586         boolean caught = false;
587         try {
588             thunk.run();
589         } catch (Throwable x) {
590             if (ex.isAssignableFrom(x.getClass())) {
591                 caught = true;
592             } else {
593                 String s = x.getMessage();
594                 if (s == null)
595                     s = x.getClass().getName();
596                 fail(s + " not expected");
597             }
598         }
599         if (!caught) {
600             fail(ex.getName() + " not thrown", b);
601         }
602     }
603 
tryCatch(char[] t, Class<?> ex, Runnable thunk)604     private static void tryCatch(char[] t, Class<?> ex, Runnable thunk) {
605         tryCatch(CharBuffer.wrap(t), ex, thunk);
606     }
607 
608     // Android-changed: Add @SuppressWarnings as the operation is tested to be reflexive.
609     @SuppressWarnings({"SelfComparison", "SelfEquals"})
test(int level, final CharBuffer b, boolean direct)610     public static void test(int level, final CharBuffer b, boolean direct) {
611 
612         show(level, b);
613 
614         if (direct != b.isDirect())
615             fail("Wrong direction", b);
616 
617         // Gets and puts
618 
619         relPut(b);
620         relGet(b);
621         absGet(b);
622         bulkGet(b);
623 
624         absPut(b);
625         relGet(b);
626         absGet(b);
627         bulkGet(b);
628 
629         bulkPutArray(b);
630         relGet(b);
631 
632         bulkPutBuffer(b);
633         relGet(b);
634 
635         absBulkPutArray(b);
636         absBulkGet(b);
637 
638 
639 
640         bulkPutString(b);
641         relGet(b);
642         b.position(1);
643         b.limit(7);
644         ck(b, b.toString().equals("bcdefg"));
645 
646         // CharSequence ops
647 
648         b.position(2);
649         ck(b, b.charAt(1), 'd');
650         CharBuffer c = b.subSequence(1, 4);
651         ck(c, c.capacity(), b.capacity());
652         ck(c, c.position(), b.position()+1);
653         ck(c, c.limit(), b.position()+4);
654         ck(c, b.subSequence(1, 4).toString().equals("def"));
655 
656         // 4938424
657         b.position(4);
658         ck(b, b.charAt(1), 'f');
659         ck(b, b.subSequence(1, 3).toString().equals("fg"));
660 
661         // String ops
662 
663         // 7190219
664         b.clear();
665         int pos = b.position();
666         tryCatch(b, BufferOverflowException.class, () ->
667                 b.put(String.valueOf(new char[b.capacity() + 1]), 0, b.capacity() + 1)
668             );
669         ck(b, b.position(), pos);
670         relGet(b);
671 
672 
673 
674         // Compact
675 
676         relPut(b);
677         b.position(13);
678         b.compact();
679         b.flip();
680         relGet(b, 13);
681 
682         // Exceptions
683 
684         relPut(b);
685         b.limit(b.capacity() / 2);
686         b.position(b.limit());
687 
688         tryCatch(b, BufferUnderflowException.class, () -> b.get());
689         tryCatch(b, BufferOverflowException.class, () -> b.put((char)42));
690         // The index must be non-negative and less than the buffer's limit.
691         catchIndexOutOfBounds(b, () -> b.get(b.limit()));
692         catchIndexOutOfBounds(b, () -> b.get(-1));
693         catchIndexOutOfBounds(b, () -> b.put(b.limit(), (char)42));
694         tryCatch(b, InvalidMarkException.class,
695                 // Android-changed: Explicit casting due to @CovariantReturnType methods.
696                 // () -> b.position(0).mark().compact().reset());
697                 () -> ((CharBuffer) b.position(0).mark()).compact().reset());
698 
699         try {
700             b.position(b.limit() + 1);
701             fail("IllegalArgumentException expected for position beyond limit");
702         } catch (IllegalArgumentException e) {
703             if (e.getMessage() == null) {
704                 fail("Non-null IllegalArgumentException message expected for"
705                      + " position beyond limit");
706             }
707         }
708 
709         try {
710             b.position(-1);
711             fail("IllegalArgumentException expected for negative position");
712         } catch (IllegalArgumentException e) {
713             if (e.getMessage() == null) {
714                 fail("Non-null IllegalArgumentException message expected for"
715                      + " negative position");
716             }
717         }
718 
719         try {
720             b.limit(b.capacity() + 1);
721             fail("IllegalArgumentException expected for limit beyond capacity");
722         } catch (IllegalArgumentException e) {
723             if (e.getMessage() == null) {
724                 fail("Non-null IllegalArgumentException message expected for"
725                      + " limit beyond capacity");
726             }
727         }
728 
729         try {
730             b.limit(-1);
731             fail("IllegalArgumentException expected for negative limit");
732         } catch (IllegalArgumentException e) {
733             if (e.getMessage() == null) {
734                 fail("Non-null IllegalArgumentException message expected for"
735                      + " negative limit");
736             }
737         }
738 
739         // Exceptions in absolute bulk and slice operations
740 
741         catchNullArgument(b, () -> b.get(7, null, 0, 42));
742         catchNullArgument(b, () -> b.put(7, (char[])null, 0, 42));
743 
744         char[] tmpa = new char[42];
745         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
746         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
747         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));
748         catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
749         catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
750         catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
751 
752         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
753         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
754         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));
755         catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
756         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
757         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
758 
759         catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
760         catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
761         catchIndexOutOfBounds(b, () -> b.slice(0, -1));
762         catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
763 
764         // Values
765 
766         b.clear();
767         b.put((char)0);
768         b.put((char)-1);
769         b.put((char)1);
770         b.put(Character.MAX_VALUE);
771         b.put(Character.MIN_VALUE);
772 
773 
774 
775 
776 
777 
778 
779 
780 
781 
782 
783 
784 
785 
786 
787 
788 
789         b.flip();
790         ck(b, b.get(), 0);
791         ck(b, b.get(), (char)-1);
792         ck(b, b.get(), 1);
793         ck(b, b.get(), Character.MAX_VALUE);
794         ck(b, b.get(), Character.MIN_VALUE);
795 
796 
797 
798 
799 
800 
801 
802 
803 
804 
805 
806 
807 
808 
809 
810 
811 
812 
813 
814 
815 
816 
817 
818 
819 
820 
821 
822         // Comparison
823         b.rewind();
824         CharBuffer b2 = CharBuffer.allocate(b.capacity());
825         b2.put(b);
826         b2.flip();
827         b.position(2);
828         b2.position(2);
829         if (!b.equals(b2)) {
830             for (int i = 2; i < b.limit(); i++) {
831                 char x = b.get(i);
832                 char y = b2.get(i);
833                 if (x != y
834 
835 
836 
837 
838 
839 
840                     ) {
841                     out.println("[" + i + "] " + x + " != " + y);
842                 }
843             }
844             fail("Identical buffers not equal", b, b2);
845         }
846         if (b.compareTo(b2) != 0) {
847             fail("Comparison to identical buffer != 0", b, b2);
848         }
849         b.limit(b.limit() + 1);
850         b.position(b.limit() - 1);
851         b.put((char)99);
852         b.rewind();
853         b2.rewind();
854         if (b.equals(b2))
855             fail("Non-identical buffers equal", b, b2);
856         if (b.compareTo(b2) <= 0)
857             fail("Comparison to shorter buffer <= 0", b, b2);
858         b.limit(b.limit() - 1);
859 
860         b.put(2, (char)42);
861         if (b.equals(b2))
862             fail("Non-identical buffers equal", b, b2);
863         if (b.compareTo(b2) <= 0)
864             fail("Comparison to lesser buffer <= 0", b, b2);
865 
866         // Check equals and compareTo with interesting values
867         for (char x : VALUES) {
868             CharBuffer xb = CharBuffer.wrap(new char[] { x });
869             if (xb.compareTo(xb) != 0) {
870                 fail("compareTo not reflexive", xb, xb, x, x);
871             }
872             if (!xb.equals(xb)) {
873                 fail("equals not reflexive", xb, xb, x, x);
874             }
875             for (char y : VALUES) {
876                 CharBuffer yb = CharBuffer.wrap(new char[] { y });
877                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
878                     fail("compareTo not anti-symmetric",
879                          xb, yb, x, y);
880                 }
881                 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
882                     fail("compareTo inconsistent with equals",
883                          xb, yb, x, y);
884                 }
885                 if (xb.compareTo(yb) != Character.compare(x, y)) {
886 
887 
888 
889 
890 
891 
892                     fail("Incorrect results for CharBuffer.compareTo",
893                          xb, yb, x, y);
894                 }
895 
896 
897 
898 
899 
900 
901 
902 
903 
904                 if (xb.equals(yb) != (x == y)) {
905                     fail("Incorrect results for CharBuffer.equals",
906                          xb, yb, x, y);
907                 }
908 
909 
910 
911 
912 
913 
914 
915             }
916         }
917 
918         // Sub, dup
919 
920         relPut(b);
921         relGet(b.duplicate());
922         b.position(13);
923         relGet(b.duplicate(), 13);
924         relGet(b.duplicate().slice(), 13);
925         relGet(b.slice(), 13);
926         relGet(b.slice().duplicate(), 13);
927 
928         // Slice
929 
930         b.position(5);
931         CharBuffer sb = b.slice();
932         checkSlice(b, sb);
933         b.position(0);
934         CharBuffer sb2 = sb.slice();
935         checkSlice(sb, sb2);
936 
937         if (!sb.equals(sb2))
938             fail("Sliced slices do not match", sb, sb2);
939         if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {
940             fail("Array offsets do not match: "
941                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
942         }
943 
944         int bPos = b.position();
945         int bLim = b.limit();
946 
947         b.position(7);
948         b.limit(42);
949         CharBuffer rsb = b.slice();
950         b.position(0);
951         b.limit(b.capacity());
952         CharBuffer asb = b.slice(7, 35);
953         checkSlice(rsb, asb);
954 
955         b.position(bPos);
956         b.limit(bLim);
957 
958 
959 
960 
961 
962 
963 
964 
965 
966 
967 
968 
969 
970 
971 
972 
973 
974 
975 
976 
977 
978 
979 
980 
981 
982 
983 
984 
985 
986 
987 
988 
989         // Read-only views
990 
991         b.rewind();
992         final CharBuffer rb = b.asReadOnlyBuffer();
993         if (!b.equals(rb))
994             fail("Buffer not equal to read-only view", b, rb);
995         show(level + 1, rb);
996 
997         catchReadOnlyBuffer(b, () -> relPut(rb));
998         catchReadOnlyBuffer(b, () -> absPut(rb));
999         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
1000         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
1001         catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
1002 
1003         // put(CharBuffer) should not change source position
1004         final CharBuffer src = CharBuffer.allocate(1);
1005         catchReadOnlyBuffer(b, () -> rb.put(src));
1006         ck(src, src.position(), 0);
1007 
1008         catchReadOnlyBuffer(b, () -> rb.compact());
1009 
1010 
1011 
1012 
1013 
1014 
1015 
1016 
1017 
1018 
1019 
1020 
1021 
1022 
1023 
1024 
1025 
1026 
1027 
1028 
1029         // 7199551
1030         catchReadOnlyBuffer(b, () -> rb.put(new String(new char[rb.remaining() + 1])));
1031         catchReadOnlyBuffer(b, () -> rb.append(new String(new char[rb.remaining() + 1])));
1032 
1033 
1034 
1035         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
1036             catchReadOnlyBuffer(b, () -> rb.array());
1037             catchReadOnlyBuffer(b, () -> rb.arrayOffset());
1038             if (rb.hasArray()) {
1039                 fail("Read-only heap buffer's backing array is accessible", rb);
1040             }
1041         }
1042 
1043         // Bulk puts from read-only buffers
1044 
1045         b.clear();
1046         rb.rewind();
1047         b.put(rb);
1048 
1049 
1050 
1051 
1052 
1053 
1054 
1055 
1056 
1057 
1058 
1059         relPut(b);                       // Required by testViews
1060 
1061 
1062 
1063 
1064 
1065 
1066     }
1067 
1068 
1069 
testStr()1070     private static void testStr() {
1071         final String s = "abcdefghijklm";
1072         int start = 3;
1073         int end = 9;
1074         final CharBuffer b = CharBuffer.wrap(s, start, end);
1075         show(0, b);
1076         ck(b, b.toString().equals(s.substring(start, end)));
1077         ck(b, b.toString().equals("defghi"));
1078         ck(b, b.isReadOnly());
1079         catchReadOnlyBuffer(b, () -> b.put('x'));
1080         ck(b, start, b.position());
1081         ck(b, end, b.limit());
1082         ck(b, s.length(), b.capacity());
1083         b.position(6);
1084         ck(b, b.subSequence(0,3).toString().equals("ghi"));
1085 
1086         // absolute bulk get
1087         char[] c = new char[end + 1 - (start - 1) + 1]; // [start - 1, end + 1]
1088         b.limit(end + 2);
1089         b.get(start - 1, c, 0, c.length);
1090         for (int i = 0; i < c.length; i++)
1091             ck(b, c[i], s.charAt(start - 1 + i));
1092 
1093         // The index, relative to the position, must be non-negative and
1094         // smaller than remaining().
1095         catchIndexOutOfBounds(b, () -> b.charAt(-1));
1096         catchIndexOutOfBounds(b, () -> b.charAt(b.remaining()));
1097         // The index must be non-negative and less than the buffer's limit.
1098         catchIndexOutOfBounds(b, () -> b.get(b.limit()));
1099         catchIndexOutOfBounds(b, () -> b.get(-1));
1100         // The start must be non-negative and no larger than remaining().
1101         catchIndexOutOfBounds(b, () -> b.subSequence(-1, b.remaining()));
1102         catchIndexOutOfBounds(b, () -> b.subSequence(b.remaining() + 1, b.remaining()));
1103 
1104         // The end must be no smaller than start and no larger than
1105         // remaining().
1106         catchIndexOutOfBounds(b, () -> b.subSequence(2, 1));
1107         catchIndexOutOfBounds(b, () -> b.subSequence(0, b.remaining() + 1));
1108 
1109         // The offset must be non-negative and no larger than <array.length>.
1110         catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, -1, s.length()));
1111         catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, s.length() + 1, s.length()));
1112         catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, 1, 0));
1113         catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, 0, s.length() + 1));
1114     }
1115 
1116 
1117 
test(final char [] ba)1118     public static void test(final char [] ba) {
1119         int offset = 47;
1120         int length = 900;
1121         final CharBuffer b = CharBuffer.wrap(ba, offset, length);
1122         show(0, b);
1123         ck(b, b.capacity(), ba.length);
1124         ck(b, b.position(), offset);
1125         ck(b, b.limit(), offset + length);
1126 
1127         // The offset must be non-negative and no larger than <array.length>.
1128         catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, -1, ba.length));
1129         catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, ba.length + 1, ba.length));
1130         catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, 0, -1));
1131         catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, 0, ba.length + 1));
1132 
1133         // A NullPointerException will be thrown if the array is null.
1134         tryCatch(ba, NullPointerException.class,
1135                 () -> CharBuffer.wrap((char []) null, 0, 5));
1136         tryCatch(ba, NullPointerException.class,
1137                 () -> CharBuffer.wrap((char []) null));
1138     }
1139 
testAllocate()1140     private static void testAllocate() {
1141         // An IllegalArgumentException will be thrown for negative capacities.
1142         catchIllegalArgument((Buffer) null, () -> CharBuffer.allocate(-1));
1143         try {
1144             CharBuffer.allocate(-1);
1145         } catch (IllegalArgumentException e) {
1146             if (e.getMessage() == null) {
1147                 fail("Non-null IllegalArgumentException message expected for"
1148                      + " attempt to allocate negative capacity buffer");
1149             }
1150         }
1151 
1152 
1153 
1154 
1155 
1156 
1157 
1158 
1159 
1160 
1161 
1162     }
1163 
testToString()1164     public static void testToString() {
1165         final int cap = 10;
1166 
1167 
1168 
1169 
1170 
1171 
1172 
1173 
1174 
1175 
1176 
1177 
1178 
1179 
1180 
1181 
1182     }
1183 
test()1184     public static void test() {
1185         testAllocate();
1186         test(0, CharBuffer.allocate(7 * 1024), false);
1187         test(0, CharBuffer.wrap(new char[7 * 1024], 0, 7 * 1024), false);
1188         test(new char[1024]);
1189 
1190 
1191 
1192 
1193 
1194 
1195 
1196         testStr();
1197 
1198 
1199         callReset(CharBuffer.allocate(10));
1200 
1201 
1202 
1203         putBuffer();
1204 
1205 
1206         testToString();
1207 
1208         // Android-added: Add API coverage for get(), put().
1209         testGetPutArrayWithIndex();
1210 
1211         testPutBuffer();
1212 
1213     }
1214 
1215     // BEGIN Android-added: Add API coverage for get(), put().
testGetPutArrayWithIndex()1216     private static void testGetPutArrayWithIndex() {
1217         CharBuffer buf = CharBuffer.allocate(16);
1218         char firstElement = 11, secondElement = 12;
1219         buf.put(firstElement);
1220         buf.put(secondElement);
1221         buf.position(0);
1222         char[] arr = new char[] { 4, 3, 2, 1 };
1223         buf.put(2, arr);
1224         char[] actual = new char[4];
1225         buf.get(2, actual);
1226         assertEquals(actual, arr);
1227         buf.get(0, actual);
1228         assertEquals(actual, new char[] {firstElement, secondElement, 4, 3});
1229     }
1230 
testPutBuffer()1231     private static void testPutBuffer() {
1232         Supplier<CharBuffer>[] newBuffers = new Supplier[] {
1233                 () -> CharBuffer.allocate(512),
1234                 () -> CharBuffer.allocate(512).slice(100, 412),
1235                 () -> ByteBuffer.allocate(512 * Character.BYTES).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(),
1236                 () -> ByteBuffer.allocate(512 * Character.BYTES).order(ByteOrder.BIG_ENDIAN).asCharBuffer(),
1237                 () -> ByteBuffer.allocateDirect(512 * Character.BYTES).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(),
1238                 () -> ByteBuffer.allocateDirect(512 * Character.BYTES).order(ByteOrder.BIG_ENDIAN).asCharBuffer(),
1239                 () -> ByteBuffer.allocateDirect(512 * Character.BYTES).asCharBuffer().slice(100, 412),
1240                 () -> ((ByteBuffer) ByteBuffer.allocateDirect(512 * Character.BYTES)
1241                         .order(ByteOrder.LITTLE_ENDIAN).position(100)).asCharBuffer(),
1242                 () -> ((ByteBuffer) ByteBuffer.allocateDirect(512 * Character.BYTES)
1243                         .order(ByteOrder.BIG_ENDIAN).position(100)).asCharBuffer(),
1244         };
1245 
1246         char[] samples = new char[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1247         for (var newSrc : newBuffers) {
1248             for (var newDst : newBuffers) {
1249                 char[] out = new char[10];
1250                 CharBuffer src = newSrc.get();
1251                 src.put(samples);
1252                 src.get(0, out);
1253                 assertEquals(out, samples);
1254                 assertEquals(src.get(10), (char) 0);
1255                 src.limit(10);
1256                 src.rewind();
1257 
1258                 out = new char[10];
1259                 CharBuffer dst = newDst.get();
1260                 dst.put(src);
1261                 dst.get(0, out);
1262                 assertEquals(out, samples);
1263                 dst.rewind();
1264 
1265                 dst.put(6, src, 1, 2);
1266                 assertEquals(dst.get(6), (char) 1);
1267                 assertEquals(dst.get(7), (char) 2);
1268                 assertEquals(dst.get(8), (char) 8);
1269                 assertEquals(dst.get(10), (char) 0);
1270 
1271                 dst.put(12, src, 2, 2);
1272                 out = new char[5];
1273                 dst.get(10, out);
1274                 assertEquals(out, new char[] {0, 0, 2, 3, 0});
1275 
1276             }
1277         }
1278     }
1279 
1280     // END Android-added: Add API coverage for get(), put().
1281 }
1282