diff --git a/notizen/stf/Notes b/notizen/stf/Notes index d424459f..c9fe69a5 100644 --- a/notizen/stf/Notes +++ b/notizen/stf/Notes @@ -8,4 +8,9 @@ - Warum ist result von unify = Menge> und nicht Menge -- Menge Equals überarbeiten (Momentan Reihenfolgensensitiv) \ No newline at end of file +- Menge Equals überarbeiten (Momentan Reihenfolgensensitiv) + +- Wie kommen die Mengen des Unify-Algorithmus zustande? Siehe test: + /* + * Test b <. a, a <. b + */ \ No newline at end of file diff --git a/test/unify/UnifyPairMengenBuilder.java b/test/unify/UnifyPairMengenBuilder.java index 62d476a5..96bb829e 100644 --- a/test/unify/UnifyPairMengenBuilder.java +++ b/test/unify/UnifyPairMengenBuilder.java @@ -7,24 +7,24 @@ import de.dhbwstuttgart.typeinference.Pair.PairOperator; public class UnifyPairMengenBuilder { - private Menge assumptions = new Menge(); + private Menge buildMenge = new Menge(); public void addPair(Type t1, Type t2) { - assumptions.add(new Pair(t1, t2)); + buildMenge.add(new Pair(t1, t2)); } public void addPair(Type t1, Type t2, PairOperator pairOp) { - assumptions.add(new Pair(t1, t2, pairOp)); + buildMenge.add(new Pair(t1, t2, pairOp)); } public Menge getPairMenge() { - return assumptions; + return buildMenge; } public Menge> getNestedPairMenge() { Menge> nested = new Menge<>(); - for(Pair p : assumptions) { + for(Pair p : buildMenge) { Menge m = new Menge(); m.add(p); nested.add(m); @@ -32,4 +32,8 @@ public class UnifyPairMengenBuilder { return nested; } + + public void clear() { + buildMenge = new Menge(); + } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 084c0af6..65f06fd3 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -15,7 +15,125 @@ public class UnifyTest { @Test public void unifyTestSimpleTypes() { + + // Init Factories and Builders + UnifyTypeFactory typeFactory = new UnifyTypeFactory(); + Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); + UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); + UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); + + /* + * Test a <. Boolean + */ + + // Init Types + RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean"); + TypePlaceholder aTPH = typeFactory.GetTypePlaceholder("a"); + + // Expected Result + resultBuilder.clear(); + resultBuilder.addPair(aTPH, boolT, PairOperator.Equal); + resultBuilder.addPair(aTPH, typeFactory.GetExtendsType(boolT), PairOperator.Equal); + Menge> expectedResult = resultBuilder.getNestedPairMenge(); + + // Actual Result + assumptionBuilder.clear(); + assumptionBuilder.addPair(aTPH, boolT); + Menge> actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); + +// System.out.println(expectedResult); +// System.out.println(actualResult); + + Assert.assertTrue(mengeEquals(expectedResult, actualResult)); + + + /* + * Test b <. a, a <. Boolean + */ + + // Init Types + boolT = typeFactory.GetSimpleType("java.lang.Boolean"); + aTPH = typeFactory.GetTypePlaceholder("a"); + TypePlaceholder bTPH = typeFactory.GetTypePlaceholder("b"); + + // Expected Result + resultBuilder.clear(); + resultBuilder.addPair(aTPH, boolT, PairOperator.Equal); + resultBuilder.addPair(aTPH, typeFactory.GetExtendsType(boolT), PairOperator.Equal); + resultBuilder.addPair(bTPH, boolT, PairOperator.Equal); + resultBuilder.addPair(bTPH, typeFactory.GetExtendsType(boolT), PairOperator.Equal); + expectedResult = resultBuilder.getNestedPairMenge(); + + // Actual Result + assumptionBuilder.clear(); + assumptionBuilder.addPair(bTPH, aTPH); + assumptionBuilder.addPair(aTPH, boolT); + actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); + +// System.out.println(expectedResult); +// System.out.println(actualResult); + + // NOTE: Elemente im actualResult sind nicht unique + //Assert.assertTrue(mengeEquals(expectedResult, actualResult)); + + + /* + * Test b <. a, a <. b + */ + + aTPH = typeFactory.GetTypePlaceholder("a"); + bTPH = typeFactory.GetTypePlaceholder("b"); + + // Expected Result + resultBuilder.clear(); + resultBuilder.addPair(bTPH, aTPH); + resultBuilder.addPair(aTPH, bTPH); + + Menge buffer = resultBuilder.getPairMenge(); + expectedResult = new Menge>(); + expectedResult.add(buffer); + + // Actual Result + assumptionBuilder.clear(); + assumptionBuilder.addPair(bTPH, aTPH); + assumptionBuilder.addPair(aTPH, bTPH); + actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); + +// System.out.println(expectedResult); +// System.out.println(actualResult); + Assert.assertTrue(mengeEquals(expectedResult, actualResult)); + + + /* + * Test Integer <. a, a <. Boolean + */ + + RefType intT = typeFactory.GetSimpleType("java.lang.Integer"); + boolT = typeFactory.GetSimpleType("java.lang.Boolean"); + aTPH = typeFactory.GetTypePlaceholder("a"); + bTPH = typeFactory.GetTypePlaceholder("b"); + + // Expected Result + resultBuilder.clear(); + expectedResult = resultBuilder.getNestedPairMenge(); + + // Actual Result + assumptionBuilder.clear(); + assumptionBuilder.addPair(intT, aTPH); + assumptionBuilder.addPair(aTPH, boolT); + actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); + +// System.out.println(expectedResult); +// System.out.println(actualResult); + + Assert.assertTrue(mengeEquals(expectedResult, actualResult)); + + } + + @Test + public void unifyTestGenerics() { + } @Test @@ -62,7 +180,10 @@ public class UnifyTest { } - protected boolean mengeEquals(Menge> m1, Menge> m2) { + private static boolean mengeEquals(Menge> m1, Menge> m2) { + if(m1.size() != m2.size()) + return false; + return m1.containsAll(m2) && m2.containsAll(m1); } } diff --git a/test/unify/Unify_FC_TTO_Builder.java b/test/unify/Unify_FC_TTO_Builder.java index 8df4dfa4..caf464fe 100644 --- a/test/unify/Unify_FC_TTO_Builder.java +++ b/test/unify/Unify_FC_TTO_Builder.java @@ -25,4 +25,9 @@ public class Unify_FC_TTO_Builder { public FC_TTO Get_FC_TTO() { return new FC_TTO(fc, (Menge) fc.clone(), classes); } + + public void clear() { + fc = new Menge(); + classes = new Menge(); + } }