2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import junit.framework.TestCase;
21 import java.lang.reflect.Array;
22 import android.test.suitebuilder.annotation.SmallTest;
25 * Test java.lang.reflect.Array methods.
27 public class ReflectArrayTest extends TestCase {
30 public void testSingleInt() throws Exception {
31 Object intArray = Array.newInstance(Integer.TYPE, 2);
33 int[] array = (int[]) intArray;
35 Array.setInt(intArray, 1, 6);
37 assertEquals(5, Array.getInt(intArray, 0));
38 assertEquals(6, array[1]);
42 fail("store should have failed");
43 } catch (ArrayIndexOutOfBoundsException abe) {
47 assertEquals(2, array.length);
48 assertEquals(Array.getLength(intArray), array.length);
51 int[][] wrongArray = (int[][]) intArray;
52 fail("cast should have failed");
53 } catch (ClassCastException cce) {
57 intArray = Array.newInstance(Integer.TYPE, 0);
58 assertEquals(0, Array.getLength(intArray));
62 public void testSingle() throws Exception {
63 Object strArray = Array.newInstance(String.class, 2);
65 String[] array = (String[]) strArray;
66 array[0] = "entry zero";
67 Array.set(strArray, 1, "entry one");
69 //System.out.println("array: " + array);
71 assertEquals("entry zero", Array.get(strArray, 0));
72 assertEquals("entry one", array[1]);
74 assertEquals(2, array.length);
75 assertEquals(Array.getLength(strArray), array.length);
79 public void testMultiInt() throws Exception {
80 int[] dimensions = {3, 2, 1};
81 Object intIntIntArray = Array.newInstance(Integer.TYPE, dimensions);
82 int[][][] array3 = (int[][][]) intIntIntArray;
84 array3[0][0][0] = 123;
85 array3[2][1][0] = 456;
88 array3[2][1][1] = 768;
89 fail("store should have failed");
90 } catch (ArrayIndexOutOfBoundsException abe) {
94 //System.out.println("array3: " + array3);
98 public void testMulti() throws Exception {
99 int[] dimensions = {1, 2, 3};
100 Object strStrStrArray = Array.newInstance(String.class, dimensions);
101 String[][][] array3 = (String[][][]) strStrStrArray;
103 array3[0][0][0] = "zero zero zero";
104 array3[0][1][2] = "zero one two";
107 array3[1][0][0] = "bad store";
108 fail("store should have failed");
109 } catch (ArrayIndexOutOfBoundsException abe) {
114 String[][] array2 = (String[][]) strStrStrArray;
115 fail("expecting bad cast");
116 } catch (ClassCastException cce) {
119 //System.out.println("array3: " + array3);
122 int[] dimensions2 = {1, 2};
123 strStrStrArray = Array.newInstance(String[].class, dimensions2);
124 array3 = (String[][][]) strStrStrArray;
125 array3[0][1] = new String[3];
126 array3[0][1][2] = "zero one two";
128 array3[1][0][0] = "bad store";
129 fail("store should have failed");
130 } catch (ArrayIndexOutOfBoundsException abe) {
133 //System.out.println("array3: " + array3);