2015-10-17 17:04:32 +00:00
|
|
|
package unify;
|
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
import junit.framework.Assert;
|
|
|
|
|
2015-10-17 17:04:32 +00:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
|
|
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
2015-10-18 10:51:07 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.Menge;
|
2015-10-17 17:04:32 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.Pair;
|
2015-10-18 13:13:28 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
|
2015-10-18 10:51:07 +00:00
|
|
|
import de.dhbwstuttgart.typeinference.unify.Unify;
|
2015-10-17 17:04:32 +00:00
|
|
|
|
|
|
|
public class UnifyTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void unifyTestSimpleTypes() {
|
2015-10-19 07:34:44 +00:00
|
|
|
|
|
|
|
// 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));
|
2015-10-18 13:13:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-19 07:34:44 +00:00
|
|
|
@Test
|
|
|
|
public void unifyTestGenerics() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
@Test
|
|
|
|
public void unifyTestInheritance() {
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
// 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();
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
// 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");
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
// Build inheritance hierachy
|
|
|
|
// Bool <. String <. Int
|
|
|
|
fcBuilder.AddInheritance(tBool, tString);
|
|
|
|
fcBuilder.AddInheritance(tString, tInt);
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
// Build Assumptions
|
|
|
|
assumptionBuilder.addPair(tphA, tString);
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
// 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);
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
// Assert
|
|
|
|
Menge<Menge<Pair>> actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
System.out.println(actualResult);
|
|
|
|
System.out.println("-------------------");
|
|
|
|
System.out.println(resultBuilder.getNestedPairMenge());
|
2015-10-18 10:51:07 +00:00
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(), actualResult));
|
2015-10-17 17:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2015-10-18 13:13:28 +00:00
|
|
|
public void unifyTestWildcards() {
|
2015-10-17 17:04:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-19 07:34:44 +00:00
|
|
|
private static boolean mengeEquals(Menge<Menge<Pair>> m1, Menge<Menge<Pair>> m2) {
|
|
|
|
if(m1.size() != m2.size())
|
|
|
|
return false;
|
|
|
|
|
2015-10-18 13:13:28 +00:00
|
|
|
return m1.containsAll(m2) && m2.containsAll(m1);
|
2015-10-17 17:04:32 +00:00
|
|
|
}
|
|
|
|
}
|