001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.loop;
024
025import org.junit.*;
026
027import com.oracle.graal.jtt.*;
028
029/*
030 * see java.lang.String.lastIndexOf(char[], int, int, char[], int ,int, int)
031 */
032public class LoopLastIndexOf extends JTTTest {
033
034    private static final char[] v1 = new char[]{'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'};
035    private static final char[] v2 = new char[]{'d', 'a'};
036    private static final char[] v3 = new char[]{'d', 'b', 'c'};
037    private static final char[] v4 = new char[]{'z', 'a', 'b', 'c'};
038
039    public static int test(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndexParam) {
040        int rightIndex = sourceCount - targetCount;
041        int fromIndex = fromIndexParam;
042        if (fromIndex < 0) {
043            return -1;
044        }
045        if (fromIndex > rightIndex) {
046            fromIndex = rightIndex;
047        }
048        /* Empty string always matches. */
049        if (targetCount == 0) {
050            return fromIndex;
051        }
052
053        int strLastIndex = targetOffset + targetCount - 1;
054        char strLastChar = target[strLastIndex];
055        int min = sourceOffset + targetCount - 1;
056        int i = min + fromIndex;
057
058        startSearchForLastChar: while (true) {
059            while (i >= min && source[i] != strLastChar) {
060                i--;
061            }
062            if (i < min) {
063                return -1;
064            }
065            int j = i - 1;
066            int start = j - (targetCount - 1);
067            int k = strLastIndex - 1;
068
069            while (j > start) {
070                if (source[j--] != target[k--]) {
071                    i--;
072                    continue startSearchForLastChar;
073                }
074            }
075            return start - sourceOffset + 1;
076        }
077    }
078
079    @Test
080    public void run0() throws Throwable {
081        runTest("test", v1, 0, v1.length, v2, 0, v2.length, 10);
082    }
083
084    @Test
085    public void run1() throws Throwable {
086        runTest("test", v1, 0, v1.length, v3, 0, v3.length, 10);
087    }
088
089    @Test
090    public void run2() throws Throwable {
091        runTest("test", v1, 0, v1.length, v4, 0, v4.length, 10);
092    }
093
094    @Test
095    public void run3() throws Throwable {
096        runTest("test", v1, 1, v1.length - 1, v3, 0, v3.length, 10);
097    }
098
099    @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}