Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame^] | 1 | /* |
| 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Exercise arrays. |
| 19 | */ |
| 20 | public class Array { |
| 21 | |
| 22 | /* |
| 23 | * Verify array contents. |
| 24 | */ |
| 25 | static void checkBytes(byte[] bytes) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 26 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 31 | } |
| 32 | static void checkShorts(short[] shorts) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 33 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 38 | } |
| 39 | static void checkChars(char[] chars) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 40 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 45 | } |
| 46 | static void checkInts(int[] ints) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 47 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 52 | } |
| 53 | static void checkBooleans(boolean[] booleans) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 54 | Main.assertTrue(booleans[0]); |
| 55 | Main.assertTrue(booleans[1]); |
| 56 | Main.assertTrue(!booleans[2]); |
| 57 | Main.assertTrue(booleans[3]); |
| 58 | Main.assertTrue(!booleans[4]); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 59 | } |
| 60 | static void checkFloats(float[] floats) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 61 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 66 | } |
| 67 | static void checkLongs(long[] longs) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 68 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 73 | } |
| 74 | static void checkStrings(String[] strings) { |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 75 | 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")); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 80 | } |
| 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 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 89 | Main.assertTrue(ints.length == 5); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 90 | |
| 91 | try { |
| 92 | i = ints[5]; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 93 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 94 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 95 | // good |
| 96 | } |
| 97 | try { |
| 98 | ints[5] = i; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 99 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 100 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 101 | // good |
| 102 | } |
| 103 | try { |
| 104 | i = ints[6]; // one past |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 105 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 106 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 107 | // good |
| 108 | } |
| 109 | try { |
| 110 | i = ints[negVal1]; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 111 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 112 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 113 | // good |
| 114 | } |
| 115 | try { |
| 116 | ints[negVal1] = i; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 117 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 118 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 119 | // good |
| 120 | } |
| 121 | try { |
| 122 | i = ints[negVal2]; // min int |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 123 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 124 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 125 | // good |
| 126 | } |
| 127 | |
| 128 | |
| 129 | try { |
| 130 | i = empty[1]; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 131 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 132 | } 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 | |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 144 | Main.assertTrue(longs.length == 5); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 145 | |
| 146 | try { |
| 147 | l = longs[5]; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 148 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 149 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 150 | // good |
| 151 | } |
| 152 | try { |
| 153 | longs[5] = l; // exact bound |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 154 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 155 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 156 | // good |
| 157 | } |
| 158 | try { |
| 159 | l = longs[6]; // one past |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 160 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 161 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 162 | // good |
| 163 | } |
| 164 | try { |
| 165 | l = longs[negVal1]; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 166 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 167 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 168 | // good |
| 169 | } |
| 170 | try { |
| 171 | longs[negVal1] = l; // -1 |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 172 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 173 | } catch (ArrayIndexOutOfBoundsException aioobe) { |
| 174 | // good |
| 175 | } |
| 176 | try { |
| 177 | l = longs[negVal2]; // min int |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 178 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 179 | } 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]; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 194 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 195 | } catch (NegativeArraySizeException nase) { |
| 196 | // good |
| 197 | } |
| 198 | |
| 199 | try { |
| 200 | strings = new String[count]; |
jeffhao | 795d78f | 2011-09-30 18:34:35 -0700 | [diff] [blame] | 201 | Main.assertTrue(false); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 202 | } 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 | } |