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