/test/kilim/test/TestTypeDesc.java
Java | 155 lines | 103 code | 31 blank | 21 comment | 6 complexity | f3c8bc73dd89a5754fd17586231610d6 MD5 | raw file
1/* Copyright (c) 2006, Sriram Srinivasan 2 * 3 * You may distribute this software under the terms of the license 4 * specified in the file "License" 5 */ 6 7package kilim.test; 8 9import kilim.analysis.IncompatibleTypesException; 10import kilim.analysis.TypeDesc; 11import junit.framework.TestCase; 12import static kilim.Constants.*; 13import java.lang.reflect.*; 14import java.util.Arrays; 15import kilim.analysis.KilimContext; 16 17public class TestTypeDesc extends TestCase { 18 19 public static String commonSuperType(String oa,String ob) { 20 return TypeDesc.commonSuperType(KilimContext.DEFAULT.detector,oa,ob); 21 } 22 public static String mergeType(String a,String b) throws IncompatibleTypesException { 23 return TypeDesc.mergeType(KilimContext.DEFAULT.detector,a,b); 24 } 25 26 public void testInterning() throws Exception { 27 // Verify all strings in Constants that start with "D_" 28 // are indeed interned. 29 Class<?> c = Class.forName("kilim.Constants"); 30 Field[] fields = c.getFields(); 31 for (Field f:fields) { 32 if (f.getName().startsWith("D_")) { 33 String val = f.get(null).toString(); 34 assertSame(TypeDesc.getInterned(new String(val)), val); 35 } 36 } 37 } 38 39 public void testComponentType() { 40 assertSame(TypeDesc.getComponentType("[J"), D_LONG); 41 assertSame(TypeDesc.getComponentType("[Ljava/lang/String;"), D_STRING); 42 } 43 44 public void testCommonSuperTypes() { 45 // Two interfaces => Object. Checking interning at the same time. 46 assertSame(commonSuperType("Ljava/io/Serializable;", "Ljava/lang/Comparable;"), 47 D_OBJECT); 48 assertEquals(commonSuperType("Lkilim/BasicBlock;", 49 "Lkilim/BasicBlock;"), "Lkilim/BasicBlock;"); 50 assertSame(commonSuperType("[Z", "[Z"), D_ARRAY_BOOLEAN); 51 52 // least upper bound of Field and Method is AccessibleObject 53 assertEquals("Ljava/lang/reflect/AccessibleObject;", 54 commonSuperType("Ljava/lang/reflect/Field;", 55 "Ljava/lang/reflect/Method;")); 56 57 // least upper bound of Field and AccessibleObject is AccessibleObject 58 assertEquals("Ljava/lang/reflect/AccessibleObject;", 59 commonSuperType("Ljava/lang/reflect/Field;", 60 "Ljava/lang/reflect/AccessibleObject;")); 61 62 // Same as above, but flip the order to see if it is sensitive. 63 assertEquals("Ljava/lang/reflect/AccessibleObject;", 64 commonSuperType("Ljava/lang/reflect/Field;", 65 "Ljava/lang/reflect/AccessibleObject;")); 66 67 assertEquals("Lkilim/test/ex/ExA;", 68 commonSuperType("Lkilim/test/ex/ExA;", "Lkilim/test/ex/ExD;")); 69 70 assertEquals("Lkilim/test/ex/ExA;", 71 commonSuperType("Lkilim/test/ex/ExD;", "Lkilim/test/ex/ExA;")); 72 73 assertEquals("Lkilim/test/ex/ExA;", 74 commonSuperType("Lkilim/test/ex/ExC;", "Lkilim/test/ex/ExD;")); 75 76 } 77 78 public void testArray() throws IncompatibleTypesException { 79 assertSame(D_OBJECT, 80 mergeType("Lkilim/test/ex/ExC;", "[Z")); 81 82 assertSame(D_OBJECT, 83 mergeType("[Z", "Lkilim/test/ex/ExC;")); 84 } 85 86 public void testNull() throws IncompatibleTypesException { 87 assertSame(D_NULL, mergeType(D_NULL, D_NULL)); 88 assertSame(D_OBJECT, mergeType(D_OBJECT, D_NULL)); 89 assertSame(D_OBJECT, mergeType(D_NULL, D_OBJECT)); 90 } 91 public void testNumArgs() throws IncompatibleTypesException { 92 assertTrue(TypeDesc.getNumArgumentTypes("()V") == 0); 93 assertTrue(TypeDesc.getNumArgumentTypes("(Ljava/lang/String;[[[ZZBCDSIJF)V") == 10); 94 } 95 96 public void testReturnType() throws IncompatibleTypesException { 97 assertTrue(TypeDesc.getReturnTypeDesc("()V") == D_VOID); 98 assertTrue(TypeDesc.getReturnTypeDesc("()[I") == D_ARRAY_INT); 99 assertTrue(TypeDesc.getReturnTypeDesc("(IIII)[Ljava/lang/Throwable;").equals("[Ljava/lang/Throwable;")); 100 } 101 102 public void testArgTypes() throws IncompatibleTypesException { 103 String[] types = TypeDesc.getArgumentTypes("([Ljava/lang/String;[[[ZZBCDSIJF)V"); 104 String[] expected = new String[] {"[Ljava/lang/String;","[[[Z", D_BOOLEAN, D_BYTE,D_CHAR, 105 D_DOUBLE,D_SHORT,D_INT,D_LONG,D_FLOAT}; 106 assertTrue(Arrays.equals(types, expected)); 107 } 108 109 public void testMerge() throws IncompatibleTypesException { 110 // testCommonSuperTypes() has already checked many combinations of 111 // classes, arrays and interfaces. Handle null etc. 112 113 // Null + String => String 114 assertSame(D_STRING, mergeType(D_NULL, D_STRING)); 115 116 // Null + X == X (order of D_NULL flipped this time) 117 assertSame(D_ARRAY_DOUBLE, mergeType("[D", D_NULL)); 118 119 // primitive types should return the same 120 assertEquals(D_DOUBLE, mergeType(D_DOUBLE, D_DOUBLE)); 121 122 // Array + Object -> Array 123 assertSame(D_OBJECT, mergeType("[I", D_OBJECT)); 124 125 assertSame(D_OBJECT, mergeType("[I", "[D")); 126 127 // common supertype of arrays 128 assertEquals("[Ljava/lang/reflect/AccessibleObject;", 129 mergeType("[Ljava/lang/reflect/Field;","[Ljava/lang/reflect/Method;")); 130 131 // A inherits from B ==> merge(A[], B[]) = B[] 132 assertEquals("[Ljava/lang/reflect/AccessibleObject;", 133 mergeType("[Ljava/lang/reflect/Method;", "[Ljava/lang/reflect/AccessibleObject;")); 134 135 // A inherits from B ==> merge(A[], B[]) = A[] 136 assertEquals("[Ljava/lang/reflect/AccessibleObject;", 137 mergeType("[Ljava/lang/reflect/AccessibleObject;", "[Ljava/lang/reflect/Method;")); 138 139 } 140 141 public void testInvalidCombinations() { 142 assertInvalidCombo("I", D_OBJECT); 143 assertInvalidCombo(D_OBJECT, D_INT); 144 assertInvalidCombo("Meaningless", D_OBJECT); 145 } 146 147 private void assertInvalidCombo(String a, String b) { 148 try { 149 mergeType(a,b); 150 fail("Types '" + a + "' and '" + b + "' aren't supposed to be compatible"); 151 } catch (IncompatibleTypesException ignore) { 152 // Good. It is supposed to fail 153 } 154 } 155}