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 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(Parameterized.class)
  36 public class StringIndexOfCharTest extends GraalCompilerTest {
  37 
  38     @Parameterized.Parameters(name = "{0},{1},{2}")
  39     public static Collection<Object[]> data() {
  40         ArrayList<Object[]> tests = new ArrayList<>();
  41         String longString = "ab";
  42         for (int i = 0; i < 15; i++) {
  43             longString = longString + longString;
  44         }
  45         longString = longString + "xx";
  46         String longUTF16String = "\u03bb" + longString;
  47         String mediumString = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax" +
  48                         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  49         String mediumUTF16String = "\u03bbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax" +
  50                         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  51         String[] targets = new String[]{"foobar", "foo", "bar", "\u03bbfoobar", mediumString, mediumUTF16String, longString, longUTF16String};
  52         int[] targetChars = new int[]{'f', 'o', 'r', 'x', Character.MIN_SUPPLEMENTARY_CODE_POINT};
  53         int[] targetOffsets = new int[12];
  54         for (int i = 0; i < targetOffsets.length; i++) {
  55             targetOffsets[i] = i - 1;
  56         }
  57         for (String source : targets) {
  58             for (int targetChar : targetChars) {
  59                 for (int offset : targetOffsets) {
  60                     tests.add(new Object[]{source, targetChar, offset});
  61                 }
  62             }
  63         }
  64 
  65         return tests;
  66     }
  67 
  68     private final String sourceString;
  69     private final int constantChar;
  70     private final int fromIndex;
  71 
  72     public StringIndexOfCharTest(String sourceString, int constantChar, int fromIndex) {
  73         this.sourceString = sourceString;
  74         this.constantChar = constantChar;
  75         this.fromIndex = fromIndex;
  76     }
  77 
  78     public int testStringIndexOf(String a, int b) {
  79         return a.indexOf(b);
  80     }
  81 
  82     public int testStringIndexOfOffset(String a, int b, int offset) {
  83         return a.indexOf(b, offset);
  84     }
  85 
  86     @Test
  87     public void testStringIndexOfConstant() {
  88         test("testStringIndexOf", this.sourceString, this.constantChar);
  89     }
  90 
  91     @Test
  92     public void testStringIndexOfConstantOffset() {
  93         test("testStringIndexOfOffset", this.sourceString, this.constantChar, this.fromIndex);
  94     }
  95 }