forked from JavaTX/JavaCompilerCore
isInSolvedForm(), Unit-Tests, removed PairOp.Equals
This commit is contained in:
parent
304a5693a6
commit
d89d06797e
@ -6,7 +6,6 @@ public class MPair {
|
||||
SMALLER,
|
||||
SMALLERDOT,
|
||||
SMALLERDOTWC,
|
||||
EQUALS,
|
||||
EQUALSDOT;
|
||||
|
||||
@Override
|
||||
@ -18,8 +17,6 @@ public class MPair {
|
||||
return "<.";
|
||||
case SMALLERDOTWC:
|
||||
return "<.?";
|
||||
case EQUALS:
|
||||
return "=";
|
||||
default:
|
||||
return "=.";
|
||||
}
|
||||
|
@ -56,32 +56,27 @@ public class Mapping {
|
||||
return result.size() == pairs.size() ? Optional.of(result) : Optional.empty();
|
||||
}
|
||||
|
||||
private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) {
|
||||
/*
|
||||
* TODO
|
||||
* Warum hat der PairOp nur drei Werte? Wie wird SMALLERDOTWC etc im anderen Pair geregelt?
|
||||
*/
|
||||
|
||||
private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) {
|
||||
switch(op) {
|
||||
case Equal:
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS;
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALSDOT;
|
||||
case Smaller:
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLER;
|
||||
case SmallerExtends:
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT;
|
||||
default:
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS;
|
||||
case SmallerExtends:
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOTWC;
|
||||
default:
|
||||
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALSDOT;
|
||||
}
|
||||
}
|
||||
|
||||
private de.dhbwstuttgart.typeinference.Pair.PairOperator unmapOp(de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator op) {
|
||||
switch(op) {
|
||||
case EQUALS:
|
||||
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
|
||||
case SMALLER:
|
||||
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller;
|
||||
case SMALLERDOT:
|
||||
return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends;
|
||||
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller;
|
||||
case SMALLERDOTWC:
|
||||
return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends;
|
||||
default:
|
||||
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ public class MartelliMontanariUnify implements IUnify {
|
||||
Type prev = iter.next();
|
||||
while(iter.hasNext()) {
|
||||
Type next = iter.next();
|
||||
termsQ.add(new MPair(prev, next, PairOperator.EQUALS));
|
||||
termsQ.add(new MPair(prev, next, PairOperator.EQUALSDOT));
|
||||
prev = next;
|
||||
}
|
||||
|
||||
|
@ -542,12 +542,12 @@ public class RuleSet implements IRuleSet{
|
||||
typeMap.put(t2, typeMap.get(t2)+1);
|
||||
}
|
||||
|
||||
ArrayList<MPair> result = new ArrayList<MPair>(pairs);
|
||||
|
||||
Queue<MPair> result1 = new LinkedList<MPair>(pairs);
|
||||
ArrayList<MPair> result = new ArrayList<MPair>();
|
||||
boolean applied = false;
|
||||
|
||||
for(int i = 0; i < result.size(); i++) {
|
||||
MPair pair = result.get(i);
|
||||
while(!result1.isEmpty()) {
|
||||
MPair pair = result1.poll();
|
||||
PlaceholderType lhsType = null;
|
||||
Type rhsType;
|
||||
|
||||
@ -560,8 +560,11 @@ public class RuleSet implements IRuleSet{
|
||||
&& !rhsType.getTypeParams().occurs(lhsType)) {
|
||||
Unifier uni = new Unifier(lhsType, rhsType);
|
||||
result = result.stream().map(uni::apply).collect(Collectors.toCollection(ArrayList::new));
|
||||
result1 = result1.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new));
|
||||
applied = true;
|
||||
}
|
||||
|
||||
result.add(pair);
|
||||
}
|
||||
|
||||
return applied ? Optional.of(new HashSet<>(result)) : Optional.empty();
|
||||
|
@ -12,9 +12,6 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.dhbwstuttgart.typeinference.Menge;
|
||||
import de.dhbwstuttgart.typeinference.Pair;
|
||||
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations;
|
||||
@ -68,7 +65,7 @@ public class Unify {
|
||||
}
|
||||
|
||||
// Add the set of [a =. Theta | (a=. Theta) in Eq2']
|
||||
Set<MPair> bufferSet = eq2s.stream()
|
||||
Set<MPair> bufferSet = eq2s.stream()
|
||||
.filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
@ -133,17 +130,31 @@ public class Unify {
|
||||
* b) Build the union over everything
|
||||
*/
|
||||
|
||||
for(Set<MPair> eqss : changed) {
|
||||
for(Set<MPair> eqss : changed)
|
||||
eqPrimePrimeSet.addAll(this.unify(eqss, fc));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Step 7: Filter empty sets;
|
||||
*/
|
||||
return eqPrimePrimeSet.stream().filter(x -> !x.isEmpty()).collect(Collectors.toCollection(HashSet::new));
|
||||
return eqPrimePrimeSet.stream().filter(x -> isSolvedForm(x)).collect(Collectors.toCollection(HashSet::new));
|
||||
|
||||
}
|
||||
|
||||
protected boolean isSolvedForm(Set<MPair> eqPrimePrime) {
|
||||
for(MPair pair : eqPrimePrime) {
|
||||
Type lhsType = pair.getLhsType();
|
||||
Type rhsType = pair.getRhsType();
|
||||
|
||||
if(!(lhsType instanceof PlaceholderType))
|
||||
return false;
|
||||
|
||||
if(pair.getPairOp() != PairOperator.EQUALSDOT && !(rhsType instanceof PlaceholderType))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Set<MPair> applyTypeUnificationRules(Set<MPair> eq, IFiniteClosure fc) {
|
||||
|
||||
/*
|
||||
@ -288,7 +299,8 @@ public class Unify {
|
||||
result.get(7).add(unifyCase8(lhsType, (PlaceholderType) rhsType, fc));
|
||||
}
|
||||
|
||||
return result.stream().filter(x -> x.size() > 0).collect(Collectors.toCollection(HashSet::new));
|
||||
return result.stream().map(x -> x.stream().filter(y -> y.size() > 0).collect(Collectors.toCollection(HashSet::new)))
|
||||
.filter(x -> x.size() > 0).collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
|
||||
protected Set<MPair> unifyCase1(PlaceholderType a, Type thetaPrime, IFiniteClosure fc) {
|
||||
|
@ -7,7 +7,9 @@ import org.junit.Test;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.Unify_FC_TTO_Builder;
|
||||
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.Menge;
|
||||
import de.dhbwstuttgart.typeinference.Pair;
|
||||
@ -17,287 +19,322 @@ import de.dhbwstuttgart.typeinference.unify.Unify;
|
||||
public class UnifyOldTest {
|
||||
|
||||
@Test
|
||||
public void unifyTestSimpleTypes() {
|
||||
public void unifyTest1() {
|
||||
// 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<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
||||
|
||||
// Actual Result
|
||||
assumptionBuilder.clear();
|
||||
assumptionBuilder.addPair(aTph, boolT);
|
||||
Menge<Menge<Pair>> 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<Pair> buffer = resultBuilder.getPairMenge();
|
||||
expectedResult = new Menge<Menge<Pair>>();
|
||||
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));
|
||||
TypePlaceholder a = typeFactory.GetTypePlaceholder("a");
|
||||
ExtendsWildcardType extA = typeFactory.GetExtendsType(a);
|
||||
TypePlaceholder b = typeFactory.GetTypePlaceholder("b");
|
||||
ExtendsWildcardType extB = typeFactory.GetExtendsType(b);
|
||||
RefType integer = typeFactory.GetSimpleType("Integer");
|
||||
SuperWildcardType supInt = typeFactory.GetSuperType(integer);
|
||||
RefType listsupint = typeFactory.GetSimpleType("List", supInt);
|
||||
RefType number = typeFactory.GetSimpleType("Number");
|
||||
RefType object = typeFactory.GetSimpleType("XObjectX");
|
||||
ExtendsWildcardType extNum = typeFactory.GetExtendsType(number);
|
||||
RefType intlist = typeFactory.GetSimpleType("List", integer);
|
||||
RefType alist = typeFactory.GetSimpleType("List", a);
|
||||
RefType extBlist = typeFactory.GetSimpleType("List", extB);
|
||||
RefType blist = typeFactory.GetSimpleType("List", b);
|
||||
RefType extNumlist = typeFactory.GetSimpleType("List", extNum);
|
||||
|
||||
fcBuilder.AddInheritance(number, object);
|
||||
fcBuilder.AddInheritance(integer, number);
|
||||
|
||||
|
||||
assumptionBuilder.addPair(alist, extBlist, PairOperator.Smaller);
|
||||
assumptionBuilder.addPair(blist, extNumlist, PairOperator.Smaller);
|
||||
|
||||
System.out.println(Unify.unify(assumptionBuilder.getPairMenge(),
|
||||
fcBuilder.Get_FC_TTO()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unifyTestGenerics() {
|
||||
|
||||
// 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 <. MyClass<T, F>
|
||||
*/
|
||||
|
||||
TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
|
||||
RefType myType = typeFactory.GetSimpleType("MyClass",
|
||||
typeFactory.GetTypePlaceholder("T"),
|
||||
typeFactory.GetTypePlaceholder("F"));
|
||||
|
||||
// Expected Result
|
||||
resultBuilder.clear();
|
||||
resultBuilder.addPair(aTph, myType, PairOperator.Equal);
|
||||
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(myType));
|
||||
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
||||
|
||||
// Actual Result
|
||||
assumptionBuilder.clear();
|
||||
assumptionBuilder.addPair(aTph, myType);
|
||||
Menge<Menge<Pair>> actualResult = Unify.unify(
|
||||
assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
|
||||
|
||||
// System.out.println(expectedResult);
|
||||
// System.out.println(actualResult);
|
||||
|
||||
Assert.assertTrue(mengeEquals(expectedResult, actualResult));
|
||||
|
||||
/*
|
||||
* Test List<List<T>> <. List<T>
|
||||
*/
|
||||
|
||||
TypePlaceholder tTph = typeFactory.GetTypePlaceholder("T");
|
||||
RefType list = typeFactory.GetSimpleType("List", tTph);
|
||||
RefType listlist = typeFactory.GetSimpleType("List", list);
|
||||
|
||||
// Expected Result
|
||||
resultBuilder.clear();
|
||||
resultBuilder.addPair(typeFactory.GetExtendsType(list), tTph,
|
||||
PairOperator.Equal);
|
||||
expectedResult = resultBuilder.getNestedPairMenge();
|
||||
|
||||
// Actual Result
|
||||
assumptionBuilder.clear();
|
||||
assumptionBuilder.addPair(listlist, list);
|
||||
actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
|
||||
fcBuilder.Get_FC_TTO());
|
||||
|
||||
System.out.println(expectedResult);
|
||||
System.out.println(actualResult);
|
||||
|
||||
Assert.assertTrue(mengeEquals(expectedResult, actualResult));
|
||||
|
||||
/*
|
||||
* Test List<T> <. List<List<T>>
|
||||
*/
|
||||
}
|
||||
|
||||
@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<Menge<Pair>> 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() {
|
||||
|
||||
}
|
||||
|
||||
private static boolean mengeEquals(Menge<Menge<Pair>> m1,
|
||||
Menge<Menge<Pair>> m2) {
|
||||
if (m1.size() != m2.size())
|
||||
return false;
|
||||
|
||||
return containsAll(m1, m2) && containsAll(m2, m1);
|
||||
}
|
||||
|
||||
private static boolean containsAll(Menge<Menge<Pair>> m1,
|
||||
Menge<Menge<Pair>> m2) {
|
||||
for (Menge<Pair> elem : m2)
|
||||
if (!contains(m1, elem))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean contains(Menge<Menge<Pair>> m1, Menge<Pair> m2) {
|
||||
for (Menge<Pair> elem : m1)
|
||||
if (mengePairEquals(elem, m2))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean mengePairEquals(Menge<Pair> m1, Menge<Pair> m2) {
|
||||
if (m1.size() != m2.size())
|
||||
return false;
|
||||
|
||||
return containsAllPair(m1, m2) && containsAllPair(m2, m1);
|
||||
}
|
||||
|
||||
private static boolean containsAllPair(Menge<Pair> m1, Menge<Pair> m2) {
|
||||
for (Pair elem : m1)
|
||||
if (contains(m2, elem))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean contains(Menge<Pair> m, Pair p) {
|
||||
for (Pair elem : m)
|
||||
if (pairEquals(elem, p))
|
||||
return true;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private static boolean pairEquals(Pair p1, Pair p2) {
|
||||
return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2))
|
||||
|| (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1));
|
||||
}
|
||||
//
|
||||
// @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<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
||||
//
|
||||
// // Actual Result
|
||||
// assumptionBuilder.clear();
|
||||
// assumptionBuilder.addPair(aTph, boolT);
|
||||
// Menge<Menge<Pair>> 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<Pair> buffer = resultBuilder.getPairMenge();
|
||||
// expectedResult = new Menge<Menge<Pair>>();
|
||||
// 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() {
|
||||
//
|
||||
// // 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 <. MyClass<T, F>
|
||||
// */
|
||||
//
|
||||
// TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
|
||||
// RefType myType = typeFactory.GetSimpleType("MyClass",
|
||||
// typeFactory.GetTypePlaceholder("T"),
|
||||
// typeFactory.GetTypePlaceholder("F"));
|
||||
//
|
||||
// // Expected Result
|
||||
// resultBuilder.clear();
|
||||
// resultBuilder.addPair(aTph, myType, PairOperator.Equal);
|
||||
// resultBuilder.addPair(aTph, typeFactory.GetExtendsType(myType));
|
||||
// Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
|
||||
//
|
||||
// // Actual Result
|
||||
// assumptionBuilder.clear();
|
||||
// assumptionBuilder.addPair(aTph, myType);
|
||||
// Menge<Menge<Pair>> actualResult = Unify.unify(
|
||||
// assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
|
||||
//
|
||||
// // System.out.println(expectedResult);
|
||||
// // System.out.println(actualResult);
|
||||
//
|
||||
// Assert.assertTrue(mengeEquals(expectedResult, actualResult));
|
||||
//
|
||||
// /*
|
||||
// * Test List<List<T>> <. List<T>
|
||||
// */
|
||||
//
|
||||
// TypePlaceholder tTph = typeFactory.GetTypePlaceholder("T");
|
||||
// RefType list = typeFactory.GetSimpleType("List", tTph);
|
||||
// RefType listlist = typeFactory.GetSimpleType("List", list);
|
||||
//
|
||||
// // Expected Result
|
||||
// resultBuilder.clear();
|
||||
// resultBuilder.addPair(typeFactory.GetExtendsType(list), tTph,
|
||||
// PairOperator.Equal);
|
||||
// expectedResult = resultBuilder.getNestedPairMenge();
|
||||
//
|
||||
// // Actual Result
|
||||
// assumptionBuilder.clear();
|
||||
// assumptionBuilder.addPair(listlist, list);
|
||||
// actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
|
||||
// fcBuilder.Get_FC_TTO());
|
||||
//
|
||||
// System.out.println(expectedResult);
|
||||
// System.out.println(actualResult);
|
||||
//
|
||||
// Assert.assertTrue(mengeEquals(expectedResult, actualResult));
|
||||
//
|
||||
// /*
|
||||
// * Test List<T> <. List<List<T>>
|
||||
// */
|
||||
// }
|
||||
//
|
||||
// @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<Menge<Pair>> 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() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private static boolean mengeEquals(Menge<Menge<Pair>> m1,
|
||||
// Menge<Menge<Pair>> m2) {
|
||||
// if (m1.size() != m2.size())
|
||||
// return false;
|
||||
//
|
||||
// return containsAll(m1, m2) && containsAll(m2, m1);
|
||||
// }
|
||||
//
|
||||
// private static boolean containsAll(Menge<Menge<Pair>> m1,
|
||||
// Menge<Menge<Pair>> m2) {
|
||||
// for (Menge<Pair> elem : m2)
|
||||
// if (!contains(m1, elem))
|
||||
// return false;
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// private static boolean contains(Menge<Menge<Pair>> m1, Menge<Pair> m2) {
|
||||
// for (Menge<Pair> elem : m1)
|
||||
// if (mengePairEquals(elem, m2))
|
||||
// return true;
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// private static boolean mengePairEquals(Menge<Pair> m1, Menge<Pair> m2) {
|
||||
// if (m1.size() != m2.size())
|
||||
// return false;
|
||||
//
|
||||
// return containsAllPair(m1, m2) && containsAllPair(m2, m1);
|
||||
// }
|
||||
//
|
||||
// private static boolean containsAllPair(Menge<Pair> m1, Menge<Pair> m2) {
|
||||
// for (Pair elem : m1)
|
||||
// if (contains(m2, elem))
|
||||
// return true;
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// private static boolean contains(Menge<Pair> m, Pair p) {
|
||||
// for (Pair elem : m)
|
||||
// if (pairEquals(elem, p))
|
||||
// return true;
|
||||
// return false;
|
||||
//
|
||||
// }
|
||||
//
|
||||
// private static boolean pairEquals(Pair p1, Pair p2) {
|
||||
// return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2))
|
||||
// || (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1));
|
||||
// }
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
@ -242,6 +241,100 @@ public class UnifyTest extends Unify {
|
||||
|
||||
@Test
|
||||
public void unifyTestSimple() {
|
||||
/*
|
||||
* INIT
|
||||
*/
|
||||
TypeFactory tf = new TypeFactory();
|
||||
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
|
||||
|
||||
Type number = tf.getSimpleType("Number");
|
||||
Type object = tf.getSimpleType("Object");
|
||||
Type integer = tf.getSimpleType("Integer");
|
||||
Type doubl = tf.getSimpleType("Double");
|
||||
|
||||
fcb.add(number, object);
|
||||
fcb.add(integer, number);
|
||||
fcb.add(doubl, number);
|
||||
|
||||
IFiniteClosure fc = fcb.getCollectionExample();
|
||||
|
||||
/*
|
||||
* Test 1:
|
||||
*
|
||||
* (Vector<a> <. Vector<? extends b>)
|
||||
* (List<b> <. List<? extends Number>)
|
||||
*/
|
||||
|
||||
Type tphA = tf.getPlaceholderType("a");
|
||||
Type tphB = tf.getPlaceholderType("b");
|
||||
Type extB = tf.getExtendsType(tphB);
|
||||
Type extNum = tf.getExtendsType(number);
|
||||
|
||||
Set<MPair> eq = new HashSet<MPair>();
|
||||
eq.add(new MPair(tf.getSimpleType("Vector", tphA), tf.getSimpleType("Vector", extB), PairOperator.SMALLERDOT));
|
||||
eq.add(new MPair(tf.getSimpleType("List", tphB), tf.getSimpleType("List", extNum), PairOperator.SMALLERDOT));
|
||||
|
||||
Set<Set<MPair>> expected = new HashSet<>();
|
||||
Set<Set<MPair>> actual = unify(eq, fc);
|
||||
|
||||
System.out.println(actual);
|
||||
//Assert.assertEquals(actual, expected);
|
||||
|
||||
/*
|
||||
* Test 2:
|
||||
*
|
||||
* Vector<? extends a> <. List<? extends Number>
|
||||
*
|
||||
*/
|
||||
|
||||
Type extA = tf.getExtendsType(tphA);
|
||||
|
||||
eq = new HashSet<MPair>();
|
||||
eq.add(new MPair(tf.getSimpleType("Vector", extA), tf.getSimpleType("Vector", extNum), PairOperator.SMALLERDOT));
|
||||
|
||||
expected = new HashSet<>();
|
||||
actual = unify(eq, fc);
|
||||
|
||||
System.out.println(actual);
|
||||
//Assert.assertEquals(actual, expected);
|
||||
|
||||
/*
|
||||
* Test 3:
|
||||
*
|
||||
* Vector<? extends Number> <. List<? extends a>
|
||||
*
|
||||
*/
|
||||
|
||||
eq = new HashSet<MPair>();
|
||||
eq.add(new MPair(tf.getSimpleType("Vector", extNum), tf.getSimpleType("Vector", extA), PairOperator.SMALLERDOT));
|
||||
|
||||
expected = new HashSet<>();
|
||||
actual = unify(eq, fc);
|
||||
|
||||
System.out.println(actual);
|
||||
//Assert.assertEquals(actual, expected);
|
||||
|
||||
/*
|
||||
* Test 4:
|
||||
*
|
||||
* LinkedList <. Deque <. Queue <. Collection
|
||||
*
|
||||
* Vector<Number> <. List<a>
|
||||
* List<a> <. AbstractList<b>
|
||||
* ? extends Number <.? b
|
||||
*/
|
||||
|
||||
eq = new HashSet<MPair>();
|
||||
eq.add(new MPair(tf.getSimpleType("LinkedList", number), tf.getSimpleType("Deque", tphA), PairOperator.SMALLERDOT));
|
||||
eq.add(new MPair(tf.getSimpleType("Deque", tphA), tf.getSimpleType("Queue", tphB), PairOperator.SMALLERDOT));
|
||||
eq.add(new MPair(extNum, tphB, PairOperator.SMALLERDOTWC));
|
||||
|
||||
expected = new HashSet<>();
|
||||
actual = unify(eq, fc);
|
||||
|
||||
System.out.println(actual);
|
||||
//Assert.assertEquals(actual, expected);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user