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 17package libcore.java.lang.reflect; 18 19import java.lang.reflect.Array; 20import junit.framework.TestCase; 21 22/** 23 * Test java.lang.reflect.Array methods. 24 */ 25public class OldAndroidArrayTest extends TestCase { 26 27 public void testSingleInt() throws Exception { 28 Object intArray = Array.newInstance(Integer.TYPE, 2); 29 30 int[] array = (int[]) intArray; 31 array[0] = 5; 32 Array.setInt(intArray, 1, 6); 33 34 assertEquals(5, Array.getInt(intArray, 0)); 35 assertEquals(6, array[1]); 36 37 try { 38 array[2] = 27; 39 fail("store should have failed"); 40 } catch (ArrayIndexOutOfBoundsException abe) { 41 // expected 42 } 43 44 assertEquals(2, array.length); 45 assertEquals(Array.getLength(intArray), array.length); 46 47 try { 48 int[][] wrongArray = (int[][]) intArray; 49 fail("cast should have failed"); 50 } catch (ClassCastException cce) { 51 // expected 52 } 53 54 intArray = Array.newInstance(Integer.TYPE, 0); 55 assertEquals(0, Array.getLength(intArray)); 56 } 57 58 public void testSingle() throws Exception { 59 Object strArray = Array.newInstance(String.class, 2); 60 61 String[] array = (String[]) strArray; 62 array[0] = "entry zero"; 63 Array.set(strArray, 1, "entry one"); 64 65 //System.out.println("array: " + array); 66 67 assertEquals("entry zero", Array.get(strArray, 0)); 68 assertEquals("entry one", array[1]); 69 70 assertEquals(2, array.length); 71 assertEquals(Array.getLength(strArray), array.length); 72 } 73 74 public void testMultiInt() throws Exception { 75 int[] dimensions = {3, 2, 1}; 76 Object intIntIntArray = Array.newInstance(Integer.TYPE, dimensions); 77 int[][][] array3 = (int[][][]) intIntIntArray; 78 79 array3[0][0][0] = 123; 80 array3[2][1][0] = 456; 81 82 try { 83 array3[2][1][1] = 768; 84 fail("store should have failed"); 85 } catch (ArrayIndexOutOfBoundsException abe) { 86 // expected 87 } 88 89 //System.out.println("array3: " + array3); 90 } 91 92 public void testMulti() throws Exception { 93 int[] dimensions = {1, 2, 3}; 94 Object strStrStrArray = Array.newInstance(String.class, dimensions); 95 String[][][] array3 = (String[][][]) strStrStrArray; 96 97 array3[0][0][0] = "zero zero zero"; 98 array3[0][1][2] = "zero one two"; 99 100 try { 101 array3[1][0][0] = "bad store"; 102 fail("store should have failed"); 103 } catch (ArrayIndexOutOfBoundsException abe) { 104 // expected 105 } 106 107 try { 108 String[][] array2 = (String[][]) strStrStrArray; 109 fail("expecting bad cast"); 110 } catch (ClassCastException cce) { 111 // expected 112 } 113 //System.out.println("array3: " + array3); 114 115 116 int[] dimensions2 = {1, 2}; 117 strStrStrArray = Array.newInstance(String[].class, dimensions2); 118 array3 = (String[][][]) strStrStrArray; 119 array3[0][1] = new String[3]; 120 array3[0][1][2] = "zero one two"; 121 try { 122 array3[1][0][0] = "bad store"; 123 fail("store should have failed"); 124 } catch (ArrayIndexOutOfBoundsException abe) { 125 // expected 126 } 127 //System.out.println("array3: " + array3); 128 } 129} 130 131