Browse Source

attempt to make parameterized tests

Casper Thule 4 years ago
parent
commit
a4fdabdf30

+ 151 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppAutoTest.xtend

@@ -0,0 +1,151 @@
+/*
+ * generated by Xtext 2.10.0
+ */
+package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests
+
+import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.CppGenerator
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
+import be.uantwerpen.ansymo.semanticadaptation.tests.AbstractSemanticAdaptationTest
+import be.uantwerpen.ansymo.semanticadaptation.tests.SemanticAdaptationInjectorProvider
+import com.google.inject.Inject
+import java.io.File
+import java.util.ArrayList
+import java.util.List
+import java.util.regex.Pattern
+import org.eclipse.emf.ecore.EObject
+import org.eclipse.emf.ecore.resource.ResourceSet
+import org.eclipse.xtext.generator.IGeneratorContext
+import org.eclipse.xtext.generator.InMemoryFileSystemAccess
+import org.eclipse.xtext.testing.InjectWith
+import org.eclipse.xtext.testing.util.ParseHelper
+import org.eclipse.xtext.testing.validation.ValidationTestHelper
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.Parameterized
+
+import java.util.Arrays
+import java.util.Collection
+import org.junit.runners.Parameterized.Parameters
+
+import static org.junit.Assert.assertTrue
+
+@RunWith(typeof(Parameterized))
+@InjectWith(SemanticAdaptationInjectorProvider)
+@Parameterized.UseParametersRunnerFactory(XtextParametersRunnerFactory)
+class CgCppAutoTest extends AbstractSemanticAdaptationTest {
+
+	new (List<File> files)
+	{
+		f = files;
+	}
+
+	@Inject extension ParseHelper<SemanticAdaptation>
+	@Inject extension  ValidationTestHelper
+
+	@Parameters(name = "{index}")
+	def static Collection<Object[]> data() {
+		val files = new ArrayList<List<File>>();
+		listf("test_input", files);
+		val test = new ArrayList();
+		test.add(files.get(0));
+		return Arrays.asList(test.toArray(), test.toArray());
+	}
+
+	def static void listf(String directoryName, List<List<File>> files) {
+		val File directory = new File(directoryName);
+		val filesInDir = new ArrayList<File>();
+		// get all the files from a directory
+		val File[] fList = directory.listFiles();
+		var boolean added = false;
+		for (File file : fList) {
+			if (file.isFile()) {
+				filesInDir.add(file);
+			} else if (file.isDirectory()) {
+				if (filesInDir.length > 0) {
+					added = true;
+					files.add(filesInDir);
+				}
+				listf(file.getAbsolutePath(), files);
+			}
+		}
+		if (!added) {
+			if (filesInDir.length > 0) {
+				files.add(filesInDir);
+			}
+		}
+	}
+
+	var List<File> f;
+
+	@Test def allSemanticAdaptations() {
+		System.out.println("fail");
+		assertTrue(false);
+		//__parseNoErrors(f);
+	}
+
+	def __parseNoErrors(List<File> files) {
+		val hdFile = files.get(0);
+		files.remove(0);
+		val tailFiles = files;
+		val model = __parse(hdFile, tailFiles)
+		__assertNoParseErrors(model, hdFile)
+
+		val fsa = new InMemoryFileSystemAccess()
+		val IGeneratorContext ctxt = null;
+		new CppGenerator().doGenerate(model.eResource, fsa, ctxt)
+
+		System.out.println(fsa.allFiles)
+	}
+
+	def __parse(File hdFile, Iterable<File> tailFiles) {
+		var prevModel = readFile(hdFile).parse;
+		for (File file : tailFiles) {
+			val model = __parse(file, prevModel.eResource.resourceSet);
+			prevModel = model;
+		}
+
+		return prevModel;
+	}
+
+	def __parse(String filename, ResourceSet resourceSetToUse) {
+
+		return readFile(filename).parse(resourceSetToUse)
+	}
+
+	def __parse(File file, ResourceSet resourceSetToUse) {
+
+		return readFile(file).parse(resourceSetToUse)
+	}
+
+	def __assertNoParseErrors(EObject root, File file) {
+		try {
+			root.assertNoErrors
+		} catch (AssertionError e) {
+			val p = Pattern.compile(".*, offset (?<offset>[0-9]+), length (?<length>[0-9]+)")
+			val code = readFile(file)
+			for (String line : e.message.split("\n")) {
+				val m = p.matcher(line)
+				m.matches()
+				val count = __occurrencesInString(code.subSequence(0, Integer.valueOf(m.group("offset"))).toString(),
+					"\n")
+				print(file.getName() + " at line " + (count + 1) + ": ")
+				println(line)
+			}
+			throw e
+		}
+	}
+
+	def __occurrencesInString(String str, String findstr) {
+		var lastIndex = 0
+		var count = 0
+		while (lastIndex != -1) {
+			lastIndex = str.indexOf(findstr, lastIndex)
+			if (lastIndex != -1) {
+				count++
+				lastIndex += findstr.length()
+			}
+		}
+		return count
+	}
+
+}

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppBasicTest.xtend

