JavaCompilerCore/test/unify/RuleSetTest.java
2016-04-14 14:21:19 +02:00

697 lines
29 KiB
Java

package unify;
import java.util.Optional;
import java.util.Set;
import junit.framework.Assert;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.RuleSet;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.ReferenceType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
public class RuleSetTest {
@Test
public void testReduceUp() {
IRuleSet rules = new RuleSet();
TypeFactory tf = new TypeFactory();
/*
* Positive Tests
*/
UnifyPair reduce1 = new UnifyPair(tf.getSimpleType("type"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair reduce2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair expected1 = new UnifyPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair expected2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
Optional<UnifyPair> opt1 = rules.reduceUp(reduce1);
Optional<UnifyPair> opt2 = rules.reduceUp(reduce2);
Assert.assertTrue(opt1.isPresent());
Assert.assertTrue(opt2.isPresent());
Assert.assertEquals(opt1.get(), expected1);
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
UnifyPair noreduce1 = new UnifyPair(tf.getSuperType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair noreduce2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair noreduce3 = new UnifyPair(tf.getPlaceholderType("T"), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair noreduce4 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduceUp(noreduce1).isPresent());
Assert.assertFalse(rules.reduceUp(noreduce2).isPresent());
Assert.assertFalse(rules.reduceUp(noreduce3).isPresent());
Assert.assertFalse(rules.reduceUp(noreduce4).isPresent());
}
@Test
public void testReduceLow() {
IRuleSet rules = new RuleSet();
TypeFactory tf = new TypeFactory();
/*
* Positive Tests
*/
UnifyPair reduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair reduce2 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT);
UnifyPair expected1 = new UnifyPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair expected2 = new UnifyPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT);
Optional<UnifyPair> opt1 = rules.reduceLow(reduce1);
Optional<UnifyPair> opt2 = rules.reduceLow(reduce2);
Assert.assertTrue(opt1.isPresent());
Assert.assertTrue(opt2.isPresent());
Assert.assertEquals(opt1.get(), expected1);
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
UnifyPair noreduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair noreduce2 = new UnifyPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair noreduce3 = new UnifyPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair noreduce4 = new UnifyPair(tf.getExtendsType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduceLow(noreduce1).isPresent());
Assert.assertFalse(rules.reduceLow(noreduce2).isPresent());
Assert.assertFalse(rules.reduceLow(noreduce3).isPresent());
Assert.assertFalse(rules.reduceLow(noreduce4).isPresent());
}
@Test
public void testReduceUpLow() {
IRuleSet rules = new RuleSet();
TypeFactory tf = new TypeFactory();
/*
* Positive Tests
*/
UnifyPair reduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair reduce2 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSuperType(tf.getPlaceholderType("T")), PairOperator.SMALLERDOT);
UnifyPair expected1 = new UnifyPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair expected2 = new UnifyPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT);
Optional<UnifyPair> opt1 = rules.reduceUpLow(reduce1);
Optional<UnifyPair> opt2 = rules.reduceUpLow(reduce2);
Assert.assertTrue(opt1.isPresent());
Assert.assertTrue(opt2.isPresent());
Assert.assertEquals(opt1.get(), expected1);
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
UnifyPair noreduce1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair noreduce2 = new UnifyPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
UnifyPair noreduce3 = new UnifyPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
UnifyPair noreduce4 = new UnifyPair(tf.getExtendsType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduceUpLow(noreduce1).isPresent());
Assert.assertFalse(rules.reduceUpLow(noreduce2).isPresent());
Assert.assertFalse(rules.reduceUpLow(noreduce3).isPresent());
Assert.assertFalse(rules.reduceUpLow(noreduce4).isPresent());
}
@Test
public void testReduce1() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
/*
* Positive Tests
*/
// C<T1, T2, T3, T4>
ReferenceType c1 = tf.getSimpleType("C", "T1", "T2", "T3", "T4");
// D<T4, T1, T2, T3>
ReferenceType d1 = tf.getSimpleType("D", "T4", "T1", "T2", "T3");
ReferenceType buffer = tf.getSimpleType("Buffer");
//C<Int, ? extends Double, M, N>
ReferenceType c2 = tf.getSimpleType("C", tf.getSimpleType("Int"), tf.getExtendsType(tf.getSimpleType("Double")), tf.getPlaceholderType("M"), tf.getPlaceholderType("N"));
//D<? super HashSet<Int>, Number, Double, N>
ReferenceType d2 = tf.getSimpleType("D", tf.getSuperType(tf.getSimpleType("HashSet", "Int")), tf.getSimpleType("Number"), tf.getSimpleType("Double"), tf.getPlaceholderType("N"));
// C<...> < buffer < D<...>
fcb.add(c1, buffer);
fcb.add(buffer, d1);
IRuleSet rules = new RuleSet();
IFiniteClosure fc = fcb.getFiniteClosure();
UnifyPair pair = new UnifyPair(c2, d2, PairOperator.SMALLERDOT);
System.out.println("------ Reduce1 ------");
Optional<Set<UnifyPair>> res = rules.reduce1(pair, fc);
System.out.println(res);
pair = new UnifyPair(c2, c2, PairOperator.SMALLERDOT);
res = rules.reduce1(pair, fc);
System.out.println(res);
/*
* Negative Tests
*/
// Case 1: D <. C and C <* D
pair = new UnifyPair(d2, c2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.reduce1(pair, fc).isPresent());
// Case 2: D =. C
pair = new UnifyPair(c2, d2, PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduce1(pair, fc).isPresent());
// Case 3: C <. D and !(C <* D)
fcb.clear();
fc = fcb.getFiniteClosure();
rules = new RuleSet();
pair = new UnifyPair(c1, d1, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.reduce1(pair, fc).isPresent());
}
@Test
public void testReduce2() {
TypeFactory tf = new TypeFactory();
IRuleSet rules = new RuleSet();
/*
* Positive Tests
*/
System.out.println("----- Reduce2 ------");
// C<T1, SType1, ? extends SType1>
ReferenceType c1 = tf.getSimpleType("C", tf.getPlaceholderType("T1"), tf.getSimpleType("SType1"), tf.getExtendsType(tf.getSimpleType("SType1")));
// C<T2, SType2, ? extends SType2>
ReferenceType c2 = tf.getSimpleType("C", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2")));
UnifyPair pair1 = new UnifyPair(c1, c2, PairOperator.EQUALSDOT);
UnifyPair pair2 = new UnifyPair(tf.getExtendsType(c1), tf.getExtendsType(c2), PairOperator.EQUALSDOT);
UnifyPair pair3 = new UnifyPair(tf.getSuperType(c1), tf.getSuperType(c2), PairOperator.EQUALSDOT);
Optional<Set<UnifyPair>> opt1 = rules.reduce2(pair1);
System.out.println(opt1);
Optional<Set<UnifyPair>> opt2 = rules.reduce2(pair2);
System.out.println(opt2);
Optional<Set<UnifyPair>> opt3 = rules.reduce2(pair3);
System.out.println(opt3);
/*
* Negative Tests
*/
ReferenceType d1 = tf.getSimpleType("D", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2")));
pair1 = new UnifyPair(d1, c1, PairOperator.EQUALSDOT); // Case 1: D =. C
pair2 = new UnifyPair(tf.getExtendsType(c1), c2, PairOperator.EQUALSDOT); // Case 2: ? extends C =. C
pair3 = new UnifyPair(tf.getExtendsType(c1), tf.getSuperType(c2), PairOperator.EQUALSDOT); // Case 3: ? extends C =. ? super C
UnifyPair pair4 = new UnifyPair(c1, c2, PairOperator.SMALLERDOT); // Case 4: C <. C
Assert.assertFalse(rules.reduceEq(pair1).isPresent());
Assert.assertFalse(rules.reduceEq(pair2).isPresent());
Assert.assertFalse(rules.reduceEq(pair3).isPresent());
Assert.assertFalse(rules.reduceEq(pair4).isPresent());
}
@Test
public void testReduceExt() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
/*
* Positive Tests
*/
// X<T1, T2, T3, T4>
ReferenceType x1 = tf.getSimpleType("X", "T1", "T2", "T3", "T4");
// Y<T4, T1, T2, T3>
ReferenceType y1 = tf.getSimpleType("Y", "T4", "T1", "T2", "T3");
ReferenceType buffer = tf.getSimpleType("Buffer");
//X<Int, ? extends Double, M, N>
ReferenceType x2 = tf.getSimpleType("X", tf.getSimpleType("Int"), tf.getExtendsType(tf.getSimpleType("Double")), tf.getPlaceholderType("M"), tf.getPlaceholderType("N"));
//? extends Y<? super HashSet<Int>, Number, Double, N>
ExtendsType extY1 = tf.getExtendsType(tf.getSimpleType("Y", tf.getSuperType(tf.getSimpleType("HashSet", "Int")), tf.getSimpleType("Number"), tf.getSimpleType("Double"), tf.getPlaceholderType("N")));
// Y<...> < buffer < X<...>
fcb.add(x1, buffer);
fcb.add(buffer, y1);
IRuleSet rules = new RuleSet();
IFiniteClosure fc = fcb.getFiniteClosure();
UnifyPair pair1 = new UnifyPair(x2, extY1, PairOperator.SMALLERDOTWC);
UnifyPair pair2 = new UnifyPair(tf.getExtendsType(x2), extY1, PairOperator.SMALLERDOTWC);
UnifyPair pair3 = new UnifyPair(extY1, extY1, PairOperator.SMALLERDOTWC);
System.out.println("------ ReduceExt ------");
Optional<Set<UnifyPair>> opt1 = rules.reduceExt(pair1, fc);
System.out.println(opt1);
Optional<Set<UnifyPair>> opt2 = rules.reduceExt(pair2, fc);
System.out.println(opt2);
Optional<Set<UnifyPair>> opt3 = rules.reduceExt(pair3, fc);
System.out.println(opt3);
/*
* Negative Tests
*/
// Case 1: X <.? Y
pair1 = new UnifyPair(x2, extY1.getExtendedType(), PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceExt(pair1, fc).isPresent());
// Case 2: X =. ? super Y
pair2 = new UnifyPair(x2, extY1, PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduceExt(pair2, fc).isPresent());
// Case 3: ? extends Y <.? ? extends X
pair3 = new UnifyPair(extY1, tf.getExtendsType(x2), PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceExt(pair3, fc).isPresent());
// Case 4: X <. ? extends Y and ? extends Y not in grArg(X)
fcb.clear();
rules = new RuleSet();
fc = fcb.getFiniteClosure();
pair1 = new UnifyPair(x2, extY1, PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceExt(pair1, fc).isPresent());
}
@Test
public void testReduceSup() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
/*
* Positive Tests
*/
// X<T1, T2, T3, T4>
ReferenceType x1 = tf.getSimpleType("X", "T1", "T2", "T3", "T4");
// Y<T4, T1, T2, T3>
ReferenceType y1 = tf.getSimpleType("Y", "T4", "T1", "T2", "T3");
ReferenceType buffer = tf.getSimpleType("Buffer");
//X<Int, ? extends Double, M, N>
ReferenceType x2 = tf.getSimpleType("X", tf.getSimpleType("Int"), tf.getExtendsType(tf.getSimpleType("Double")), tf.getPlaceholderType("M"), tf.getPlaceholderType("N"));
//? super Y<? super HashSet<Int>, Number, Double, N>
SuperType supY1 = tf.getSuperType(tf.getSimpleType("Y", tf.getSuperType(tf.getSimpleType("HashSet", "Int")), tf.getSimpleType("Number"), tf.getSimpleType("Double"), tf.getPlaceholderType("N")));
// Y<...> < buffer < X<...>
fcb.add(y1, buffer);
fcb.add(buffer, x1);
IRuleSet rules = new RuleSet();
IFiniteClosure fc = fcb.getFiniteClosure();
UnifyPair pair1 = new UnifyPair(x2, supY1, PairOperator.SMALLERDOTWC);
UnifyPair pair2 = new UnifyPair(tf.getSuperType(x2), supY1, PairOperator.SMALLERDOTWC);
UnifyPair pair3 = new UnifyPair(supY1, supY1, PairOperator.SMALLERDOTWC);
System.out.println("------ ReduceSup ------");
Optional<Set<UnifyPair>> opt1 = rules.reduceSup(pair1, fc);
System.out.println(opt1);
Optional<Set<UnifyPair>> opt2 = rules.reduceSup(pair2, fc);
System.out.println(opt2);
Optional<Set<UnifyPair>> opt3 = rules.reduceSup(pair3, fc);
System.out.println(opt3);
/*
* Negative Tests
*/
// Case 1: X <.? Y
pair1 = new UnifyPair(x2, supY1.getSuperedType(), PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceSup(pair1, fc).isPresent());
// Case 2: X =. ? super Y
pair2 = new UnifyPair(x2, supY1, PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduceSup(pair2, fc).isPresent());
// Case 3: ? super Y <.? ? super X
pair3 = new UnifyPair(supY1, tf.getSuperType(x2), PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceSup(pair3, fc).isPresent());
// Case 4: X <. ? super Y and ? super Y not in grArg(X)
fcb.clear();
rules = new RuleSet();
fc = fcb.getFiniteClosure();
pair1 = new UnifyPair(x2, supY1, PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceSup(pair1, fc).isPresent());
}
@Test
public void testReduceEq() {
TypeFactory tf = new TypeFactory();
IRuleSet rules = new RuleSet();
/*
* Positive Tests
*/
System.out.println("----- ReduceEq ------");
// C<T1, SType1, ? extends SType1>
ReferenceType c1 = tf.getSimpleType("C", tf.getPlaceholderType("T1"), tf.getSimpleType("SType1"), tf.getExtendsType(tf.getSimpleType("SType1")));
// C<T2, SType2, ? extends SType2>
ReferenceType c2 = tf.getSimpleType("C", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2")));
UnifyPair pair = new UnifyPair(c1, c2, PairOperator.SMALLERDOTWC);
Optional<Set<UnifyPair>> res = rules.reduceEq(pair);
System.out.println(res);
/*
* Negative Tests
*/
// Case 1: D <.? C
ReferenceType d1 = tf.getSimpleType("D", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2")));
pair = new UnifyPair(d1, c1, PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceEq(pair).isPresent());
// Case 2: C <. C
pair = new UnifyPair(c1, c2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.reduceEq(pair).isPresent());
}
@Test
public void testErase1() {
TypeFactory tf = new TypeFactory();
IRuleSet rules = new RuleSet();
IFiniteClosure fc = new FiniteClosureBuilder().getCollectionExample();
/*
* Positive Tests
*/
UnifyPair erase1 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT);
UnifyPair erase2 = new UnifyPair(tf.getSimpleType("HashSet", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOT);
UnifyPair erase3 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT);
Assert.assertTrue(rules.erase1(erase1, fc));
Assert.assertTrue(rules.erase1(erase2, fc));
Assert.assertTrue(rules.erase1(erase3, fc));
/*
* Negative Tests
*/
UnifyPair noerase1 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT);
UnifyPair noerase2 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.EQUALSDOT);
UnifyPair noerase3 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("Collection"), PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.erase1(noerase1, fc));
Assert.assertFalse(rules.erase1(noerase2, fc));
Assert.assertFalse(rules.erase1(noerase3, fc));
}
@Test
public void testErase2() {
TypeFactory tf = new TypeFactory();
IRuleSet rules = new RuleSet();
IFiniteClosure fc = new FiniteClosureBuilder().getCollectionExample();
/*
* Positive Tests
*/
UnifyPair erase1 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getExtendsType(tf.getSimpleType("Collection")), PairOperator.SMALLERDOTWC);
UnifyPair erase2 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSuperType(tf.getSimpleType("HashSet", "T")), PairOperator.SMALLERDOTWC);
UnifyPair erase3 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC);
UnifyPair erase4 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("LinkedList", "T")), tf.getExtendsType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC);
UnifyPair erase5 = new UnifyPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("Stack", "T")), PairOperator.SMALLERDOTWC);
Assert.assertTrue(rules.erase2(erase1, fc));
Assert.assertTrue(rules.erase2(erase2, fc));
Assert.assertTrue(rules.erase2(erase3, fc));
Assert.assertTrue(rules.erase2(erase4, fc));
Assert.assertTrue(rules.erase2(erase5, fc));
/*
* Negative Tests
*/
UnifyPair noerase1 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOTWC);
UnifyPair noerase2 = new UnifyPair(tf.getSuperType(tf.getSimpleType("List", "T")), tf.getSimpleType("ArrayList", "T"), PairOperator.SMALLERDOTWC);
UnifyPair noerase3 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("List", "T")), tf.getSuperType(tf.getSimpleType("List", "T")), PairOperator.SMALLERDOTWC);
UnifyPair noerase4 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT);
Assert.assertFalse(rules.erase2(noerase1, fc));
Assert.assertFalse(rules.erase2(noerase2, fc));
Assert.assertFalse(rules.erase2(noerase3, fc));
Assert.assertFalse(rules.erase2(noerase4, fc));
}
@Test
public void testErase3() {
TypeFactory tf = new TypeFactory();
IRuleSet rules = new RuleSet();
/*
* Positive Tests
*/
UnifyPair erase1 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.EQUALSDOT);
UnifyPair erase2 = new UnifyPair(tf.getPlaceholderType("W"), tf.getPlaceholderType("W"), PairOperator.EQUALSDOT);
Assert.assertTrue(rules.erase3(erase1));
Assert.assertTrue(rules.erase3(erase2));
/*
* Negative Tests
*/
UnifyPair noerase1 = new UnifyPair(tf.getSimpleType("Collection"), tf.getSimpleType("List", "T"), PairOperator.EQUALSDOT);
UnifyPair noerase2 = new UnifyPair(tf.getSimpleType("List", "T"), tf.getSimpleType("List", "T"), PairOperator.SMALLERDOT);
Assert.assertFalse(rules.erase3(noerase1));
Assert.assertFalse(rules.erase3(noerase2));
}
@Test
public void testSwap() {
TypeFactory tf = new TypeFactory();
IRuleSet rules = new RuleSet();
/*
* Positive Tests
*/
UnifyPair swap1 = new UnifyPair(tf.getExtendsType(tf.getSimpleType("MyClass")), tf.getPlaceholderType("P"), PairOperator.EQUALSDOT);
UnifyPair swap2 = new UnifyPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT);
UnifyPair expected1 = new UnifyPair(tf.getPlaceholderType("P"), tf.getExtendsType(tf.getSimpleType("MyClass")), PairOperator.EQUALSDOT);
UnifyPair expected2 = new UnifyPair(tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT);
Optional<UnifyPair> opt1 = rules.swap(swap1);
Optional<UnifyPair> opt2 = rules.swap(swap2);
// swap((? extends MyClass =. P)) = (P =. MyClass)
Assert.assertEquals(opt1.get(), expected1);
// swap((MyClass =. X)) = (X =. MyClass)
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
UnifyPair noswap1 = new UnifyPair(tf.getPlaceholderType("Z"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT);
UnifyPair noswap2 = new UnifyPair(tf.getSimpleType("MyClass"), tf.getExtendsType(tf.getPlaceholderType("X")), PairOperator.EQUALSDOT);
UnifyPair noswap3 = new UnifyPair( tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT);
UnifyPair noswap4 = new UnifyPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.SMALLERDOT);
opt1 = rules.swap(noswap1);
opt2 = rules.swap(noswap2);
Optional<UnifyPair> opt3 = rules.swap(noswap3);
Optional<UnifyPair> opt4 = rules.swap(noswap4);
Assert.assertFalse(opt1.isPresent());
Assert.assertFalse(opt2.isPresent());
Assert.assertFalse(opt3.isPresent());
Assert.assertFalse(opt4.isPresent());
}
@Test
public void testAdapt() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
ReferenceType t1 = tf.getSimpleType("Type1", "T", "U");
ReferenceType t2 = tf.getSimpleType("Type2", "T");
ReferenceType t3 = tf.getSimpleType("Type3", tf.getPlaceholderType("T"), tf.getSimpleType("Integer"));
fcb.add(t1, t2);
fcb.add(t2, t3);
IRuleSet rules = new RuleSet();
IFiniteClosure fc = fcb.getFiniteClosure();
/*
* Positive Tests
*/
ReferenceType c1 = tf.getSimpleType("Type1", "String", "Double");
ReferenceType c2 = tf.getSimpleType("Type2", "Object");
ReferenceType c3 = tf.getSimpleType("Type3", "Object", "Number");
UnifyPair pair1 = new UnifyPair(c1, c2, PairOperator.SMALLERDOT);
UnifyPair pair2 = new UnifyPair(c2, c3, PairOperator.SMALLERDOT);
UnifyPair pair3 = new UnifyPair(c1, c3, PairOperator.SMALLERDOT);
System.out.println("------ Adapt ------");
System.out.println(rules.adapt(pair1, fc));
System.out.println(rules.adapt(pair2, fc));
System.out.println(rules.adapt(pair3, fc));
/*
* Negative Tests
*/
UnifyPair noAdapt1 = new UnifyPair(c2, c1, PairOperator.SMALLERDOT);
UnifyPair noAdapt2 = new UnifyPair(c1, c1, PairOperator.SMALLERDOT);
UnifyPair noAdapt3 = new UnifyPair(c1, c2, PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.adapt(noAdapt1, fc).isPresent());
Assert.assertFalse(rules.adapt(noAdapt2, fc).isPresent());
Assert.assertFalse(rules.adapt(noAdapt3, fc).isPresent());
}
@Test
public void testAdaptExt() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
ReferenceType t1 = tf.getSimpleType("Type1", "T", "U");
ReferenceType t2 = tf.getSimpleType("Type2", "T");
ReferenceType t3 = tf.getSimpleType("Type3", tf.getPlaceholderType("T"), tf.getSimpleType("Integer"));
ReferenceType t32 = tf.getSimpleType("Type3", "T", "U");
ReferenceType t4 = tf.getSimpleType("Object");
fcb.add(t1, t2);
fcb.add(t2, t3);
fcb.add(t32, t4);
IRuleSet rules = new RuleSet();
IFiniteClosure fc = fcb.getFiniteClosure();
/*
* Positive Tests
*/
ReferenceType c1 = tf.getSimpleType("Type1", "String", "Double");
ReferenceType c2 = tf.getSimpleType("Type2", "Object");
ReferenceType c3 = tf.getSimpleType("Type3", "Object", "Number");
ExtendsType extc1 = new ExtendsType(c1);
ExtendsType extc2 = new ExtendsType(c2);
ExtendsType extc3 = new ExtendsType(c3);
UnifyPair pair1 = new UnifyPair(c1, extc2, PairOperator.SMALLERDOTWC);
UnifyPair pair2 = new UnifyPair(c2, extc3, PairOperator.SMALLERDOTWC);
UnifyPair pair3 = new UnifyPair(c1, extc3, PairOperator.SMALLERDOTWC);
UnifyPair pair4 = new UnifyPair(extc1, extc2, PairOperator.SMALLERDOTWC);
UnifyPair pair5 = new UnifyPair(extc2, extc3, PairOperator.SMALLERDOTWC);
UnifyPair pair6 = new UnifyPair(extc1, extc3, PairOperator.SMALLERDOTWC);
UnifyPair pair7 = new UnifyPair(extc1, extc1, PairOperator.SMALLERDOTWC);
System.out.println("------ AdaptExt ------");
System.out.println(rules.adaptExt(pair1, fc));
System.out.println(rules.adaptExt(pair2, fc));
System.out.println(rules.adaptExt(pair3, fc));
System.out.println(rules.adaptExt(pair4, fc));
System.out.println(rules.adaptExt(pair5, fc));
System.out.println(rules.adaptExt(pair6, fc));
System.out.println(rules.adaptExt(pair7, fc));
/*
* Negative Tests
*/
UnifyPair noAdapt1 = new UnifyPair(extc2, extc1, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt2 = new UnifyPair(extc1, c2, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt3 = new UnifyPair(tf.getSuperType(c1), extc2, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt4 = new UnifyPair(extc3, extc1, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt5 = new UnifyPair(c1, extc2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.adaptExt(noAdapt1, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt2, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt3, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt4, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt5, fc).isPresent());
}
@Test
public void testAdaptSup() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
ReferenceType t1 = tf.getSimpleType("Type1", "T", "U");
ReferenceType t2 = tf.getSimpleType("Type2", "T");
ReferenceType t3 = tf.getSimpleType("Type3", tf.getPlaceholderType("T"), tf.getSimpleType("Integer"));
ReferenceType t32 = tf.getSimpleType("Type3", "T", "U");
ReferenceType t4 = tf.getSimpleType("Object");
fcb.add(t1, t2);
fcb.add(t2, t3);
fcb.add(t32, t4);
IRuleSet rules = new RuleSet();
IFiniteClosure fc = fcb.getFiniteClosure();
/*
* Positive Tests
*/
ReferenceType c1 = tf.getSimpleType("Type1", "String", "Double");
ReferenceType c2 = tf.getSimpleType("Type2", "Object");
ReferenceType c3 = tf.getSimpleType("Type3", "Object", "Number");
SuperType supc1 = new SuperType(c1);
SuperType supc2 = new SuperType(c2);
SuperType supc3 = new SuperType(c3);
UnifyPair pair1 = new UnifyPair(c2, supc1, PairOperator.SMALLERDOTWC);
UnifyPair pair2 = new UnifyPair(c3, supc2, PairOperator.SMALLERDOTWC);
UnifyPair pair3 = new UnifyPair(c3, supc1, PairOperator.SMALLERDOTWC);
UnifyPair pair4 = new UnifyPair(supc2, supc1, PairOperator.SMALLERDOTWC);
UnifyPair pair5 = new UnifyPair(supc3, supc2, PairOperator.SMALLERDOTWC);
UnifyPair pair6 = new UnifyPair(supc3, supc1, PairOperator.SMALLERDOTWC);
UnifyPair pair7 = new UnifyPair(supc1, supc1, PairOperator.SMALLERDOTWC);
System.out.println("------ AdaptSup ------");
System.out.println(rules.adaptSup(pair1, fc));
System.out.println(rules.adaptSup(pair2, fc));
System.out.println(rules.adaptSup(pair3, fc));
System.out.println(rules.adaptSup(pair4, fc));
System.out.println(rules.adaptSup(pair5, fc));
System.out.println(rules.adaptSup(pair6, fc));
System.out.println(rules.adaptSup(pair7, fc));
/*
* Negative Tests
*/
UnifyPair noAdapt1 = new UnifyPair(supc2, supc1, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt2 = new UnifyPair(supc1, c2, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt3 = new UnifyPair(tf.getExtendsType(c1), supc2, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt4 = new UnifyPair(supc3, supc1, PairOperator.SMALLERDOTWC);
UnifyPair noAdapt5 = new UnifyPair(c1, supc2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.adaptExt(noAdapt1, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt2, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt3, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt4, fc).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt5, fc).isPresent());
}
}