Skip to content

Commit 52e45a3

Browse files
Evgeny Nikitiniignatev
authored andcommitted
8229186: Improve error messages for TestStringIntrinsics failures
Reviewed-by: iignatyev, lmesnik
1 parent 6d2c1a6 commit 52e45a3

File tree

6 files changed

+1190
-14
lines changed

6 files changed

+1190
-14
lines changed

test/hotspot/jtreg/compiler/intrinsics/string/TestStringIntrinsics.java

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,12 +25,16 @@
2525
* @test
2626
* @bug 8054307
2727
* @summary Tests correctness of string related intrinsics and C2 optimizations.
28+
* @library /test/lib
2829
*
2930
* @run main/timeout=240 compiler.intrinsics.string.TestStringIntrinsics
3031
*/
3132

3233
package compiler.intrinsics.string;
3334

35+
import jdk.test.lib.format.Format;
36+
import jdk.test.lib.format.ArrayCodec;
37+
3438
import java.lang.annotation.ElementType;
3539
import java.lang.annotation.Retention;
3640
import java.lang.annotation.RetentionPolicy;
@@ -77,8 +81,8 @@ public void run() throws Exception {
7781
for (Method m : TestStringIntrinsics.class.getMethods()) {
7882
if (m.isAnnotationPresent(Test.class)) {
7983
System.out.print("Checking " + m.getName() + "... ");
80-
Operation op = m.getAnnotation(Test.class).op();
8184
Test antn = m.getAnnotation(Test.class);
85+
Operation op = antn.op();
8286
if (isStringConcatTest(op)) {
8387
checkStringConcat(op, m, antn);
8488
} else {
@@ -121,13 +125,13 @@ private void checkIntrinsics(Operation op, Method m, String latin1, String utf16
121125

122126
switch (op) {
123127
case ARR_EQUALS_B:
124-
invokeAndCheck(m, (incL == 0), latin1.getBytes("ISO-8859-1"), latin1Copy.getBytes("ISO-8859-1"));
125-
invokeAndCheck(m, true, new byte[] {1, 2, 3}, new byte[] {1, 2, 3});
126-
invokeAndCheck(m, true, new byte[] {1}, new byte[] {1});
127-
invokeAndCheck(m, true, new byte[] {}, new byte[] {});
128+
invokeAndCompareArrays(m, (incL == 0), latin1.getBytes("ISO-8859-1"), latin1Copy.getBytes("ISO-8859-1"));
129+
invokeAndCompareArrays(m, true, new byte[] {1, 2, 3}, new byte[] {1, 2, 3});
130+
invokeAndCompareArrays(m, true, new byte[] {1}, new byte[] {1});
131+
invokeAndCompareArrays(m, true, new byte[] {}, new byte[] {});
128132
break;
129133
case ARR_EQUALS_C:
130-
invokeAndCheck(m, (incU == 0), utf16.toCharArray(), arrU);
134+
invokeAndCompareArrays(m, (incU == 0), utf16.toCharArray(), arrU);
131135
break;
132136
case EQUALS:
133137
invokeAndCheck(m, (incL == 0), latin1, latin1Copy);
@@ -240,18 +244,49 @@ private void checkStringConcat(Operation op, Method m, Test antn) throws Excepti
240244
}
241245
}
242246

247+
/**
248+
* Invokes method 'm' by passing arguments the two 'args' (which are supposed to be arrays)
249+
* checks if the returned value. In case of error and arrays being not equal, prints their difference.
250+
*/
251+
private void invokeAndCompareArrays(Method m, boolean expectedResult, Object arg0, Object arg1) throws Exception {
252+
boolean result = (Boolean)m.invoke(null, arg0, arg1);
253+
if (expectedResult == result)
254+
return;
255+
256+
String cause = String.format("Result: (%b) of '%s' is not equal to expected (%b)",
257+
result, m.getName(), expectedResult);
258+
259+
if (expectedResult == true) {
260+
System.err.println(cause);
261+
System.err.println(Format.arrayDiff(arg0, arg1));
262+
} else {
263+
System.err.println(cause);
264+
System.err.printf("First array argument: %n %s%n", ArrayCodec.format(arg0));
265+
}
266+
267+
throw new RuntimeException(cause);
268+
}
269+
243270
/**
244271
* Invokes method 'm' by passing arguments 'args' and checks if the
245272
* returned value equals 'expectedResult'.
246273
*/
247274
private void invokeAndCheck(Method m, Object expectedResult, Object... args) throws Exception {
248-
Object result = m.invoke(null, args);
249-
if (!result.equals(expectedResult)) {
250-
// System.out.println("Expected:");
251-
// System.out.println(expectedResult);
252-
// System.out.println("Returned:");
253-
// System.out.println(result);
254-
throw new RuntimeException("Result of '" + m.getName() + "' not equal to expected value.");
275+
Object actualResult = m.invoke(null, args);
276+
if (!actualResult.equals(expectedResult)) {
277+
var nl = System.lineSeparator();
278+
StringBuilder msgBuilder = new StringBuilder();
279+
msgBuilder.append("Actual result of '" + m.getName() + "' is not equal to expected value." + nl);
280+
msgBuilder.append("Expected: " + Format.asLiteral(expectedResult) + nl);
281+
msgBuilder.append("Actual: " + Format.asLiteral(actualResult));
282+
283+
for (int i = 0; i < args.length; i++) {
284+
msgBuilder.append(nl + " Arg" + i + ": " + Format.asLiteral(args[i]));
285+
}
286+
287+
final String message = msgBuilder.toString();
288+
System.err.println(message);
289+
throw new RuntimeException(message);
255290
}
256291
}
257292

0 commit comments

Comments
 (0)