diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index 608f5584..c391bd02 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -280,7 +280,7 @@ public class RuleSet implements IRuleSet{ if(!(rhsType instanceof SimpleType) && !(rhsType instanceof PlaceholderType)) return false; - return finiteClosure.smaller(lhsType).contains(rhsType); + return finiteClosure.greater(lhsType).contains(rhsType); } @Override @@ -288,15 +288,10 @@ public class RuleSet implements IRuleSet{ if(pair.getPairOp() != PairOperator.SMALLERDOTWC) return false; - Type lhsType = pair.getLhsType(); - if(!(lhsType instanceof SimpleType) && !(lhsType instanceof PlaceholderType)) - return false; - + Type lhsType = pair.getLhsType(); Type rhsType = pair.getRhsType(); - if(!(rhsType instanceof ExtendsType)) - return false; - return finiteClosure.grArg(lhsType).contains(((ExtendsType) rhsType).getExtendedType()); + return finiteClosure.grArg(lhsType).contains(rhsType); } @Override diff --git a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java index 7a65fc30..c01ff5d4 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java @@ -34,4 +34,18 @@ public class ExtendsType extends Type { public Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + + @Override + public int hashCode() { + return extendedType.hashCode() + 17; + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof ExtendsType)) + return false; + + ExtendsType other = (ExtendsType) obj; + return other.getExtendedType().equals(extendedType); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java index 0c9aacd1..48cf2e6f 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java @@ -19,4 +19,17 @@ public class PlaceholderType extends Type{ public Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + + @Override + public int hashCode() { + return typeName.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof PlaceholderType)) + return false; + + return ((PlaceholderType) obj).getName().equals(typeName); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java index 8f1ceaae..199c6518 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java @@ -19,4 +19,22 @@ public class SimpleType extends Type { public Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + + @Override + public int hashCode() { + return typeName.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof SimpleType)) + return false; + + SimpleType other = (SimpleType) obj; + + if(!other.getName().equals(typeName)) + return false; + + return other.getTypeParams().equals(typeParams); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java index d7f0f38a..5a734621 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java @@ -35,4 +35,18 @@ public class SuperType extends Type { public Set grArg(IFiniteClosure fc) { return fc.grArg(this); } + + @Override + public int hashCode() { + return superedType.hashCode() + 17; + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof SuperType)) + return false; + + SuperType other = (SuperType) obj; + return other.getSuperedType().equals(superedType); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Type.java b/src/de/dhbwstuttgart/typinference/unify/model/Type.java index 676bb574..dc5a9d91 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/Type.java @@ -21,31 +21,10 @@ public abstract class Type { public TypeParams getTypeParams() { return typeParams; } - - @Override - public boolean equals(Object obj) { - if(!(obj instanceof Type)) - return false; - if(obj.hashCode() != hashCode()) - return false; - - Type other = (Type) obj; - - if(!other.getName().equals(typeName)) - return false; - - return other.getTypeParams().equals(typeParams); - } public abstract Set smArg(IFiniteClosure fc); public abstract Set grArg(IFiniteClosure fc); - - - @Override - public int hashCode() { - return typeName.hashCode(); - } @Override public String toString() { diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index 833cd392..dc52c9ec 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -136,12 +136,65 @@ public class RuleSetTest { @Test public void testErase1() { + TypeFactory tf = new TypeFactory(); + RuleSet rules = new RuleSet(new FiniteClosureBuilder().getCollectionExample()); + /* + * Positive Tests + */ + MPair erase1 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT); + MPair erase2 = new MPair(tf.getSimpleType("HashSet", "W"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT); + MPair erase3 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + + Assert.assertTrue(rules.erase1(erase1)); + Assert.assertTrue(rules.erase1(erase2)); + Assert.assertTrue(rules.erase1(erase3)); + + /* + * Negative Tests + */ + MPair noerase1 = new MPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + MPair noerase2 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.EQUALSDOT); + MPair noerase3 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOTWC); + + Assert.assertFalse(rules.erase1(noerase1)); + Assert.assertFalse(rules.erase1(noerase2)); + Assert.assertFalse(rules.erase1(noerase3)); } @Test public void testErase2() { + TypeFactory tf = new TypeFactory(); + RuleSet rules = new RuleSet(new FiniteClosureBuilder().getCollectionExample()); + /* + * Positive Tests + */ + MPair erase1 = new MPair(tf.getSimpleType("List", "T"), tf.getExtendsType(tf.getSimpleType("Collection")), PairOperator.SMALLERDOTWC); + MPair erase2 = new MPair(tf.getSimpleType("Collection"), tf.getSuperType(tf.getSimpleType("HashSet", "T")), PairOperator.SMALLERDOTWC); + MPair erase3 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC); + MPair erase4 = new MPair(tf.getExtendsType(tf.getSimpleType("LinkedList", "T")), tf.getExtendsType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC); + MPair erase5 = new MPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("Stack", "T")), PairOperator.SMALLERDOTWC); + + + Assert.assertTrue(rules.erase2(erase1)); + Assert.assertTrue(rules.erase2(erase2)); + Assert.assertTrue(rules.erase2(erase3)); + Assert.assertTrue(rules.erase2(erase4)); + Assert.assertTrue(rules.erase2(erase5)); + + /* + * Negative Tests + */ + MPair noerase1 = new MPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC); + MPair noerase2 = new MPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSimpleType("ArrayList", "T"), PairOperator.SMALLERDOTWC); + MPair noerase3 = new MPair(tf.getExtendsType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC); + MPair noerase4 = new MPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT); + + Assert.assertFalse(rules.erase2(noerase1)); + Assert.assertFalse(rules.erase2(noerase2)); + Assert.assertFalse(rules.erase2(noerase3)); + Assert.assertFalse(rules.erase2(noerase4)); } @Test @@ -166,7 +219,10 @@ public class RuleSetTest { Optional opt1 = rules.swap(swap1); Optional opt2 = rules.swap(swap2); + // swap((? extends MyClass =. P)) = (P =. MyClass) Assert.assertEquals(opt1.get(), expected1); + + // swap((MyClass =. X)) = (X =. MyClass) Assert.assertEquals(opt2.get(), expected2); /*