reduceUp and reduceLow tests

This commit is contained in:
Florian Steurer 2015-11-08 17:02:25 +01:00
parent 4c6c77929f
commit b3514a8375
3 changed files with 67 additions and 24 deletions

View File

@ -101,9 +101,9 @@ public class Unify {
MPair pair = eqQueue.getFirst();
// ReduceUp, ReduceLow, ReduceUpLow
Optional<MPair> opt = rules.reduceUp(pair);
Optional<MPair> opt = rules.reduceUpLow(pair);
opt = opt.isPresent() ? opt : rules.reduceLow(pair);
opt = opt.isPresent() ? opt : rules.reduceUpLow(pair);
opt = opt.isPresent() ? opt : rules.reduceUp(pair);
// One of the rules has been applied
if(opt.isPresent()) {

View File

@ -58,9 +58,7 @@ public class MPair {
public boolean equals(Object obj) {
if(!(obj instanceof MPair))
return false;
if(obj.hashCode() != hashCode())
return false;
MPair other = (MPair) obj;
return other.getPairOp() == pairOp

View File

@ -23,31 +23,76 @@ public class RuleSetTest {
@Test
public void testReduceUp() {
Set<MPair> pairs = new HashSet<MPair>();
IRuleSet rules = new RuleSet(new FiniteClosureBuilder().getFiniteClosure());
TypeFactory tf = new TypeFactory();
/*
* Positive Tests
*/
SimpleType t1 = new SimpleType("Set");
SimpleType t2 = new SimpleType("HashSet");
SimpleType t3 = new SimpleType("Something");
MPair reduce1 = new MPair(tf.getSimpleType("type"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
MPair reduce2 = new MPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
MPair expected1 = new MPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
MPair expected2 = new MPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
SuperType s = new SuperType(t2);
Optional<MPair> opt1 = rules.reduceUp(reduce1);
Optional<MPair> opt2 = rules.reduceUp(reduce2);
pairs.add(new MPair(t1, t2, PairOperator.SMALLER));
Assert.assertTrue(opt1.isPresent());
Assert.assertTrue(opt2.isPresent());
Assert.assertEquals(opt1.get(), expected1);
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
FiniteClosure fc = new FiniteClosure(pairs);
MPair noreduce1 = new MPair(tf.getSuperType(tf.getSimpleType("type")), tf.getSuperType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
MPair noreduce2 = new MPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
MPair noreduce3 = new MPair(tf.getPlaceholderType("T"), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
MPair noreduce4 = new MPair(tf.getPlaceholderType("T"), tf.getSuperType(tf.getSimpleType("type")), PairOperator.EQUALSDOT);
IRuleSet rules = new RuleSet(fc);
MPair test = new MPair(t3, s, PairOperator.SMALLERDOT);
Optional<MPair> res = rules.reduceUp(test);
System.out.println(res);
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(new FiniteClosureBuilder().getFiniteClosure());
TypeFactory tf = new TypeFactory();
/*
* Positive Tests
*/
MPair reduce1 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
MPair reduce2 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT);
MPair expected1 = new MPair(tf.getSimpleType("type"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
MPair expected2 = new MPair(tf.getSimpleType("type"), tf.getPlaceholderType("T"), PairOperator.SMALLERDOT);
Optional<MPair> opt1 = rules.reduceLow(reduce1);
Optional<MPair> opt2 = rules.reduceLow(reduce2);
Assert.assertTrue(opt1.isPresent());
Assert.assertTrue(opt2.isPresent());
Assert.assertEquals(opt1.get(), expected1);
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
MPair noreduce1 = new MPair(tf.getExtendsType(tf.getSimpleType("type")), tf.getExtendsType(tf.getSimpleType("type")), PairOperator.SMALLERDOT);
MPair noreduce2 = new MPair(tf.getPlaceholderType("T"), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
MPair noreduce3 = new MPair(tf.getSuperType(tf.getPlaceholderType("T")), tf.getSimpleType("type"), PairOperator.SMALLERDOT);
MPair noreduce4 = new MPair(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
@ -137,7 +182,7 @@ public class RuleSetTest {
@Test
public void testErase1() {
TypeFactory tf = new TypeFactory();
RuleSet rules = new RuleSet(new FiniteClosureBuilder().getCollectionExample());
IRuleSet rules = new RuleSet(new FiniteClosureBuilder().getCollectionExample());
/*
* Positive Tests
@ -165,7 +210,7 @@ public class RuleSetTest {
@Test
public void testErase2() {
TypeFactory tf = new TypeFactory();
RuleSet rules = new RuleSet(new FiniteClosureBuilder().getCollectionExample());
IRuleSet rules = new RuleSet(new FiniteClosureBuilder().getCollectionExample());
/*
* Positive Tests
@ -200,7 +245,7 @@ public class RuleSetTest {
@Test
public void testErase3() {
TypeFactory tf = new TypeFactory();
RuleSet rules = new RuleSet(new FiniteClosureBuilder().getFiniteClosure());
IRuleSet rules = new RuleSet(new FiniteClosureBuilder().getFiniteClosure());
/*
* Positive Tests
@ -224,7 +269,7 @@ public class RuleSetTest {
@Test
public void testSwap() {
TypeFactory tf = new TypeFactory();
RuleSet rules = new RuleSet(new FiniteClosureBuilder().getFiniteClosure());
IRuleSet rules = new RuleSet(new FiniteClosureBuilder().getFiniteClosure());
/*
* Positive Tests