JavaPatternMatching/test/unify/UnifyTest.java

433 lines
11 KiB
Java
Raw Normal View History

2015-11-09 13:39:26 +00:00
package unify;
2016-03-14 13:46:10 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2015-11-23 00:03:01 +00:00
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
2015-11-23 00:03:01 +00:00
import org.junit.Test;
2016-03-24 10:57:17 +00:00
import de.dhbwstuttgart.typeinference.unify.Unify;
2015-11-23 00:03:01 +00:00
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
2015-12-26 23:29:23 +00:00
import de.dhbwstuttgart.typeinference.unify.model.MPair;
2016-04-01 16:21:51 +00:00
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
2016-03-17 15:54:43 +00:00
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
2016-03-14 13:46:10 +00:00
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
2016-04-01 16:21:51 +00:00
import junit.framework.Assert;
2015-11-09 13:39:26 +00:00
public class UnifyTest extends Unify {
2015-11-23 00:03:01 +00:00
/**
* Testing the unification for cases with (n)one pair and without generics.
*/
2015-11-23 00:03:01 +00:00
@Test
public void unifyTestTrivial() {
/*
* INIT
*/
2015-11-23 00:03:01 +00:00
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
2016-04-01 16:21:51 +00:00
UnifyType number = tf.getSimpleType("Number");
UnifyType object = tf.getSimpleType("Object");
UnifyType integer = tf.getSimpleType("Integer");
UnifyType doubl = tf.getSimpleType("Double");
fcb.add(number, object);
fcb.add(integer, number);
fcb.add(doubl, number);
IFiniteClosure fc = fcb.getCollectionExample();
/*
* Test 1:
*
* unify({ }) = { }
*/
2015-11-23 00:03:01 +00:00
Set<MPair> eq = new HashSet<MPair>();
Set<Set<MPair>> expected = new HashSet<>();
2016-04-02 09:29:31 +00:00
expected.add(new HashSet<>());
Set<Set<MPair>> actual = unify(eq, fc);
2016-04-02 09:29:31 +00:00
Assert.assertEquals(expected, actual);
2015-11-23 00:03:01 +00:00
/*
* Test 2:
*
* (a <. Number)
*/
2015-11-23 00:03:01 +00:00
2016-04-01 16:21:51 +00:00
UnifyType tphA = tf.getPlaceholderType("a");
eq = new HashSet<>();
eq.add(new MPair(tphA, number, PairOperator.SMALLERDOT));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, doubl, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 3:
*
* (Integer <. a)
*/
eq = new HashSet<>();
eq.add(new MPair(integer, tphA, PairOperator.SMALLERDOT));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, object, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 4:
*
* (a <.? Number)
*/
eq = new HashSet<>();
eq.add(new MPair(tphA, number, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 5:
*
* (a <.? ? super Integer)
*/
2016-04-01 16:21:51 +00:00
UnifyType supInteger = tf.getSuperType(integer);
UnifyType supNumber = tf.getSuperType(number);
UnifyType supObject = tf.getSuperType(object);
eq = new HashSet<>();
eq.add(new MPair(tphA, supInteger, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, object, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, supInteger, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, supNumber, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, supObject, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 6:
*
* (Number <.? a)
*
*/
eq = new HashSet<>();
2016-04-01 16:21:51 +00:00
UnifyType extNumber = tf.getExtendsType(number);
UnifyType extObject = tf.getExtendsType(object);
UnifyType supDouble = tf.getSuperType(doubl);
eq.add(new MPair(number, tphA, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, extNumber, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, extObject, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, supInteger, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, supDouble, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, supNumber, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 7:
*
* (? extends Number <.? a)
*/
eq = new HashSet<>();
eq.add(new MPair(extNumber, tphA, PairOperator.SMALLERDOTWC));
2015-11-23 00:03:01 +00:00
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, extNumber, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, extObject, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 8:
*
* (a <.? ? extends Number)
*/
UnifyType extInteger = tf.getExtendsType(integer);
UnifyType extDouble = tf.getExtendsType(doubl);
eq = new HashSet<>();
eq.add(new MPair(tphA, extNumber, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, extNumber, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, extInteger, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, extDouble, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, doubl, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, integer, PairOperator.EQUALSDOT));
addAsSet(expected, new MPair(tphA, number, PairOperator.EQUALSDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 8:
*
* (Integer <. Number)
*/
eq = new HashSet<>();
eq.add(new MPair(integer, number, PairOperator.SMALLERDOT));
expected = new HashSet<>();
actual = unify(eq, fc);
//Assert.assertEquals(expected, actual);
/*
* Test 9:
*
* (Integer <.? Number)
*/
eq = new HashSet<>();
eq.add(new MPair(integer, number, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
actual = unify(eq, fc);
//Assert.assertEquals(expected, actual);
/*
* Test 10:
*
* (a <. b)
*/
2016-04-01 16:21:51 +00:00
UnifyType tphB = tf.getPlaceholderType("b");
eq = new HashSet<>();
eq.add(new MPair(tphA, tphB, PairOperator.SMALLERDOT));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, tphB, PairOperator.SMALLERDOT));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
/*
* Test 11:
*
* (a <.? b)
*/
eq = new HashSet<>();
eq.add(new MPair(tphA, tphB, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
addAsSet(expected, new MPair(tphA, tphB, PairOperator.SMALLERDOTWC));
actual = unify(eq, fc);
Assert.assertEquals(expected, actual);
}
@Test
public void unifyTestSimple() {
/*
* INIT
*/
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
2016-04-01 16:21:51 +00:00
UnifyType number = tf.getSimpleType("Number");
UnifyType object = tf.getSimpleType("Object");
UnifyType integer = tf.getSimpleType("Integer");
UnifyType doubl = tf.getSimpleType("Double");
fcb.add(number, object);
fcb.add(integer, number);
fcb.add(doubl, number);
IFiniteClosure fc = fcb.getCollectionExample();
/*
* Test 1:
*
* (Vector<a> <. Vector<? extends b>)
* (List<b> <. List<? extends Number>)
*/
2016-04-01 16:21:51 +00:00
UnifyType tphA = tf.getPlaceholderType("a");
UnifyType tphB = tf.getPlaceholderType("b");
UnifyType extB = tf.getExtendsType(tphB);
UnifyType extNum = tf.getExtendsType(number);
Set<MPair> eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("Vector", tphA), tf.getSimpleType("Vector", extB), PairOperator.SMALLERDOT));
eq.add(new MPair(tf.getSimpleType("List", tphB), tf.getSimpleType("List", extNum), PairOperator.SMALLERDOT));
Set<Set<MPair>> expected = new HashSet<>();
Set<Set<MPair>> actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
/*
* Test 8:
*
* (a <.? ? sup b)
* (b = Number)
*/
UnifyType supB = tf.getSuperType(tphB);
eq = new HashSet<>();
eq.add(new MPair(tphA, supB, PairOperator.SMALLERDOTWC));
eq.add(new MPair(tphB, number, PairOperator.EQUALSDOT));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(expected, actual);
/*
* Test 2:
*
* Vector<? extends a> <. List<? extends Number>
*
*/
2016-04-01 16:21:51 +00:00
UnifyType extA = tf.getExtendsType(tphA);
eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("Vector", extA), tf.getSimpleType("Vector", extNum), PairOperator.SMALLERDOT));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
/*
* Test 3:
*
* Vector<? extends Number> <. List<? extends a>
*
*/
eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("Vector", extNum), tf.getSimpleType("Vector", extA), PairOperator.SMALLERDOT));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
/*
* Test 4:
*
* LinkedList <. Deque <. Queue <. Collection
*
* Vector<Number> <. List<a>
* List<a> <. AbstractList<b>
* ? extends Number <.? b
*/
eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("LinkedList", number), tf.getSimpleType("Deque", tphA), PairOperator.SMALLERDOT));
eq.add(new MPair(tf.getSimpleType("Deque", tphA), tf.getSimpleType("Queue", tphB), PairOperator.SMALLERDOT));
eq.add(new MPair(extNum, tphB, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
}
@Test
public void unifyTestComplex() {
2015-11-23 00:03:01 +00:00
}
@Test
public void applyTypeUnificationRulesTest() {
}
@Test
public void calculatePairSetsTest() {
}
2016-03-14 13:46:10 +00:00
@Test
public void permuteParamsTest() {
TypeFactory tf = new TypeFactory();
2016-03-17 15:54:43 +00:00
ArrayList<Set<UnifyType>> candidates = new ArrayList<>();
2016-03-14 13:46:10 +00:00
2016-03-17 15:54:43 +00:00
Set<UnifyType> p1 = new HashSet<>();
2016-03-14 13:46:10 +00:00
p1.add(tf.getPlaceholderType("p11"));
p1.add(tf.getExtendsType(tf.getSimpleType("p12")));
p1.add(tf.getSimpleType("p13"));
2016-03-17 15:54:43 +00:00
Set<UnifyType> p2 = new HashSet<>();
2016-03-14 13:46:10 +00:00
p2.add(tf.getPlaceholderType("p21"));
p2.add(tf.getPlaceholderType("p22"));
2016-03-17 15:54:43 +00:00
Set<UnifyType> p3 = new HashSet<>();
2016-03-14 13:46:10 +00:00
p3.add(tf.getSimpleType("p31", "T"));
p3.add(tf.getSimpleType("p32"));
candidates.add(p1);
candidates.add(p2);
candidates.add(p3);
2016-03-22 14:13:51 +00:00
Set<TypeParams> result = permuteParams(candidates);
2016-03-14 13:46:10 +00:00
2016-03-14 13:46:10 +00:00
System.out.println(result);
}
private void addAsSet(Set<Set<MPair>> addTo, MPair... mPairs) {
addTo.add(new HashSet<>(Arrays.stream(mPairs).collect(Collectors.toSet())));
}
2015-11-09 13:39:26 +00:00
}