blob: d5c56045419617e21674e1bf37704b5e474be70f [file] [log] [blame]
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Simple test for array accesses.
18
19public class Main extends TestCase {
20 public static void main(String[] args) {
21 $opt$testReads(new boolean[1], new byte[1], new char[1], new short[1],
22 new int[1], new Object[1], new long[1], 0);
23 $opt$testWrites(new boolean[2], new byte[2], new char[2], new short[2],
24 new int[2], new Object[2], new long[2], 1);
25 ensureThrows(new boolean[2], 2);
26 ensureThrows(new boolean[2], 4);
27 ensureThrows(new boolean[2], -1);
28 ensureThrows(new boolean[2], Integer.MIN_VALUE);
29 ensureThrows(new boolean[2], Integer.MAX_VALUE);
30 }
31
32 static void $opt$testReads(boolean[] bools, byte[] bytes, char[] chars, short[] shorts,
33 int[] ints, Object[] objects, long[] longs, int index) {
34 assertEquals(false, bools[0]);
35 assertEquals(false, bools[index]);
36
37 assertEquals(0, bytes[0]);
38 assertEquals(0, bytes[index]);
39
40 assertEquals(0, chars[0]);
41 assertEquals(0, chars[index]);
42
43 assertEquals(0, shorts[0]);
44 assertEquals(0, shorts[index]);
45
46 assertEquals(0, ints[0]);
47 assertEquals(0, ints[index]);
48
49 assertNull(objects[0]);
50 assertNull(objects[index]);
51
52 assertEquals(0, longs[0]);
53 assertEquals(0, longs[index]);
54 }
55
56 static void $opt$testWrites(boolean[] bools, byte[] bytes, char[] chars, short[] shorts,
57 int[] ints, Object[] objects, long[] longs, int index) {
58 bools[0] = true;
59 assertEquals(true, bools[0]);
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 bools[index] = true;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010061 assertEquals(true, bools[index]);
62
63 bytes[0] = -4;
64 assertEquals(-4, bytes[0]);
65 bytes[index] = -8;
66 assertEquals(-8, bytes[index]);
67
68 chars[0] = 'c';
69 assertEquals('c', chars[0]);
70 chars[index] = 'd';
71 assertEquals('d', chars[index]);
72
Nicolas Geoffrayb6e72062014-10-07 14:54:48 +010073 chars[0] = 65535;
74 assertEquals(65535, chars[0]);
75 // Do an update between the two max value updates, to avoid
76 // optimizing the second away.
77 chars[index] = 0;
78 assertEquals(0, chars[index]);
79 chars[index] = 65535;
80 assertEquals(65535, chars[index]);
81
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010082 shorts[0] = -42;
83 assertEquals(-42, shorts[0]);
84 shorts[index] = -84;
85 assertEquals(-84, shorts[index]);
86
87 ints[0] = -32;
88 assertEquals(-32, ints[0]);
89 ints[index] = -64;
90 assertEquals(-64, ints[index]);
91
92 Object o1 = new Object();
93 objects[0] = o1;
94 assertEquals(o1, objects[0]);
95 Object o2 = new Object();
96 objects[index] = o2;
97 assertEquals(o2, objects[index]);
Nicolas Geoffrayb6e72062014-10-07 14:54:48 +010098 // Longs are initially not supported in the linear scan register allocator
99 // on 32bits. So we call out a long helper to ensure this method gets
100 // optimized.
101 $opt$testLongWrites(longs, index);
102 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100103
Nicolas Geoffrayb6e72062014-10-07 14:54:48 +0100104 public static void $opt$testLongWrites(long[] longs, int index) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100105 long l = -21876876876876876L;
106 longs[0] = l;
107 assertEquals(l, longs[0]);
108 l = -21876876876876877L;
109 longs[index] = l;
110 assertEquals(l, longs[index]);
111 }
112
113 public static void ensureThrows(boolean[] array, int index) {
114 ArrayIndexOutOfBoundsException exception = null;
115 try {
116 $opt$doArrayLoad(array, index);
117 } catch (ArrayIndexOutOfBoundsException e) {
118 exception = e;
119 }
120
121 assertNotNull(exception);
122 assertTrue(exception.toString().contains(Integer.toString(index)));
123
124 exception = null;
125 try {
126 $opt$doArrayStore(array, index);
127 } catch (ArrayIndexOutOfBoundsException e) {
128 exception = e;
129 }
130
131 assertNotNull(exception);
132 assertTrue(exception.toString().contains(Integer.toString(index)));
133 }
134
135 public static void $opt$doArrayLoad(boolean[] array, int index) {
136 boolean res = array[index];
137 }
138
139 public static void $opt$doArrayStore(boolean[] array, int index) {
140 array[index] = false;
141 }
142}