* tests/regression/base/TestArrayClasses.java: Adapted this testcase to JUnit.
[cacao.git] / tests / regression / base / TestArrayClasses.java
1 /* tests/regression/base/TestArrayClasses.java
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 import org.junit.Test;
27 import static org.junit.Assert.*;
28
29 import java.io.Serializable;
30
31 public class TestArrayClasses {
32   public static class Foo {
33   }
34
35   public static class FooChild extends Foo {
36   }
37   
38   private void doStore(Object[] array, Object obj, boolean oktostore, String msg) {
39     try {
40       array[0] = obj;
41       assertTrue(msg, oktostore);
42     }
43     catch (ArrayStoreException x) {
44       assertFalse(msg, oktostore);
45     }
46   }
47
48   @Test
49   public void testClone() {
50     int[] ia1 = new int[100];
51     Integer[] Ia1 = new Integer[ia1.length];
52     int i;
53     
54     for (i=0; i<ia1.length; ++i) {
55       ia1[i] = i*i;
56       Ia1[i] = new Integer(ia1[i]);
57     }
58
59     int[] ia2 = (int[]) ia1.clone();
60
61     assertEquals("cloned int array length", ia1.length, ia2.length);
62
63     boolean eq = true;
64     for (i=0; i<ia1.length; ++i) {
65       if (ia2[i] != ia1[i])
66         eq = false;
67       //      System.out.println(ia2[i]);
68     }
69     assertTrue("cloned int array data", eq);
70
71     Integer[] Ia2 = (Integer[]) Ia1.clone();
72     
73     assertEquals("cloned Integer array length", Ia1.length, Ia2.length);
74     
75     eq = true;
76     for (i=0; i<ia1.length; ++i) {
77       if (Ia2[i].intValue() != ia1[i])
78         eq = false;
79       //      System.out.println(ia2[i]);
80     }
81     assertTrue("cloned Integer array data", eq);
82   }
83
84   @Test
85   public void testArraycopy() {
86     int len;
87     int i;
88
89     long[] la1 = new long[1024];
90     long[] la2 = (long[]) la1.clone();
91     int size = la1.length;
92     Long[] La1 = new Long[size];
93     Long[] La2 = (Long[]) La1.clone();
94     Number[] Na2 = new Number[size];
95
96     for (i=0; i<la1.length; ++i) {
97       la1[i] = i*i;
98       La1[i] = new Long(la1[i]);
99     }
100
101     i = size;
102     while (i>1) {
103       if ((i & 1) != 0)
104         System.err.println("ERROR: arracopy test only works for powers of two");
105       i >>= 1;
106     }
107     
108     // reverse array
109     len = size / 2;
110     while (len>0) {
111       for (int j=0;j<(size/2)/len;++j) {
112         //        System.err.println(len);
113         //        System.err.println(j);
114         System.arraycopy(la1,(2*j+1)*len,la2,2*j*len,len);
115         System.arraycopy(la1,2*j*len,la1,(2*j+1)*len,len);
116         System.arraycopy(la2,2*j*len,la1,2*j*len,len);
117       }
118       len /= 2;
119     }
120
121     boolean eq = true;
122     for (i=0; i<size; ++i) {
123       if (la1[i] != (size-i-1)*(size-i-1))
124         eq = false;
125       //      System.out.println(la1[i]);
126     }
127     assertTrue("arraycopy primitive", eq);
128     
129     // reverse array
130     len = size / 2;
131     while (len>0) {
132       for (int j=0;j<(size/2)/len;++j) {
133         //        System.err.println(len);
134         //        System.err.println(j);
135         System.arraycopy(La1,(2*j+1)*len,La2,2*j*len,len);
136         System.arraycopy(La1,2*j*len,La1,(2*j+1)*len,len);
137         System.arraycopy(La2,2*j*len,La1,2*j*len,len);
138       }
139       len /= 2;
140     }
141
142     eq = true;
143     for (i=0; i<size; ++i) {
144       if (La1[i].intValue() != (size-i-1)*(size-i-1))
145         eq = false;
146     }
147     assertTrue("arraycopy ref", eq);
148     
149     // reverse array
150     len = size / 2;
151     while (len>0) {
152       for (int j=0;j<(size/2)/len;++j) {
153         //        System.err.println(len);
154         //        System.err.println(j);
155         System.arraycopy(La1,(2*j+1)*len,Na2,2*j*len,len);
156         System.arraycopy(La1,2*j*len,La1,(2*j+1)*len,len);
157         System.arraycopy(Na2,2*j*len,La1,2*j*len,len);
158       }
159       len /= 2;
160     }
161
162     eq = true;
163     for (i=0; i<size; ++i) {
164       if (La1[i].intValue() != i*i)
165         eq = false;
166     }
167     assertTrue("arraycopy ref different classes", eq);
168
169     Integer[] Ia = new Integer[size];
170
171     try {
172       System.arraycopy(Ia,0,Na2,0,1);
173       assertTrue("arraycopy Integer to Number", true);
174     }
175     catch (ArrayStoreException x) {
176       fail("arraycopy Integer to Number");
177     }
178
179     try {
180       System.arraycopy(Na2,1,Ia,1,1);
181       fail("!arraycopy Number to Integer");
182     }
183     catch (ArrayStoreException x) {
184       assertTrue("!arraycopy Number to Integer", true);
185     }
186   }
187
188   @Test
189   public void testMain() {
190     int[] ia = new int[5];
191     Object[] oa = new Object[2];
192     Object[][] oaa = new Object[1][1];
193     String[] sa = new String[3];
194     String[][] saa = new String[1][1];
195     String[][] saa2 = new String[2][];
196     String[][][] saaa = new String[1][2][3];
197     Object o = new Object();
198     java.io.Serializable[] sera = new java.io.Serializable[1];
199     Cloneable[] cloa = new Cloneable[1];
200     StringBuffer[][] sbaa = new StringBuffer[1][1];
201     Foo[] fooa = new Foo[1];
202     FooChild[] fooca = new FooChild[1];
203
204     Class[] ifs = String[].class.getInterfaces();
205     assertEquals("String[] implements 2 interfaces", 2, ifs.length);
206     assertTrue("String[] implements Cloneable", ifs[0] == java.lang.Cloneable.class || ifs[1] == java.lang.Cloneable.class);
207     assertTrue("String[] implements Serializable", ifs[0] == java.io.Serializable.class || ifs[1] == java.io.Serializable.class);
208
209     assertEquals("String[] is public final abstract", 1041, String[].class.getModifiers());
210
211     assertEquals("classname ref", "[Ljava.lang.Object;", oa.getClass().getName());
212     assertEquals("classname primitive", "[I", ia.getClass().getName());
213     assertEquals("arraylength primitive", 5, ia.length);
214     assertEquals("arraylength ref", 2, oa.length);
215
216     assertEquals("arraylength of saa2", 2, saa2.length);
217     saa2[1] = new String[4];
218     assertEquals("arraylength of saa2[1]", 4, saa2[1].length);
219
220     assertEquals("arraylength of saaa", 1, saaa.length);
221     assertEquals("arraylength of saaa[0]", 2, saaa[0].length);
222     assertEquals("arraylength of saaa[0][1]", 3, saaa[0][1].length);
223
224     assertTrue("Object[].isArray", oa.getClass().isArray());
225     assertTrue("int[].isArray", ia.getClass().isArray());
226     assertFalse("!Object.isArray", o.getClass().isArray());
227     assertFalse("!Object.isPrimitive", o.getClass().isPrimitive());
228
229     assertEquals("component ref", "java.lang.Object", oa.getClass().getComponentType().getName());
230     assertFalse("component ref !isPrimitive", oa.getClass().getComponentType().isPrimitive());
231     assertEquals("component primitive", "int", ia.getClass().getComponentType().getName());
232     assertTrue("component primitive isPrimitive", ia.getClass().getComponentType().isPrimitive());
233
234     assertTrue("component of String[][] equals String[]", saa.getClass().getComponentType().equals(sa.getClass()));
235     assertFalse("component of String[][] !equals Object[]", saa.getClass().getComponentType().equals(oa.getClass()));
236
237     assertTrue("saa[0].getClass equals component of String[][]", saa[0].getClass().equals(saa.getClass().getComponentType()));
238
239     doStore(sa, new Object(), false, "!store Object in String[]");
240     doStore(sa, new String("test"), true, "store String in String[]");
241     doStore(oa, new Object(), true, "store Object in Object[]");
242     doStore(oa, new String("test"), true, "store String in Object[]");
243
244     doStore(oaa, sa, true, "store String[] in Object[][]");
245     doStore(saa, oa, false, "!store Object[] in String[][]");
246
247     doStore(sera, sa, true, "store String[] in java.io.Serializable[]");
248     doStore(cloa, sa, true, "store String[] in Cloneable[]");
249     
250     doStore(sbaa, sa, false, "!store String[] in StringBuffer[][]");
251
252     doStore(fooa, new Foo(), true, "store Foo in Foo[]");
253     doStore(fooa, new FooChild(), true, "store FooChild in Foo[]");
254     doStore(fooca, new Foo(), false, "!store Foo in FooChild[]");
255     doStore(fooca, new FooChild(), true, "store FooChild in FooChild[]");
256     
257     try {
258       Object[] oa2 = (Object[]) sa;
259       assertTrue("cast String[] to Object[]", true);
260     }
261     catch (ClassCastException x) {
262       fail("cast String[] to Object[]");
263     }
264
265     try {
266       String[] sa2 = (String[]) oa;
267       fail("!cast Object[] to String[]");
268     }
269     catch (ClassCastException x) {
270       assertTrue("!cast Object[] to String[]", true);
271     }
272
273     assertTrue("String[] instanceof String[]", sa instanceof String[]);
274     assertTrue("String[] instanceof Object[]", sa instanceof Object[]);
275     assertFalse("Object[] !instanceof String[]", oa instanceof String[]);
276     assertTrue("Object[] instanceof Object[]", oa instanceof Object[]);
277
278     assertTrue("Object[][] instanceof Object[]", oaa instanceof Object[]);
279     assertTrue("String[][] instanceof Object[]", saa instanceof Object[]);
280
281     assertTrue("String[] instanceof java.io.Serializable", sa instanceof java.io.Serializable);
282     assertTrue("String[] instanceof java.lang.Cloneable", sa instanceof java.lang.Cloneable);
283     assertTrue("String[] instanceof java.lang.Object", sa instanceof java.lang.Object);
284     assertTrue("saa[0] instanceof java.io.Serializable", saa[0] instanceof java.io.Serializable);
285     assertTrue("saa[0] instanceof java.lang.Cloneable", saa[0] instanceof java.lang.Cloneable);
286     assertTrue("saa[0] instanceof java.lang.Object", saa[0] instanceof java.lang.Object);
287   }
288 }