|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 | * @test
|
26 | 26 | * @bug 8054307
|
27 | 27 | * @summary Tests correctness of string related intrinsics and C2 optimizations.
|
| 28 | + * @library /test/lib |
28 | 29 | *
|
29 | 30 | * @run main/timeout=240 compiler.intrinsics.string.TestStringIntrinsics
|
30 | 31 | */
|
31 | 32 |
|
32 | 33 | package compiler.intrinsics.string;
|
33 | 34 |
|
| 35 | +import jdk.test.lib.format.Format; |
| 36 | +import jdk.test.lib.format.ArrayCodec; |
| 37 | + |
34 | 38 | import java.lang.annotation.ElementType;
|
35 | 39 | import java.lang.annotation.Retention;
|
36 | 40 | import java.lang.annotation.RetentionPolicy;
|
@@ -77,8 +81,8 @@ public void run() throws Exception {
|
77 | 81 | for (Method m : TestStringIntrinsics.class.getMethods()) {
|
78 | 82 | if (m.isAnnotationPresent(Test.class)) {
|
79 | 83 | System.out.print("Checking " + m.getName() + "... ");
|
80 |
| - Operation op = m.getAnnotation(Test.class).op(); |
81 | 84 | Test antn = m.getAnnotation(Test.class);
|
| 85 | + Operation op = antn.op(); |
82 | 86 | if (isStringConcatTest(op)) {
|
83 | 87 | checkStringConcat(op, m, antn);
|
84 | 88 | } else {
|
@@ -121,13 +125,13 @@ private void checkIntrinsics(Operation op, Method m, String latin1, String utf16
|
121 | 125 |
|
122 | 126 | switch (op) {
|
123 | 127 | 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[] {}); |
128 | 132 | break;
|
129 | 133 | case ARR_EQUALS_C:
|
130 |
| - invokeAndCheck(m, (incU == 0), utf16.toCharArray(), arrU); |
| 134 | + invokeAndCompareArrays(m, (incU == 0), utf16.toCharArray(), arrU); |
131 | 135 | break;
|
132 | 136 | case EQUALS:
|
133 | 137 | invokeAndCheck(m, (incL == 0), latin1, latin1Copy);
|
@@ -240,18 +244,49 @@ private void checkStringConcat(Operation op, Method m, Test antn) throws Excepti
|
240 | 244 | }
|
241 | 245 | }
|
242 | 246 |
|
| 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 | + |
243 | 270 | /**
|
244 | 271 | * Invokes method 'm' by passing arguments 'args' and checks if the
|
245 | 272 | * returned value equals 'expectedResult'.
|
246 | 273 | */
|
247 | 274 | 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); |
255 | 290 | }
|
256 | 291 | }
|
257 | 292 |
|
|
0 commit comments