implemented equal for menge

This commit is contained in:
Florian Steurer 2015-10-19 11:45:49 +02:00
parent cfc6711897
commit edf1d817d2

View File

@ -28,47 +28,51 @@ public class UnifyTest {
// Init Types
RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean");
TypePlaceholder aTPH = typeFactory.GetTypePlaceholder("a");
TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
// Expected Result
resultBuilder.clear();
resultBuilder.addPair(aTPH, boolT, PairOperator.Equal);
resultBuilder.addPair(aTPH, typeFactory.GetExtendsType(boolT), PairOperator.Equal);
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());
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");
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);
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());
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);
@ -76,18 +80,17 @@ public class UnifyTest {
// 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");
aTph = typeFactory.GetTypePlaceholder("a");
bTph = typeFactory.GetTypePlaceholder("b");
// Expected Result
resultBuilder.clear();
resultBuilder.addPair(bTPH, aTPH);
resultBuilder.addPair(aTPH, bTPH);
resultBuilder.addPair(bTph, aTph);
resultBuilder.addPair(aTph, bTph);
Menge<Pair> buffer = resultBuilder.getPairMenge();
expectedResult = new Menge<Menge<Pair>>();
@ -95,24 +98,24 @@ public class UnifyTest {
// Actual Result
assumptionBuilder.clear();
assumptionBuilder.addPair(bTPH, aTPH);
assumptionBuilder.addPair(aTPH, bTPH);
actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
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");
aTph = typeFactory.GetTypePlaceholder("a");
bTph = typeFactory.GetTypePlaceholder("b");
// Expected Result
resultBuilder.clear();
@ -120,9 +123,10 @@ public class UnifyTest {
// Actual Result
assumptionBuilder.clear();
assumptionBuilder.addPair(intT, aTPH);
assumptionBuilder.addPair(aTPH, boolT);
actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
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);
@ -134,6 +138,66 @@ public class UnifyTest {
@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
@ -161,18 +225,22 @@ public class UnifyTest {
// Build expected result
resultBuilder.addPair(tphA, tBool, PairOperator.Equal);
resultBuilder.addPair(tphA, typeFactory.GetExtendsType(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);
resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tString),
PairOperator.Equal);
// Assert
Menge<Menge<Pair>> actualResult = Unify.unify(assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
Menge<Menge<Pair>> actualResult = Unify.unify(
assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
System.out.println(actualResult);
System.out.println("-------------------");
System.out.println(resultBuilder.getNestedPairMenge());
// System.out.println(actualResult);
// System.out.println("-------------------");
// System.out.println(resultBuilder.getNestedPairMenge());
Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(), actualResult));
Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(),
actualResult));
}
@Test
@ -180,10 +248,54 @@ public class UnifyTest {
}
private static boolean mengeEquals(Menge<Menge<Pair>> m1, Menge<Menge<Pair>> m2) {
private static boolean mengeEquals(Menge<Menge<Pair>> m1,
Menge<Menge<Pair>> m2) {
if (m1.size() != m2.size())
return false;
return m1.containsAll(m2) && m2.containsAll(m1);
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));
}
}