blob: cb3ecde21ad931881e763bba384dc5372f3a8f3b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2008 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 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
17/**
18 * Exercise arrays.
19 */
20public class Array {
21
22 /*
23 * Verify array contents.
24 */
25 static void checkBytes(byte[] bytes) {
jeffhao795d78f2011-09-30 18:34:35 -070026 Main.assertTrue(bytes[0] == 0);
27 Main.assertTrue(bytes[1] == -1);
28 Main.assertTrue(bytes[2] == -2);
29 Main.assertTrue(bytes[3] == -3);
30 Main.assertTrue(bytes[4] == -4);
jeffhao5d1ac922011-09-29 17:41:15 -070031 }
32 static void checkShorts(short[] shorts) {
jeffhao795d78f2011-09-30 18:34:35 -070033 Main.assertTrue(shorts[0] == 20);
34 Main.assertTrue(shorts[1] == 10);
35 Main.assertTrue(shorts[2] == 0);
36 Main.assertTrue(shorts[3] == -10);
37 Main.assertTrue(shorts[4] == -20);
jeffhao5d1ac922011-09-29 17:41:15 -070038 }
39 static void checkChars(char[] chars) {
jeffhao795d78f2011-09-30 18:34:35 -070040 Main.assertTrue(chars[0] == 40000);
41 Main.assertTrue(chars[1] == 40001);
42 Main.assertTrue(chars[2] == 40002);
43 Main.assertTrue(chars[3] == 40003);
44 Main.assertTrue(chars[4] == 40004);
jeffhao5d1ac922011-09-29 17:41:15 -070045 }
46 static void checkInts(int[] ints) {
jeffhao795d78f2011-09-30 18:34:35 -070047 Main.assertTrue(ints[0] == 70000);
48 Main.assertTrue(ints[1] == 70001);
49 Main.assertTrue(ints[2] == 70002);
50 Main.assertTrue(ints[3] == 70003);
51 Main.assertTrue(ints[4] == 70004);
jeffhao5d1ac922011-09-29 17:41:15 -070052 }
53 static void checkBooleans(boolean[] booleans) {
jeffhao795d78f2011-09-30 18:34:35 -070054 Main.assertTrue(booleans[0]);
55 Main.assertTrue(booleans[1]);
56 Main.assertTrue(!booleans[2]);
57 Main.assertTrue(booleans[3]);
58 Main.assertTrue(!booleans[4]);
jeffhao5d1ac922011-09-29 17:41:15 -070059 }
60 static void checkFloats(float[] floats) {
jeffhao795d78f2011-09-30 18:34:35 -070061 Main.assertTrue(floats[0] == -1.5);
62 Main.assertTrue(floats[1] == -0.5);
63 Main.assertTrue(floats[2] == 0.0);
64 Main.assertTrue(floats[3] == 0.5);
65 Main.assertTrue(floats[4] == 1.5);
jeffhao5d1ac922011-09-29 17:41:15 -070066 }
67 static void checkLongs(long[] longs) {
jeffhao795d78f2011-09-30 18:34:35 -070068 Main.assertTrue(longs[0] == 0x1122334455667788L);
69 Main.assertTrue(longs[1] == 0x8877665544332211L);
70 Main.assertTrue(longs[2] == 0L);
71 Main.assertTrue(longs[3] == 1L);
72 Main.assertTrue(longs[4] == -1L);
jeffhao5d1ac922011-09-29 17:41:15 -070073 }
74 static void checkStrings(String[] strings) {
jeffhao795d78f2011-09-30 18:34:35 -070075 Main.assertTrue(strings[0].equals("zero"));
76 Main.assertTrue(strings[1].equals("one"));
77 Main.assertTrue(strings[2].equals("two"));
78 Main.assertTrue(strings[3].equals("three"));
79 Main.assertTrue(strings[4].equals("four"));
jeffhao5d1ac922011-09-29 17:41:15 -070080 }
81
82 /*
83 * Try bad range values, 32 bit get/put.
84 */
85 static void checkRange32(int[] ints, int[] empty, int negVal1, int negVal2){
86 System.out.println("Array.checkRange32");
87 int i = 0;
88
jeffhao795d78f2011-09-30 18:34:35 -070089 Main.assertTrue(ints.length == 5);
jeffhao5d1ac922011-09-29 17:41:15 -070090
91 try {
92 i = ints[5]; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -070093 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070094 } catch (ArrayIndexOutOfBoundsException aioobe) {
95 // good
96 }
97 try {
98 ints[5] = i; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -070099 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700100 } catch (ArrayIndexOutOfBoundsException aioobe) {
101 // good
102 }
103 try {
104 i = ints[6]; // one past
jeffhao795d78f2011-09-30 18:34:35 -0700105 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700106 } catch (ArrayIndexOutOfBoundsException aioobe) {
107 // good
108 }
109 try {
110 i = ints[negVal1]; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700111 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700112 } catch (ArrayIndexOutOfBoundsException aioobe) {
113 // good
114 }
115 try {
116 ints[negVal1] = i; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700117 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700118 } catch (ArrayIndexOutOfBoundsException aioobe) {
119 // good
120 }
121 try {
122 i = ints[negVal2]; // min int
jeffhao795d78f2011-09-30 18:34:35 -0700123 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700124 } catch (ArrayIndexOutOfBoundsException aioobe) {
125 // good
126 }
127
128
129 try {
130 i = empty[1];
jeffhao795d78f2011-09-30 18:34:35 -0700131 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700132 } catch (ArrayIndexOutOfBoundsException aioobe) {
133 // good
134 }
135 }
136
137 /*
138 * Try bad range values, 64 bit get/put.
139 */
140 static void checkRange64(long[] longs, int negVal1, int negVal2) {
141 System.out.println("Array.checkRange64");
142 long l = 0L;
143
jeffhao795d78f2011-09-30 18:34:35 -0700144 Main.assertTrue(longs.length == 5);
jeffhao5d1ac922011-09-29 17:41:15 -0700145
146 try {
147 l = longs[5]; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -0700148 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700149 } catch (ArrayIndexOutOfBoundsException aioobe) {
150 // good
151 }
152 try {
153 longs[5] = l; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -0700154 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700155 } catch (ArrayIndexOutOfBoundsException aioobe) {
156 // good
157 }
158 try {
159 l = longs[6]; // one past
jeffhao795d78f2011-09-30 18:34:35 -0700160 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700161 } catch (ArrayIndexOutOfBoundsException aioobe) {
162 // good
163 }
164 try {
165 l = longs[negVal1]; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700166 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700167 } catch (ArrayIndexOutOfBoundsException aioobe) {
168 // good
169 }
170 try {
171 longs[negVal1] = l; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700172 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700173 } catch (ArrayIndexOutOfBoundsException aioobe) {
174 // good
175 }
176 try {
177 l = longs[negVal2]; // min int
jeffhao795d78f2011-09-30 18:34:35 -0700178 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700179 } catch (ArrayIndexOutOfBoundsException aioobe) {
180 // good
181 }
182 }
183
184 /*
185 * Test negative allocations of object and primitive arrays.
186 */
187 static void checkNegAlloc(int count) {
188 System.out.println("Array.checkNegAlloc");
189 String[] strings;
190 int[] ints;
191
192 try {
193 ints = new int[count];
jeffhao795d78f2011-09-30 18:34:35 -0700194 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700195 } catch (NegativeArraySizeException nase) {
196 // good
197 }
198
199 try {
200 strings = new String[count];
jeffhao795d78f2011-09-30 18:34:35 -0700201 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700202 } catch (NegativeArraySizeException nase) {
203 // good
204 }
205 }
206
207 public static void run() {
208 System.out.println("Array check...");
209
210 byte[] xBytes = new byte[] { 0, -1, -2, -3, -4 };
211 short[] xShorts = new short[] { 20, 10, 0, -10, -20 };
212 char[] xChars = new char[] { 40000, 40001, 40002, 40003, 40004 };
213 int[] xInts = new int[] { 70000, 70001, 70002, 70003, 70004 };
214 boolean[] xBooleans = new boolean[] { true, true, false, true, false };
215 float[] xFloats = new float[] { -1.5f, -0.5f, 0.0f, 0.5f, 1.5f };
216 long[] xLongs = new long[] {
217 0x1122334455667788L, 0x8877665544332211L, 0L, 1L, -1l };
218 String[] xStrings = new String[] {
219 "zero", "one", "two", "three", "four" };
220
221 int[] xEmpty = new int[0];
222
223 checkBytes(xBytes);
224 checkShorts(xShorts);
225 checkChars(xChars);
226 checkInts(xInts);
227 checkBooleans(xBooleans);
228 checkFloats(xFloats);
229 checkLongs(xLongs);
230 checkStrings(xStrings);
231
232 checkRange32(xInts, xEmpty, -1, (int) 0x80000000);
233 checkRange64(xLongs, -1, (int) 0x80000000);
234
235 checkNegAlloc(-1);
236 }
237}