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