Fixed generation tests for windows.

parent 121c0db5
No preview for this file type
......@@ -22,18 +22,26 @@ package de.monticore.lang.monticar.cnnarch;
import de.monticore.ModelingLanguageFamily;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.monticar.cnnarch._ast.ASTCNNArchCompilationUnit;
import de.monticore.lang.monticar.cnnarch._symboltable.CNNArchCompilationUnitSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.CNNArchLanguage;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import org.junit.Assert;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import static de.monticore.lang.monticar.cnnarch.ParserTest.ENABLE_FAIL_QUICK;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Common methods for symboltable tests
*/
public class AbstractSymtabTest {
private static final String MODEL_PATH = "src/test/resources/";
......@@ -69,4 +77,66 @@ public class AbstractSymtabTest {
return comp;
}
public static void checkFilesAreEqual(Path generationPath, Path resultsPath, List<String> fileNames) {
for (String fileName : fileNames){
File genFile = new File(generationPath.toString() + "/" + fileName);
File fileTarget = new File(resultsPath.toString() + "/" + fileName);
assertTrue(areBothFilesEqual(genFile, fileTarget));
}
}
public static boolean areBothFilesEqual(File file1, File file2) {
if (!file1.exists()) {
Assert.fail("file does not exist: " + file1.getAbsolutePath());
return false;
}
if (!file2.exists()) {
Assert.fail("file does not exist: " + file2.getAbsolutePath());
return false;
}
List<String> lines1;
List<String> lines2;
try {
lines1 = Files.readAllLines(file1.toPath());
lines2 = Files.readAllLines(file2.toPath());
} catch (IOException e) {
e.printStackTrace();
Assert.fail("IO error: " + e.getMessage());
return false;
}
lines1 = discardEmptyLines(lines1);
lines2 = discardEmptyLines(lines2);
if (lines1.size() != lines2.size()) {
Assert.fail(
"files have different number of lines: "
+ file1.getAbsolutePath()
+ " has " + lines1
+ " lines and " + file2.getAbsolutePath() + " has " + lines2 + " lines"
);
return false;
}
int len = lines1.size();
for (int i = 0; i < len; i++) {
String l1 = lines1.get(i);
String l2 = lines2.get(i);
Assert.assertEquals("files differ in " + i + " line: "
+ file1.getAbsolutePath()
+ " has " + l1
+ " and " + file2.getAbsolutePath() + " has " + l2,
l1,
l2
);
}
return true;
}
private static List<String> discardEmptyLines(List<String> lines) {
return lines.stream()
.map(String::trim)
.filter(l -> !l.isEmpty())
.collect(Collectors.toList());
}
}
......@@ -20,32 +20,20 @@
*/
package de.monticore.lang.monticar.cnnarch;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.monticar.cnnarch._cocos.CNNArchCocos;
import de.monticore.lang.monticar.cnnarch._symboltable.CNNArchCompilationUnitSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.CNNArchLanguage;
import de.monticore.lang.monticar.cnnarch.generator.CNNArchGenerator;
import de.monticore.lang.monticar.cnnarch.generator.CNNArchGeneratorCli;
import de.monticore.symboltable.GlobalScope;
import de.se_rwth.commons.logging.Log;
import freemarker.template.TemplateException;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.Arrays;
import static de.monticore.lang.monticar.cnnarch.ParserTest.ENABLE_FAIL_QUICK;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
public class GenerationTest {
public class GenerationTest extends AbstractSymtabTest{
@Before
public void setUp() {
......@@ -54,77 +42,53 @@ public class GenerationTest {
Log.enableFailQuick(ENABLE_FAIL_QUICK);
}
private Map<String,String> generateStrings(String modelsDirPath, String modelName) throws IOException, TemplateException {
final ModelPath mp = new ModelPath(Paths.get(modelsDirPath));
GlobalScope scope = new GlobalScope(mp, new CNNArchLanguage());
CNNArchCompilationUnitSymbol compilationUnit = scope.<CNNArchCompilationUnitSymbol>
resolve(modelName, CNNArchCompilationUnitSymbol.KIND).get();
CNNArchCocos.checkAll(compilationUnit);
CNNArchGenerator gen = new CNNArchGenerator();
return gen.generateStrings(compilationUnit.getArchitecture());
}
private String readFileFromResources(String relativePath) throws IOException{
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(relativePath).getFile());
Scanner scanner = new Scanner(file);
scanner.useDelimiter("\\Z");
String content = scanner.next() + "\n";
scanner.close();
return content;
}
@Test
public void testCifar10Classifier() throws IOException, TemplateException {
Log.getFindings().clear();
Map<String,String> stringMap = generateStrings(
"src/test/resources/valid_tests",
"CifarClassifierNetwork");
String[] args = {"-m", "src/test/resources/valid_tests", "-r", "CifarClassifierNetwork", "-o", "./target/generated-sources-cnnarch/"};
CNNArchGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
assertEquals(stringMap.get("CNNCreator_CifarClassifierNetwork.py"),
readFileFromResources("target_code/CNNCreator_CifarClassifierNetwork.py"));
assertEquals(stringMap.get("CNNPredictor_CifarClassifierNetwork.h"),
readFileFromResources("target_code/CNNPredictor_CifarClassifierNetwork.h"));
assertEquals(stringMap.get("execute_CifarClassifierNetwork"),
readFileFromResources("target_code/execute_CifarClassifierNetwork"));
assertEquals(stringMap.get("CNNBufferFile.h"),
readFileFromResources("target_code/CNNBufferFile.h"));
checkFilesAreEqual(
Paths.get("./target/generated-sources-cnnarch"),
Paths.get("./src/test/resources/target_code"),
Arrays.asList(
"CNNCreator_CifarClassifierNetwork.py",
"CNNPredictor_CifarClassifierNetwork.h",
"execute_CifarClassifierNetwork",
"CNNBufferFile.h"));
}
@Test
public void testAlexnetGeneration() throws IOException, TemplateException {
Log.getFindings().clear();
Map<String,String> stringMap = generateStrings(
"src/test/resources/architectures",
"Alexnet");
String[] args = {"-m", "src/test/resources/architectures", "-r", "Alexnet", "-o", "./target/generated-sources-cnnarch/"};
CNNArchGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
assertEquals(stringMap.get("CNNCreator_Alexnet.py"),
readFileFromResources("target_code/CNNCreator_Alexnet.py"));
assertEquals(stringMap.get("CNNPredictor_Alexnet.h"),
readFileFromResources("target_code/CNNPredictor_Alexnet.h"));
assertEquals(stringMap.get("execute_Alexnet"),
readFileFromResources("target_code/execute_Alexnet"));
checkFilesAreEqual(
Paths.get("./target/generated-sources-cnnarch"),
Paths.get("./src/test/resources/target_code"),
Arrays.asList(
"CNNCreator_Alexnet.py",
"CNNPredictor_Alexnet.h",
"execute_Alexnet"));
}
@Test
public void testGeneratorVGG16() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/architectures", "-r", "VGG16"};
String[] args = {"-m", "src/test/resources/architectures", "-r", "VGG16", "-o", "./target/generated-sources-cnnarch/"};
CNNArchGeneratorCli.main(args);
Map<String,String> stringMap = generateStrings(
"src/test/resources/architectures",
"VGG16");
assertTrue(Log.getFindings().isEmpty());
assertEquals(stringMap.get("CNNCreator_VGG16.py"),
readFileFromResources("target_code/CNNCreator_VGG16.py"));
assertEquals(stringMap.get("CNNPredictor_VGG16.h"),
readFileFromResources("target_code/CNNPredictor_VGG16.h"));
assertEquals(stringMap.get("execute_VGG16"),
readFileFromResources("target_code/execute_VGG16"));
checkFilesAreEqual(
Paths.get("./target/generated-sources-cnnarch"),
Paths.get("./src/test/resources/target_code"),
Arrays.asList(
"CNNCreator_VGG16.py",
"CNNPredictor_VGG16.h",
"execute_VGG16"));
}
......@@ -132,7 +96,7 @@ public class GenerationTest {
public void testThreeInputCNNGeneration() throws IOException, TemplateException {
Log.getFindings().clear();
String[] args = {"-m", "src/test/resources/architectures", "-r", "ThreeInputCNN_M14"};
CNNArchGeneratorCli.main(args);;
CNNArchGeneratorCli.main(args);
assertTrue(Log.getFindings().size() == 1);
}
......@@ -140,7 +104,7 @@ public class GenerationTest {
public void testResNeXtGeneration() throws IOException, TemplateException {
Log.getFindings().clear();;
String[] args = {"-m", "src/test/resources/architectures", "-r", "ResNeXt50"};
CNNArchGeneratorCli.main(args);;
CNNArchGeneratorCli.main(args);
assertTrue(Log.getFindings().isEmpty());
}
......
......@@ -23,9 +23,8 @@ package de.monticore.lang.monticar.cnnarch.cocos;
import de.monticore.lang.monticar.cnnarch.AbstractSymtabTest;
import de.monticore.lang.monticar.cnnarch._ast.ASTCNNArchCompilationUnit;
import de.monticore.lang.monticar.cnnarch._cocos.CNNArchCoCoChecker;
import de.monticore.lang.monticar.cnnarch._cocos.CNNArchSymbolCoCoChecker;
import de.monticore.lang.monticar.cnnarch._cocos.CNNArchCocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch._cocos.CNNArchSymbolCoCoChecker;
import de.monticore.lang.monticar.cnnarch._symboltable.CNNArchCompilationUnitSymbol;
import de.se_rwth.commons.logging.Finding;
import de.se_rwth.commons.logging.Log;
......
......@@ -26,8 +26,6 @@ import de.se_rwth.commons.logging.Log;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static de.monticore.lang.monticar.cnnarch.ParserTest.ENABLE_FAIL_QUICK;
public class AllCoCoTest extends AbstractCoCoTest {
......
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