1 /* tests/regression/base/TestArrayClasses.java
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
26 import org.junit.Test;
27 import static org.junit.Assert.*;
29 import java.io.Serializable;
31 public class TestArrayClasses {
32 public static class Foo {
35 public static class FooChild extends Foo {
38 private void doStore(Object[] array, Object obj, boolean oktostore, String msg) {
41 assertTrue(msg, oktostore);
43 catch (ArrayStoreException x) {
44 assertFalse(msg, oktostore);
49 public void testClone() {
50 int[] ia1 = new int[100];
51 Integer[] Ia1 = new Integer[ia1.length];
54 for (i=0; i<ia1.length; ++i) {
56 Ia1[i] = new Integer(ia1[i]);
59 int[] ia2 = (int[]) ia1.clone();
61 assertEquals("cloned int array length", ia1.length, ia2.length);
64 for (i=0; i<ia1.length; ++i) {
67 // System.out.println(ia2[i]);
69 assertTrue("cloned int array data", eq);
71 Integer[] Ia2 = (Integer[]) Ia1.clone();
73 assertEquals("cloned Integer array length", Ia1.length, Ia2.length);
76 for (i=0; i<ia1.length; ++i) {
77 if (Ia2[i].intValue() != ia1[i])
79 // System.out.println(ia2[i]);
81 assertTrue("cloned Integer array data", eq);
85 public void testArraycopy() {
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];
96 for (i=0; i<la1.length; ++i) {
98 La1[i] = new Long(la1[i]);
104 System.err.println("ERROR: arracopy test only works for powers of two");
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);
122 for (i=0; i<size; ++i) {
123 if (la1[i] != (size-i-1)*(size-i-1))
125 // System.out.println(la1[i]);
127 assertTrue("arraycopy primitive", eq);
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);
143 for (i=0; i<size; ++i) {
144 if (La1[i].intValue() != (size-i-1)*(size-i-1))
147 assertTrue("arraycopy ref", eq);
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);
163 for (i=0; i<size; ++i) {
164 if (La1[i].intValue() != i*i)
167 assertTrue("arraycopy ref different classes", eq);
169 Integer[] Ia = new Integer[size];
172 System.arraycopy(Ia,0,Na2,0,1);
173 assertTrue("arraycopy Integer to Number", true);
175 catch (ArrayStoreException x) {
176 fail("arraycopy Integer to Number");
180 System.arraycopy(Na2,1,Ia,1,1);
181 fail("!arraycopy Number to Integer");
183 catch (ArrayStoreException x) {
184 assertTrue("!arraycopy Number to Integer", true);
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];
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);
209 assertEquals("String[] is public final abstract", 1041, String[].class.getModifiers());
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);
216 assertEquals("arraylength of saa2", 2, saa2.length);
217 saa2[1] = new String[4];
218 assertEquals("arraylength of saa2[1]", 4, saa2[1].length);
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);
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());
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());
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()));
237 assertTrue("saa[0].getClass equals component of String[][]", saa[0].getClass().equals(saa.getClass().getComponentType()));
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[]");
244 doStore(oaa, sa, true, "store String[] in Object[][]");
245 doStore(saa, oa, false, "!store Object[] in String[][]");
247 doStore(sera, sa, true, "store String[] in java.io.Serializable[]");
248 doStore(cloa, sa, true, "store String[] in Cloneable[]");
250 doStore(sbaa, sa, false, "!store String[] in StringBuffer[][]");
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[]");
258 Object[] oa2 = (Object[]) sa;
259 assertTrue("cast String[] to Object[]", true);
261 catch (ClassCastException x) {
262 fail("cast String[] to Object[]");
266 String[] sa2 = (String[]) oa;
267 fail("!cast Object[] to String[]");
269 catch (ClassCastException x) {
270 assertTrue("!cast Object[] to String[]", true);
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[]);
278 assertTrue("Object[][] instanceof Object[]", oaa instanceof Object[]);
279 assertTrue("String[][] instanceof Object[]", saa instanceof Object[]);
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);