* tests/Makefile.am: Cleanup, removed checkall, added build target.
[cacao.git] / tests / TestArrayClasses.java
1 import java.io.Serializable;
2
3 public class TestArrayClasses extends TestBase {
4   public static class Foo {
5   }
6
7   public static class FooChild extends Foo {
8   }
9   
10   public static void test_store(Object[] array,Object obj,boolean oktostore,String msg) {
11     try {
12       array[0] = obj;
13       ok(oktostore,msg);
14     }
15     catch (ArrayStoreException x) {
16       System.out.println("    ArrayStoreException");
17       ok(!oktostore,msg);
18     }
19   }
20
21   public static void test_clone() {
22     int[] ia1 = new int[100];
23     Integer[] Ia1 = new Integer[ia1.length];
24     int i;
25     
26     for (i=0; i<ia1.length; ++i) {
27       ia1[i] = i*i;
28       Ia1[i] = new Integer(ia1[i]);
29     }
30
31     int[] ia2 = (int[]) ia1.clone();
32
33     is(ia2.length,ia1.length,"cloned int array length");
34
35     boolean eq = true;
36     for (i=0; i<ia1.length; ++i) {
37       if (ia2[i] != ia1[i])
38         eq = false;
39       //      System.out.println(ia2[i]);
40     }
41     ok(eq,"cloned int array data");
42
43     Integer[] Ia2 = (Integer[]) Ia1.clone();
44     
45     is(Ia2.length,Ia1.length,"cloned Integer array length");
46     
47     eq = true;
48     for (i=0; i<ia1.length; ++i) {
49       if (Ia2[i].intValue() != ia1[i])
50         eq = false;
51       //      System.out.println(ia2[i]);
52     }
53     ok(eq,"cloned Integer array data");
54   }
55
56   public static void test_arraycopy() {
57     int len;
58     int i;
59
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];
66
67     for (i=0; i<la1.length; ++i) {
68       la1[i] = i*i;
69       La1[i] = new Long(la1[i]);
70     }
71
72     i = size;
73     while (i>1) {
74       if ((i & 1) != 0)
75         System.err.println("ERROR: arracopy test only works for powers of two");
76       i >>= 1;
77     }
78     
79     // reverse array
80     len = size / 2;
81     while (len>0) {
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);
88       }
89       len /= 2;
90     }
91
92     boolean eq = true;
93     for (i=0; i<size; ++i) {
94       if (la1[i] != (size-i-1)*(size-i-1))
95         eq = false;
96       //      System.out.println(la1[i]);
97     }
98     ok(eq,"arraycopy primitive");
99     
100     // reverse array
101     len = size / 2;
102     while (len>0) {
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);
109       }
110       len /= 2;
111     }
112
113     eq = true;
114     for (i=0; i<size; ++i) {
115       if (La1[i].intValue() != (size-i-1)*(size-i-1))
116         eq = false;
117     }
118     ok(eq,"arraycopy ref");
119     
120     // reverse array
121     len = size / 2;
122     while (len>0) {
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);
129       }
130       len /= 2;
131     }
132
133     eq = true;
134     for (i=0; i<size; ++i) {
135       if (La1[i].intValue() != i*i)
136         eq = false;
137     }
138     ok(eq,"arraycopy ref different classes");
139
140     Integer[] Ia = new Integer[size];
141
142     try {
143       System.arraycopy(Ia,0,Na2,0,1);
144       ok(true,"arraycopy Integer to Number");
145     }
146     catch (ArrayStoreException x) {
147       System.out.println("    ArrayStoreException");
148       ok(false,"arraycopy Integer to Number");
149     }
150
151     try {
152       System.arraycopy(Na2,1,Ia,1,1);
153       ok(false,"!arraycopy Number to Integer");
154     }
155     catch (ArrayStoreException x) {
156       System.out.println("    ArrayStoreException");
157       ok(true,"!arraycopy Number to Integer");
158     }
159   }
160   
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];
175
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");
180
181     is(String[].class.getModifiers(),1041,"String[] is public final abstract");
182
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");
187
188     is(saa2.length,2,"arraylength of saa2");
189     saa2[1] = new String[4];
190     is(saa2[1].length,4,"arraylength of saa2[1]");
191
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]");
195
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");
200
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");
205
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[]");
208
209     ok(saa[0].getClass().equals(saa.getClass().getComponentType()),"saa[0].getClass equals component of String[][]");
210
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[]");
215
216     test_store(oaa,sa,true,"store String[] in Object[][]");
217     test_store(saa,oa,false,"!store Object[] in String[][]");
218
219     test_store(sera,sa,true,"store String[] in java.io.Serializable[]");
220     test_store(cloa,sa,true,"store String[] in Cloneable[]");
221     
222     test_store(sbaa,sa,false,"!store String[] in StringBuffer[][]");
223
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[]");
228     
229     try {
230       Object[] oa2 = (Object[]) sa;
231       ok(true,"cast String[] to Object[]");
232     }
233     catch (ClassCastException x) {
234       System.out.println("    ClassCastException");
235       ok(false,"cast String[] to Object[]");
236     }
237
238     try {
239       String[] sa2 = (String[]) oa;
240       ok(false,"!cast Object[] to String[]");
241     }
242     catch (ClassCastException x) {
243       System.out.println("    ClassCastException");
244       ok(true,"!cast Object[] to String[]");
245     }
246
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[]");
251
252     ok(oaa instanceof Object[],"Object[][] instanceof Object[]");
253     ok(saa instanceof Object[],"String[][] instanceof Object[]");
254
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");
261
262     test_clone();
263     test_arraycopy();
264   }
265 }