1 import java.io.Serializable;
3 public class TestArrayClasses extends TestBase {
4 public static class Foo {
7 public static class FooChild extends Foo {
10 public static void test_store(Object[] array,Object obj,boolean oktostore,String msg) {
15 catch (ArrayStoreException x) {
16 System.out.println(" ArrayStoreException");
21 public static void test_clone() {
22 int[] ia1 = new int[100];
23 Integer[] Ia1 = new Integer[ia1.length];
26 for (i=0; i<ia1.length; ++i) {
28 Ia1[i] = new Integer(ia1[i]);
31 int[] ia2 = (int[]) ia1.clone();
33 is(ia2.length,ia1.length,"cloned int array length");
36 for (i=0; i<ia1.length; ++i) {
39 // System.out.println(ia2[i]);
41 ok(eq,"cloned int array data");
43 Integer[] Ia2 = (Integer[]) Ia1.clone();
45 is(Ia2.length,Ia1.length,"cloned Integer array length");
48 for (i=0; i<ia1.length; ++i) {
49 if (Ia2[i].intValue() != ia1[i])
51 // System.out.println(ia2[i]);
53 ok(eq,"cloned Integer array data");
56 public static void test_arraycopy() {
60 long[] la1 = new long[1024];
61 long[] la2 = (long[]) la1.clone();
62 int size = la1.length;
63 Long[] La1 = new Long[size];
64 Long[] La2 = (Long[]) La1.clone();
65 Number[] Na2 = new Number[size];
67 for (i=0; i<la1.length; ++i) {
69 La1[i] = new Long(la1[i]);
75 System.err.println("ERROR: arracopy test only works for powers of two");
82 for (int j=0;j<(size/2)/len;++j) {
83 // System.err.println(len);
84 // System.err.println(j);
85 System.arraycopy(la1,(2*j+1)*len,la2,2*j*len,len);
86 System.arraycopy(la1,2*j*len,la1,(2*j+1)*len,len);
87 System.arraycopy(la2,2*j*len,la1,2*j*len,len);
93 for (i=0; i<size; ++i) {
94 if (la1[i] != (size-i-1)*(size-i-1))
96 // System.out.println(la1[i]);
98 ok(eq,"arraycopy primitive");
103 for (int j=0;j<(size/2)/len;++j) {
104 // System.err.println(len);
105 // System.err.println(j);
106 System.arraycopy(La1,(2*j+1)*len,La2,2*j*len,len);
107 System.arraycopy(La1,2*j*len,La1,(2*j+1)*len,len);
108 System.arraycopy(La2,2*j*len,La1,2*j*len,len);
114 for (i=0; i<size; ++i) {
115 if (La1[i].intValue() != (size-i-1)*(size-i-1))
118 ok(eq,"arraycopy ref");
123 for (int j=0;j<(size/2)/len;++j) {
124 // System.err.println(len);
125 // System.err.println(j);
126 System.arraycopy(La1,(2*j+1)*len,Na2,2*j*len,len);
127 System.arraycopy(La1,2*j*len,La1,(2*j+1)*len,len);
128 System.arraycopy(Na2,2*j*len,La1,2*j*len,len);
134 for (i=0; i<size; ++i) {
135 if (La1[i].intValue() != i*i)
138 ok(eq,"arraycopy ref different classes");
140 Integer[] Ia = new Integer[size];
143 System.arraycopy(Ia,0,Na2,0,1);
144 ok(true,"arraycopy Integer to Number");
146 catch (ArrayStoreException x) {
147 System.out.println(" ArrayStoreException");
148 ok(false,"arraycopy Integer to Number");
152 System.arraycopy(Na2,1,Ia,1,1);
153 ok(false,"!arraycopy Number to Integer");
155 catch (ArrayStoreException x) {
156 System.out.println(" ArrayStoreException");
157 ok(true,"!arraycopy Number to Integer");
161 public static void main(String[] args) {
162 int[] ia = new int[5];
163 Object[] oa = new Object[2];
164 Object[][] oaa = new Object[1][1];
165 String[] sa = new String[3];
166 String[][] saa = new String[1][1];
167 String[][] saa2 = new String[2][];
168 String[][][] saaa = new String[1][2][3];
169 Object o = new Object();
170 java.io.Serializable[] sera = new java.io.Serializable[1];
171 Cloneable[] cloa = new Cloneable[1];
172 StringBuffer[][] sbaa = new StringBuffer[1][1];
173 Foo[] fooa = new Foo[1];
174 FooChild[] fooca = new FooChild[1];
176 Class[] ifs = String[].class.getInterfaces();
177 is(ifs.length,2,"String[] implements 2 interfaces");
178 ok(ifs[0] == java.lang.Cloneable.class || ifs[1] == java.lang.Cloneable.class,"String[] implements Cloneable");
179 ok(ifs[0] == java.io.Serializable.class || ifs[1] == java.io.Serializable.class,"String[] implements Serializable");
181 is(String[].class.getModifiers(),1041,"String[] is public final abstract");
183 is(oa.getClass().getName(),"[Ljava.lang.Object;","classname ref");
184 is(ia.getClass().getName(),"[I","classname primitive");
185 is(ia.length,5,"arraylength primitive");
186 is(oa.length,2,"arraylength ref");
188 is(saa2.length,2,"arraylength of saa2");
189 saa2[1] = new String[4];
190 is(saa2[1].length,4,"arraylength of saa2[1]");
192 is(saaa.length,1,"arraylength of saaa");
193 is(saaa[0].length,2,"arraylength of saaa[0]");
194 is(saaa[0][1].length,3,"arraylength of saaa[0][1]");
196 ok(oa.getClass().isArray(),"Object[].isArray");
197 ok(ia.getClass().isArray(),"int[].isArray");
198 ok(!o.getClass().isArray(),"!Object.isArray");
199 ok(!o.getClass().isPrimitive(),"!Object.isPrimitive");
201 is(oa.getClass().getComponentType().getName(),"java.lang.Object","component ref");
202 ok(!oa.getClass().getComponentType().isPrimitive(),"component ref !isPrimitive");
203 is(ia.getClass().getComponentType().getName(),"int","component primitive");
204 ok(ia.getClass().getComponentType().isPrimitive(),"component primitive isPrimitive");
206 ok(saa.getClass().getComponentType().equals(sa.getClass()),"component of String[][] equals String[]");
207 ok(!saa.getClass().getComponentType().equals(oa.getClass()),"component of String[][] !equals Object[]");
209 ok(saa[0].getClass().equals(saa.getClass().getComponentType()),"saa[0].getClass equals component of String[][]");
211 test_store(sa,new Object(),false,"!store Object in String[]");
212 test_store(sa,new String("test"),true,"store String in String[]");
213 test_store(oa,new Object(),true,"store Object in Object[]");
214 test_store(oa,new String("test"),true,"store String in Object[]");
216 test_store(oaa,sa,true,"store String[] in Object[][]");
217 test_store(saa,oa,false,"!store Object[] in String[][]");
219 test_store(sera,sa,true,"store String[] in java.io.Serializable[]");
220 test_store(cloa,sa,true,"store String[] in Cloneable[]");
222 test_store(sbaa,sa,false,"!store String[] in StringBuffer[][]");
224 test_store(fooa,new Foo(),true,"store Foo in Foo[]");
225 test_store(fooa,new FooChild(),true,"store FooChild in Foo[]");
226 test_store(fooca,new Foo(),false,"!store Foo in FooChild[]");
227 test_store(fooca,new FooChild(),true,"store FooChild in FooChild[]");
230 Object[] oa2 = (Object[]) sa;
231 ok(true,"cast String[] to Object[]");
233 catch (ClassCastException x) {
234 System.out.println(" ClassCastException");
235 ok(false,"cast String[] to Object[]");
239 String[] sa2 = (String[]) oa;
240 ok(false,"!cast Object[] to String[]");
242 catch (ClassCastException x) {
243 System.out.println(" ClassCastException");
244 ok(true,"!cast Object[] to String[]");
247 ok(sa instanceof String[],"String[] instanceof String[]");
248 ok(sa instanceof Object[],"String[] instanceof Object[]");
249 ok(!(oa instanceof String[]),"Object[] !instanceof String[]");
250 ok(oa instanceof Object[],"Object[] instanceof Object[]");
252 ok(oaa instanceof Object[],"Object[][] instanceof Object[]");
253 ok(saa instanceof Object[],"String[][] instanceof Object[]");
255 ok(sa instanceof java.io.Serializable,"String[] instanceof java.io.Serializable");
256 ok(sa instanceof java.lang.Cloneable,"String[] instanceof java.lang.Cloneable");
257 ok(sa instanceof java.lang.Object,"String[] instanceof java.lang.Object");
258 ok(saa[0] instanceof java.io.Serializable,"saa[0] instanceof java.io.Serializable");
259 ok(saa[0] instanceof java.lang.Cloneable,"saa[0] instanceof java.lang.Cloneable");
260 ok(saa[0] instanceof java.lang.Object,"saa[0] instanceof java.lang.Object");