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 /*
25 * @test
26 * @bug 8033148 8141409
27 * @summary tests for array equals and compare
28 * @run junit ArraysEqCmpTest
29 */
30
31
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import java.lang.reflect.Array;
36 import java.util.Arrays;
37 import java.util.Comparator;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.function.BiFunction;
43 import java.util.function.LongFunction;
44 import java.util.stream.IntStream;
45
46 import org.junit.jupiter.api.Assertions;
47 import org.junit.jupiter.api.Test;
48 import org.junit.jupiter.api.TestInstance;
49 import org.junit.jupiter.params.ParameterizedTest;
50 import org.junit.jupiter.params.provider.MethodSource;
51
52 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
53 public class ArraysEqCmpTest {
54
55 // Maximum width in bits
56 static final int MAX_WIDTH = 512;
57
58 static final Map<Class, Integer> typeToWidth;
59
60 static {
61 typeToWidth = new HashMap<>();
62 typeToWidth.put(boolean.class, Byte.SIZE);
63 typeToWidth.put(byte.class, Byte.SIZE);
64 typeToWidth.put(short.class, Short.SIZE);
65 typeToWidth.put(char.class, Character.SIZE);
66 typeToWidth.put(int.class, Integer.SIZE);
67 typeToWidth.put(long.class, Long.SIZE);
68 typeToWidth.put(float.class, Float.SIZE);
69 typeToWidth.put(double.class, Double.SIZE);
70 typeToWidth.put(Object.class, Integer.SIZE); // @@@ 32 or 64?
71 }
72
73 static int arraySizeFor(Class<?> type) {
74 type = type.isPrimitive() ? type : Object.class;
75 return 4 * MAX_WIDTH / typeToWidth.get(type);
76 }
77
78 static abstract class ArrayType<T> {
79 final Class<?> arrayType;
571
572 static class Doubles extends ArrayType<double[]> {
573 public Doubles() {
574 super(double[].class);
575 }
576
577 @Override
578 void set(Object a, int i, Object v) {
579 double pv;
580 if (v instanceof Double) {
581 pv = (Double) v;
582 }
583 else if (v instanceof Integer) {
584 pv = ((Integer) v).doubleValue();
585 }
586 else throw new IllegalStateException();
587
588 ((double[]) a)[i] = pv;
589 }
590 }
591 }
592
593 static Object[][] arrayTypes;
594
595 public static Object[][] arrayTypesProvider() {
596 if (arrayTypes == null) {
597 arrayTypes = new Object[][]{
598 new Object[]{new ArrayType.BoxedIntegers()},
599 new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()},
600 new Object[]{new ArrayType.Booleans()},
601 new Object[]{new ArrayType.Bytes(false)},
602 new Object[]{new ArrayType.Bytes(true)},
603 new Object[]{new ArrayType.Characters()},
604 new Object[]{new ArrayType.Shorts(false)},
605 new Object[]{new ArrayType.Shorts(true)},
606 new Object[]{new ArrayType.Integers(false)},
607 new Object[]{new ArrayType.Integers(true)},
608 new Object[]{new ArrayType.Longs(false)},
609 new Object[]{new ArrayType.Longs(true)},
610 new Object[]{new ArrayType.Floats()},
611 new Object[]{new ArrayType.Doubles()},
612 };
613 }
614 return arrayTypes;
615 }
616
617 static Object[][] floatArrayTypes;
618
619 public static Object[][] floatArrayTypesProvider() {
620 if (floatArrayTypes == null) {
621 LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb);
622 LongFunction<Object> bToD = Double::longBitsToDouble;
623
624 floatArrayTypes = new Object[][]{
625 new Object[]{new ArrayType.Floats(), 0x7fc00000L, 0x7f800001L, bTof},
626 new Object[]{new ArrayType.Doubles(), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD},
627 };
628 }
629 return floatArrayTypes;
630 }
631
632 static Object[][] objectArrayTypes;
633
634 public static Object[][] objectArrayTypesProvider() {
635 if (objectArrayTypes == null) {
636 LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb);
637 LongFunction<Object> bToD = Double::longBitsToDouble;
638
639 objectArrayTypes = new Object[][]{
640 new Object[]{new ArrayType.BoxedIntegers()},
641 new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()},
642 };
643 }
644 return objectArrayTypes;
645 }
646
647
648 static Object[][] signedUnsignedArrayTypes;
649
650 public static Object[][] signedUnsignedArrayTypes() {
651 if (signedUnsignedArrayTypes == null) {
652 signedUnsignedArrayTypes = new Object[][]{
653 new Object[]{new ArrayType.Bytes(false), new ArrayType.Bytes(true)},
654 new Object[]{new ArrayType.Shorts(false), new ArrayType.Shorts(true)},
655 new Object[]{new ArrayType.Integers(false), new ArrayType.Integers(true)},
656 new Object[]{new ArrayType.Longs(false), new ArrayType.Longs(true)},
657 };
658 }
659 return signedUnsignedArrayTypes;
660 }
661
750 at.set(a, x, v == 0 ? null : v);
751 }
752 return a;
753 },
754 cloner);
755
756 Integer[] a = new Integer[]{null, 0};
757 Integer[] b = new Integer[]{0, 0};
758 Assertions.assertTrue(Arrays.compare(a, b) < 0);
759 Assertions.assertTrue(Arrays.compare(b, a) > 0);
760 }
761
762 @ParameterizedTest
763 @MethodSource("objectArrayTypesProvider")
764 public void testSameRefElementsInObjectArray(ArrayType<?> arrayType) {
765 BiFunction<ArrayType<?>, Object, Object> cloner = ArrayType::copyOf;
766
767 // One ref
768 Integer one = 1;
769 testArrayType(arrayType,
770 (at, s) -> {
771 Integer[] a = (Integer[]) at.construct(s);
772 for (int x = 0; x < s; x++) {
773 a[x] = one;
774 }
775 return a;
776 },
777 cloner);
778
779 // All ref
780 testArrayType(arrayType,
781 (at, s) -> {
782 Integer[] a = (Integer[]) at.construct(s);
783 for (int x = 0; x < s; x++) {
784 a[x] = Integer.valueOf(s);
785 }
786 return a;
787 },
788 cloner);
789
790 // Some same ref
791 testArrayType(arrayType,
792 (at, s) -> {
793 Integer[] a = (Integer[]) at.construct(s);
794 for (int x = 0; x < s; x++) {
795 int v = x % 8;
796 a[x] = v == 1 ? one : new Integer(v);
797 }
798 return a;
799 },
800 cloner);
801 }
802
803 @ParameterizedTest
804 @MethodSource("signedUnsignedArrayTypes")
805 public void testSignedUnsignedArray(ArrayType<?> sat, ArrayType<?> uat) {
806 BiFunction<ArrayType<?>, Integer, Object> constructor = (at, s) -> {
807 Object a = at.construct(s);
808 for (int x = 0; x < s; x++) {
809 at.set(a, x, 1);
810 }
811 return a;
812 };
813
814 int n = arraySizeFor(sat.componentType);
815
816 for (int s : ranges(0, n)) {
817 Object a = constructor.apply(sat, s);
818
819 for (int aFrom : ranges(0, s)) {
820 for (int aTo : ranges(aFrom, s)) {
|
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 /*
25 * @test
26 * @bug 8033148 8141409
27 * @summary tests for array equals and compare
28 * @library /test/lib
29 * @run junit ArraysEqCmpTest
30 */
31
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import java.lang.reflect.Array;
36 import java.util.Arrays;
37 import java.util.Comparator;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.function.BiFunction;
43 import java.util.function.LongFunction;
44 import java.util.stream.IntStream;
45
46 import jdk.test.lib.valueclass.AsValueClass;
47
48 import org.junit.jupiter.api.Assertions;
49 import org.junit.jupiter.api.Test;
50 import org.junit.jupiter.api.TestInstance;
51 import org.junit.jupiter.params.ParameterizedTest;
52 import org.junit.jupiter.params.provider.MethodSource;
53
54 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
55 public class ArraysEqCmpTest {
56
57 // Maximum width in bits
58 static final int MAX_WIDTH = 512;
59
60 static final Map<Class, Integer> typeToWidth;
61
62 @AsValueClass
63 record Point(int x, int y) implements Comparable<Point> {
64 @Override
65 public int compareTo(Point o) {
66 int r = Integer.compare(this.x, o.x);
67 return (r != 0) ? r : Integer.compare(this.y, o.y);
68 }
69 }
70
71 static {
72 typeToWidth = new HashMap<>();
73 typeToWidth.put(boolean.class, Byte.SIZE);
74 typeToWidth.put(byte.class, Byte.SIZE);
75 typeToWidth.put(short.class, Short.SIZE);
76 typeToWidth.put(char.class, Character.SIZE);
77 typeToWidth.put(int.class, Integer.SIZE);
78 typeToWidth.put(long.class, Long.SIZE);
79 typeToWidth.put(float.class, Float.SIZE);
80 typeToWidth.put(double.class, Double.SIZE);
81 typeToWidth.put(Object.class, Integer.SIZE); // @@@ 32 or 64?
82 }
83
84 static int arraySizeFor(Class<?> type) {
85 type = type.isPrimitive() ? type : Object.class;
86 return 4 * MAX_WIDTH / typeToWidth.get(type);
87 }
88
89 static abstract class ArrayType<T> {
90 final Class<?> arrayType;
582
583 static class Doubles extends ArrayType<double[]> {
584 public Doubles() {
585 super(double[].class);
586 }
587
588 @Override
589 void set(Object a, int i, Object v) {
590 double pv;
591 if (v instanceof Double) {
592 pv = (Double) v;
593 }
594 else if (v instanceof Integer) {
595 pv = ((Integer) v).doubleValue();
596 }
597 else throw new IllegalStateException();
598
599 ((double[]) a)[i] = pv;
600 }
601 }
602
603 static class ValuePoints extends ArrayType<Point[]> {
604 public ValuePoints() {
605 super(Point[].class);
606 }
607
608 @Override
609 void set(Object a, int i, Object v) {
610 if (v == null) {
611 ((Point[]) a)[i] = null;
612 } else if (v instanceof Point) {
613 ((Point[]) a)[i] = (Point) v;
614 } else if (v instanceof Integer) {
615 int val = (Integer) v;
616 ((Point[]) a)[i] = new Point(val, val);
617 } else throw new IllegalStateException();
618 }
619 }
620 }
621
622 static Object[][] arrayTypes;
623
624 public static Object[][] arrayTypesProvider() {
625 if (arrayTypes == null) {
626 arrayTypes = new Object[][]{
627 new Object[]{new ArrayType.BoxedIntegers()},
628 new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()},
629 new Object[]{new ArrayType.Booleans()},
630 new Object[]{new ArrayType.Bytes(false)},
631 new Object[]{new ArrayType.Bytes(true)},
632 new Object[]{new ArrayType.Characters()},
633 new Object[]{new ArrayType.Shorts(false)},
634 new Object[]{new ArrayType.Shorts(true)},
635 new Object[]{new ArrayType.Integers(false)},
636 new Object[]{new ArrayType.Integers(true)},
637 new Object[]{new ArrayType.Longs(false)},
638 new Object[]{new ArrayType.Longs(true)},
639 new Object[]{new ArrayType.Floats()},
640 new Object[]{new ArrayType.Doubles()},
641 new Object[]{new ArrayType.ValuePoints()}
642 };
643 }
644 return arrayTypes;
645 }
646
647 static Object[][] floatArrayTypes;
648
649 public static Object[][] floatArrayTypesProvider() {
650 if (floatArrayTypes == null) {
651 LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb);
652 LongFunction<Object> bToD = Double::longBitsToDouble;
653
654 floatArrayTypes = new Object[][]{
655 new Object[]{new ArrayType.Floats(), 0x7fc00000L, 0x7f800001L, bTof},
656 new Object[]{new ArrayType.Doubles(), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD},
657 };
658 }
659 return floatArrayTypes;
660 }
661
662 static Object[][] objectArrayTypes;
663
664 public static Object[][] objectArrayTypesProvider() {
665 if (objectArrayTypes == null) {
666 LongFunction<Object> bTof = rb -> Float.intBitsToFloat((int) rb);
667 LongFunction<Object> bToD = Double::longBitsToDouble;
668
669 objectArrayTypes = new Object[][]{
670 new Object[]{new ArrayType.BoxedIntegers()},
671 new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()},
672 new Object[]{new ArrayType.ValuePoints()},
673 };
674 }
675 return objectArrayTypes;
676 }
677
678
679 static Object[][] signedUnsignedArrayTypes;
680
681 public static Object[][] signedUnsignedArrayTypes() {
682 if (signedUnsignedArrayTypes == null) {
683 signedUnsignedArrayTypes = new Object[][]{
684 new Object[]{new ArrayType.Bytes(false), new ArrayType.Bytes(true)},
685 new Object[]{new ArrayType.Shorts(false), new ArrayType.Shorts(true)},
686 new Object[]{new ArrayType.Integers(false), new ArrayType.Integers(true)},
687 new Object[]{new ArrayType.Longs(false), new ArrayType.Longs(true)},
688 };
689 }
690 return signedUnsignedArrayTypes;
691 }
692
781 at.set(a, x, v == 0 ? null : v);
782 }
783 return a;
784 },
785 cloner);
786
787 Integer[] a = new Integer[]{null, 0};
788 Integer[] b = new Integer[]{0, 0};
789 Assertions.assertTrue(Arrays.compare(a, b) < 0);
790 Assertions.assertTrue(Arrays.compare(b, a) > 0);
791 }
792
793 @ParameterizedTest
794 @MethodSource("objectArrayTypesProvider")
795 public void testSameRefElementsInObjectArray(ArrayType<?> arrayType) {
796 BiFunction<ArrayType<?>, Object, Object> cloner = ArrayType::copyOf;
797
798 // One ref
799 Integer one = 1;
800 testArrayType(arrayType,
801 (at, s) -> {
802 Object a = at.construct(s);
803 for (int x = 0; x < s; x++) {
804 at.set(a, x, one);
805 }
806 return a;
807 },
808 cloner);
809
810 // All ref
811 testArrayType(arrayType,
812 (at, s) -> {
813 Object a = at.construct(s);
814 for (int x = 0; x < s; x++) {
815 at.set(a, x, s);
816 }
817 return a;
818 },
819 cloner);
820
821 // Some same ref
822 testArrayType(arrayType,
823 (at, s) -> {
824 Object a = at.construct(s);
825 for (int x = 0; x < s; x++) {
826 int v = x % 8;
827 at.set(a, x, v == 1 ? one : v);
828 }
829 return a;
830 },
831 cloner);
832 }
833
834 @ParameterizedTest
835 @MethodSource("signedUnsignedArrayTypes")
836 public void testSignedUnsignedArray(ArrayType<?> sat, ArrayType<?> uat) {
837 BiFunction<ArrayType<?>, Integer, Object> constructor = (at, s) -> {
838 Object a = at.construct(s);
839 for (int x = 0; x < s; x++) {
840 at.set(a, x, 1);
841 }
842 return a;
843 };
844
845 int n = arraySizeFor(sat.componentType);
846
847 for (int s : ranges(0, n)) {
848 Object a = constructor.apply(sat, s);
849
850 for (int aFrom : ranges(0, s)) {
851 for (int aTo : ranges(aFrom, s)) {
|