jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | // Copyright 2008 The Android Open Source Project |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Exercise arrays. |
| 6 | */ |
| 7 | public class Array { |
| 8 | |
| 9 | /* |
| 10 | * Verify array contents. |
| 11 | */ |
| 12 | static void checkBytes(byte[] bytes) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 13 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 18 | } |
| 19 | static void checkShorts(short[] shorts) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 20 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 25 | } |
| 26 | static void checkChars(char[] chars) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 27 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 32 | } |
| 33 | static void checkInts(int[] ints) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 34 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 39 | } |
| 40 | static void checkBooleans(boolean[] booleans) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 41 | Main.assertTrue(booleans[0]); |
| 42 | Main.assertTrue(booleans[1]); |
| 43 | Main.assertTrue(!booleans[2]); |
| 44 | Main.assertTrue(booleans[3]); |
| 45 | Main.assertTrue(!booleans[4]); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 46 | } |
| 47 | static void checkFloats(float[] floats) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 48 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 53 | } |
| 54 | static void checkLongs(long[] longs) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 55 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 60 | } |
| 61 | static void checkStrings(String[] strings) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 62 | 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")); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 67 | } |
| 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 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 76 | Main.assertTrue(ints.length == 5); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 77 | |
| 78 | try { |
| 79 | i = ints[5]; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 80 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 81 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 82 | // good |
| 83 | } |
| 84 | try { |
| 85 | ints[5] = i; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 86 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 87 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 88 | // good |
| 89 | } |
| 90 | try { |
| 91 | i = ints[6]; // one past |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 92 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 93 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 94 | // good |
| 95 | } |
| 96 | try { |
| 97 | i = ints[negVal1]; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 98 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 99 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 100 | // good |
| 101 | } |
| 102 | try { |
| 103 | ints[negVal1] = i; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 104 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 105 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 106 | // good |
| 107 | } |
| 108 | try { |
| 109 | i = ints[negVal2]; // min int |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 110 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 111 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 112 | // good |
| 113 | } |
| 114 | |
| 115 | |
| 116 | try { |
| 117 | i = empty[1]; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 118 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 119 | } 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 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 131 | Main.assertTrue(longs.length == 5); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 132 | |
| 133 | try { |
| 134 | l = longs[5]; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 135 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 136 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 137 | // good |
| 138 | } |
| 139 | try { |
| 140 | longs[5] = l; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 141 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 142 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 143 | // good |
| 144 | } |
| 145 | try { |
| 146 | l = longs[6]; // one past |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 147 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 148 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 149 | // good |
| 150 | } |
| 151 | try { |
| 152 | l = longs[negVal1]; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 153 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 154 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 155 | // good |
| 156 | } |
| 157 | try { |
| 158 | longs[negVal1] = l; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 159 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 160 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 161 | // good |
| 162 | } |
| 163 | try { |
| 164 | l = longs[negVal2]; // min int |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 165 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 166 | } 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]; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 181 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 182 | } catch (NegativeArraySizeException nase) { |
| 183 | // good |
| 184 | } |
| 185 | |
| 186 | try { |
| 187 | strings = new String[count]; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 188 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 189 | } 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 | } |