• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  */
4 
5 import java.lang.reflect.Array;
6 
7 /**
8  * Test java.lang.reflect.Array.
9  */
10 public class Main {
main(String[] args)11     public static void main(String[] args) {
12         testSingleInt();
13         testSingleChar();
14         testSingleShort();
15         testSingleLong();
16         testSingle();
17         testMultiInt();
18         testMulti();
19 
20         System.out.println("ReflectArrayTest passed");
21     }
22 
testSingleInt()23     static void testSingleInt() {
24         Object intArray;
25 
26         intArray = Array.newInstance(Integer.TYPE, 2);
27 
28         int[] array = (int[]) intArray;
29         array[0] = 5;
30         Array.setInt(intArray, 1, 6);
31 
32         if (Array.getInt(intArray, 0) != 5)
33             throw new RuntimeException();
34         if (array[1] != 6)
35             throw new RuntimeException();
36         try {
37             array[2] = 27;
38             throw new RuntimeException("store should have failed");
39         } catch (ArrayIndexOutOfBoundsException abe) { }
40         try {
41             Array.setInt(intArray, 2, 27);
42             throw new RuntimeException("store should have failed");
43         } catch (ArrayIndexOutOfBoundsException abe) { }
44         if (array.length != Array.getLength(intArray) ||
45             array.length != 2)
46         {
47             throw new RuntimeException("bad len");
48         }
49 
50         Integer x123 = Integer.valueOf(123);
51         Integer x456 = Integer.valueOf(456);
52 
53         Array.set(intArray, 0, x123);
54         Array.set(intArray, 1, x456);
55         if (!Array.get(intArray, 0).equals(x123) || !Array.get(intArray, 1).equals(x456)) {
56             throw new RuntimeException("bad 123 or 456");
57         }
58 
59         int[][] wrongArray;
60         try {
61             wrongArray = (int[][]) intArray;
62             throw new RuntimeException("cast should have failed");
63         } catch (ClassCastException cce) { }
64 
65         intArray = Array.newInstance(Integer.TYPE, 0);
66         if (Array.getLength(intArray) != 0)
67             throw new RuntimeException();
68         System.out.println("ReflectArrayTest.testSingleInt passed");
69     }
70 
testSingleChar()71     static void testSingleChar() {
72         Object charArray = Array.newInstance(Character.TYPE, 7);
73 
74         char[] array = (char[]) charArray;
75         array[0] = '0';
76         array[1] = 'W';
77         array[2] = '2';
78         array[3] = '3';
79         array[4] = 'X';
80         array[5] = '5';
81         array[6] = '6';
82         Array.setChar(charArray, 1, '1');
83         Array.setChar(charArray, 4, '4');
84         try {
85             Array.setShort(charArray, 3, (short) 'Y');
86             throw new RuntimeException("shouldn't allow short in char array");
87         } catch (IllegalArgumentException iae) {}
88         try {
89             Array.setInt(charArray, 5, 'Z');
90             throw new RuntimeException("shouldn't allow int in char array");
91         } catch (IllegalArgumentException iae) {}
92 
93         try {
94             for (int i = 0; i < array.length; i++) {
95                 if (Array.getInt(charArray, i) - '0' != i) {
96                     throw new RuntimeException("mismatch: " + i + " is " + array[i]);
97                 }
98             }
99 
100             if (Array.getInt(charArray, 4) != '4') {
101                 throw new RuntimeException("load should have worked");
102             }
103         } catch (IllegalArgumentException iae) {
104             System.err.println("Couldn't Array.getInt(charArray)");
105         }
106         try {
107             Array.getByte(charArray, 2);
108             throw new RuntimeException("shouldn't allow read of char as byte");
109         } catch (IllegalArgumentException iae) {}
110 
111         Array.setChar(charArray, 3, (char) 0xffff);
112         try {
113             if (Array.getInt(charArray, 3) != 0xffff) {
114                 throw new RuntimeException("char got sign-extended? "
115                     + Array.getInt(charArray, 3));
116             }
117         } catch (IllegalArgumentException iae) {
118             System.err.println("Couldn't Array.getInt(charArray)");
119         }
120 
121         System.out.println("ReflectArrayTest.testSingleChar passed");
122     }
123 
testSingleShort()124     static void testSingleShort() {
125         Object shortArray = Array.newInstance(Short.TYPE, 1);
126         Array.setShort(shortArray, 0, (short) -1);
127         if (Array.getInt(shortArray, 0) != -1) {
128             throw new RuntimeException("short didn't get sign-extended");
129         }
130 
131         Short box = (Short) Array.get(shortArray, 0);
132 
133         System.out.println("ReflectArrayTest.testSingleShort passed");
134     }
135 
testSingleLong()136     static void testSingleLong() {
137         Object longArray = Array.newInstance(Long.TYPE, 2);
138         Array.setInt(longArray, 0, 123);
139         Array.setLong(longArray, 1, 0x1122334455667788L);
140         try {
141             Array.getInt(longArray, 0);
142             throw new RuntimeException("shouldn't allow read of long as int");
143         } catch (IllegalArgumentException iae) {}
144 
145         long[] array = (long[]) longArray;
146         if (array[0] != 123 || array[1] != 0x1122334455667788L) {
147             throw new RuntimeException();
148         }
149 
150         float f = Array.getFloat(longArray, 0);
151         if (f < 122.9 || f > 123.1) {
152             throw new RuntimeException("long-as-float failed - " + f);
153         }
154         if (Array.getLong(longArray, 1) != 0x1122334455667788L) {
155             throw new RuntimeException("long1 failed");
156         }
157 
158         System.out.println("ReflectArrayTest.testSingleLong passed");
159     }
160 
testSingle()161     static void testSingle() {
162         Object strArray;
163 
164         strArray = Array.newInstance(String.class, 2);
165 
166         String[] array = (String[]) strArray;
167         array[0] = "entry zero";
168         Array.set(strArray, 1, "entry one");
169         try {
170             Array.set(strArray, 2, "entry two");
171             throw new RuntimeException("store should have failed");
172         } catch (ArrayIndexOutOfBoundsException abe) { }
173 
174         //System.out.println("array: " + array);
175 
176         if (!"entry zero".equals(Array.get(strArray, 0)))
177             throw new RuntimeException();
178         if (!"entry one".equals(array[1]))
179             throw new RuntimeException();
180 
181         if (array.length != Array.getLength(strArray) ||
182             array.length != 2)
183         {
184             throw new RuntimeException("bad len");
185         }
186 
187         try {
188             Array.set(strArray, 0, new Integer(5));
189             throw new RuntimeException("store of Integer should have failed");
190         } catch (IllegalArgumentException iae) {}
191         System.out.println("ReflectArrayTest.testSingle passed");
192     }
193 
testMultiInt()194     static void testMultiInt() {
195         Object intIntIntArray;
196         int[] dimensions = { 3, 2, 1 };
197 
198         intIntIntArray = Array.newInstance(Integer.TYPE, dimensions);
199         int[][][] array3 = (int[][][]) intIntIntArray;
200 
201         array3[0][0][0] = 123;      // trouble
202         array3[2][1][0] = 456;
203 
204         try {
205             array3[2][1][1] = 768;
206             throw new RuntimeException("store should have failed");
207         }
208         catch (ArrayIndexOutOfBoundsException abe) {
209         }
210         System.out.println("ReflectArrayTest.testMultiInt passed");
211     }
212 
testMulti()213     static void testMulti() {
214         Object strStrStrArray;
215         int[] dimensions = { 1, 2, 3 };
216 
217         strStrStrArray = Array.newInstance(String.class, dimensions);
218         String[][][] array3 = (String[][][]) strStrStrArray;
219 
220         array3[0][0][0] = "zero zero zero";
221         array3[0][1][2] = "zero one two";
222 
223         try {
224             array3[1][0][0] = "bad store";
225             throw new RuntimeException("store should have failed");
226         }
227         catch (ArrayIndexOutOfBoundsException abe) {
228         }
229 
230         try {
231             String[][] array2 = (String[][]) strStrStrArray;
232             throw new RuntimeException("expecting bad cast");
233         }
234         catch (ClassCastException cce) {
235         }
236 
237         String[] strar = new String[4];
238         strar[2] = "zero one two ++";
239         array3[0][1] = strar;
240         System.out.println(array3[0][1][2]);
241         //System.out.println("array3: " + array3);
242 
243 
244         int[] dimensions2 = { 1, 2 };
245         strStrStrArray = Array.newInstance(String[].class, dimensions2);
246         array3 = (String[][][]) strStrStrArray;
247 
248         array3[0][1] = new String[3];
249         array3[0][1][2] = "zero one two";
250         try {
251             array3[1][0][0] = "bad store";
252             throw new RuntimeException("store should have failed");
253         }
254         catch (ArrayIndexOutOfBoundsException abe) {
255         }
256         System.out.println("ReflectArrayTest.testMulti passed");
257     }
258 }
259