1 /*
   2  * Copyright (c) 2016, 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 java.util.ArrayList;
  28 import java.util.Collection;
  29 
  30 import org.graalvm.compiler.core.test.GraalCompilerTest;
  31 import org.junit.Test;
  32 import org.junit.runner.RunWith;
  33 import org.junit.runners.Parameterized;
  34 
  35 @RunWith(value = Parameterized.class)
  36 public abstract class StringIndexOfTestBase extends GraalCompilerTest {
  37     @Parameterized.Parameter(value = 0) public String sourceString;
  38     @Parameterized.Parameter(value = 1) public String constantString;
  39 
  40     @Parameterized.Parameters(name = "{0},{1}")
  41     public static Collection<Object[]> data() {
  42         ArrayList<Object[]> tests = new ArrayList<>();
  43         String[] targets = new String[]{"foobar", "foo", "bar"};
  44         String[] utf16targets = new String[]{"grga " + ((char) 0x10D) + "varak", "grga", ((char) 0x10D) + "varak"};
  45         addTargets(tests, targets);
  46         addTargets(tests, utf16targets);
  47         return tests;
  48     }
  49 
  50     private static void addTargets(ArrayList<Object[]> tests, String[] targets) {
  51         for (String source : targets) {
  52             for (String target : targets) {
  53                 tests.add(new Object[]{source, target});
  54             }
  55             tests.add(new Object[]{source, ""});
  56             tests.add(new Object[]{"", source});
  57             tests.add(new Object[]{"", ""});
  58         }
  59         for (String source : targets) {
  60             String s = "";
  61             for (int i = 0; i < 10; i++) {
  62                 s = s + source.substring(0, source.length() - 1);
  63             }
  64             for (String target : targets) {
  65                 tests.add(new Object[]{s, target});
  66                 tests.add(new Object[]{s + target, target});
  67                 tests.add(new Object[]{s.substring(0, s.length() - 1) + s, s});
  68             }
  69         }
  70     }
  71 
  72     public int testStringIndexOf(String a, String b) {
  73         return a.indexOf(b);
  74     }
  75 
  76     public int testStringIndexOfOffset(String a, String b, int fromIndex) {
  77         return a.indexOf(b, fromIndex);
  78     }
  79 
  80     public int testStringBuilderIndexOf(StringBuilder a, String b) {
  81         return a.indexOf(b);
  82     }
  83 
  84     public int testStringBuilderIndexOfOffset(StringBuilder a, String b, int fromIndex) {
  85         return a.indexOf(b, fromIndex);
  86     }
  87 
  88     @Test
  89     public void testStringIndexOfConstant() {
  90         test("testStringIndexOf", new Object[]{this.sourceString, this.constantString});
  91     }
  92 
  93     @Test
  94     public void testStringIndexOfConstantOffset() {
  95         test("testStringIndexOfOffset", new Object[]{this.sourceString, this.constantString, -1});
  96         test("testStringIndexOfOffset", new Object[]{this.sourceString, this.constantString, 0});
  97         test("testStringIndexOfOffset", new Object[]{this.sourceString, this.constantString, Math.max(0, sourceString.length() - constantString.length())});
  98     }
  99 
 100     @Test
 101     public void testStringBuilderIndexOfConstant() {
 102         /*
 103          * Put a copy of the target string in the space after the current string to detect cases
 104          * where we search too far.
 105          */
 106         StringBuilder sb = new StringBuilder(this.sourceString);
 107         sb.append(constantString);
 108         sb.setLength(sourceString.length());
 109         test("testStringBuilderIndexOf", new Object[]{sb, this.constantString});
 110     }
 111 
 112     @Test
 113     public void testStringBuilderIndexOfConstantOffset() {
 114         /*
 115          * Put a copy of the target string in the space after the current string to detect cases
 116          * where we search too far.
 117          */
 118         StringBuilder sb = new StringBuilder(this.sourceString);
 119         sb.append(constantString);
 120         sb.setLength(sourceString.length());
 121         test("testStringBuilderIndexOfOffset", new Object[]{sb, this.constantString, -1});
 122         test("testStringBuilderIndexOfOffset", new Object[]{sb, this.constantString, 0});
 123         test("testStringBuilderIndexOfOffset", new Object[]{sb, this.constantString, Math.max(0, sourceString.length() - constantString.length())});
 124     }
 125 }