From 9e1f4172807ef832072e5c1a203615907fb72dd4 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sun, 18 Oct 2015 15:13:28 +0200 Subject: [PATCH] factories, builder and first unit test --- notizen/stf/Notes | 9 ++- test/unify/UnifyPairMengenBuilder.java | 35 +++++++++++ test/unify/UnifyTest.java | 81 +++++++++++++------------- test/unify/UnifyTypeFactory.java | 43 ++++++++++++++ test/unify/Unify_FC_TTO_Builder.java | 28 +++++++++ 5 files changed, 155 insertions(+), 41 deletions(-) create mode 100644 test/unify/UnifyPairMengenBuilder.java create mode 100644 test/unify/UnifyTypeFactory.java create mode 100644 test/unify/Unify_FC_TTO_Builder.java diff --git a/notizen/stf/Notes b/notizen/stf/Notes index 8293ed62..d424459f 100644 --- a/notizen/stf/Notes +++ b/notizen/stf/Notes @@ -1,4 +1,11 @@ - Was sind / wofür brauch man TTO und Classes - Unify löst auch zirkuläre Abhängigkeiten a <. b <. c <. a - Tests dazu? - - Prüfung im Builder? \ No newline at end of file + - Prüfung im Builder? + +- Unterschied Wildcard und FreshWildcard, ExtendsWildcard und FreshExtendsWildcard etc... + - FreshWildcard = TPH für Wildcards? + +- Warum ist result von unify = Menge> und nicht Menge + +- Menge Equals überarbeiten (Momentan Reihenfolgensensitiv) \ No newline at end of file diff --git a/test/unify/UnifyPairMengenBuilder.java b/test/unify/UnifyPairMengenBuilder.java new file mode 100644 index 00000000..62d476a5 --- /dev/null +++ b/test/unify/UnifyPairMengenBuilder.java @@ -0,0 +1,35 @@ +package unify; + +import de.dhbwstuttgart.syntaxtree.type.Type; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.Pair; +import de.dhbwstuttgart.typeinference.Pair.PairOperator; + +public class UnifyPairMengenBuilder { + + private Menge assumptions = new Menge(); + + public void addPair(Type t1, Type t2) { + assumptions.add(new Pair(t1, t2)); + } + + public void addPair(Type t1, Type t2, PairOperator pairOp) { + assumptions.add(new Pair(t1, t2, pairOp)); + } + + public Menge getPairMenge() { + return assumptions; + } + + public Menge> getNestedPairMenge() { + Menge> nested = new Menge<>(); + + for(Pair p : assumptions) { + Menge m = new Menge(); + m.add(p); + nested.add(m); + } + + return nested; + } +} diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 3f1aeafe..084c0af6 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -1,67 +1,68 @@ package unify; +import junit.framework.Assert; + import org.junit.Test; -import de.dhbwstuttgart.syntaxtree.Class; import de.dhbwstuttgart.syntaxtree.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Pair; -import de.dhbwstuttgart.typeinference.unify.FC_TTO; +import de.dhbwstuttgart.typeinference.Pair.PairOperator; import de.dhbwstuttgart.typeinference.unify.Unify; public class UnifyTest { @Test public void unifyTestSimpleTypes() { - - /* - * Input - */ - Type tph1 = TypePlaceholder.backdoorCreate("b"); - Type tph2 = TypePlaceholder.backdoorCreate("a"); - - RefType rt1 = new RefType("java.lang.Boolean", null, 0); - RefType rt2 = new RefType("java.lang.Integer", null, 0); - - Menge assumptions = new Menge(); - - // b <. Boolean <. a <. Integer - assumptions.add(new Pair(tph1, rt1)); - assumptions.add(new Pair(rt1, tph2)); - assumptions.add(new Pair(tph2, rt2)); - - /* - * FC TTO - */ - Menge fc = new Menge(); - // String <. Boolean <. Integer - fc.add(new Pair(new RefType("java.lang.String", null, 0), new RefType("java.lang.Boolean", null, 0))); - fc.add(new Pair(new RefType("java.lang.Boolean", null, 0), new RefType("java.lang.Integer", null, 0))); - - Menge tto = (Menge) fc.clone(); - - Menge classes = new Menge(); - - classes.add(new Class("java.lang.Boolean", 0)); - classes.add(new Class("java.lang.String", 0)); - classes.add(new Class("java.lang.Integer", 0)); - - // Was ist tto und classes? - FC_TTO fctto = new FC_TTO(fc, tto, null); - - System.out.println(Unify.unify(assumptions, fctto)); } @Test public void unifyTestInheritance() { + // 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(); + + // Init Types + RefType tBool = typeFactory.GetSimpleType("java.lang.Boolean"); + RefType tString = typeFactory.GetSimpleType("java.lang.String"); + RefType tInt = typeFactory.GetSimpleType("java.lang.Integer"); + TypePlaceholder tphA = typeFactory.GetTypePlaceholder("a"); + + // Build inheritance hierachy + // Bool <. String <. Int + fcBuilder.AddInheritance(tBool, tString); + fcBuilder.AddInheritance(tString, tInt); + + // Build Assumptions + assumptionBuilder.addPair(tphA, tString); + + // Build expected result + resultBuilder.addPair(tphA, tBool, PairOperator.Equal); + resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tBool), PairOperator.Equal); + resultBuilder.addPair(tphA, tString, PairOperator.Equal); + resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tString), PairOperator.Equal); + + // Assert + Menge> actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); + + System.out.println(actualResult); + System.out.println("-------------------"); + System.out.println(resultBuilder.getNestedPairMenge()); + + Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(), actualResult)); } @Test public void unifyTestWildcards() { } + + protected boolean mengeEquals(Menge> m1, Menge> m2) { + return m1.containsAll(m2) && m2.containsAll(m1); + } } diff --git a/test/unify/UnifyTypeFactory.java b/test/unify/UnifyTypeFactory.java new file mode 100644 index 00000000..39365f3e --- /dev/null +++ b/test/unify/UnifyTypeFactory.java @@ -0,0 +1,43 @@ +package unify; + +import de.dhbwstuttgart.syntaxtree.type.Type; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.WildcardType; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.ObjectType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.Pair; +import de.dhbwstuttgart.typeinference.Pair.PairOperator; + +public class UnifyTypeFactory { + + public RefType GetSimpleType(String name, Type... parameters) { + if(parameters.length == 0) + return new RefType(name, null, 0); + + Menge typeParams = new Menge(); + + for(Type t : parameters) + typeParams.add(t); + + return new RefType(name, typeParams, null, 0); + } + + public ExtendsWildcardType GetExtendsType(ObjectType extendedType) { + return new ExtendsWildcardType(extendedType); + } + + public SuperWildcardType GetSuperType(ObjectType superedType) { + return new SuperWildcardType(superedType); + } + + public WildcardType GetWildcardType() { + return new WildcardType(null, null, 0); + } + + public TypePlaceholder GetTypePlaceholder(String name) { + return TypePlaceholder.backdoorCreate(name); + } +} diff --git a/test/unify/Unify_FC_TTO_Builder.java b/test/unify/Unify_FC_TTO_Builder.java new file mode 100644 index 00000000..8df4dfa4 --- /dev/null +++ b/test/unify/Unify_FC_TTO_Builder.java @@ -0,0 +1,28 @@ +package unify; + +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.Pair; +import de.dhbwstuttgart.typeinference.unify.FC_TTO; +import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.Type; + +public class Unify_FC_TTO_Builder { + + private Menge fc = new Menge(); + private Menge classes = new Menge(); + + public void AddInheritance(Type t1, Type t2) { + if(t1 instanceof RefType) + classes.add(new Class(t1.get_Name(), t1.getOffset())); + + if(t2 instanceof RefType) + classes.add(new Class(t2.get_Name(), t2.getOffset())); + + fc.add(new Pair(t1, t2)); + } + + public FC_TTO Get_FC_TTO() { + return new FC_TTO(fc, (Menge) fc.clone(), classes); + } +}