reduce1 and reduceEq Tests

This commit is contained in:
Florian Steurer 2015-11-08 21:23:20 +01:00
parent e695429262
commit 777a9b5beb
3 changed files with 84 additions and 25 deletions

View File

@ -167,6 +167,10 @@ public class RuleSet implements IRuleSet{
return Optional.empty(); return Optional.empty();
Type rhsType = pair.getRhsType(); Type rhsType = pair.getRhsType();
if(!rhsType.getName().equals(lhsType.getName()))
return Optional.empty();
if(rhsType instanceof PlaceholderType || rhsType.getTypeParams().empty()) if(rhsType instanceof PlaceholderType || rhsType.getTypeParams().empty())
return Optional.empty(); return Optional.empty();

View File

@ -18,9 +18,11 @@ public class FiniteClosureBuilder {
} }
public IFiniteClosure getFiniteClosure() { public IFiniteClosure getFiniteClosure() {
FiniteClosure fc = new FiniteClosure(pairs); return new FiniteClosure(pairs);
}
public void clear() {
pairs = new HashSet<>(); pairs = new HashSet<>();
return fc;
} }
public IFiniteClosure getCollectionExample() { public IFiniteClosure getCollectionExample() {
@ -55,6 +57,8 @@ public class FiniteClosureBuilder {
add(arrayList, list); add(arrayList, list);
add(stack, vector); add(stack, vector);
return getFiniteClosure(); IFiniteClosure fc = getFiniteClosure();
clear();
return fc;
} }
} }

View File

@ -132,34 +132,56 @@ public class RuleSetTest {
@Test @Test
public void testReduce1() { public void testReduce1() {
Set<MPair> pairs = new HashSet<MPair>(); TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
SimpleType t1 = new SimpleType("t1"); /*
SimpleType t2 = new SimpleType("t2"); * Positive Tests
SimpleType t3 = new SimpleType("t3"); */
SimpleType t4 = new SimpleType("t4");
SimpleType t5 = new SimpleType("t5");
SimpleType t6 = new SimpleType("t6");
SimpleType t7 = new SimpleType("t7");
SimpleType t8 = new SimpleType("t8");
SimpleType t9 = new SimpleType("t9");
SimpleType ta = new SimpleType("a", t1, t2, t3); // C<T1, T2, T3, T4>
SimpleType taa = new SimpleType("a", t4, t5, t6); SimpleType c1 = tf.getSimpleType("C", "T1", "T2", "T3", "T4");
SimpleType tb = new SimpleType("b", t3, t1, t2); // D<T4, T1, T2, T3>
SimpleType tbb = new SimpleType("b", t7, t8, t9); SimpleType d1 = tf.getSimpleType("D", "T4", "T1", "T2", "T3");
SimpleType buffer = tf.getSimpleType("Buffer");
//C<Int, ? extends Double, M, N>
SimpleType 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>
SimpleType d2 = tf.getSimpleType("D", tf.getSuperType(tf.getSimpleType("HashSet", "Int")), tf.getSimpleType("Number"), tf.getSimpleType("Double"), tf.getPlaceholderType("N"));
pairs.add(new MPair(ta, tb, PairOperator.SMALLER)); // C<...> < buffer < D<...>
FiniteClosure fc = new FiniteClosure(pairs); fcb.add(c1, buffer);
fcb.add(buffer, d1);
IRuleSet rules = new RuleSet(fc); IRuleSet rules = new RuleSet(fcb.getFiniteClosure());
MPair pair = new MPair(c2, d2, PairOperator.SMALLERDOT);
MPair test = new MPair(taa, tbb, PairOperator.SMALLERDOT);
Optional<Set<MPair>> res = rules.reduce1(test);
System.out.println("------ Reduce1 ------");
Optional<Set<MPair>> res = rules.reduce1(pair);
System.out.println(res); System.out.println(res);
pair = new MPair(c2, c2, PairOperator.SMALLERDOT);
res = rules.reduce1(pair);
System.out.println(res);
/*
* Negative Tests
*/
// Case 1: D <. C and C <* D
pair = new MPair(d2, c2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.reduce1(pair).isPresent());
// Case 2: D =. C
pair = new MPair(c2, d2, PairOperator.EQUALSDOT);
Assert.assertFalse(rules.reduce1(pair).isPresent());
// Case 3: C <. D and !(C <* D)
fcb.clear();
rules = new RuleSet(fcb.getFiniteClosure());
pair = new MPair(c1, d1, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.reduce1(pair).isPresent());
} }
@Test @Test
@ -196,7 +218,7 @@ public class RuleSetTest {
Optional<Set<MPair>> res = rules.reduceExt(test); Optional<Set<MPair>> res = rules.reduceExt(test);
System.out.println(res); //System.out.println(res);
} }
@Test @Test
@ -206,7 +228,36 @@ public class RuleSetTest {
@Test @Test
public void testReduceEq() { public void testReduceEq() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
IRuleSet rules = new RuleSet(fcb.getFiniteClosure());
/*
* Positive Tests
*/
System.out.println("----- ReduceEq ------");
// C<T1, SType1, ? extends SType1>
SimpleType c1 = tf.getSimpleType("C", tf.getPlaceholderType("T1"), tf.getSimpleType("SType1"), tf.getExtendsType(tf.getSimpleType("SType1")));
// D<T2, SType2, ? extends SType2>
SimpleType c2 = tf.getSimpleType("C", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2")));
MPair pair = new MPair(c1, c2, PairOperator.SMALLERDOTWC);
Optional<Set<MPair>> res = rules.reduceEq(pair);
System.out.println(res);
/*
* Negative Tests
*/
// Case 1: D <.? C
SimpleType d1 = tf.getSimpleType("D", tf.getPlaceholderType("T2"), tf.getSimpleType("SType2"), tf.getExtendsType(tf.getSimpleType("SType2")));
pair = new MPair(d1, c1, PairOperator.SMALLERDOTWC);
Assert.assertFalse(rules.reduceEq(pair).isPresent());
// Case 2: C <. C
pair = new MPair(c1, c2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.reduceEq(pair).isPresent());
} }
@Test @Test