From 97483714e7fe525c89bad774a40450bc73255230 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Thu, 20 May 2021 11:46:50 +0200 Subject: [PATCH] Add example for lib function --- .../TestInferWildcardsSingleLib.java | 116 ++++++++++++++++++ .../TestClassWildcardsSingleLib.java | 13 +- 2 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 src/test/java/inferWildcards/TestInferWildcardsSingleLib.java diff --git a/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java b/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java new file mode 100644 index 00000000..74c4dc2e --- /dev/null +++ b/src/test/java/inferWildcards/TestInferWildcardsSingleLib.java @@ -0,0 +1,116 @@ +package inferWildcards; + +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils; +import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards; +import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class TestInferWildcardsSingleLib +{ + + private String resourceFilePath; + private String resourceDirPath; + + @Before + public void setup () { + resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards"; + resourceFilePath = resourceDirPath + "/TestClassWildcardsSingleLib.java"; + } + + private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException { + File[] files1 = { new File(resourceFilePath) }; + return new JavaTXCompiler(files1); + } + + private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException { + File[] files1 = { new File(resourceFilePath) }; + return new JavaTXCompilerWildcards(files1); + } + + private static Map generateTph ( + JavaTXCompiler javaTXCompiler) { + System.out.println("\nReplacements:"); + + return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler); + } + + @Test + public void testGenerateTph () throws ClassNotFoundException, IOException { + System.out.println("\n--------- Test Generate TPH --------------\n"); + + JavaTXCompiler javaTXCompiler = getStandardCompiler(); + Map generateTph = generateTph( + javaTXCompiler); + + System.out.println(generateTph); + + assertThat("Number of TPH is 2", 2, CoreMatchers.is(generateTph.size())); + } + + @Test + public void testGeneratedConstraints () throws ClassNotFoundException, IOException { + System.out.println("\n--------- Test Generate Constraints --------------\n"); + + JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); + Map tphMap = javaTXCompilerWildcards + .getTphMap(); + ConstraintSet generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap); + + System.out.println(generateConstraints); + + int size = 0; + for (Set> l : generateConstraints.getOderConstraints()) + for (Constraint c : l) + size += c.size(); + + assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size)); + } + + @Test + public void testCombinedConstraints () throws ClassNotFoundException, IOException { + System.out.println("\n--------- Test Combined Constraints --------------\n"); + + JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler(); + ConstraintSet constraints = javaTXCompilerWildcards.getConstraints(); + + System.out.println(constraints); + } + + @Test + public void testTypeInference () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + List typeInference = wildcardsCompiler.typeInference(); + + System.out.println(typeInference); + + assertThat("Type Inference Results containing the correct Wildcard results", typeInference, + CoreMatchers.anything()); + } + + @Test + public void testGenrationBytecode () throws ClassNotFoundException, IOException { + JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler(); + + wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC")); + + assertThat("Generation Succeeded", null, CoreMatchers.anything()); + } +} diff --git a/src/test/resources/inferWildcards/TestClassWildcardsSingleLib.java b/src/test/resources/inferWildcards/TestClassWildcardsSingleLib.java index 0cf2c938..6a9dc6dd 100644 --- a/src/test/resources/inferWildcards/TestClassWildcardsSingleLib.java +++ b/src/test/resources/inferWildcards/TestClassWildcardsSingleLib.java @@ -3,8 +3,13 @@ import java.util.List; class TestClassWildcardsLib { - public List merge (List l1, List l2) { - l2.forEach(s -> {if(!l1.contains(s)) l1.add(s);}); - return l2; - } + //public List merge (List l1, List l2) { +// l2.forEach(s -> {if(!l1.contains(s)) l1.add(s);}); +// return l2; +// } + + public List di(List dd){ + T t = dd.get(1); + return dd; + } } \ No newline at end of file