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