diff a/test/jdk/java/util/Arrays/ArraysEqCmpTest.java b/test/jdk/java/util/Arrays/ArraysEqCmpTest.java --- a/test/jdk/java/util/Arrays/ArraysEqCmpTest.java +++ b/test/jdk/java/util/Arrays/ArraysEqCmpTest.java @@ -23,14 +23,14 @@ /* * @test * @bug 8033148 8141409 * @summary tests for array equals and compare + * @library /test/lib * @run junit ArraysEqCmpTest */ - import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.Array; import java.util.Arrays; @@ -41,10 +41,12 @@ import java.util.Objects; import java.util.function.BiFunction; import java.util.function.LongFunction; import java.util.stream.IntStream; +import jdk.test.lib.valueclass.AsValueClass; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -55,10 +57,19 @@ // Maximum width in bits static final int MAX_WIDTH = 512; static final Map typeToWidth; + @AsValueClass + record Point(int x, int y) implements Comparable { + @Override + public int compareTo(Point o) { + int r = Integer.compare(this.x, o.x); + return (r != 0) ? r : Integer.compare(this.y, o.y); + } + } + static { typeToWidth = new HashMap<>(); typeToWidth.put(boolean.class, Byte.SIZE); typeToWidth.put(byte.class, Byte.SIZE); typeToWidth.put(short.class, Short.SIZE); @@ -586,10 +597,28 @@ else throw new IllegalStateException(); ((double[]) a)[i] = pv; } } + + static class ValuePoints extends ArrayType { + public ValuePoints() { + super(Point[].class); + } + + @Override + void set(Object a, int i, Object v) { + if (v == null) { + ((Point[]) a)[i] = null; + } else if (v instanceof Point) { + ((Point[]) a)[i] = (Point) v; + } else if (v instanceof Integer) { + int val = (Integer) v; + ((Point[]) a)[i] = new Point(val, val); + } else throw new IllegalStateException(); + } + } } static Object[][] arrayTypes; public static Object[][] arrayTypesProvider() { @@ -607,10 +636,11 @@ new Object[]{new ArrayType.Integers(true)}, new Object[]{new ArrayType.Longs(false)}, new Object[]{new ArrayType.Longs(true)}, new Object[]{new ArrayType.Floats()}, new Object[]{new ArrayType.Doubles()}, + new Object[]{new ArrayType.ValuePoints()} }; } return arrayTypes; } @@ -637,10 +667,11 @@ LongFunction bToD = Double::longBitsToDouble; objectArrayTypes = new Object[][]{ new Object[]{new ArrayType.BoxedIntegers()}, new Object[]{new ArrayType.BoxedIntegersWithReverseComparator()}, + new Object[]{new ArrayType.ValuePoints()}, }; } return objectArrayTypes; } @@ -765,41 +796,41 @@ BiFunction, Object, Object> cloner = ArrayType::copyOf; // One ref Integer one = 1; testArrayType(arrayType, - (at, s) -> { - Integer[] a = (Integer[]) at.construct(s); - for (int x = 0; x < s; x++) { - a[x] = one; - } - return a; - }, - cloner); + (at, s) -> { + Object a = at.construct(s); + for (int x = 0; x < s; x++) { + at.set(a, x, one); + } + return a; + }, + cloner); // All ref testArrayType(arrayType, - (at, s) -> { - Integer[] a = (Integer[]) at.construct(s); - for (int x = 0; x < s; x++) { - a[x] = Integer.valueOf(s); - } - return a; - }, - cloner); + (at, s) -> { + Object a = at.construct(s); + for (int x = 0; x < s; x++) { + at.set(a, x, s); + } + return a; + }, + cloner); // Some same ref testArrayType(arrayType, - (at, s) -> { - Integer[] a = (Integer[]) at.construct(s); - for (int x = 0; x < s; x++) { - int v = x % 8; - a[x] = v == 1 ? one : new Integer(v); - } - return a; - }, - cloner); + (at, s) -> { + Object a = at.construct(s); + for (int x = 0; x < s; x++) { + int v = x % 8; + at.set(a, x, v == 1 ? one : v); + } + return a; + }, + cloner); } @ParameterizedTest @MethodSource("signedUnsignedArrayTypes") public void testSignedUnsignedArray(ArrayType sat, ArrayType uat) {