Re-added tests for array classes
authoredwin <none@none>
Sun, 23 Nov 2003 13:06:29 +0000 (13:06 +0000)
committeredwin <none@none>
Sun, 23 Nov 2003 13:06:29 +0000 (13:06 +0000)
tests/TestArrayClasses.java [new file with mode: 0644]
tests/TestArrayClasses.output [new file with mode: 0644]
tests/TestBase.java [new file with mode: 0644]

diff --git a/tests/TestArrayClasses.java b/tests/TestArrayClasses.java
new file mode 100644 (file)
index 0000000..97b53d7
--- /dev/null
@@ -0,0 +1,258 @@
+import java.io.Serializable;
+
+public class TestArrayClasses extends TestBase {
+  public static class Foo {
+  }
+
+  public static class FooChild extends Foo {
+  }
+  
+  public static void test_store(Object[] array,Object obj,boolean oktostore,String msg) {
+    try {
+      array[0] = obj;
+      ok(oktostore,msg);
+    }
+    catch (ArrayStoreException x) {
+      System.out.println("    ArrayStoreException");
+      ok(!oktostore,msg);
+    }
+  }
+
+  public static void test_clone() {
+    int[] ia1 = new int[100];
+    Integer[] Ia1 = new Integer[ia1.length];
+    int i;
+    
+    for (i=0; i<ia1.length; ++i) {
+      ia1[i] = i*i;
+      Ia1[i] = new Integer(ia1[i]);
+    }
+
+    int[] ia2 = (int[]) ia1.clone();
+
+    is(ia2.length,ia1.length,"cloned int array length");
+
+    boolean eq = true;
+    for (i=0; i<ia1.length; ++i) {
+      if (ia2[i] != ia1[i])
+        eq = false;
+      //      System.out.println(ia2[i]);
+    }
+    ok(eq,"cloned int array data");
+
+    Integer[] Ia2 = (Integer[]) Ia1.clone();
+    
+    is(Ia2.length,Ia1.length,"cloned Integer array length");
+    
+    eq = true;
+    for (i=0; i<ia1.length; ++i) {
+      if (Ia2[i].intValue() != ia1[i])
+        eq = false;
+      //      System.out.println(ia2[i]);
+    }
+    ok(eq,"cloned Integer array data");
+  }
+
+  public static void test_arraycopy() {
+    int len;
+    int i;
+
+    long[] la1 = new long[1024];
+    long[] la2 = (long[]) la1.clone();
+    int size = la1.length;
+    Long[] La1 = new Long[size];
+    Long[] La2 = (Long[]) La1.clone();
+    Number[] Na2 = new Number[size];
+
+    for (i=0; i<la1.length; ++i) {
+      la1[i] = i*i;
+      La1[i] = new Long(la1[i]);
+    }
+
+    i = size;
+    while (i>1) {
+      if ((i & 1) != 0)
+        System.err.println("ERROR: arracopy test only works for powers of two");
+      i >>= 1;
+    }
+    
+    // reverse array
+    len = size / 2;
+    while (len>0) {
+      for (int j=0;j<(size/2)/len;++j) {
+        //        System.err.println(len);
+        //        System.err.println(j);
+        System.arraycopy(la1,(2*j+1)*len,la2,2*j*len,len);
+        System.arraycopy(la1,2*j*len,la1,(2*j+1)*len,len);
+        System.arraycopy(la2,2*j*len,la1,2*j*len,len);
+      }
+      len /= 2;
+    }
+
+    boolean eq = true;
+    for (i=0; i<size; ++i) {
+      if (la1[i] != (size-i-1)*(size-i-1))
+        eq = false;
+      //      System.out.println(la1[i]);
+    }
+    ok(eq,"arraycopy primitive");
+    
+    // reverse array
+    len = size / 2;
+    while (len>0) {
+      for (int j=0;j<(size/2)/len;++j) {
+        //        System.err.println(len);
+        //        System.err.println(j);
+        System.arraycopy(La1,(2*j+1)*len,La2,2*j*len,len);
+        System.arraycopy(La1,2*j*len,La1,(2*j+1)*len,len);
+        System.arraycopy(La2,2*j*len,La1,2*j*len,len);
+      }
+      len /= 2;
+    }
+
+    eq = true;
+    for (i=0; i<size; ++i) {
+      if (La1[i].intValue() != (size-i-1)*(size-i-1))
+        eq = false;
+    }
+    ok(eq,"arraycopy ref");
+    
+    // reverse array
+    len = size / 2;
+    while (len>0) {
+      for (int j=0;j<(size/2)/len;++j) {
+        //        System.err.println(len);
+        //        System.err.println(j);
+        System.arraycopy(La1,(2*j+1)*len,Na2,2*j*len,len);
+        System.arraycopy(La1,2*j*len,La1,(2*j+1)*len,len);
+        System.arraycopy(Na2,2*j*len,La1,2*j*len,len);
+      }
+      len /= 2;
+    }
+
+    eq = true;
+    for (i=0; i<size; ++i) {
+      if (La1[i].intValue() != i*i)
+        eq = false;
+    }
+    ok(eq,"arraycopy ref different classes");
+
+    Integer[] Ia = new Integer[size];
+
+    try {
+      System.arraycopy(Ia,0,Na2,0,1);
+      ok(true,"arraycopy Integer to Number");
+    }
+    catch (ArrayStoreException x) {
+      System.out.println("    ArrayStoreException");
+      ok(false,"arraycopy Integer to Number");
+    }
+
+    try {
+      System.arraycopy(Na2,1,Ia,1,1);
+      ok(false,"!arraycopy Number to Integer");
+    }
+    catch (ArrayStoreException x) {
+      System.out.println("    ArrayStoreException");
+      ok(true,"!arraycopy Number to Integer");
+    }
+  }
+  
+  public static void main(String[] args) {
+    int[] ia = new int[5];
+    Object[] oa = new Object[2];
+    Object[][] oaa = new Object[1][1];
+    String[] sa = new String[3];
+    String[][] saa = new String[1][1];
+    String[][] saa2 = new String[2][];
+    String[][][] saaa = new String[1][2][3];
+    Object o = new Object();
+    java.io.Serializable[] sera = new java.io.Serializable[1];
+    Cloneable[] cloa = new Cloneable[1];
+    StringBuffer[][] sbaa = new StringBuffer[1][1];
+    Foo[] fooa = new Foo[1];
+    FooChild[] fooca = new FooChild[1];
+
+    is(oa.getClass().getName(),"[Ljava.lang.Object;","classname ref");
+    is(ia.getClass().getName(),"[I","classname primitive");
+    is(ia.length,5,"arraylength primitive");
+    is(oa.length,2,"arraylength ref");
+
+    is(saa2.length,2,"arraylength of saa2");
+    saa2[1] = new String[4];
+    is(saa2[1].length,4,"arraylength of saa2[1]");
+
+    is(saaa.length,1,"arraylength of saaa");
+    is(saaa[0].length,2,"arraylength of saaa[0]");
+    is(saaa[0][1].length,3,"arraylength of saaa[0][1]");
+
+    ok(oa.getClass().isArray(),"Object[].isArray");
+    ok(ia.getClass().isArray(),"int[].isArray");
+    ok(!o.getClass().isArray(),"!Object.isArray");
+    ok(!o.getClass().isPrimitive(),"!Object.isPrimitive");
+
+    is(oa.getClass().getComponentType().getName(),"java.lang.Object","component ref");
+    ok(!oa.getClass().getComponentType().isPrimitive(),"component ref !isPrimitive");
+    is(ia.getClass().getComponentType().getName(),"int","component primitive");
+    ok(ia.getClass().getComponentType().isPrimitive(),"component primitive isPrimitive");
+
+    ok(saa.getClass().getComponentType().equals(sa.getClass()),"component of String[][] equals String[]");
+    ok(!saa.getClass().getComponentType().equals(oa.getClass()),"component of String[][] !equals Object[]");
+
+    ok(saa[0].getClass().equals(saa.getClass().getComponentType()),"saa[0].getClass equals component of String[][]");
+
+    test_store(sa,new Object(),false,"!store Object in String[]");
+    test_store(sa,new String("test"),true,"store String in String[]");
+    test_store(oa,new Object(),true,"store Object in Object[]");
+    test_store(oa,new String("test"),true,"store String in Object[]");
+
+    test_store(oaa,sa,true,"store String[] in Object[][]");
+    test_store(saa,oa,false,"!store Object[] in String[][]");
+
+    test_store(sera,sa,true,"store String[] in java.io.Serializable[]");
+    test_store(cloa,sa,true,"store String[] in Cloneable[]");
+    
+    test_store(sbaa,sa,false,"!store String[] in StringBuffer[][]");
+
+    test_store(fooa,new Foo(),true,"store Foo in Foo[]");
+    test_store(fooa,new FooChild(),true,"store FooChild in Foo[]");
+    test_store(fooca,new Foo(),false,"!store Foo in FooChild[]");
+    test_store(fooca,new FooChild(),true,"store FooChild in FooChild[]");
+    
+    try {
+      Object[] oa2 = (Object[]) sa;
+      ok(true,"cast String[] to Object[]");
+    }
+    catch (ClassCastException x) {
+      System.out.println("    ClassCastException");
+      ok(false,"cast String[] to Object[]");
+    }
+
+    try {
+      String[] sa2 = (String[]) oa;
+      ok(false,"!cast Object[] to String[]");
+    }
+    catch (ClassCastException x) {
+      System.out.println("    ClassCastException");
+      ok(true,"!cast Object[] to String[]");
+    }
+
+    ok(sa instanceof String[],"String[] instanceof String[]");
+    ok(sa instanceof Object[],"String[] instanceof Object[]");
+    ok(!(oa instanceof String[]),"Object[] !instanceof String[]");
+    ok(oa instanceof Object[],"Object[] instanceof Object[]");
+
+    ok(oaa instanceof Object[],"Object[][] instanceof Object[]");
+    ok(saa instanceof Object[],"String[][] instanceof Object[]");
+
+    ok(sa instanceof java.io.Serializable,"String[] instanceof java.io.Serializable");
+    ok(sa instanceof java.lang.Cloneable,"String[] instanceof java.lang.Cloneable");
+    ok(sa instanceof java.lang.Object,"String[] instanceof java.lang.Object");
+    ok(saa[0] instanceof java.io.Serializable,"saa[0] instanceof java.io.Serializable");
+    ok(saa[0] instanceof java.lang.Cloneable,"saa[0] instanceof java.lang.Cloneable");
+    ok(saa[0] instanceof java.lang.Object,"saa[0] instanceof java.lang.Object");
+
+    test_clone();
+    test_arraycopy();
+  }
+}
diff --git a/tests/TestArrayClasses.output b/tests/TestArrayClasses.output
new file mode 100644 (file)
index 0000000..43053c2
--- /dev/null
@@ -0,0 +1,62 @@
+ok classname ref
+ok classname primitive
+ok arraylength primitive
+ok arraylength ref
+ok arraylength of saa2
+ok arraylength of saa2[1]
+ok arraylength of saaa
+ok arraylength of saaa[0]
+ok arraylength of saaa[0][1]
+ok Object[].isArray
+ok int[].isArray
+ok !Object.isArray
+ok !Object.isPrimitive
+ok component ref
+ok component ref !isPrimitive
+ok component primitive
+ok component primitive isPrimitive
+ok component of String[][] equals String[]
+ok component of String[][] !equals Object[]
+ok saa[0].getClass equals component of String[][]
+    ArrayStoreException
+ok !store Object in String[]
+ok store String in String[]
+ok store Object in Object[]
+ok store String in Object[]
+ok store String[] in Object[][]
+    ArrayStoreException
+ok !store Object[] in String[][]
+ok store String[] in java.io.Serializable[]
+ok store String[] in Cloneable[]
+    ArrayStoreException
+ok !store String[] in StringBuffer[][]
+ok store Foo in Foo[]
+ok store FooChild in Foo[]
+    ArrayStoreException
+ok !store Foo in FooChild[]
+ok store FooChild in FooChild[]
+ok cast String[] to Object[]
+    ClassCastException
+ok !cast Object[] to String[]
+ok String[] instanceof String[]
+ok String[] instanceof Object[]
+ok Object[] !instanceof String[]
+ok Object[] instanceof Object[]
+ok Object[][] instanceof Object[]
+ok String[][] instanceof Object[]
+ok String[] instanceof java.io.Serializable
+ok String[] instanceof java.lang.Cloneable
+ok String[] instanceof java.lang.Object
+ok saa[0] instanceof java.io.Serializable
+ok saa[0] instanceof java.lang.Cloneable
+ok saa[0] instanceof java.lang.Object
+ok cloned int array length
+ok cloned int array data
+ok cloned Integer array length
+ok cloned Integer array data
+ok arraycopy primitive
+ok arraycopy ref
+ok arraycopy ref different classes
+ok arraycopy Integer to Number
+    ArrayStoreException
+ok !arraycopy Number to Integer
diff --git a/tests/TestBase.java b/tests/TestBase.java
new file mode 100644 (file)
index 0000000..b8d134c
--- /dev/null
@@ -0,0 +1,26 @@
+public class TestBase {
+  public static boolean ok(boolean test,String msg) {
+    if (test) {
+      System.out.println("ok "+msg);
+      return true;
+    }
+    else {
+      System.out.println("NOT ok "+msg);
+      return false;
+    }
+  }
+
+  public static boolean is(String a,String b,String msg) {
+    if (ok(a.equals(b),msg)) return true;
+    System.out.println("    a='"+a+"' b='"+b+"'");
+    return false;
+  }
+
+  public static boolean is(int a,int b,String msg) {
+    return ok(a == b,msg);
+  }
+
+  public static boolean equals(Object a,Object b,String msg) {
+    return ok(a.equals(b),msg);
+  }
+}