blob: d7f937c8671c6e091446dd0fd116f865802e4c0c [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001// Copyright 2008 The Android Open Source Project
2
3import java.io.Serializable;
4import java.util.Arrays;
5
6/**
7 * Exercise some class-related instructions.
8 */
9public class Classes {
10 int mSome;
11
12 public void subFunc(boolean wantSub) {
jeffhao795d78f2011-09-30 18:34:35 -070013 Main.assertTrue(!wantSub);
jeffhao5d1ac922011-09-29 17:41:15 -070014 }
15
16 void checkCast(Object thisRef, Object moreRef, Object nullRef) {
17 System.out.println("Classes.checkCast");
18
19 Classes classes;
20 MoreClasses more;
21
22 classes = (Classes) thisRef;
jeffhao795d78f2011-09-30 18:34:35 -070023 Main.assertTrue(thisRef instanceof Classes);
jeffhao5d1ac922011-09-29 17:41:15 -070024 classes = (Classes) moreRef;
jeffhao795d78f2011-09-30 18:34:35 -070025 Main.assertTrue(moreRef instanceof Classes);
jeffhao5d1ac922011-09-29 17:41:15 -070026
27 more = (MoreClasses) moreRef;
jeffhao795d78f2011-09-30 18:34:35 -070028 Main.assertTrue(moreRef instanceof MoreClasses);
29 Main.assertTrue(!(thisRef instanceof MoreClasses));
jeffhao5d1ac922011-09-29 17:41:15 -070030
31 try {
32 more = (MoreClasses) thisRef;
jeffhao795d78f2011-09-30 18:34:35 -070033 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070034 } catch (ClassCastException cce) {
35 //System.out.println(" class cast msg: " + cce.getMessage());
36 //Dalvik throws terser message than Hotspot VM
jeffhao795d78f2011-09-30 18:34:35 -070037 Main.assertTrue(cce.getMessage().regionMatches(false, 0, "Classes", 0, 7));
jeffhao5d1ac922011-09-29 17:41:15 -070038 }
jeffhao795d78f2011-09-30 18:34:35 -070039 Main.assertTrue(!(thisRef instanceof MoreClasses));
jeffhao5d1ac922011-09-29 17:41:15 -070040
41 /* hopefully these classes cause a resolve */
42 try {
43 java.math.RoundingMode mode = (java.math.RoundingMode) thisRef;
jeffhao795d78f2011-09-30 18:34:35 -070044 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -070045 } catch (ClassCastException cce) {
46 //System.out.println(" class cast msg: " + cce.getMessage());
47 //Dalvik throws terser message than Hotspot VM
jeffhao795d78f2011-09-30 18:34:35 -070048 Main.assertTrue(cce.getMessage().regionMatches(false, 0, "Classes", 0, 7));
jeffhao5d1ac922011-09-29 17:41:15 -070049 }
jeffhao795d78f2011-09-30 18:34:35 -070050 Main.assertTrue(!(thisRef instanceof java.math.BigDecimal));
jeffhao5d1ac922011-09-29 17:41:15 -070051
52 /* try some stuff with a null reference */
53 classes = (Classes) nullRef;
54 classes = (MoreClasses) nullRef;
55 more = (MoreClasses) nullRef;
jeffhao795d78f2011-09-30 18:34:35 -070056 Main.assertTrue(!(nullRef instanceof Classes));
jeffhao5d1ac922011-09-29 17:41:15 -070057
58 }
59
60
61 static void xTests(Object x) {
jeffhao795d78f2011-09-30 18:34:35 -070062 Main.assertTrue( x instanceof Classes);
63 Main.assertTrue(!(x instanceof MoreClasses));
jeffhao5d1ac922011-09-29 17:41:15 -070064 }
65 static void yTests(Object y) {
jeffhao795d78f2011-09-30 18:34:35 -070066 Main.assertTrue( y instanceof Classes);
67 Main.assertTrue( y instanceof MoreClasses);
jeffhao5d1ac922011-09-29 17:41:15 -070068 }
69 static void xarTests(Object xar) {
jeffhao795d78f2011-09-30 18:34:35 -070070 Main.assertTrue( xar instanceof Object);
71 Main.assertTrue(!(xar instanceof Classes));
72 Main.assertTrue( xar instanceof Classes[]);
73 Main.assertTrue(!(xar instanceof MoreClasses[]));
74 Main.assertTrue( xar instanceof Object[]);
75 Main.assertTrue(!(xar instanceof Object[][]));
jeffhao5d1ac922011-09-29 17:41:15 -070076 }
77 static void yarTests(Object yar) {
jeffhao795d78f2011-09-30 18:34:35 -070078 Main.assertTrue( yar instanceof Classes[]);
79 Main.assertTrue( yar instanceof MoreClasses[]);
jeffhao5d1ac922011-09-29 17:41:15 -070080 }
81 static void xarararTests(Object xararar) {
jeffhao795d78f2011-09-30 18:34:35 -070082 Main.assertTrue( xararar instanceof Object);
83 Main.assertTrue( xararar instanceof Object[]);
84 Main.assertTrue(!(xararar instanceof Classes));
85 Main.assertTrue(!(xararar instanceof Classes[]));
86 Main.assertTrue(!(xararar instanceof Classes[][]));
87 Main.assertTrue( xararar instanceof Classes[][][]);
88 Main.assertTrue(!(xararar instanceof MoreClasses[][][]));
89 Main.assertTrue( xararar instanceof Object[][][]);
90 Main.assertTrue( xararar instanceof Serializable);
91 Main.assertTrue( xararar instanceof Serializable[]);
92 Main.assertTrue( xararar instanceof Serializable[][]);
93 Main.assertTrue(!(xararar instanceof Serializable[][][]));
jeffhao5d1ac922011-09-29 17:41:15 -070094 }
95 static void yarararTests(Object yararar) {
jeffhao795d78f2011-09-30 18:34:35 -070096 Main.assertTrue( yararar instanceof Classes[][][]);
97 Main.assertTrue( yararar instanceof MoreClasses[][][]);
jeffhao5d1ac922011-09-29 17:41:15 -070098 }
99 static void iarTests(Object iar) {
jeffhao795d78f2011-09-30 18:34:35 -0700100 Main.assertTrue( iar instanceof Object);
101 Main.assertTrue(!(iar instanceof Object[]));
jeffhao5d1ac922011-09-29 17:41:15 -0700102 }
103 static void iararTests(Object iarar) {
jeffhao795d78f2011-09-30 18:34:35 -0700104 Main.assertTrue( iarar instanceof Object);
105 Main.assertTrue( iarar instanceof Object[]);
106 Main.assertTrue(!(iarar instanceof Object[][]));
jeffhao5d1ac922011-09-29 17:41:15 -0700107 }
108
109 /*
110 * Exercise filled-new-array and test instanceof on arrays.
111 *
112 * We call out instead of using "instanceof" directly to avoid
113 * compiler optimizations.
114 */
115 static void arrayInstance() {
116 System.out.println("Classes.arrayInstance");
117
118 Classes x = new Classes();
119 Classes[] xar = new Classes[1];
120 Classes[][] xarar = new Classes[1][1];
121 Classes[][][] xararar = new Classes[1][2][3];
122 MoreClasses y = new MoreClasses();
123 MoreClasses[] yar = new MoreClasses[3];
124 MoreClasses[][] yarar = new MoreClasses[2][3];
125 MoreClasses[][][] yararar = new MoreClasses[1][2][3];
126 int[] iar = new int[1];
127 int[][] iarar = new int[1][1];
128 Object test;
129
130 xTests(x);
131 yTests(y);
132 xarTests(xar);
133 yarTests(yar);
134 xarararTests(xararar);
135 yarararTests(yararar);
136 iarTests(iar);
137 iararTests(iarar);
138
139 yararar[0] = yarar;
140 yararar[0][0] = yar;
141 yararar[0][1] = yar;
142 yararar[0][0][0] = y;
143 yararar[0][0][1] = y;
144 yararar[0][0][2] = y;
145 yararar[0][1][0] = y;
146 yararar[0][1][1] = y;
147 yararar[0][1][2] = y;
148
149 String strForm;
150
151 String[][][][] multi1 = new String[2][3][2][1];
152 multi1[0] = new String[2][3][2];
153 multi1[0][1] = new String[3][2];
154 multi1[0][1][2] = new String[2];
155 multi1[0][1][2][1] = "HELLO-1";
156 strForm = Arrays.deepToString(multi1);
157
158 String[][][][][] multi2 = new String[5][2][3][2][1];
159 multi2[0] = new String[5][2][3][2];
160 multi2[0][1] = new String[5][2][3];
161 multi2[0][1][2] = new String[5][2];
162 multi2[0][1][2][1] = new String[5];
163 multi2[0][1][2][1][4] = "HELLO-2";
164 strForm = Arrays.deepToString(multi2);
165
166
167 String[][][][][][] multi3 = new String[2][5][2][3][2][1];
168 multi3[0] = new String[2][][][][];
169 multi3[0][1] = new String[3][][][];
170 multi3[0][1][2] = new String[2][][];
171 multi3[0][1][2][1] = new String[5][];
172 multi3[0][1][2][1][4] = new String[2];
173 multi3[0][1][2][1][4][1] = "HELLO-3";
174 strForm = Arrays.deepToString(multi3);
175
176 // build up pieces
177 String[][][][][][] multi4 = new String[1][][][][][];
178 multi4[0] = new String[2][][][][];
179 multi4[0][1] = new String[3][][][];
180 multi4[0][1][2] = new String[2][][];
181 multi4[0][1][2][1] = new String[5][];
182 multi4[0][1][2][1][4] = new String[2];
183 multi4[0][1][2][1][4][1] = "HELLO-4";
184 strForm = Arrays.deepToString(multi4);
185
186 /* this is expected to fail; 1073921584 * 4 overflows 32 bits */
187 try {
188 String[][][][][] multiX = new String[5][2][3][2][1073921584];
jeffhao795d78f2011-09-30 18:34:35 -0700189 Main.assertTrue(false);
jeffhao5d1ac922011-09-29 17:41:15 -0700190 } catch (Error e) {
191 //System.out.println(" Got expected failure: " + e);
192 }
193
194 }
195
196 public static void run() {
197 Classes classes = new Classes();
198 MoreClasses more = new MoreClasses();
199 classes.checkCast(classes, more, null);
200
201 more.subFunc(true);
202 more.superFunc(false);
203 arrayInstance();
204 }
205}
206
207class MoreClasses extends Classes {
208 int mMore;
209
210 public MoreClasses() {}
211
212 public void subFunc(boolean wantSub) {
jeffhao795d78f2011-09-30 18:34:35 -0700213 Main.assertTrue(wantSub);
jeffhao5d1ac922011-09-29 17:41:15 -0700214 }
215
216 public void superFunc(boolean wantSub) {
217 super.subFunc(wantSub);
218 }
219}