summaryrefslogtreecommitdiff
path: root/tests/082-inline-execute/src/Main.java
blob: b512091cf3257893b6c4b6c4bcd1a17efe5ecc51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Test for Jit's handling of string inline-execute.  Should be tested
 * twice - once using self-cosimulation (if available) and once without.
 * The non-self-cosimulation test ensures that the answer computed the first
 * time through (via the interpreter) is the same after looping enough
 * to trigger translation.
 */

import junit.framework.Assert;

public class Main {
    public static void main(String args[]) {
        int i;
        stringLengthTest();
        stringCharAtTest();
        stringIndexOfTest();
        for (i = 0; i < 1000; i++)
            stringCompareToTest();
    }

    public static void stringLengthTest() {
        String str0 = "";
        String str1 = "x";
        String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
        int len0 = str0.length();
        int len1 = str1.length();
        int len80 = str80.length();
        int i;

        System.out.println("Length of " + str0 + " : " + len0);
        System.out.println("Length of " + str1 + " : " + len1);
        System.out.println("Length of " + str80 + " : " + len80);

        for (i = 0; i < 1000; i++) {
            assert(str0.length() == len0);
            assert(str1.length() == len1);
            assert(str80.length() == len80);
        }
    }

    public static void stringCharAtTest() {
        String testStr = "Now is the time";
        int under = -1;
        int over = testStr.length();
        int numThrown = 0;
        int numNotThrown = 0;
        int at0 = testStr.charAt(0);
        int at1 = testStr.charAt(1);
        int at10 = testStr.charAt(10);
        int atLast = testStr.charAt(testStr.length()-1);
        int i;

        System.out.println(testStr + "[0] = \"" + (char)at0 + "\"");
        System.out.println(testStr + "[1] = \"" + (char)at1 + "\"");
        System.out.println(testStr + "[10] = \"" + (char)at10 + "\"");
        System.out.println(testStr + "[last] = \"" + (char)atLast + "\"");

        for (i = 0; i < 1000; i++) {
            assert(at0 == testStr.charAt(0));
            assert(at1 == testStr.charAt(1));
            assert(at10 == testStr.charAt(10));
            assert(atLast == testStr.charAt(testStr.length()-1));
        }

        for (i = 0; i < 1000; i++) {
            try {
                testStr.charAt(under);
                numNotThrown++;
            } catch (StringIndexOutOfBoundsException sioobe) {
                numThrown++;
            }
            try {
                testStr.charAt(over);
                numNotThrown++;
            } catch (StringIndexOutOfBoundsException sioobe) {
                numThrown++;
            }
        }
        assert(numNotThrown == 0);
        System.out.println("Num throws " + numThrown);
    }


    public static void stringIndexOfTest() {
        String str0 = "";
        String str3 = "abc";
        String str10 = "abcdefghij";
        String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
        int i;

        for (i = 0; i < 1000; i++) {
            assert(str0.indexOf('a') == -1);
            assert(str3.indexOf('a') == 0);
            assert(str3.indexOf('b') == 1);
            assert(str3.indexOf('c') == 2);
            assert(str10.indexOf('j') == 9);
            assert(str40.indexOf('a') == 0);
            assert(str40.indexOf('b') == 38);
            assert(str40.indexOf('c') == 39);
            assert(str0.indexOf('a',20) == -1);
            assert(str0.indexOf('a',0) == -1);
            assert(str0.indexOf('a',-1) == -1);
            assert(str3.indexOf('a',0) == 0);
            assert(str3.indexOf('a',1) == -1);
            assert(str3.indexOf('a',1234) == -1);
            assert(str3.indexOf('b',0) == 1);
            assert(str3.indexOf('b',1) == 1);
            assert(str3.indexOf('c',2) == 2);
            assert(str10.indexOf('j',5) == 9);
            assert(str10.indexOf('j',9) == 9);
            assert(str40.indexOf('a',10) == 10);
            assert(str40.indexOf('b',40) == -1);
        }

    }

    public static void stringCompareToTest() {
        String test = "0123456789";
        String test1 = new String("0123456789");    // different object
        String test2 = new String("0123456780");    // different value
        String offset = new String("xxx0123456789yyy");
        String sub = offset.substring(3, 13);
        String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
        String lc = "abcdefg";
        String uc = "ABCDEFG";
        Object blah = new Object();

        for (int i = 0; i < 100; i++) {
            String y = lc.toUpperCase();
            Assert.assertTrue(y.equals(uc));
        }

        Assert.assertEquals(str32.compareTo(str33), -1);
        Assert.assertEquals(str33.compareTo(str32), 1);

        Assert.assertTrue(test.equals(test));
        Assert.assertTrue(test.equals(test1));
        Assert.assertFalse(test.equals(test2));

        Assert.assertEquals(test.compareTo(test1), 0);
        Assert.assertTrue(test1.compareTo(test2) > 0);
        Assert.assertTrue(test2.compareTo(test1) < 0);

        /* compare string with a nonzero offset, in left/right side */
        Assert.assertEquals(test.compareTo(sub), 0);
        Assert.assertEquals(sub.compareTo(test), 0);
        Assert.assertTrue(test.equals(sub));
        Assert.assertTrue(sub.equals(test));
        /* same base, one is a substring */
        Assert.assertFalse(offset.equals(sub));
        Assert.assertFalse(sub.equals(offset));
        /* wrong class */
        Assert.assertFalse(test.equals(blah));

        /* null ptr - throw */
        try {
            test.compareTo(null);
            Assert.fail("didn't get expected npe");
        } catch (NullPointerException npe) {
        }
        /* null ptr - ok */
        Assert.assertFalse(test.equals(null));

        test = test.substring(1);
        Assert.assertTrue(test.equals("123456789"));
        Assert.assertFalse(test.equals(test1));

        test = test.substring(1);
        Assert.assertTrue(test.equals("23456789"));

        test = test.substring(1);
        Assert.assertTrue(test.equals("3456789"));

        test = test.substring(1);
        Assert.assertTrue(test.equals("456789"));

        test = test.substring(3,5);
        Assert.assertTrue(test.equals("78"));

        test = "this/is/a/path";
        String[] strings = test.split("/");
        Assert.assertEquals(4, strings.length);

        Assert.assertEquals("this is a path", test.replaceAll("/", " "));
        Assert.assertEquals("this is a path", test.replace("/", " "));
    }

}