8346106: Verify.checkEQ: testing utility for recursive value verification

Reviewed-by: kvn, tweidmann
This commit is contained in:
Emanuel Peter 2024-12-20 06:52:22 +00:00
parent b2811a0ccd
commit 35fafbc597
4 changed files with 843 additions and 0 deletions

View File

@ -0,0 +1,309 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.lib.verify;
import java.util.Optional;
import java.lang.foreign.*;
/**
* The {@link Verify} class provides a single {@link Verify#checkEQ} static method, which recursively
* compares the two {@link Object}s by value. It deconstructs {@link Object[]}, compares boxed primitive
* types, and compares the content of arrays and {@link MemorySegment}s.
*
* When a comparison fail, then methods print helpful messages, before throwing a {@link VerifyException}.
*/
public final class Verify {
private Verify() {}
/**
* Verify the content of two Objects, possibly recursively. Only limited types are implemented.
*
* @param a First object to be recursively compared with the second.
* @param b Second object to be recursively compared with the first.
* @throws VerifyException If the comparison fails.
*/
public static void checkEQ(Object a, Object b) {
checkEQ(a, b, "");
}
/**
* Verify the content of two Objects, possibly recursively. Only limited types are implemented.
*/
private static void checkEQ(Object a, Object b, String context) {
// Both null
if (a == null && b == null) {
return;
}
// Null mismatch
if (a == null || b == null) {
System.err.println("ERROR: Verify.checkEQ failed: null mismatch");
print(a, "a " + context);
print(b, "b " + context);
throw new VerifyException("Object array null mismatch.");
}
// Class mismatch
Class ca = a.getClass();
Class cb = b.getClass();
if (ca != cb) {
System.err.println("ERROR: Verify.checkEQ failed: class mismatch.");
System.err.println(" " + ca.getName() + " vs " + cb.getName());
print(a, "a " + context);
print(b, "b " + context);
throw new VerifyException("Object class mismatch.");
}
switch (a) {
case Object[] x -> checkEQimpl(x, (Object[])b, context);
case Byte x -> checkEQimpl(x, ((Byte)b).byteValue(), context);
case Character x -> checkEQimpl(x, ((Character)b).charValue(), context);
case Short x -> checkEQimpl(x, ((Short)b).shortValue(), context);
case Integer x -> checkEQimpl(x, ((Integer)b).intValue(), context);
case Long x -> checkEQimpl(x, ((Long)b).longValue(), context);
case Float x -> checkEQimpl(x, ((Float)b).floatValue(), context);
case Double x -> checkEQimpl(x, ((Double)b).doubleValue(), context);
case byte[] x -> checkEQimpl(x, (byte[])b, context);
case char[] x -> checkEQimpl(x, (char[])b, context);
case short[] x -> checkEQimpl(x, (short[])b, context);
case int[] x -> checkEQimpl(x, (int[])b, context);
case long[] x -> checkEQimpl(x, (long[])b, context);
case float[] x -> checkEQimpl(x, (float[])b, context);
case double[] x -> checkEQimpl(x, (double[])b, context);
case MemorySegment x -> checkEQimpl(x, (MemorySegment) b, context);
default -> {
System.err.println("ERROR: Verify.checkEQ failed: type not supported: " + ca.getName());
print(a, "a " + context);
print(b, "b " + context);
throw new VerifyException("Object array type not supported: " + ca.getName());
}
}
}
/**
* Verify that two bytes are identical.
*/
private static void checkEQimpl(byte a, byte b, String context) {
if (a != b) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch: " + a + " vs " + b + " for " + context);
throw new VerifyException("Value mismatch: " + a + " vs " + b);
}
}
/**
* Verify that two chars are identical.
*/
private static void checkEQimpl(char a, char b, String context) {
if (a != b) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch: " + (int)a + " vs " + (int)b + " for " + context);
throw new VerifyException("Value mismatch: " + (int)a + " vs " + (int)b);
}
}
/**
* Verify that two shorts are identical.
*/
private static void checkEQimpl(short a, short b, String context) {
if (a != b) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch: " + (int)a + " vs " + (int)b + " for " + context);
throw new VerifyException("Value mismatch: " + (int)a + " vs " + (int)b);
}
}
/**
* Verify that two ints are identical.
*/
private static void checkEQimpl(int a, int b, String context) {
if (a != b) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch: " + a + " vs " + b + " for " + context);
throw new VerifyException("Value mismatch: " + a + " vs " + b);
}
}
/**
* Verify that two longs are identical.
*/
private static void checkEQimpl(long a, long b, String context) {
if (a != b) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch: " + a + " vs " + b + " for " + context);
throw new VerifyException("Value mismatch: " + a + " vs " + b);
}
}
/**
* Verify that two floats have identical bits.
*/
private static void checkEQimpl(float a, float b, String context) {
if (Float.floatToRawIntBits(a) != Float.floatToRawIntBits(b)) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch for " + context);
System.err.println(" Values: " + a + " vs " + b);
System.err.println(" Values: " + Float.floatToRawIntBits(a) + " vs " + Float.floatToRawIntBits(b));
throw new VerifyException("Value mismatch: " + a + " vs " + b);
}
}
/**
* Verify that two doubles have identical bits.
*/
private static void checkEQimpl(double a, double b, String context) {
if (Double.doubleToRawLongBits(a) != Double.doubleToRawLongBits(b)) {
System.err.println("ERROR: Verify.checkEQ failed: value mismatch for " + context);
System.err.println(" Values: " + a + " vs " + b);
System.err.println(" Values: " + Double.doubleToRawLongBits(a) + " vs " + Double.doubleToRawLongBits(b));
throw new VerifyException("Value mismatch: " + a + " vs " + b);
}
}
/**
* Verify that the content of two MemorySegments is identical. Note: we do not check the
* backing type, only the size and content.
*/
private static void checkEQimpl(MemorySegment a, MemorySegment b, String context) {
long offset = a.mismatch(b);
if (offset == -1) { return; }
// Print some general info
System.err.println("ERROR: Verify.checkEQ failed for: " + context);
printMemorySegment(a, "a " + context);
printMemorySegment(b, "b " + context);
// (1) Mismatch on size
if (a.byteSize() != b.byteSize()) {
throw new VerifyException("MemorySegment byteSize mismatch.");
}
// (2) Value mismatch
System.err.println(" Value mismatch at byte offset: " + offset);
printMemorySegmentValue(a, offset, 16);
printMemorySegmentValue(b, offset, 16);
throw new VerifyException("MemorySegment value mismatch.");
}
/**
* Verify that the content of two byte arrays is identical.
*/
private static void checkEQimpl(byte[] a, byte[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two char arrays is identical.
*/
private static void checkEQimpl(char[] a, char[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two short arrays is identical.
*/
private static void checkEQimpl(short[] a, short[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two int arrays is identical.
*/
private static void checkEQimpl(int[] a, int[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two long arrays is identical.
*/
private static void checkEQimpl(long[] a, long[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two float arrays is identical.
*/
private static void checkEQimpl(float[] a, float[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two double arrays is identical.
*/
private static void checkEQimpl(double[] a, double[] b, String context) {
checkEQimpl(MemorySegment.ofArray(a), MemorySegment.ofArray(b), context);
}
/**
* Verify that the content of two Object arrays is identical, recursively:
* every element is compared with checkEQimpl for the corresponding type.
*/
private static void checkEQimpl(Object[] a, Object[] b, String context) {
// (1) Length mismatch
if (a.length != b.length) {
System.err.println("ERROR: Verify.checkEQ failed: length mismatch: " + a.length + " vs " + b.length);
throw new VerifyException("Object array length mismatch.");
}
for (int i = 0; i < a.length; i++) {
// Recursive checkEQ call.
checkEQ(a[i], b[i], "[" + i + "]" + context);
}
}
private static void print(Object a, String context) {
if (a == null) {
System.err.println(" " + context + ": null");
} else {
System.err.println(" " + context + ": " + a);
}
}
private static void printMemorySegment(MemorySegment a, String context) {
Optional<Object> maybeBase = a.heapBase();
System.err.println(" " + context + " via MemorySegment:");
if (maybeBase.isEmpty()) {
System.err.println(" no heap base (native).");
} else {
Object base = maybeBase.get();
System.err.println(" heap base: " + base);
}
System.err.println(" address: " + a.address());
System.err.println(" byteSize: " + a.byteSize());
}
private static void printMemorySegmentValue(MemorySegment a, long offset, int range) {
long start = Long.max(offset - range, 0);
long end = Long.min(offset + range, a.byteSize());
for (long i = start; i < end; i++) {
byte b = a.get(ValueLayout.JAVA_BYTE, i);
System.err.print(String.format("%02x ", b));
}
System.err.println("");
for (long i = start; i < end; i++) {
if (i == offset) {
System.err.print("^^ ");
} else {
System.err.print(" ");
}
}
System.err.println("");
}
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.lib.verify;
/**
* Exception thrown in verification.
*/
public class VerifyException extends RuntimeException {
/**
* Creates a new verification exception.
*
* @param message Exception message for context when debugging.
*/
public VerifyException(String message) {
super("Value verification failed:" + System.lineSeparator() + message);
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Example test to show Verify.checkEQ with IR framework.
* @modules java.base/jdk.internal.misc
* @library /test/lib /
* @run driver verify.examples.TestVerifyInCheckMethod
*/
package verify.examples;
import compiler.lib.verify.*;
import compiler.lib.ir_framework.*;
/**
* Example to show the use of Verify.checkEQ in @Check method.
*/
public class TestVerifyInCheckMethod {
public static int[] INPUT_A = new int[100];
static {
for (int i = 0; i < INPUT_A.length; i++) {
INPUT_A[i] = i;
}
}
public static float INPUT_B = 42;
// Must make sure to clone input arrays, if it is mutated in the test.
public static Object GOLD = test(INPUT_A.clone(), INPUT_B);;
public static void main(String[] args) {
TestFramework.run();
}
@Setup
public static Object[] setup() {
// Must make sure to clone input arrays, if it is mutated in the test.
return new Object[] {INPUT_A.clone(), INPUT_B};
}
@Test
@Arguments(setup = "setup")
public static Object test(int[] a, float b) {
for (int i = 0; i < a.length; i++) {
a[i] = (int)(a[i] * b);
}
// Since we have more than one value, we wrap them in an Object[].
return new Object[] {a, b};
}
@Check(test = "test")
public static void check(Object result) {
Verify.checkEQ(result, GOLD);
}
}

View File

@ -0,0 +1,419 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Test functionality of IntGenerator implementations.
* @modules java.base/jdk.internal.misc
* @library /test/lib /
* @run driver verify.tests.TestVerify
*/
package verify.tests;
import java.lang.foreign.*;
import java.util.Random;
import jdk.test.lib.Utils;
import compiler.lib.verify.*;
public class TestVerify {
private static final Random RANDOM = Utils.getRandomInstance();
public static void main(String[] args) {
// Test consecutive memory: array, MemorySegment, etc.
testArrayByte();
testArrayChar();
testArrayShort();
testArrayInt();
testArrayLong();
testArrayFloat();
testArrayDouble();
testNativeMemorySegment();
// Test recursive data: Object array of values, etc.
testRecursive();
}
public static void testArrayByte() {
byte[] a = new byte[1000];
byte[] b = new byte[1001];
byte[] c = new byte[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testArrayShort() {
short[] a = new short[1000];
short[] b = new short[1001];
short[] c = new short[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testArrayChar() {
char[] a = new char[1000];
char[] b = new char[1001];
char[] c = new char[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testArrayInt() {
int[] a = new int[1000];
int[] b = new int[1001];
int[] c = new int[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testArrayLong() {
long[] a = new long[1000];
long[] b = new long[1001];
long[] c = new long[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testArrayFloat() {
float[] a = new float[1000];
float[] b = new float[1001];
float[] c = new float[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testArrayDouble() {
double[] a = new double[1000];
double[] b = new double[1001];
double[] c = new double[1000];
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(a));
Verify.checkEQ(MemorySegment.ofArray(b), MemorySegment.ofArray(b));
Verify.checkEQ(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
Verify.checkEQ(MemorySegment.ofArray(c), MemorySegment.ofArray(a));
// Size mismatch
checkNE(a, b);
// Size mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(b));
c[RANDOM.nextInt(c.length)] = 1;
// Value mismatch
checkNE(a, c);
// Value mismatch
checkNE(MemorySegment.ofArray(a), MemorySegment.ofArray(c));
}
public static void testNativeMemorySegment() {
MemorySegment a = Arena.ofAuto().allocate(1000, 1);
MemorySegment b = Arena.ofAuto().allocate(1001, 1);
MemorySegment c = Arena.ofAuto().allocate(1000, 1);
Verify.checkEQ(a, a);
Verify.checkEQ(b, b);
Verify.checkEQ(a, c);
Verify.checkEQ(c, a);
// Size mismatch
checkNE(a, b);
c.set(ValueLayout.JAVA_BYTE, RANDOM.nextLong(c.byteSize()), (byte)1);
// Value mismatch
checkNE(a, c);
}
public static void testRecursive() {
Verify.checkEQ(null, null);
// Null mismatch
checkNE(42, null);
byte[] a = new byte[1000];
int[] b = new int[1000];
int[] c = new int[1001];
int[] d = new int[1000];
Object[] o1 = new Object[]{a, a};
Object[] o2 = new Object[]{a, a, a};
Object[] o3 = new Object[]{a, a, null};
Object[] o4 = new Object[]{a, a, b};
Object[] o5 = new Object[]{a, a, c};
Object[] o6 = new Object[]{a, a, d};
Verify.checkEQ(o1, o1);
Verify.checkEQ(o2, o2);
Verify.checkEQ(o3, o3);
Verify.checkEQ(o4, o6);
// Size mismatch
checkNE(o1, o2);
// First level value mismatch: a vs null on position 2
checkNE(o2, o3);
// First level class mismatch: byte[] vs int[]
checkNE(o2, o4);
// Second level length mismatch on arrays b and c.
checkNE(o4, o5);
d[RANDOM.nextInt(d.length)] = 1;
// Second level value mismatch between b and d.
checkNE(o4, o6);
// Now test all primitive array types.
byte[] aB = new byte[100];
char[] aC = new char[100];
short[] aS = new short[100];
int[] aI = new int[100];
long[] aL = new long[100];
float[] aF = new float[100];
double[] aD = new double[100];
Verify.checkEQ(new Object[] {aB, aC, aS, aI, aL, aF, aD}, new Object[] {aB, aC, aS, aI, aL, aF, aD});
// First level class mismatch: char[] vs short[]
checkNE(new Object[] {aC}, new Object[] {aS});
// Verify MemorySegment
MemorySegment mC = MemorySegment.ofArray(aC);
MemorySegment mS = MemorySegment.ofArray(aS);
Verify.checkEQ(new Object[] {mC}, new Object[] {mC});
Verify.checkEQ(new Object[] {mS}, new Object[] {mS});
// Second level type mismatch: backing type short[] vs char[]
checkNE(new Object[] {mC}, new Object[] {mS});
// Second level type mismatch: backing type int[] vs char[]
MemorySegment mI = MemorySegment.ofArray(aI);
checkNE(new Object[] {mI}, new Object[] {mC});
// Verify boxed primitives:
Byte bb1 = 42;
Byte bb2 = 42;
Byte bb3 = 11;
Verify.checkEQ(new Object[] {(byte)42}, new Object[] {(byte)42});
Verify.checkEQ(new Object[] {(byte)42}, new Object[] {bb1});
Verify.checkEQ(new Object[] {bb1}, new Object[] {bb2});
// Second level value mismatch: 42 vs 11
checkNE(new Object[] {bb1}, new Object[] {bb3});
Verify.checkEQ((byte)42, (byte)42);
Verify.checkEQ((short)42, (short)42);
Verify.checkEQ((char)42, (char)42);
Verify.checkEQ((int)42, (int)42);
Verify.checkEQ((long)42, (long)42);
Verify.checkEQ((float)42, (float)42);
Verify.checkEQ((double)42, (double)42);
// Boxed type mismatch: float vs int
checkNE((int)42, (float)42);
// Boxed value mismatch.
for (int i = 0; i < 10; i++) {
byte v1 = (byte)RANDOM.nextInt();
byte v2 = (byte)(v1 ^ (1 << RANDOM.nextInt(8)));
checkNE(v1, v2);
}
for (int i = 0; i < 10; i++) {
char v1 = (char)RANDOM.nextInt();
char v2 = (char)(v1 ^ (1 << RANDOM.nextInt(16)));
checkNE(v1, v2);
}
for (int i = 0; i < 10; i++) {
char v1 = (char)RANDOM.nextInt();
char v2 = (char)(v1 ^ (1 << RANDOM.nextInt(16)));
checkNE(v1, v2);
}
for (int i = 0; i < 10; i++) {
int v1 = (int)RANDOM.nextInt();
int v2 = (int)(v1 ^ (1 << RANDOM.nextInt(32)));
checkNE(v1, v2);
checkNE(Float.intBitsToFloat(v1), Float.intBitsToFloat(v2));
}
for (int i = 0; i < 10; i++) {
long v1 = (long)RANDOM.nextLong();
long v2 = (long)(v1 ^ (1L << RANDOM.nextInt(64)));
checkNE(v1, v2);
checkNE(Double.longBitsToDouble(v1), Double.longBitsToDouble(v2));
}
}
public static void checkNE(Object a, Object b) {
try {
Verify.checkEQ(a, b);
throw new RuntimeException("Should have thrown");
} catch (VerifyException e) {}
}
}