From 1b1fae6b13f76a524852cf23ffc037ae5c361c1b Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Sat, 7 Nov 2015 20:37:29 +0100 Subject: [PATCH] swap rule test --- .../typeinference/unifynew/Unify.java | 6 ++-- .../typinference/unify/model/TypeParams.java | 17 +++++++++ test/unify/FiniteClosureTest.java | 6 +++- test/unify/RuleSetTest.java | 36 +++++++++++++++++++ .../{UnifyTest.java => UnifyOldTest.java} | 2 +- 5 files changed, 62 insertions(+), 5 deletions(-) rename test/unify/{UnifyTest.java => UnifyOldTest.java} (96%) diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 9cfc16c4f..c347dd81a 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -62,7 +62,7 @@ public class Unify { throw new NotImplementedException(); } - private void splitEq(Set eq, Set eq1s, Set eq2s) { + protected void splitEq(Set eq, Set eq1s, Set eq2s) { for(MPair pair : eq) if(pair.getLhsType() instanceof PlaceholderType && pair.getRhsType() instanceof PlaceholderType) eq1s.add(pair); @@ -70,7 +70,7 @@ public class Unify { eq2s.add(pair); } - private Set applyTypeUnificationRules(Set eq, IFiniteClosure fc) { + protected Set applyTypeUnificationRules(Set eq, IFiniteClosure fc) { /* * Rule Application Strategy: @@ -133,7 +133,7 @@ public class Unify { return targetSet; } - private void swapAddOrErase(MPair pair, IRuleSet rules, Collection collection) { + protected void swapAddOrErase(MPair pair, IRuleSet rules, Collection collection) { Optional opt = rules.swap(pair); MPair pair2 = opt.isPresent() ? opt.get() : pair; diff --git a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java index bf2bdde92..c89b207d4 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java @@ -34,4 +34,21 @@ public class TypeParams implements Iterable{ public Iterator iterator() { return Arrays.stream(typeParams).iterator(); } + + @Override + public int hashCode() { + return Arrays.hashCode(typeParams); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof TypeParams)) + return false; + + TypeParams other = (TypeParams) obj; + + // TODO + return other.size() == this.size(); + } } + diff --git a/test/unify/FiniteClosureTest.java b/test/unify/FiniteClosureTest.java index 15fdf2c3a..d3744abea 100644 --- a/test/unify/FiniteClosureTest.java +++ b/test/unify/FiniteClosureTest.java @@ -1,8 +1,12 @@ package unify; +import java.util.HashSet; +import java.util.Set; + import org.junit.Test; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.Type; public class FiniteClosureTest { @@ -11,7 +15,7 @@ public class FiniteClosureTest { public void testGreater() { IFiniteClosure fc = new FiniteClosureBuilder().getCollectionExample(); TypeFactory tf = new TypeFactory(); - + System.out.println("\n\n----- Greater Test -----"); System.out.println("Greater(LinkedList) = " + fc.greater(tf.getSimpleType("LinkedList", "T"))); System.out.println("Greater(TreeSet) = " + fc.greater(tf.getSimpleType("TreeSet", "T"))); diff --git a/test/unify/RuleSetTest.java b/test/unify/RuleSetTest.java index e561dfee1..833cd3921 100644 --- a/test/unify/RuleSetTest.java +++ b/test/unify/RuleSetTest.java @@ -5,6 +5,8 @@ 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; @@ -149,7 +151,41 @@ public class RuleSetTest { @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 opt1 = rules.swap(swap1); + Optional 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 opt3 = rules.swap(noswap3); + Optional opt4 = rules.swap(noswap4); + + Assert.assertFalse(opt1.isPresent()); + Assert.assertFalse(opt2.isPresent()); + Assert.assertFalse(opt3.isPresent()); + Assert.assertFalse(opt4.isPresent()); } @Test diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyOldTest.java similarity index 96% rename from test/unify/UnifyTest.java rename to test/unify/UnifyOldTest.java index 93f0d9c09..85f5ffd52 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyOldTest.java @@ -14,7 +14,7 @@ import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.Pair.PairOperator; import de.dhbwstuttgart.typeinference.unify.Unify; -public class UnifyTest { +public class UnifyOldTest { @Test public void unifyTestSimpleTypes() {