JavaPatternMatching/test/unify/RuleSetTest.java
2015-11-07 20:37:29 +01:00

206 lines
5.4 KiB
Java

package unify;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import junit.framework.Assert;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
import de.dhbwstuttgart.typeinference.unifynew.RuleSet;
import de.dhbwstuttgart.typinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typinference.unify.model.FiniteClosure;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typinference.unify.model.SimpleType;
import de.dhbwstuttgart.typinference.unify.model.SuperType;
public class RuleSetTest {
@Test
public void testReduceUp() {
Set<MPair> pairs = new HashSet<MPair>();
SimpleType t1 = new SimpleType("Set");
SimpleType t2 = new SimpleType("HashSet");
SimpleType t3 = new SimpleType("Something");
SuperType s = new SuperType(t2);
pairs.add(new MPair(t1, t2, PairOperator.SMALLER));
FiniteClosure fc = new FiniteClosure(pairs);
IRuleSet rules = new RuleSet(fc);
MPair test = new MPair(t3, s, PairOperator.SMALLERDOT);
Optional<MPair> res = rules.reduceUp(test);
System.out.println(res);
}
@Test
public void testReduceLow() {
}
@Test
public void testReduceUpLow() {
}
@Test
public void testReduce1() {
Set<MPair> pairs = new HashSet<MPair>();
SimpleType t1 = new SimpleType("t1");
SimpleType t2 = new SimpleType("t2");
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);
SimpleType taa = new SimpleType("a", t4, t5, t6);
SimpleType tb = new SimpleType("b", t3, t1, t2);
SimpleType tbb = new SimpleType("b", t7, t8, t9);
pairs.add(new MPair(ta, tb, PairOperator.SMALLER));
FiniteClosure fc = new FiniteClosure(pairs);
IRuleSet rules = new RuleSet(fc);
MPair test = new MPair(taa, tbb, PairOperator.SMALLERDOT);
Optional<Set<MPair>> res = rules.reduce1(test);
System.out.println(res);
}
@Test
public void testReduce2() {
}
@Test
public void testReduceExt() {
Set<MPair> pairs = new HashSet<MPair>();
SimpleType t1 = new SimpleType("t1");
SimpleType t2 = new SimpleType("t2");
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);
SimpleType taa = new SimpleType("a", t4, t5, t6);
SimpleType tb = new SimpleType("b", t3, t1, t2);
SimpleType tbb = new SimpleType("b", t7, t8, t9);
pairs.add(new MPair(ta, tb, PairOperator.SMALLER));
FiniteClosure fc = new FiniteClosure(pairs);
IRuleSet rules = new RuleSet(fc);
MPair test = new MPair(taa, new ExtendsType(tbb), PairOperator.SMALLERDOTWC);
Optional<Set<MPair>> res = rules.reduceExt(test);
System.out.println(res);
}
@Test
public void testReduceSup() {
}
@Test
public void testReduceEq() {
}
@Test
public void testErase1() {
}
@Test
public void testErase2() {
}
@Test
public void testErase3() {
}
@Test
public void testSwap() {
TypeFactory tf = new TypeFactory();
RuleSet rules = new RuleSet(new FiniteClosureBuilder().getFiniteClosure());
/*
* Positive Tests
*/
MPair swap1 = new MPair(tf.getExtendsType(tf.getSimpleType("MyClass")), tf.getPlaceholderType("P"), PairOperator.EQUALSDOT);
MPair swap2 = new MPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT);
MPair expected1 = new MPair(tf.getPlaceholderType("P"), tf.getExtendsType(tf.getSimpleType("MyClass")), PairOperator.EQUALSDOT);
MPair expected2 = new MPair(tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT);
Optional<MPair> opt1 = rules.swap(swap1);
Optional<MPair> opt2 = rules.swap(swap2);
Assert.assertEquals(opt1.get(), expected1);
Assert.assertEquals(opt2.get(), expected2);
/*
* Negative Tests
*/
MPair noswap1 = new MPair(tf.getPlaceholderType("Z"), tf.getPlaceholderType("X"), PairOperator.EQUALSDOT);
MPair noswap2 = new MPair(tf.getSimpleType("MyClass"), tf.getExtendsType(tf.getPlaceholderType("X")), PairOperator.EQUALSDOT);
MPair noswap3 = new MPair( tf.getPlaceholderType("X"), tf.getSimpleType("MyClass"), PairOperator.EQUALSDOT);
MPair noswap4 = new MPair(tf.getSimpleType("MyClass"), tf.getPlaceholderType("X"), PairOperator.SMALLERDOT);
opt1 = rules.swap(noswap1);
opt2 = rules.swap(noswap2);
Optional<MPair> opt3 = rules.swap(noswap3);
Optional<MPair> opt4 = rules.swap(noswap4);
Assert.assertFalse(opt1.isPresent());
Assert.assertFalse(opt2.isPresent());
Assert.assertFalse(opt3.isPresent());
Assert.assertFalse(opt4.isPresent());
}
@Test
public void testAdapt() {
}
@Test
public void testAdaptExt() {
}
@Test
public void testAdaptSup() {
}
}