1 /*
   2  * Copyright (c) 2016, 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 static org.junit.Assume.assumeFalse;
  28 
  29 import java.util.ArrayList;
  30 import java.util.Collection;
  31 
  32 import org.graalvm.compiler.core.test.GraalCompilerTest;
  33 import org.junit.Test;
  34 import org.junit.runner.RunWith;
  35 import org.junit.runners.Parameterized;
  36 
  37 import jdk.vm.ci.aarch64.AArch64;
  38 
  39 @RunWith(value = Parameterized.class)
  40 public abstract class StringIndexOfTestBase extends GraalCompilerTest {
  41 
  42     @Parameterized.Parameters(name = "{0},{1}")
  43     public static Collection<Object[]> data() {
  44         ArrayList<Object[]> tests = new ArrayList<>();
  45         String[] targets = new String[]{"foobar", "foo", "bar"};
  46         String[] utf16targets = new String[]{"grga " + ((char) 0x10D) + "varak", "grga", ((char) 0x10D) + "varak"};
  47         addTargets(tests, targets);
  48         addTargets(tests, utf16targets);
  49 
  50         // Check long targets
  51         // Checkstyle: stop
  52         String lipsum = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata ";
  53         // Checkstyle: resume
  54         String lipsumUTF16 = lipsum + ((char) 0x10D);
  55         int[] subStringLengths = {7, 8, 15, 16, 31, 32, 63, 64};
  56         for (int len : subStringLengths) {
  57             String target = lipsum.substring(50, 50 + len);
  58             tests.add(new Object[]{lipsum, target});
  59             tests.add(new Object[]{lipsum, target + "X"});
  60             tests.add(new Object[]{lipsumUTF16, target});
  61             tests.add(new Object[]{lipsumUTF16, target + "X"});
  62             tests.add(new Object[]{lipsumUTF16, target + ((char) 0x10D)});
  63         }
  64         tests.add(new Object[]{
  65                         "\u0100\u0101\u0102\u0103\u0104\u0105\u0106\u0107\u00f9\u00fa\u00fb\u00fc\u00fd\u00fe\u00ff\u0108\u0109\u010a\u010b\u010c",
  66                         "\u00f9\u00fa\u00fb\u00fc\u00fd\u00fe\u00ff"});
  67 
  68         return tests;
  69     }
  70 
  71     private static void addTargets(ArrayList<Object[]> tests, String[] targets) {
  72         for (String source : targets) {
  73             for (String target : targets) {
  74                 tests.add(new Object[]{source, target});
  75             }
  76             tests.add(new Object[]{source, ""});
  77             tests.add(new Object[]{"", source});
  78             tests.add(new Object[]{"", ""});
  79         }
  80         for (String source : targets) {
  81             String s = "";
  82             for (int i = 0; i < 10; i++) {
  83                 s = s + source.substring(0, source.length() - 1);
  84             }
  85             for (String target : targets) {
  86                 tests.add(new Object[]{s, target});
  87                 tests.add(new Object[]{s + target, target});
  88                 tests.add(new Object[]{s.substring(0, s.length() - 1) + s, s});
  89             }
  90         }
  91     }
  92 
  93     protected final String sourceString;
  94     protected final String constantString;
  95 
  96     public StringIndexOfTestBase(String sourceString, String constantString) {
  97         this.sourceString = sourceString;
  98         this.constantString = constantString;
  99     }
 100 
 101     public int testStringIndexOf(String a, String b) {
 102         return a.indexOf(b);
 103     }
 104 
 105     public int testStringIndexOfOffset(String a, String b, int fromIndex) {
 106         return a.indexOf(b, fromIndex);
 107     }
 108 
 109     public int testStringBuilderIndexOf(StringBuilder a, String b) {
 110         return a.indexOf(b);
 111     }
 112 
 113     public int testStringBuilderIndexOfOffset(StringBuilder a, String b, int fromIndex) {
 114         return a.indexOf(b, fromIndex);
 115     }
 116 
 117     @Test
 118     public void testStringIndexOfConstant() {
 119         test("testStringIndexOf", new Object[]{this.sourceString, this.constantString});
 120     }
 121 
 122     @Test
 123     public void testStringIndexOfConstantOffset() {
 124         test("testStringIndexOfOffset", new Object[]{this.sourceString, this.constantString, -1});
 125         test("testStringIndexOfOffset", new Object[]{this.sourceString, this.constantString, 0});
 126         test("testStringIndexOfOffset", new Object[]{this.sourceString, this.constantString, Math.max(0, sourceString.length() - constantString.length())});
 127     }
 128 
 129     @Test
 130     public void testStringBuilderIndexOfConstant() {
 131         assumeFalse("Disabled on AArch64 due to issues on AArch64; see GR-13100 or JDK-8215792", getTarget().arch instanceof AArch64);
 132         /*
 133          * Put a copy of the target string in the space after the current string to detect cases
 134          * where we search too far.
 135          */
 136         StringBuilder sb = new StringBuilder(this.sourceString);
 137         sb.append(constantString);
 138         sb.setLength(sourceString.length());
 139         test("testStringBuilderIndexOf", new Object[]{sb, this.constantString});
 140     }
 141 
 142     @Test
 143     public void testStringBuilderIndexOfConstantOffset() {
 144         assumeFalse("Disabled on AArch64 due to issues on AArch64; see GR-13100 or JDK-8215792", getTarget().arch instanceof AArch64);
 145         /*
 146          * Put a copy of the target string in the space after the current string to detect cases
 147          * where we search too far.
 148          */
 149         StringBuilder sb = new StringBuilder(this.sourceString);
 150         sb.append(constantString);
 151         sb.setLength(sourceString.length());
 152         test("testStringBuilderIndexOfOffset", new Object[]{sb, this.constantString, -1});
 153         test("testStringBuilderIndexOfOffset", new Object[]{sb, this.constantString, 0});
 154         test("testStringBuilderIndexOfOffset", new Object[]{sb, this.constantString, Math.max(0, sourceString.length() - constantString.length())});
 155     }
 156 }