Commit 0158ff31 authored by Alexander David Hellwig's avatar Alexander David Hellwig

Merge branch 'StructStreamTest' into 'master'

StreamTesting for structs

See merge request !35
parents fa323882 38449b17
Pipeline #176753 passed with stages
in 34 minutes and 48 seconds
......@@ -10,13 +10,13 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-generator</artifactId>
<version>0.1.13-SNAPSHOT</version>
<version>0.1.14-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
<properties>
<Embedded-MontiArc-Math.version>0.1.10-SNAPSHOT</Embedded-MontiArc-Math.version>
<Embedded-MontiArc-Math.version>0.1.11-SNAPSHOT</Embedded-MontiArc-Math.version>
<!-- .. Libraries .................................................. -->
<guava.version>18.0</guava.version>
......
......@@ -8,6 +8,7 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc.StreamScanner;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel.EMAComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel.EMAPortSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.cmake.CMakeConfig;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
......@@ -26,6 +27,7 @@ import de.monticore.lang.monticar.streamunits._visitor.StreamUnitsVisitor;
import de.monticore.literals.literals._ast.ASTBooleanLiteral;
import de.monticore.numberunit._ast.ASTNumberWithUnit;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.types.references.TypeReference;
import de.se_rwth.commons.logging.Log;
import java.util.*;
......@@ -124,7 +126,7 @@ public final class TestsGeneratorCPP {
"add_custom_target(run_<name>_StreamTests ALL\n" +
" COMMAND <name>_StreamTests\n" +
" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})";
cmake.addCMakeCommandEnd(executeTestTplt.replace("<name>",compName));
cmake.addCMakeCommandEnd(executeTestTplt.replace("<name>", compName));
}
private String getExistingComponentNames() {
......@@ -230,37 +232,44 @@ public final class TestsGeneratorCPP {
}
private static List<ComponentCheckViewModel> getComponentPortChecks(EMAComponentSymbol cs, ComponentStreamUnitsSymbol stream) {
Map<EMAPortSymbol, ASTStream> port2NamedStream = getPort2NamedStream(cs, stream);
List<PortStreamTuple> port2NamedStream = getPort2NamedStream(cs, stream);
int streamLength = getStreamLengths(port2NamedStream, stream);
List<ComponentCheckViewModel> result = new ArrayList<>();
for (int i = 0; i < streamLength; i++) {
ComponentCheckViewModel vm = new ComponentCheckViewModel();
vm.setInputPortName2Value(new HashMap<>());
vm.setOutputPortName2Check(new HashMap<>());
for (Map.Entry<EMAPortSymbol, ASTStream> kv : port2NamedStream.entrySet()) {
ASTStreamInstruction nextInstruction = kv.getValue().getStreamInstructionList().get(i);
processInstruction(vm, nextInstruction, kv.getKey());
for (PortStreamTuple tuple : port2NamedStream) {
ASTStreamInstruction nextInstruction = tuple.getNamedStreamUnits().getStream().getStreamInstructionList().get(i);
processInstruction(vm, nextInstruction, tuple);
}
result.add(vm);
}
return result;
}
private static Map<EMAPortSymbol, ASTStream> getPort2NamedStream(EMAComponentSymbol cs, ComponentStreamUnitsSymbol stream) {
Map<EMAPortSymbol, ASTStream> port2NamedStream = new HashMap<>();
private static List<PortStreamTuple> getPort2NamedStream(EMAComponentSymbol cs, ComponentStreamUnitsSymbol stream) {
List<PortStreamTuple> port2NamedStream = new ArrayList<>();
for (EMAPortSymbol port : cs.getPortsList()) {
NamedStreamUnitsSymbol namedStreamForPort = stream.getNamedStream(port.getName()).orElse(null);
if (namedStreamForPort != null && namedStreamForPort.getAstNode().isPresent()) {
ASTNamedStreamUnits node = (ASTNamedStreamUnits) namedStreamForPort.getAstNode().get();
port2NamedStream.put(port, node.getStream());
// NamedStreamUnitsSymbol namedStreamForPort = stream.getNamedStream(port.getName()).orElse(null);
List<NamedStreamUnitsSymbol> namedStreamForPortList = stream.getNamedStreams();
for (NamedStreamUnitsSymbol namedStreamForPort : namedStreamForPortList) {
boolean matchingName = (namedStreamForPort.getName().startsWith(port.getName() + ".") || namedStreamForPort.getName().equals(port.getName()));
if (matchingName && namedStreamForPort.getAstNode().isPresent()) {
ASTNamedStreamUnits node = (ASTNamedStreamUnits) namedStreamForPort.getAstNode().get();
port2NamedStream.add(new PortStreamTuple(port, node));
}
}
}
return port2NamedStream;
}
private static int getStreamLengths(Map<EMAPortSymbol, ASTStream> port2NamedStream, ComponentStreamUnitsSymbol stream) {
private static int getStreamLengths(List<PortStreamTuple> port2NamedStream, ComponentStreamUnitsSymbol stream) {
int streamLength = -1;
for (ASTStream ns : port2NamedStream.values()) {
for (PortStreamTuple tuple : port2NamedStream) {
ASTStream ns = tuple.getNamedStreamUnits().getStream();
int l = ns.getStreamInstructionList().size();
if (streamLength == -1) {
streamLength = l;
......@@ -278,10 +287,17 @@ public final class TestsGeneratorCPP {
return streamLength;
}
private static void processInstruction(ComponentCheckViewModel vm, ASTStreamInstruction nextInstruction, EMAPortSymbol port) {
private static void processInstruction(ComponentCheckViewModel vm, ASTStreamInstruction nextInstruction, PortStreamTuple portStreamTuple) {
EMAPortSymbol port = portStreamTuple.getPort();
if (nextInstruction.getStreamValueOpt().isPresent()) {
ASTStreamValue sv = nextInstruction.getStreamValueOpt().get();
String portName = port.getName();
String portName;
ASTNamedStreamUnits namedStreamUnits = portStreamTuple.getNamedStreamUnits();
if(namedStreamUnits.isEmptyFieldQualifiers()){
portName = port.getName();
}else{
portName = port.getName() + "." + String.join(".", namedStreamUnits.getFieldQualifierList());
}
if (port.isIncoming()) {
processIncomingPort(vm, sv, portName);
} else {
......@@ -427,7 +443,7 @@ public final class TestsGeneratorCPP {
builder.append("NOT HANDLED VALUEPAIROPT!!!");
}
Log.debug("Result: " + builder.toString(), "TestGeneratorCPP");
result = RangeOutputPortCheck.from(builder.toString(), builder.toString(),true);
result = RangeOutputPortCheck.from(builder.toString(), builder.toString(), true);
}
handled = true;
}
......@@ -524,4 +540,23 @@ public final class TestsGeneratorCPP {
}
}
private static class PortStreamTuple {
private EMAPortSymbol port;
private ASTNamedStreamUnits namedStreamUnits;
public PortStreamTuple(EMAPortSymbol port, ASTNamedStreamUnits namedStreamUnits) {
this.port = port;
this.namedStreamUnits = namedStreamUnits;
}
public EMAPortSymbol getPort() {
return port;
}
public ASTNamedStreamUnits getNamedStreamUnits() {
return namedStreamUnits;
}
}
}
......@@ -4,6 +4,8 @@ package de.monticore.lang.monticar.generator;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
......@@ -12,10 +14,10 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
public class TestsGenTest extends AbstractSymtabTest {
......@@ -41,4 +43,57 @@ public class TestsGenTest extends AbstractSymtabTest {
// assertEquals(18, files.size());
assertEquals(13, files.size()); // TODO: check if 14 is correct here?
}
@Test
public void testSimpleStructComp() throws IOException {
TaggingResolver symTab = createSymTabAndTaggingResolver("src/test/resources");
EMAComponentInstanceSymbol componentSymbol = symTab.<EMAComponentInstanceSymbol>resolve(
"structs.simpleStructComp",
EMAComponentInstanceSymbol.KIND
).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setModelsDirPath(Paths.get("src/test/resources"));
generatorCPP.setGenerateTests(true);
generatorCPP.setGenerateCMake(true);
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/structs/simpleStructComp");
generatorCPP.setCheckModelDir(true);
Set<File> files = new HashSet<>(generatorCPP.generateFiles(symTab, componentSymbol, symTab));
Optional<File> testFileOpt = files.stream().filter(file -> file.getName().endsWith("structs_simpleStructComp_test.hpp")).findFirst();
assertTrue(testFileOpt.isPresent());
List<String> content = FileUtils.readLines(testFileOpt.get(), "UTF-8");
assertTrue(content.stream().anyMatch(line -> line.contains("component.in1.field = 1.0;")));
assertTrue(content.stream().anyMatch(line -> line.contains("component.in1.field = 2.0;")));
}
@Test
public void testNestedStructComp() throws IOException {
TaggingResolver symTab = createSymTabAndTaggingResolver("src/test/resources");
EMAComponentInstanceSymbol componentSymbol = symTab.<EMAComponentInstanceSymbol>resolve(
"structs.nestedStructComp",
EMAComponentInstanceSymbol.KIND
).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setModelsDirPath(Paths.get("src/test/resources"));
generatorCPP.setGenerateTests(true);
generatorCPP.setGenerateCMake(true);
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/structs/nestedStructComp");
generatorCPP.setCheckModelDir(true);
Set<File> files = new HashSet<>(generatorCPP.generateFiles(symTab, componentSymbol, symTab));
Optional<File> testFileOpt = files.stream().filter(file -> file.getName().endsWith("structs_nestedStructComp_test.hpp")).findFirst();
assertTrue(testFileOpt.isPresent());
List<String> content = FileUtils.readLines(testFileOpt.get(), "UTF-8");
assertTrue(content.stream().anyMatch(line -> line.contains("component.in1.simpleField = 1.0;")));
assertTrue(content.stream().anyMatch(line -> line.contains("component.in1.structField.field = 3.0;")));
assertTrue(content.stream().anyMatch(line -> line.contains("component.in1.simpleField = 2.0;")));
assertTrue(content.stream().anyMatch(line -> line.contains("component.in1.structField.field = 4.0;")));
}
}
package structs;
component NestedStructComp{
ports in NestedStruct in1,
out SimpleStruct out1;
implementation Math{
out1 = in1.structField;
}
}
\ No newline at end of file
package structs;
stream NestedStructTest for NestedStructComp{
in1.simpleField: 1 tick 2;
in1.structField.field: 3 tick 4;
out1.field: 3 tick 4;
}
\ No newline at end of file
package structs;
component SimpleStructComp{
ports in SimpleStruct in1,
out SimpleStruct out1;
implementation Math{
out1.field = 2 * in1.field;
}
}
\ No newline at end of file
package structs;
stream SimpleStructTest for SimpleStructComp{
in1.field: 1 tick 2;
out1.field: 2 tick 4;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment