diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index c391bd02..00ee34db 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -167,6 +167,10 @@ public class RuleSet implements IRuleSet{ return Optional.empty(); Type rhsType = pair.getRhsType(); + + if(!rhsType.getName().equals(lhsType.getName())) + return Optional.empty(); + if(rhsType instanceof PlaceholderType || rhsType.getTypeParams().empty()) return Optional.empty(); diff --git a/test/unify/FiniteClosureBuilder.java b/test/unify/FiniteClosureBuilder.java index acce7b5b..f35ce0e4 100644 --- a/test/unify/FiniteClosureBuilder.java +++ b/test/unify/FiniteClosureBuilder.java @@ -18,9 +18,11 @@ public class FiniteClosureBuilder { } public IFiniteClosure getFiniteClosure() { - FiniteClosure fc = new FiniteClosure(pairs); + return new FiniteClosure(pairs); + } + + public void clear() { pairs = new HashSet<>(); - return fc; } public IFiniteClosure getCollectionExample() { @@ -55,6 +57,8 @@ public class FiniteClosureBuilder { add(arrayList, list); add(stack, vector); - return getFiniteClosure(); + IFiniteClosure fc = getFiniteClosure(); + clear(); + return fc; } } diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index 65fdb19f..0776b2b1 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -132,34 +132,56 @@ public class RuleSetTest { @Test public void testReduce1() { - Set pairs = new HashSet(); + TypeFactory tf = new TypeFactory(); + FiniteClosureBuilder fcb = new FiniteClosureBuilder(); - SimpleType t1 = new SimpleType("t1"); - SimpleType t2 = new SimpleType("t2"); - SimpleType t3 = new SimpleType("t3"); - SimpleType t4 = new SimpleType("t4"); - SimpleType t5 = new SimpleType("t5"); - SimpleType t6 = new SimpleType("t6"); - SimpleType t7 = new SimpleType("t7"); - SimpleType t8 = new SimpleType("t8"); - SimpleType t9 = new SimpleType("t9"); + /* + * Positive Tests + */ - SimpleType ta = new SimpleType("a", t1, t2, t3); - SimpleType taa = new SimpleType("a", t4, t5, t6); - SimpleType tb = new SimpleType("b", t3, t1, t2); - SimpleType tbb = new SimpleType("b", t7, t8, t9); + // C + SimpleType c1 = tf.getSimpleType("C", "T1", "T2", "T3", "T4"); + // D + SimpleType d1 = tf.getSimpleType("D", "T4", "T1", "T2", "T3"); + SimpleType buffer = tf.getSimpleType("Buffer"); + //C + SimpleType c2 = tf.getSimpleType("C", tf.getSimpleType("Int"), tf.getExtendsType(tf.getSimpleType("Double")), tf.getPlaceholderType("M"), tf.getPlaceholderType("N")); + //D, Number, Double, N> + SimpleType d2 = tf.getSimpleType("D", tf.getSuperType(tf.getSimpleType("HashSet", "Int")), tf.getSimpleType("Number"), tf.getSimpleType("Double"), tf.getPlaceholderType("N")); - pairs.add(new MPair(ta, tb, PairOperator.SMALLER)); - FiniteClosure fc = new FiniteClosure(pairs); + // C<...> < buffer < D<...> + fcb.add(c1, buffer); + fcb.add(buffer, d1); - IRuleSet rules = new RuleSet(fc); - - MPair test = new MPair(taa, tbb, PairOperator.SMALLERDOT); - - Optional> res = rules.reduce1(test); + IRuleSet rules = new RuleSet(fcb.getFiniteClosure()); + MPair pair = new MPair(c2, d2, PairOperator.SMALLERDOT); + System.out.println("------ Reduce1 ------"); + Optional> res = rules.reduce1(pair); System.out.println(res); + + pair = new MPair(c2, c2, PairOperator.SMALLERDOT); + res = rules.reduce1(pair); + System.out.println(res); + + /* + * Negative Tests + */ + + // Case 1: D <. C and C <* D + pair = new MPair(d2, c2, PairOperator.SMALLERDOT); + Assert.assertFalse(rules.reduce1(pair).isPresent()); + + // Case 2: D =. C + pair = new MPair(c2, d2, PairOperator.EQUALSDOT); + Assert.assertFalse(rules.reduce1(pair).isPresent()); + + // Case 3: C <. D and !(C <* D) + fcb.clear(); + rules = new RuleSet(fcb.getFiniteClosure()); + pair = new MPair(c1, d1, PairOperator.SMALLERDOT); + Assert.assertFalse(rules.reduce1(pair).isPresent()); } @Test @@ -196,7 +218,7 @@ public class RuleSetTest { Optional> res = rules.reduceExt(test); - System.out.println(res); + //System.out.println(res); } @Test @@ -206,7 +228,36 @@ public class RuleSetTest { @Test public void testReduceEq() { + TypeFactory tf = new TypeFactory(); + FiniteClosureBuilder fcb = new FiniteClosureBuilder(); + IRuleSet rules = new RuleSet(fcb.getFiniteClosure()); + /* + * Positive Tests + */ + System.out.println("----- ReduceEq ------"); + + // C + SimpleType c1 = tf.getSimpleType("C", tf.getPlaceholderType("T1"), tf.getSimpleType("SType1"), tf.getExtendsType(tf.getSimpleType("SType1"))); + // D + SimpleType c2 = tf.getSimpleType("C", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2"))); + + MPair pair = new MPair(c1, c2, PairOperator.SMALLERDOTWC); + Optional> res = rules.reduceEq(pair); + System.out.println(res); + + /* + * Negative Tests + */ + + // Case 1: D <.? C + SimpleType d1 = tf.getSimpleType("D", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2"))); + pair = new MPair(d1, c1, PairOperator.SMALLERDOTWC); + Assert.assertFalse(rules.reduceEq(pair).isPresent()); + + // Case 2: C <. C + pair = new MPair(c1, c2, PairOperator.SMALLERDOT); + Assert.assertFalse(rules.reduceEq(pair).isPresent()); } @Test