src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements.test/src/org/graalvm/compiler/replacements/test/StringSubstitutionTestBase.java
author iveresov
Fri, 17 Aug 2018 13:20:53 -0700
changeset 51436 091c0d22e735
child 52910 583fd71c47d6
permissions -rw-r--r--
8206992: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51436
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     1
/*
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     4
 *
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     8
 *
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    14
 *
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    18
 *
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    22
 */
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    23
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    24
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    25
package org.graalvm.compiler.replacements.test;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    26
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    27
import org.graalvm.compiler.nodes.StructuredGraph;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    28
import org.junit.Assert;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    29
import org.junit.Assume;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    30
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    31
import jdk.vm.ci.amd64.AMD64;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    32
import jdk.vm.ci.aarch64.AArch64;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    33
import jdk.vm.ci.code.InstalledCode;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    34
import jdk.vm.ci.meta.ResolvedJavaMethod;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    35
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    36
/**
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    37
 * Tests compareTo method intrinsic.
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    38
 */
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    39
public class StringSubstitutionTestBase extends MethodSubstitutionTest {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    40
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    41
    private ResolvedJavaMethod realMethod = null;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    42
    private ResolvedJavaMethod testMethod = null;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    43
    private InstalledCode testCode = null;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    44
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    45
    protected final String[] testData = new String[]{
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    46
                    "A", "\uFF21", "AB", "A", "a", "Ab", "AA", "\uFF21",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    47
                    "A\uFF21", "ABC", "AB", "ABcD", "ABCD\uFF21\uFF21", "ABCD\uFF21", "ABCDEFG\uFF21", "ABCD",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    48
                    "ABCDEFGH\uFF21\uFF21", "\uFF22", "\uFF21\uFF22", "\uFF21A",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    49
                    "\uFF21\uFF21",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    50
                    "\u043c\u0430\u043c\u0430\u0020\u043c\u044b\u043b\u0430\u0020\u0440\u0430\u043c\u0443\u002c\u0020\u0440\u0430\u043c\u0430\u0020\u0441\u044a\u0435\u043b\u0430\u0020\u043c\u0430\u043c\u0443",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    51
                    "crazy dog jumps over laszy fox",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    52
                    "some-string\0xff",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    53
                    "XMM-XMM-YMM-YMM-ZMM-ZMM-ZMM-ZMM-",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    54
                    "XMM-XMM+YMM-YMM-ZMM-ZMM-ZMM-ZMM-",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    55
                    "XMM-XMM-YMM-YMM+ZMM-ZMM-ZMM-ZMM-",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    56
                    "XMM-XMM-YMM-YMM-ZMM-ZMM-ZMM-ZMM+",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    57
                    "XMM-XMM-XMM-XMM-YMM-YMM-YMM-YMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    58
                    "XMM-XMM-XMM-XMM+YMM-YMM-YMM-YMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    59
                    "XMM-XMM-XMM-XMM-YMM-YMM-YMM-YMM+ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    60
                    "XMM-XMM-XMM-XMM-YMM-YMM-YMM-YMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM-ZMM+",
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    61
                    ""
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    62
    };
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    63
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    64
    protected void initSubstitution(ResolvedJavaMethod theRealMethod,
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    65
                    ResolvedJavaMethod theTestMethod, Class<?> expectedNode) {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    66
        Assume.assumeTrue((getTarget().arch instanceof AMD64) || (getTarget().arch instanceof AArch64));
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    67
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    68
        realMethod = theRealMethod;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    69
        testMethod = theTestMethod;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    70
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    71
        StructuredGraph graph = testGraph(testMethod.getName());
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    72
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    73
        // Check to see if the resulting graph contains the expected node
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    74
        StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null);
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    75
        if (replacement == null) {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    76
            assertInGraph(graph, expectedNode);
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    77
        }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    78
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    79
        // Force compilation
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    80
        testCode = getCode(testMethod);
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    81
        Assert.assertNotNull(testCode);
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    82
    }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    83
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    84
    private void executeSubstitution(String s0, String s1) {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    85
        Object expected = invokeSafe(realMethod, s0, s1);
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    86
        // Verify that the original method and the substitution produce the same value
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    87
        assertDeepEquals(expected, invokeSafe(testMethod, null, s0, s1));
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    88
        // Verify that the generated code and the original produce the same value
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    89
        assertDeepEquals(expected, executeVarargsSafe(testCode, s0, s1));
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    90
    }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    91
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    92
    protected void testEqualString() {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    93
        String s = "equal-string";
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    94
        executeSubstitution(s, new String(s.toCharArray()));
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    95
    }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    96
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    97
    protected void testDifferentString() {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    98
        // Smoke test for primary cases
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
    99
        executeSubstitution("AAAAAAAA", "");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   100
        // LL
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   101
        executeSubstitution("some-stringA", "some-string\0xff");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   102
        // UU
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   103
        executeSubstitution("\u2241AAAAAAAB", "\u2241\u0041\u0041\u0041\u0041\u0041\u0041\u0041\uFF41");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   104
        // LU
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   105
        executeSubstitution("AAAAAAAAB", "\u0041\u0041\u0041\u0041\u0041\u0041\u0041\u0041\uFF41");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   106
    }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   107
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   108
    protected void testAllStrings() {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   109
        for (String s0 : testData) {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   110
            for (String s1 : testData) {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   111
                try {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   112
                    executeSubstitution(s0, s1);
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   113
                } catch (AssertionError ex) {
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   114
                    System.out.println("FAIL: '" + ex + "'");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   115
                    System.out.println(" ***: s0 '" + s0 + "'");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   116
                    System.out.println(" ***: s1 '" + s1 + "'");
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   117
                    throw ex;
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   118
                }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   119
            }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   120
        }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   121
    }
091c0d22e735 8206992: Update Graal
iveresov
parents:
diff changeset
   122
}