390 lines
9.8 KiB
Java
390 lines
9.8 KiB
Java
package unify;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
import org.junit.Test;
|
|
|
|
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
|
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
|
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
|
|
import de.dhbwstuttgart.typeinference.unify.model.Type;
|
|
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
|
|
import de.dhbwstuttgart.typeinference.unifynew.TypeFactory;
|
|
import de.dhbwstuttgart.typeinference.unifynew.Unify;
|
|
import junit.framework.Assert;
|
|
|
|
public class UnifyTest extends Unify {
|
|
|
|
/**
|
|
* Testing the unification for cases with (n)one pair and without generics.
|
|
*/
|
|
@Test
|
|
public void unifyTestTrivial() {
|
|
/*
|
|
* INIT
|
|
*/
|
|
|
|
TypeFactory tf = new TypeFactory();
|
|
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
|
|
|
|
Type number = tf.getSimpleType("Number");
|
|
Type object = tf.getSimpleType("Object");
|
|
Type integer = tf.getSimpleType("Integer");
|
|
Type doubl = tf.getSimpleType("Double");
|
|
|
|
fcb.add(number, object);
|
|
fcb.add(integer, number);
|
|
fcb.add(doubl, number);
|
|
|
|
IFiniteClosure fc = fcb.getCollectionExample();
|
|
|
|
/*
|
|
* Test 1:
|
|
*
|
|
* unify({ }) = { }
|
|
*/
|
|
|
|
Set<MPair> eq = new HashSet<MPair>();
|
|
Set<Set<MPair>> expected = new HashSet<>();
|
|
expected.add(new HashSet<>());
|
|
Set<Set<MPair>> actual = unify(eq, fc);
|
|
Assert.assertEquals(expected, actual);
|
|
|
|
/*
|
|
* Test 2:
|
|
*
|
|
* (a <. Number)
|
|
*/
|
|
|
|
Type 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)
|
|
*/
|
|
|
|
Type supInteger = tf.getSuperType(integer);
|
|
Type supNumber = tf.getSuperType(number);
|
|
Type 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<>();
|
|
Type extNumber = tf.getExtendsType(number);
|
|
Type extObject = tf.getExtendsType(object);
|
|
Type 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));
|
|
|
|
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:
|
|
*
|
|
* (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)
|
|
*/
|
|
|
|
Type 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();
|
|
|
|
Type number = tf.getSimpleType("Number");
|
|
Type object = tf.getSimpleType("Object");
|
|
Type integer = tf.getSimpleType("Integer");
|
|
Type 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>)
|
|
*/
|
|
|
|
Type tphA = tf.getPlaceholderType("a");
|
|
Type tphB = tf.getPlaceholderType("b");
|
|
Type extB = tf.getExtendsType(tphB);
|
|
Type 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 2:
|
|
*
|
|
* Vector<? extends a> <. List<? extends Number>
|
|
*
|
|
*/
|
|
|
|
Type 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() {
|
|
|
|
}
|
|
|
|
@Test
|
|
public void applyTypeUnificationRulesTest() {
|
|
|
|
}
|
|
|
|
@Test
|
|
public void calculatePairSetsTest() {
|
|
|
|
}
|
|
|
|
@Test
|
|
public void permuteParamsTest() {
|
|
TypeFactory tf = new TypeFactory();
|
|
ArrayList<Set<Type>> candidates = new ArrayList<>();
|
|
|
|
Set<Type> p1 = new HashSet<>();
|
|
p1.add(tf.getPlaceholderType("p11"));
|
|
p1.add(tf.getExtendsType(tf.getSimpleType("p12")));
|
|
p1.add(tf.getSimpleType("p13"));
|
|
|
|
Set<Type> p2 = new HashSet<>();
|
|
p2.add(tf.getPlaceholderType("p21"));
|
|
p2.add(tf.getPlaceholderType("p22"));
|
|
|
|
Set<Type> p3 = new HashSet<>();
|
|
p3.add(tf.getSimpleType("p31", "T"));
|
|
p3.add(tf.getSimpleType("p32"));
|
|
|
|
candidates.add(p1);
|
|
candidates.add(p2);
|
|
candidates.add(p3);
|
|
|
|
Set<TypeParams> result = permuteParams(candidates);
|
|
|
|
|
|
System.out.println(result);
|
|
}
|
|
|
|
|
|
private void addAsSet(Set<Set<MPair>> addTo, MPair... mPairs) {
|
|
addTo.add(new HashSet<>(Arrays.stream(mPairs).collect(Collectors.toSet())));
|
|
}
|
|
}
|