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 */ 16 17 /** 18 * Exercise arrays. 19 */ 20 public class Array { 21 22 /* 23 * Verify array contents. 24 */ 25 static void checkBytes(byte[] bytes) { 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); 31 } 32 static void checkShorts(short[] shorts) { 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); 38 } 39 static void checkChars(char[] chars) { 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); 45 } 46 static void checkInts(int[] ints) { 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); 52 } 53 static void checkBooleans(boolean[] booleans) { 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]); 59 } 60 static void checkFloats(float[] floats) { 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); 66 } 67 static void checkLongs(long[] longs) { 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); 73 } 74 static void checkStrings(String[] strings) { 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")); 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 89 Main.assertTrue(ints.length == 5); 90 91 try { 92 i = ints[5]; // exact bound 93 Main.assertTrue(false); 94 } catch (ArrayIndexOutOfBoundsException aioobe) { 95 // good 96 } 97 try { 98 ints[5] = i; // exact bound 99 Main.assertTrue(false); 100 } catch (ArrayIndexOutOfBoundsException aioobe) { 101 // good 102 } 103 try { 104 i = ints[6]; // one past 105 Main.assertTrue(false); 106 } catch (ArrayIndexOutOfBoundsException aioobe) { 107 // good 108 } 109 try { 110 i = ints[negVal1]; // -1 111 Main.assertTrue(false); 112 } catch (ArrayIndexOutOfBoundsException aioobe) { 113 // good 114 } 115 try { 116 ints[negVal1] = i; // -1 117 Main.assertTrue(false); 118 } catch (ArrayIndexOutOfBoundsException aioobe) { 119 // good 120 } 121 try { 122 i = ints[negVal2]; // min int 123 Main.assertTrue(false); 124 } catch (ArrayIndexOutOfBoundsException aioobe) { 125 // good 126 } 127 128 129 try { 130 i = empty[1]; 131 Main.assertTrue(false); 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 144 Main.assertTrue(longs.length == 5); 145 146 try { 147 l = longs[5]; // exact bound 148 Main.assertTrue(false); 149 } catch (ArrayIndexOutOfBoundsException aioobe) { 150 // good 151 } 152 try { 153 longs[5] = l; // exact bound 154 Main.assertTrue(false); 155 } catch (ArrayIndexOutOfBoundsException aioobe) { 156 // good 157 } 158 try { 159 l = longs[6]; // one past 160 Main.assertTrue(false); 161 } catch (ArrayIndexOutOfBoundsException aioobe) { 162 // good 163 } 164 try { 165 l = longs[negVal1]; // -1 166 Main.assertTrue(false); 167 } catch (ArrayIndexOutOfBoundsException aioobe) { 168 // good 169 } 170 try { 171 longs[negVal1] = l; // -1 172 Main.assertTrue(false); 173 } catch (ArrayIndexOutOfBoundsException aioobe) { 174 // good 175 } 176 try { 177 l = longs[negVal2]; // min int 178 Main.assertTrue(false); 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]; 194 Main.assertTrue(false); 195 } catch (NegativeArraySizeException nase) { 196 // good 197 } 198 199 try { 200 strings = new String[count]; 201 Main.assertTrue(false); 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 } 238