1/* 2 * Copyright (C) 2012 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 17package libcore.java.lang.reflect; 18 19import java.lang.reflect.Array; 20import junit.framework.TestCase; 21 22public class ArrayTest extends TestCase { 23 private static boolean[] booleans; 24 private static byte[] bytes; 25 private static char[] chars; 26 private static double[] doubles; 27 private static float[] floats; 28 private static int[] ints; 29 private static long[] longs; 30 private static short[] shorts; 31 32 @Override protected void setUp() throws Exception { 33 super.setUp(); 34 booleans = new boolean[] { true }; 35 bytes = new byte[] { (byte) 0xff }; 36 chars = new char[] { '\uffff' }; 37 doubles = new double[] { (double) 0xffffffffffffffffL }; 38 floats = new float[] { (float) 0xffffffff }; 39 ints = new int[] { 0xffffffff }; 40 longs = new long[] { 0xffffffffffffffffL }; 41 shorts = new short[] { (short) 0xffff }; 42 } 43 44 public void testGetBoolean() throws Exception { 45 assertEquals(booleans[0], Array.getBoolean(booleans, 0)); 46 try { Array.getBoolean(bytes, 0); fail(); } catch (IllegalArgumentException expected) {} 47 try { Array.getBoolean(chars, 0); fail(); } catch (IllegalArgumentException expected) {} 48 try { Array.getBoolean(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 49 try { Array.getBoolean(floats, 0); fail(); } catch (IllegalArgumentException expected) {} 50 try { Array.getBoolean(ints, 0); fail(); } catch (IllegalArgumentException expected) {} 51 try { Array.getBoolean(longs, 0); fail(); } catch (IllegalArgumentException expected) {} 52 try { Array.getBoolean(shorts, 0); fail(); } catch (IllegalArgumentException expected) {} 53 try { Array.getBoolean(null, 0); fail(); } catch (NullPointerException expected) {} 54 } 55 56 public void testGetByte() throws Exception { 57 try { Array.getByte(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 58 assertEquals(bytes[0], Array.getByte(bytes, 0)); 59 try { Array.getByte(chars, 0); fail(); } catch (IllegalArgumentException expected) {} 60 try { Array.getByte(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 61 try { Array.getByte(floats, 0); fail(); } catch (IllegalArgumentException expected) {} 62 try { Array.getByte(ints, 0); fail(); } catch (IllegalArgumentException expected) {} 63 try { Array.getByte(longs, 0); fail(); } catch (IllegalArgumentException expected) {} 64 try { Array.getByte(shorts, 0); fail(); } catch (IllegalArgumentException expected) {} 65 try { Array.getByte(null, 0); fail(); } catch (NullPointerException expected) {} 66 } 67 68 public void testGetChar() throws Exception { 69 try { Array.getChar(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 70 try { Array.getChar(bytes, 0); fail(); } catch (IllegalArgumentException expected) {} 71 assertEquals(chars[0], Array.getChar(chars, 0)); 72 try { Array.getChar(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 73 try { Array.getChar(floats, 0); fail(); } catch (IllegalArgumentException expected) {} 74 try { Array.getChar(ints, 0); fail(); } catch (IllegalArgumentException expected) {} 75 try { Array.getChar(longs, 0); fail(); } catch (IllegalArgumentException expected) {} 76 try { Array.getChar(shorts, 0); fail(); } catch (IllegalArgumentException expected) {} 77 try { Array.getChar(null, 0); fail(); } catch (NullPointerException expected) {} 78 } 79 80 public void testGetDouble() throws Exception { 81 try { Array.getDouble(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 82 assertEquals((double) bytes[0], Array.getDouble(bytes, 0)); 83 assertEquals((double) chars[0], Array.getDouble(chars, 0)); 84 assertEquals(doubles[0], Array.getDouble(doubles, 0)); 85 assertEquals((double) floats[0], Array.getDouble(floats, 0)); 86 assertEquals((double) ints[0], Array.getDouble(ints, 0)); 87 assertEquals((double) longs[0], Array.getDouble(longs, 0)); 88 assertEquals((double) shorts[0], Array.getDouble(shorts, 0)); 89 try { Array.getDouble(null, 0); fail(); } catch (NullPointerException expected) {} 90 } 91 92 public void testGetFloat() throws Exception { 93 try { Array.getFloat(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 94 assertEquals((float) bytes[0], Array.getFloat(bytes, 0)); 95 assertEquals((float) chars[0], Array.getFloat(chars, 0)); 96 assertEquals(floats[0], Array.getFloat(floats, 0)); 97 try { Array.getFloat(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 98 assertEquals((float) ints[0], Array.getFloat(ints, 0)); 99 assertEquals((float) longs[0], Array.getFloat(longs, 0)); 100 assertEquals((float) shorts[0], Array.getFloat(shorts, 0)); 101 try { Array.getFloat(null, 0); fail(); } catch (NullPointerException expected) {} 102 } 103 104 public void testGetInt() throws Exception { 105 try { Array.getInt(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 106 assertEquals((int) bytes[0], Array.getInt(bytes, 0)); 107 assertEquals((int) chars[0], Array.getInt(chars, 0)); 108 try { Array.getInt(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 109 try { Array.getInt(floats, 0); fail(); } catch (IllegalArgumentException expected) {} 110 assertEquals(ints[0], Array.getInt(ints, 0)); 111 try { Array.getInt(longs, 0); fail(); } catch (IllegalArgumentException expected) {} 112 assertEquals((int) shorts[0], Array.getInt(shorts, 0)); 113 try { Array.getInt(null, 0); fail(); } catch (NullPointerException expected) {} 114 } 115 116 public void testGetLong() throws Exception { 117 try { Array.getLong(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 118 assertEquals((long) bytes[0], Array.getLong(bytes, 0)); 119 assertEquals((long) chars[0], Array.getLong(chars, 0)); 120 try { Array.getLong(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 121 try { Array.getLong(floats, 0); fail(); } catch (IllegalArgumentException expected) {} 122 assertEquals((long) ints[0], Array.getLong(ints, 0)); 123 assertEquals(longs[0], Array.getLong(longs, 0)); 124 assertEquals((long) shorts[0], Array.getLong(shorts, 0)); 125 try { Array.getLong(null, 0); fail(); } catch (NullPointerException expected) {} 126 } 127 128 public void testGetShort() throws Exception { 129 try { Array.getShort(booleans, 0); fail(); } catch (IllegalArgumentException expected) {} 130 assertEquals((int) bytes[0], Array.getShort(bytes, 0)); 131 try { Array.getShort(chars, 0); fail(); } catch (IllegalArgumentException expected) {} 132 try { Array.getShort(doubles, 0); fail(); } catch (IllegalArgumentException expected) {} 133 try { Array.getShort(floats, 0); fail(); } catch (IllegalArgumentException expected) {} 134 try { Array.getShort(ints, 0); fail(); } catch (IllegalArgumentException expected) {} 135 try { Array.getShort(longs, 0); fail(); } catch (IllegalArgumentException expected) {} 136 assertEquals(shorts[0], Array.getShort(shorts, 0)); 137 try { Array.getShort(null, 0); fail(); } catch (NullPointerException expected) {} 138 } 139 140 public void testSetBoolean() throws Exception { 141 Array.setBoolean(booleans, 0, booleans[0]); 142 try { Array.setBoolean(bytes, 0, true); fail(); } catch (IllegalArgumentException expected) {} 143 try { Array.setBoolean(chars, 0, true); fail(); } catch (IllegalArgumentException expected) {} 144 try { Array.setBoolean(doubles, 0, true); fail(); } catch (IllegalArgumentException expected) {} 145 try { Array.setBoolean(floats, 0, true); fail(); } catch (IllegalArgumentException expected) {} 146 try { Array.setBoolean(ints, 0, true); fail(); } catch (IllegalArgumentException expected) {} 147 try { Array.setBoolean(longs, 0, true); fail(); } catch (IllegalArgumentException expected) {} 148 try { Array.setBoolean(shorts, 0, true); fail(); } catch (IllegalArgumentException expected) {} 149 try { Array.setBoolean(null, 0, true); fail(); } catch (NullPointerException expected) {} 150 } 151 152 public void testSetByte() throws Exception { 153 try { Array.setByte(booleans, 0, bytes[0]); fail(); } catch (IllegalArgumentException expected) {} 154 Array.setByte(bytes, 0, bytes[0]); 155 try { Array.setByte(chars, 0, bytes[0]); fail(); } catch (IllegalArgumentException expected) {} 156 Array.setByte(doubles, 0, bytes[0]); 157 Array.setByte(floats, 0, bytes[0]); 158 Array.setByte(ints, 0, bytes[0]); 159 Array.setByte(longs, 0, bytes[0]); 160 Array.setByte(shorts, 0, bytes[0]); 161 try { Array.setByte(null, 0, bytes[0]); fail(); } catch (NullPointerException expected) {} 162 } 163 164 public void testSetChar() throws Exception { 165 try { Array.setChar(booleans, 0, chars[0]); fail(); } catch (IllegalArgumentException expected) {} 166 try { Array.setChar(bytes, 0, chars[0]); fail(); } catch (IllegalArgumentException expected) {} 167 Array.setChar(chars, 0, chars[0]); 168 Array.setChar(doubles, 0, chars[0]); 169 Array.setChar(floats, 0, chars[0]); 170 Array.setChar(ints, 0, chars[0]); 171 Array.setChar(longs, 0, chars[0]); 172 try { Array.setChar(shorts, 0, chars[0]); fail(); } catch (IllegalArgumentException expected) {} 173 try { Array.setChar(null, 0, chars[0]); fail(); } catch (NullPointerException expected) {} 174 } 175 176 public void testSetDouble() throws Exception { 177 try { Array.setDouble(booleans, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 178 try { Array.setDouble(bytes, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 179 try { Array.setDouble(chars, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 180 Array.setDouble(doubles, 0, doubles[0]); 181 try { Array.setDouble(floats, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 182 try { Array.setDouble(ints, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 183 try { Array.setDouble(longs, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 184 try { Array.setDouble(shorts, 0, doubles[0]); fail(); } catch (IllegalArgumentException expected) {} 185 try { Array.setDouble(null, 0, doubles[0]); fail(); } catch (NullPointerException expected) {} 186 } 187 188 public void testSetFloat() throws Exception { 189 try { Array.setFloat(booleans, 0, floats[0]); fail(); } catch (IllegalArgumentException expected) {} 190 try { Array.setFloat(bytes, 0, floats[0]); fail(); } catch (IllegalArgumentException expected) {} 191 try { Array.setFloat(chars, 0, floats[0]); fail(); } catch (IllegalArgumentException expected) {} 192 Array.setFloat(floats, 0, floats[0]); 193 Array.setFloat(doubles, 0, floats[0]); 194 try { Array.setFloat(ints, 0, floats[0]); fail(); } catch (IllegalArgumentException expected) {} 195 try { Array.setFloat(longs, 0, floats[0]); fail(); } catch (IllegalArgumentException expected) {} 196 try { Array.setFloat(shorts, 0, floats[0]); fail(); } catch (IllegalArgumentException expected) {} 197 try { Array.setFloat(null, 0, floats[0]); fail(); } catch (NullPointerException expected) {} 198 } 199 200 public void testSetInt() throws Exception { 201 try { Array.setInt(booleans, 0, ints[0]); fail(); } catch (IllegalArgumentException expected) {} 202 try { Array.setInt(bytes, 0, ints[0]); fail(); } catch (IllegalArgumentException expected) {} 203 try { Array.setInt(chars, 0, ints[0]); fail(); } catch (IllegalArgumentException expected) {} 204 Array.setInt(doubles, 0, ints[0]); 205 Array.setInt(floats, 0, ints[0]); 206 Array.setInt(ints, 0, ints[0]); 207 Array.setInt(longs, 0, ints[0]); 208 try { Array.setInt(shorts, 0, ints[0]); fail(); } catch (IllegalArgumentException expected) {} 209 try { Array.setInt(null, 0, ints[0]); fail(); } catch (NullPointerException expected) {} 210 } 211 212 public void testSetLong() throws Exception { 213 try { Array.setLong(booleans, 0, longs[0]); fail(); } catch (IllegalArgumentException expected) {} 214 try { Array.setLong(bytes, 0, longs[0]); fail(); } catch (IllegalArgumentException expected) {} 215 try { Array.setLong(chars, 0, longs[0]); fail(); } catch (IllegalArgumentException expected) {} 216 Array.setLong(doubles, 0, longs[0]); 217 Array.setLong(floats, 0, longs[0]); 218 try { Array.setLong(ints, 0, longs[0]); fail(); } catch (IllegalArgumentException expected) {} 219 Array.setLong(longs, 0, longs[0]); 220 try { Array.setLong(shorts, 0, longs[0]); fail(); } catch (IllegalArgumentException expected) {} 221 try { Array.setLong(null, 0, longs[0]); fail(); } catch (NullPointerException expected) {} 222 } 223 224 public void testSetShort() throws Exception { 225 try { Array.setShort(booleans, 0, shorts[0]); fail(); } catch (IllegalArgumentException expected) {} 226 try { Array.setShort(bytes, 0, shorts[0]); fail(); } catch (IllegalArgumentException expected) {} 227 try { Array.setShort(chars, 0, shorts[0]); fail(); } catch (IllegalArgumentException expected) {} 228 Array.setShort(doubles, 0, shorts[0]); 229 Array.setShort(floats, 0, shorts[0]); 230 Array.setShort(ints, 0, shorts[0]); 231 Array.setShort(longs, 0, shorts[0]); 232 Array.setShort(shorts, 0, shorts[0]); 233 try { Array.setShort(null, 0, shorts[0]); fail(); } catch (NullPointerException expected) {} 234 } 235} 236