@@ -20,7 +20,7 @@ import org.eclipse.xtext.generator.InMemoryFileSystemAccess
 import org.eclipse.xtext.generator.IGeneratorContext
 import org.eclipse.emf.ecore.resource.ResourceSet
 
-@RunWith(XtextRunner)
+@RunWith(ParameterizedXtextRunner)
 @InjectWith(SemanticAdaptationInjectorProvider)
 class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 

+ 91 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppBasicTest2.xtend

@@ -0,0 +1,91 @@
+/*
+ * generated by Xtext 2.10.0
+ */
+package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests
+
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
+import be.uantwerpen.ansymo.semanticadaptation.tests.AbstractSemanticAdaptationTest
+import be.uantwerpen.ansymo.semanticadaptation.tests.SemanticAdaptationInjectorProvider
+import com.google.inject.Inject
+import java.util.regex.Pattern
+import org.eclipse.emf.ecore.EObject
+import org.eclipse.xtext.testing.InjectWith
+import org.eclipse.xtext.testing.XtextRunner
+import org.eclipse.xtext.testing.util.ParseHelper
+import org.eclipse.xtext.testing.validation.ValidationTestHelper
+import org.junit.Test
+import org.junit.runner.RunWith
+import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.CppGenerator
+import org.eclipse.xtext.generator.InMemoryFileSystemAccess
+import org.eclipse.xtext.generator.IGeneratorContext
+import org.eclipse.emf.ecore.resource.ResourceSet
+
+@RunWith(XtextRunner)
+@InjectWith(SemanticAdaptationInjectorProvider)
+class CgCppBasicTest2 extends AbstractSemanticAdaptationTest {
+
+	// @Inject CppGenerator underTest
+	@Inject extension ParseHelper<SemanticAdaptation>
+	@Inject extension  ValidationTestHelper
+
+	@Test def powerwindow_model_only() { __parseNoErrors('test_input/window/window_sa_canonical.BASE.sa') }
+
+	def __parseNoErrors(String filename) {
+		val model = __parse(filename)
+		__assertNoParseErrors(model, filename)
+
+		val fsa = new InMemoryFileSystemAccess()
+		val IGeneratorContext ctxt = null;
+		new CppGenerator().doGenerate(model.eResource, fsa, ctxt)
+
+		System.out.println(fsa.allFiles)
+	}
+
+	def __parseNoErrorsPrint(String filename) {
+		val root = __parse(filename)
+		print_ast(root)
+		__assertNoParseErrors(root, filename)
+	}
+
+	def __parse(String filename) {
+		val model = readFile(filename).parse
+		return model;
+	}
+
+	def __parse(String filename, ResourceSet resourceSetToUse) {
+
+		return readFile(filename).parse(resourceSetToUse)
+	}
+
+	def __assertNoParseErrors(EObject root, String filename) {
+		try {
+			root.assertNoErrors
+		} catch (AssertionError e) {
+			val p = Pattern.compile(".*, offset (?<offset>[0-9]+), length (?<length>[0-9]+)")
+			val code = readFile(filename)
+			for (String line : e.message.split("\n")) {
+				val m = p.matcher(line)
+				m.matches()
+				val count = __occurrencesInString(code.subSequence(0, Integer.valueOf(m.group("offset"))).toString(),
+					"\n")
+				print(filename + " at line " + (count + 1) + ": ")
+				println(line)
+			}
+			throw e
+		}
+	}
+
+	def __occurrencesInString(String str, String findstr) {
+		var lastIndex = 0
+		var count = 0
+		while (lastIndex != -1) {
+			lastIndex = str.indexOf(findstr, lastIndex)
+			if (lastIndex != -1) {
+				count++
+				lastIndex += findstr.length()
+			}
+		}
+		return count
+	}
+
+}

+ 43 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/ParameterizedXtextRunner.xtend

