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