blob: 1daf3b9fd580725b0aa5da2710eb03c385b67923 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001// Copyright 2008 The Android Open Source Project
2
3
4/**
5 * Exercise arrays.
6 */
7public class Array {
8
9 /*
10 * Verify array contents.
11 */
12 static void checkBytes(byte[] bytes) {
jeffhao795d78f2011-09-30 18:34:35 -070013 Main.assertTrue(bytes[0] == 0);
14 Main.assertTrue(bytes[1] == -1);
15 Main.assertTrue(bytes[2] == -2);
16 Main.assertTrue(bytes[3] == -3);
17 Main.assertTrue(bytes[4] == -4);
jeffhao5d1ac922011-09-29 17:41:15 -070018 }
19 static void checkShorts(short[] shorts) {
jeffhao795d78f2011-09-30 18:34:35 -070020 Main.assertTrue(shorts[0] == 20);
21 Main.assertTrue(shorts[1] == 10);
22 Main.assertTrue(shorts[2] == 0);
23 Main.assertTrue(shorts[3] == -10);
24 Main.assertTrue(shorts[4] == -20);
jeffhao5d1ac922011-09-29 17:41:15 -070025 }
26 static void checkChars(char[] chars) {
jeffhao795d78f2011-09-30 18:34:35 -070027 Main.assertTrue(chars[0] == 40000);
28 Main.assertTrue(chars[1] == 40001);
29 Main.assertTrue(chars[2] == 40002);
30 Main.assertTrue(chars[3] == 40003);
31 Main.assertTrue(chars[4] == 40004);
jeffhao5d1ac922011-09-29 17:41:15 -070032 }
33 static void checkInts(int[] ints) {
jeffhao795d78f2011-09-30 18:34:35 -070034 Main.assertTrue(ints[0] == 70000);
35 Main.assertTrue(ints[1] == 70001);
36 Main.assertTrue(ints[2] == 70002);
37 Main.assertTrue(ints[3] == 70003);
38 Main.assertTrue(ints[4] == 70004);
jeffhao5d1ac922011-09-29 17:41:15 -070039 }
40 static void checkBooleans(boolean[] booleans) {
jeffhao795d78f2011-09-30 18:34:35 -070041 Main.assertTrue(booleans[0]);
42 Main.assertTrue(booleans[1]);
43 Main.assertTrue(!booleans[2]);
44 Main.assertTrue(booleans[3]);
45 Main.assertTrue(!booleans[4]);
jeffhao5d1ac922011-09-29 17:41:15 -070046 }
47 static void checkFloats(float[] floats) {
jeffhao795d78f2011-09-30 18:34:35 -070048 Main.assertTrue(floats[0] == -1.5);
49 Main.assertTrue(floats[1] == -0.5);
50 Main.assertTrue(floats[2] == 0.0);
51 Main.assertTrue(floats[3] == 0.5);
52 Main.assertTrue(floats[4] == 1.5);
jeffhao5d1ac922011-09-29 17:41:15 -070053 }
54 static void checkLongs(long[] longs) {
jeffhao795d78f2011-09-30 18:34:35 -070055 Main.assertTrue(longs[0] == 0x1122334455667788L);
56 Main.assertTrue(longs[1] == 0x8877665544332211L);
57 Main.assertTrue(longs[2] == 0L);
58 Main.assertTrue(longs[3] == 1L);
59 Main.assertTrue(longs[4] == -1L);
jeffhao5d1ac922011-09-29 17:41:15 -070060 }
61 static void checkStrings(String[] strings) {
jeffhao795d78f2011-09-30 18:34:35 -070062 Main.assertTrue(strings[0].equals("zero"));
63 Main.assertTrue(strings[1].equals("one"));
64 Main.assertTrue(strings[2].equals("two"));
65 Main.assertTrue(strings[3].equals("three"));
66 Main.assertTrue(strings[4].equals("four"));
jeffhao5d1ac922011-09-29 17:41:15 -070067 }
68
69 /*
70 * Try bad range values, 32 bit get/put.
71 */
72 static void checkRange32(int[] ints, int[] empty, int negVal1, int negVal2){
73 System.out.println("Array.checkRange32");
74 int i = 0;
75
jeffhao795d78f2011-09-30 18:34:35 -070076 Main.assertTrue(ints.length == 5);
jeffhao5d1ac922011-09-29 17:41:15 -070077
78 try {
79 i = ints[5]; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -070080 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070081 } catch (ArrayIndexOutOfBoundsException aioobe) {
82 // good
83 }
84 try {
85 ints[5] = i; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -070086 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070087 } catch (ArrayIndexOutOfBoundsException aioobe) {
88 // good
89 }
90 try {
91 i = ints[6]; // one past
jeffhao795d78f2011-09-30 18:34:35 -070092 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070093 } catch (ArrayIndexOutOfBoundsException aioobe) {
94 // good
95 }
96 try {
97 i = ints[negVal1]; // -1
jeffhao795d78f2011-09-30 18:34:35 -070098 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070099 } catch (ArrayIndexOutOfBoundsException aioobe) {
100 // good
101 }
102 try {
103 ints[negVal1] = i; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700104 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700105 } catch (ArrayIndexOutOfBoundsException aioobe) {
106 // good
107 }
108 try {
109 i = ints[negVal2]; // min int
jeffhao795d78f2011-09-30 18:34:35 -0700110 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700111 } catch (ArrayIndexOutOfBoundsException aioobe) {
112 // good
113 }
114
115
116 try {
117 i = empty[1];
jeffhao795d78f2011-09-30 18:34:35 -0700118 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700119 } catch (ArrayIndexOutOfBoundsException aioobe) {
120 // good
121 }
122 }
123
124 /*
125 * Try bad range values, 64 bit get/put.
126 */
127 static void checkRange64(long[] longs, int negVal1, int negVal2) {
128 System.out.println("Array.checkRange64");
129 long l = 0L;
130
jeffhao795d78f2011-09-30 18:34:35 -0700131 Main.assertTrue(longs.length == 5);
jeffhao5d1ac922011-09-29 17:41:15 -0700132
133 try {
134 l = longs[5]; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -0700135 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700136 } catch (ArrayIndexOutOfBoundsException aioobe) {
137 // good
138 }
139 try {
140 longs[5] = l; // exact bound
jeffhao795d78f2011-09-30 18:34:35 -0700141 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700142 } catch (ArrayIndexOutOfBoundsException aioobe) {
143 // good
144 }
145 try {
146 l = longs[6]; // one past
jeffhao795d78f2011-09-30 18:34:35 -0700147 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700148 } catch (ArrayIndexOutOfBoundsException aioobe) {
149 // good
150 }
151 try {
152 l = longs[negVal1]; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700153 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700154 } catch (ArrayIndexOutOfBoundsException aioobe) {
155 // good
156 }
157 try {
158 longs[negVal1] = l; // -1
jeffhao795d78f2011-09-30 18:34:35 -0700159 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700160 } catch (ArrayIndexOutOfBoundsException aioobe) {
161 // good
162 }
163 try {
164 l = longs[negVal2]; // min int
jeffhao795d78f2011-09-30 18:34:35 -0700165 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700166 } catch (ArrayIndexOutOfBoundsException aioobe) {
167 // good
168 }
169 }
170
171 /*
172 * Test negative allocations of object and primitive arrays.
173 */
174 static void checkNegAlloc(int count) {
175 System.out.println("Array.checkNegAlloc");
176 String[] strings;
177 int[] ints;
178
179 try {
180 ints = new int[count];
jeffhao795d78f2011-09-30 18:34:35 -0700181 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700182 } catch (NegativeArraySizeException nase) {
183 // good
184 }
185
186 try {
187 strings = new String[count];
jeffhao795d78f2011-09-30 18:34:35 -0700188 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700189 } catch (NegativeArraySizeException nase) {
190 // good
191 }
192 }
193
194 public static void run() {
195 System.out.println("Array check...");
196
197 byte[] xBytes = new byte[] { 0, -1, -2, -3, -4 };
198 short[] xShorts = new short[] { 20, 10, 0, -10, -20 };
199 char[] xChars = new char[] { 40000, 40001, 40002, 40003, 40004 };
200 int[] xInts = new int[] { 70000, 70001, 70002, 70003, 70004 };
201 boolean[] xBooleans = new boolean[] { true, true, false, true, false };
202 float[] xFloats = new float[] { -1.5f, -0.5f, 0.0f, 0.5f, 1.5f };
203 long[] xLongs = new long[] {
204 0x1122334455667788L, 0x8877665544332211L, 0L, 1L, -1l };
205 String[] xStrings = new String[] {
206 "zero", "one", "two", "three", "four" };
207
208 int[] xEmpty = new int[0];
209
210 checkBytes(xBytes);
211 checkShorts(xShorts);
212 checkChars(xChars);
213 checkInts(xInts);
214 checkBooleans(xBooleans);
215 checkFloats(xFloats);
216 checkLongs(xLongs);
217 checkStrings(xStrings);
218
219 checkRange32(xInts, xEmpty, -1, (int) 0x80000000);
220 checkRange64(xLongs, -1, (int) 0x80000000);
221
222 checkNegAlloc(-1);
223 }
224}