@@ -0,0 +1,43 @@
+package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests
+
+import org.eclipse.xtext.testing.XtextRunner
+import org.junit.runners.parameterized.TestWithParameters
+import org.junit.runners.model.InitializationError
+import java.util.List
+import org.junit.runners.model.Statement
+import org.junit.runner.notification.RunNotifier
+
+class ParameterizedXtextRunner extends XtextRunner {
+	val Object[] parameters;
+	val String annotatedName;
+	
+	new(TestWithParameters test) throws InitializationError {
+		super(test.testClass.javaClass)
+		parameters = test.parameters;
+		annotatedName = test.name;
+	}
+	
+	override protected getName() {
+		return super.name + annotatedName;
+	}
+
+	override protected createTest() throws Exception {
+		val object = testClass.onlyConstructor.newInstance(parameters)
+		val injectorProvider = getOrCreateInjectorProvider
+		if (injectorProvider != null) {
+			val injector = injectorProvider.injector
+			if (injector != null)
+				injector.injectMembers(object)
+		}
+		return object;
+	}
+	
+	override public void run(RunNotifier notifier){
+		super.run(notifier);
+	}
+
+	override protected void validateConstructor(List<Throwable> errors) {
+		validateOnlyOneConstructor(errors)
+	}
+	
+}

+ 11 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/XtextParametersRunnerFactory.xtend

@@ -0,0 +1,11 @@
+package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests
+
+import org.junit.runners.parameterized.ParametersRunnerFactory
+import org.junit.runners.parameterized.TestWithParameters
+import org.junit.runners.model.InitializationError
+
+class XtextParametersRunnerFactory implements ParametersRunnerFactory {
+	override createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError {
+		new ParameterizedXtextRunner(test)
+	}
+}

+ 13 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/window2/powerwindow_algebraic_loop_delay.sa

@@ -0,0 +1,13 @@
+import PowerWindowModel
+import Controller_SA
+
+module Algebraic_Loop_SA
+
+// this one could be joined with the one below, if we assume certain priorities in executing the rules
+semantic adaptation WindowSA window_sa
+at "./path/to/WindowSA.fmu"
+for fmu window
+out rules {
+	reaction_torque := -reaction_torque;
+	delay at reaction_torque and height;
+}

+ 23 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/window2/powerwindow_controller_delay.sa

@@ -0,0 +1,23 @@
+import PowerWindowModel
+
+module Controller_SA
+
+semantic adaptation ArmatureCurrentSA armature_current_sa
+at "./path/to/ArmatureCurrentSA.fmu"
+for fmu controller
+triggered by signal == true
+in rules with crossing absolute tolerance = 1e-8 and relative tolerance = 0.0001, no hold {
+	armature_current >! 5 -> obj_detected := true;
+	otherwise -> obj_detected := false;
+}
+
+semantic adaptation InOutSA in_out_sa
+at "./path/to/InOutSA.fmu"
+for fmu controller
+out rules with zero order hold {
+	"up" -> up := 1;
+	"stop" -> { up := 0; down := 0; };
+	"down" -> down := 1;
+	delay at up and down;
+}
+

+ 27 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/window2/powerwindow_model_only.sa

@@ -0,0 +1,27 @@
+module PowerWindowModel
+
+fmu Environment environment
+at "./path/to/Environment.fmu"
+output ports passenger_up, passenger_down, driver_up, driver_down
+
+fmu Controller controller
+at "./path/to/Controller.fmu"
+input ports obj_detected, passenger_up, passenger_down, driver_up, driver_down
+output ports up, down, stop
+
+fmu Power power
+at "./path/to/Power.fmu"
+input ports up, down, reaction_torque (N.m)
+output ports armature_current (mA), motor_speed (m/s)
+
+fmu Window window
+at "./path/to/Window.fmu"
+input ports motor_speed (m/s), reaction_force (N)
+output ports height (cm), reaction_torque (N.m)
+
+fmu Obstacle obstacle
+at "./path/to/Obstacle.fmu"
+input ports height (m)
+output ports reaction_force (N)
+
+power.armature_current -> controller.obj_detected

+ 4 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/AbstractSemanticAdaptationTest.xtend

@@ -41,6 +41,10 @@ abstract class AbstractSemanticAdaptationTest {
 	/**
 	 * reads a file from the permanent file system
 	 */
+	 def String readFile(File file){
+	 	return new FileReader(file).readLines.join("\n");
+	 }
+	 
 	def String readFile(String basefilename) {
 		val f = new File(basefilename)
 		//println("testing " + f.name)