58 |
57 |
59 }; |
58 }; |
60 |
59 |
61 private static void relGet(ShortBuffer b) { |
60 private static void relGet(ShortBuffer b) { |
62 int n = b.capacity(); |
61 int n = b.capacity(); |
63 short v; |
|
64 for (int i = 0; i < n; i++) |
62 for (int i = 0; i < n; i++) |
65 ck(b, (long)b.get(), (long)((short)ic(i))); |
63 ck(b, (long)b.get(), (long)((short)ic(i))); |
66 b.rewind(); |
64 b.rewind(); |
67 } |
65 } |
68 |
66 |
69 private static void relGet(ShortBuffer b, int start) { |
67 private static void relGet(ShortBuffer b, int start) { |
70 int n = b.remaining(); |
68 int n = b.remaining(); |
71 short v; |
|
72 for (int i = start; i < n; i++) |
69 for (int i = start; i < n; i++) |
73 ck(b, (long)b.get(), (long)((short)ic(i))); |
70 ck(b, (long)b.get(), (long)((short)ic(i))); |
74 b.rewind(); |
71 b.rewind(); |
75 } |
72 } |
76 |
73 |
77 private static void absGet(ShortBuffer b) { |
74 private static void absGet(ShortBuffer b) { |
78 int n = b.capacity(); |
75 int n = b.capacity(); |
79 short v; |
|
80 for (int i = 0; i < n; i++) |
76 for (int i = 0; i < n; i++) |
81 ck(b, (long)b.get(), (long)((short)ic(i))); |
77 ck(b, (long)b.get(), (long)((short)ic(i))); |
82 b.rewind(); |
78 b.rewind(); |
83 } |
79 } |
84 |
80 |
85 private static void bulkGet(ShortBuffer b) { |
81 private static void bulkGet(ShortBuffer b) { |
86 int n = b.capacity(); |
82 int n = b.capacity(); |
87 short[] a = new short[n + 7]; |
83 short[] a = new short[n + 7]; |
88 b.get(a, 7, n); |
84 b.get(a, 7, n); |
89 for (int i = 0; i < n; i++) |
85 for (int i = 0; i < n; i++) { |
90 ck(b, (long)a[i + 7], (long)((short)ic(i))); |
86 ck(b, (long)a[i + 7], (long)((short)ic(i))); |
|
87 } |
91 } |
88 } |
92 |
89 |
93 private static void relPut(ShortBuffer b) { |
90 private static void relPut(ShortBuffer b) { |
94 int n = b.capacity(); |
91 int n = b.capacity(); |
95 b.clear(); |
92 b.clear(); |
433 |
430 |
434 |
431 |
435 |
432 |
436 |
433 |
437 |
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
438 private static void fail(String problem, |
449 private static void fail(String problem, |
439 ShortBuffer xb, ShortBuffer yb, |
450 ShortBuffer xb, ShortBuffer yb, |
440 short x, short y) { |
451 short x, short y) { |
441 fail(problem + String.format(": x=%s y=%s", x, y), xb, yb); |
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(short[] t, Runnable thunk) { |
|
468 tryCatch(t, IndexOutOfBoundsException.class, thunk); |
442 } |
469 } |
443 |
470 |
444 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { |
471 private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) { |
445 boolean caught = false; |
472 boolean caught = false; |
446 try { |
473 try { |
450 caught = true; |
477 caught = true; |
451 } else { |
478 } else { |
452 fail(x.getMessage() + " not expected"); |
479 fail(x.getMessage() + " not expected"); |
453 } |
480 } |
454 } |
481 } |
455 if (!caught) |
482 if (!caught) { |
456 fail(ex.getName() + " not thrown", b); |
483 fail(ex.getName() + " not thrown", b); |
457 } |
484 } |
458 |
485 } |
459 private static void tryCatch(short [] t, Class<?> ex, Runnable thunk) { |
486 |
|
487 private static void tryCatch(short[] t, Class<?> ex, Runnable thunk) { |
460 tryCatch(ShortBuffer.wrap(t), ex, thunk); |
488 tryCatch(ShortBuffer.wrap(t), ex, thunk); |
461 } |
489 } |
462 |
490 |
463 public static void test(int level, final ShortBuffer b, boolean direct) { |
491 public static void test(int level, final ShortBuffer b, boolean direct) { |
464 |
492 |
535 |
561 |
536 relPut(b); |
562 relPut(b); |
537 b.limit(b.capacity() / 2); |
563 b.limit(b.capacity() / 2); |
538 b.position(b.limit()); |
564 b.position(b.limit()); |
539 |
565 |
540 tryCatch(b, BufferUnderflowException.class, new Runnable() { |
566 tryCatch(b, BufferUnderflowException.class, () -> b.get()); |
541 public void run() { |
567 tryCatch(b, BufferOverflowException.class, () -> b.put((short)42)); |
542 b.get(); |
568 // The index must be non-negative and less than the buffer's limit. |
543 }}); |
569 catchIndexOutOfBounds(b, () -> b.get(b.limit())); |
544 |
570 catchIndexOutOfBounds(b, () -> b.get(-1)); |
545 tryCatch(b, BufferOverflowException.class, new Runnable() { |
571 catchIndexOutOfBounds(b, () -> b.put(b.limit(), (short)42)); |
546 public void run() { |
572 tryCatch(b, InvalidMarkException.class, |
547 b.put((short)42); |
573 () -> b.position(0).mark().compact().reset()); |
548 }}); |
|
549 |
|
550 // The index must be non-negative and lesss than the buffer's limit. |
|
551 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { |
|
552 public void run() { |
|
553 b.get(b.limit()); |
|
554 }}); |
|
555 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { |
|
556 public void run() { |
|
557 b.get(-1); |
|
558 }}); |
|
559 |
|
560 tryCatch(b, IndexOutOfBoundsException.class, new Runnable() { |
|
561 public void run() { |
|
562 b.put(b.limit(), (short)42); |
|
563 }}); |
|
564 |
|
565 tryCatch(b, InvalidMarkException.class, new Runnable() { |
|
566 public void run() { |
|
567 b.position(0); |
|
568 b.mark(); |
|
569 b.compact(); |
|
570 b.reset(); |
|
571 }}); |
|
572 |
574 |
573 try { |
575 try { |
574 b.position(b.limit() + 1); |
576 b.position(b.limit() + 1); |
575 fail("IllegalArgumentException expected for position beyond limit"); |
577 fail("IllegalArgumentException expected for position beyond limit"); |
576 } catch (IllegalArgumentException e) { |
578 } catch (IllegalArgumentException e) { |
681 |
686 |
682 |
687 |
683 |
688 |
684 |
689 |
685 |
690 |
686 ) |
691 ) { |
687 out.println("[" + i + "] " + x + " != " + y); |
692 out.println("[" + i + "] " + x + " != " + y); |
|
693 } |
688 } |
694 } |
689 fail("Identical buffers not equal", b, b2); |
695 fail("Identical buffers not equal", b, b2); |
690 } |
696 } |
691 if (b.compareTo(b2) != 0) |
697 if (b.compareTo(b2) != 0) { |
692 fail("Comparison to identical buffer != 0", b, b2); |
698 fail("Comparison to identical buffer != 0", b, b2); |
693 |
699 } |
694 b.limit(b.limit() + 1); |
700 b.limit(b.limit() + 1); |
695 b.position(b.limit() - 1); |
701 b.position(b.limit() - 1); |
696 b.put((short)99); |
702 b.put((short)99); |
697 b.rewind(); |
703 b.rewind(); |
698 b2.rewind(); |
704 b2.rewind(); |
712 for (short x : VALUES) { |
718 for (short x : VALUES) { |
713 ShortBuffer xb = ShortBuffer.wrap(new short[] { x }); |
719 ShortBuffer xb = ShortBuffer.wrap(new short[] { x }); |
714 if (xb.compareTo(xb) != 0) { |
720 if (xb.compareTo(xb) != 0) { |
715 fail("compareTo not reflexive", xb, xb, x, x); |
721 fail("compareTo not reflexive", xb, xb, x, x); |
716 } |
722 } |
717 if (! xb.equals(xb)) { |
723 if (!xb.equals(xb)) { |
718 fail("equals not reflexive", xb, xb, x, x); |
724 fail("equals not reflexive", xb, xb, x, x); |
719 } |
725 } |
720 for (short y : VALUES) { |
726 for (short y : VALUES) { |
721 ShortBuffer yb = ShortBuffer.wrap(new short[] { y }); |
727 ShortBuffer yb = ShortBuffer.wrap(new short[] { y }); |
722 if (xb.compareTo(yb) != - yb.compareTo(xb)) { |
728 if (xb.compareTo(yb) != - yb.compareTo(xb)) { |
806 final ShortBuffer rb = b.asReadOnlyBuffer(); |
813 final ShortBuffer rb = b.asReadOnlyBuffer(); |
807 if (!b.equals(rb)) |
814 if (!b.equals(rb)) |
808 fail("Buffer not equal to read-only view", b, rb); |
815 fail("Buffer not equal to read-only view", b, rb); |
809 show(level + 1, rb); |
816 show(level + 1, rb); |
810 |
817 |
811 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { |
818 catchReadOnlyBuffer(b, () -> relPut(rb)); |
812 public void run() { |
819 catchReadOnlyBuffer(b, () -> absPut(rb)); |
813 relPut(rb); |
820 catchReadOnlyBuffer(b, () -> bulkPutArray(rb)); |
814 }}); |
821 catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb)); |
815 |
|
816 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { |
|
817 public void run() { |
|
818 absPut(rb); |
|
819 }}); |
|
820 |
|
821 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { |
|
822 public void run() { |
|
823 bulkPutArray(rb); |
|
824 }}); |
|
825 |
|
826 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { |
|
827 public void run() { |
|
828 bulkPutBuffer(rb); |
|
829 }}); |
|
830 |
822 |
831 // put(ShortBuffer) should not change source position |
823 // put(ShortBuffer) should not change source position |
832 final ShortBuffer src = ShortBuffer.allocate(1); |
824 final ShortBuffer src = ShortBuffer.allocate(1); |
833 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { |
825 catchReadOnlyBuffer(b, () -> rb.put(src)); |
834 public void run() { |
|
835 rb.put(src); |
|
836 }}); |
|
837 ck(src, src.position(), 0); |
826 ck(src, src.position(), 0); |
838 |
827 |
839 tryCatch(b, ReadOnlyBufferException.class, new Runnable() { |
828 catchReadOnlyBuffer(b, () -> rb.compact()); |
840 public void run() { |
|
841 rb.compact(); |
|
842 }}); |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
829 |
893 |
830 |
894 |
831 |
895 |
832 |
896 |
833 |
1050 ck(b, b.capacity(), ba.length); |
936 ck(b, b.capacity(), ba.length); |
1051 ck(b, b.position(), offset); |
937 ck(b, b.position(), offset); |
1052 ck(b, b.limit(), offset + length); |
938 ck(b, b.limit(), offset + length); |
1053 |
939 |
1054 // The offset must be non-negative and no larger than <array.length>. |
940 // The offset must be non-negative and no larger than <array.length>. |
1055 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { |
941 catchIndexOutOfBounds(ba, () -> ShortBuffer.wrap(ba, -1, ba.length)); |
1056 public void run() { |
942 catchIndexOutOfBounds(ba, () -> ShortBuffer.wrap(ba, ba.length + 1, ba.length)); |
1057 ShortBuffer.wrap(ba, -1, ba.length); |
943 catchIndexOutOfBounds(ba, () -> ShortBuffer.wrap(ba, 0, -1)); |
1058 }}); |
944 catchIndexOutOfBounds(ba, () -> ShortBuffer.wrap(ba, 0, ba.length + 1)); |
1059 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { |
|
1060 public void run() { |
|
1061 ShortBuffer.wrap(ba, ba.length + 1, ba.length); |
|
1062 }}); |
|
1063 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { |
|
1064 public void run() { |
|
1065 ShortBuffer.wrap(ba, 0, -1); |
|
1066 }}); |
|
1067 tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() { |
|
1068 public void run() { |
|
1069 ShortBuffer.wrap(ba, 0, ba.length + 1); |
|
1070 }}); |
|
1071 |
945 |
1072 // A NullPointerException will be thrown if the array is null. |
946 // A NullPointerException will be thrown if the array is null. |
1073 tryCatch(ba, NullPointerException.class, new Runnable() { |
947 tryCatch(ba, NullPointerException.class, |
1074 public void run() { |
948 () -> ShortBuffer.wrap((short []) null, 0, 5)); |
1075 ShortBuffer.wrap((short []) null, 0, 5); |
949 tryCatch(ba, NullPointerException.class, |
1076 }}); |
950 () -> ShortBuffer.wrap((short []) null)); |
1077 tryCatch(ba, NullPointerException.class, new Runnable() { |
|
1078 public void run() { |
|
1079 ShortBuffer.wrap((short []) null); |
|
1080 }}); |
|
1081 } |
951 } |
1082 |
952 |
1083 private static void testAllocate() { |
953 private static void testAllocate() { |
1084 // An IllegalArgumentException will be thrown for negative capacities. |
954 // An IllegalArgumentException will be thrown for negative capacities. |
1085 tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() { |
955 catchIllegalArgument((Buffer) null, () -> ShortBuffer.allocate(-1)); |
1086 public void run() { |
|
1087 ShortBuffer.allocate(-1); |
|
1088 }}); |
|
1089 try { |
956 try { |
1090 ShortBuffer.allocate(-1); |
957 ShortBuffer.allocate(-1); |
1091 } catch (IllegalArgumentException e) { |
958 } catch (IllegalArgumentException e) { |
1092 if (e.getMessage() == null) { |
959 if (e.getMessage() == null) { |
1093 fail("Non-null IllegalArgumentException message expected for" |
960 fail("Non-null IllegalArgumentException message expected for" |