1 /*
   2  * Copyright (c) 2012, 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 package org.graalvm.compiler.jtt.loop;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.jtt.JTTTest;
  28 
  29 /*
  30  * see java.lang.String.lastIndexOf(char[], int, int, char[], int ,int, int)
  31  */
  32 public class LoopLastIndexOf extends JTTTest {
  33 
  34     private static final char[] v1 = new char[]{'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'};
  35     private static final char[] v2 = new char[]{'d', 'a'};
  36     private static final char[] v3 = new char[]{'d', 'b', 'c'};
  37     private static final char[] v4 = new char[]{'z', 'a', 'b', 'c'};
  38 
  39     public static int test(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndexParam) {
  40         int rightIndex = sourceCount - targetCount;
  41         int fromIndex = fromIndexParam;
  42         if (fromIndex < 0) {
  43             return -1;
  44         }
  45         if (fromIndex > rightIndex) {
  46             fromIndex = rightIndex;
  47         }
  48         /* Empty string always matches. */
  49         if (targetCount == 0) {
  50             return fromIndex;
  51         }
  52 
  53         int strLastIndex = targetOffset + targetCount - 1;
  54         char strLastChar = target[strLastIndex];
  55         int min = sourceOffset + targetCount - 1;
  56         int i = min + fromIndex;
  57 
  58         startSearchForLastChar: while (true) {
  59             while (i >= min && source[i] != strLastChar) {
  60                 i--;
  61             }
  62             if (i < min) {
  63                 return -1;
  64             }
  65             int j = i - 1;
  66             int start = j - (targetCount - 1);
  67             int k = strLastIndex - 1;
  68 
  69             while (j > start) {
  70                 if (source[j--] != target[k--]) {
  71                     i--;
  72                     continue startSearchForLastChar;
  73                 }
  74             }
  75             return start - sourceOffset + 1;
  76         }
  77     }
  78 
  79     @Test
  80     public void run0() throws Throwable {
  81         runTest("test", v1, 0, v1.length, v2, 0, v2.length, 10);
  82     }
  83 
  84     @Test
  85     public void run1() throws Throwable {
  86         runTest("test", v1, 0, v1.length, v3, 0, v3.length, 10);
  87     }
  88 
  89     @Test
  90     public void run2() throws Throwable {
  91         runTest("test", v1, 0, v1.length, v4, 0, v4.length, 10);
  92     }
  93 
  94     @Test
  95     public void run3() throws Throwable {
  96         runTest("test", v1, 1, v1.length - 1, v3, 0, v3.length, 10);
  97     }
  98 
  99     @Test
 100     // (expected = ArrayIndexOutOfBoundsException.class)
 101     public void run4() throws Throwable {
 102         runTest("test", v1, 1, v1.length, v3, 0, v3.length, 10);
 103     }
 104 }