From 8f8379701dcdd4607ec2a1a373fcbbbb8d472c9d Mon Sep 17 00:00:00 2001
From: jerray <jawher.jerray@eurecom.fr>
Date: Mon, 24 Jul 2023 17:51:20 +0200
Subject: [PATCH] New test in DiplodocusSimulatorTest

---
 .../DiploSimulatorCodeGenerator.java          |   6 +-
 .../DiplodocusSimulatorTest.java              | 141 ++++++++++++++++++
 2 files changed, 144 insertions(+), 3 deletions(-)

diff --git a/src/main/java/tmltranslator/tomappingsystemc2/DiploSimulatorCodeGenerator.java b/src/main/java/tmltranslator/tomappingsystemc2/DiploSimulatorCodeGenerator.java
index 9a74e7958b..3d8c16f99a 100644
--- a/src/main/java/tmltranslator/tomappingsystemc2/DiploSimulatorCodeGenerator.java
+++ b/src/main/java/tmltranslator/tomappingsystemc2/DiploSimulatorCodeGenerator.java
@@ -944,11 +944,11 @@ public class DiploSimulatorCodeGenerator implements IDiploSimulatorCodeGenerator
 
             // TraceManager.addDev("Explore bus from " + startNode.getName() + " to memory "
             // + memory.getName());
-
-            if (!exploreBuses(0, commNodes, path, startNode, memory, commElemToRoute)) {
+            List<HwCommunicationNode> pathStartNodeToMemory = new LinkedList<HwCommunicationNode>();
+            if (!exploreBuses(0, commNodes, pathStartNodeToMemory, startNode, memory, commElemToRoute)) {
                 TraceManager.addDev("1. NO route to " + memory.getName() + " found!!!!!!!!!!!!!!!!!!!!!!!!!!");
             }
-
+            path.addAll(pathStartNodeToMemory);
             path.add(memory);
             List<HwCommunicationNode> pathMemoryToDestNode = new LinkedList<HwCommunicationNode>();
             exploreBuses(0, commNodes2, pathMemoryToDestNode, memory, destNode, commElemToRoute);
diff --git a/ttool/src/test/java/tmltranslator/DiplodocusSimulatorTest.java b/ttool/src/test/java/tmltranslator/DiplodocusSimulatorTest.java
index b36f69f9d0..0bd95ec2e9 100644
--- a/ttool/src/test/java/tmltranslator/DiplodocusSimulatorTest.java
+++ b/ttool/src/test/java/tmltranslator/DiplodocusSimulatorTest.java
@@ -41,6 +41,12 @@ public class DiplodocusSimulatorTest extends AbstractTest {
     final String MODELS_DAEMON_RTNB = "testDaemon";
     final int [] DAEMON_RTNBP_1 = {12, 11, 2147483647, 0}; // 2147483647==-1
     final int [] DAEMON_RTNBP_2 = {19, 18, 2147483647, 0}; // 2147483647==-1
+
+    // model with paths allowing parallel transfers
+    private static final String SIM_KEYWORD_TIME_BEG = "Simulated time:";
+    private static final String SIM_KEYWORD_TIME_END = "time";
+    final String MODEL_PARALLEL_TRANSFERS = "testParallelTransfers";
+    final String SIMUL_END_TIME = "3";
     private String SIM_DIR;
 
     @BeforeClass
@@ -595,4 +601,139 @@ public class DiplodocusSimulatorTest extends AbstractTest {
         System.out.println("executing: maxvalue of " + s + " " + maxValue);
         assertTrue(DAEMON_RTNBP_2[3] == maxValue);
     }
+
+    @Test
+    public void testPathsWithParallelTransfers() throws Exception {
+        String s = MODEL_PARALLEL_TRANSFERS;
+        SIM_DIR = DIR_GEN + s + "/";
+        System.out.println("executing: checking syntax " + s);
+
+        // Load the TML
+        System.out.println("executing: loading " + s);
+        TMLMappingTextSpecification tmts = new TMLMappingTextSpecification(s);
+        File f = new File(RESOURCES_DIR + s + ".tmap");
+        System.out.println("executing: new file loaded " + s);
+        String spec = null;
+        try {
+            spec = FileUtils.loadFileData(f);
+        } catch (Exception e) {
+            System.out.println("Exception executing: loading " + s);
+            assertTrue(false);
+        }
+        System.out.println("executing: testing spec " + s);
+        assertTrue(spec != null);
+        System.out.println("executing: testing parsed " + s);
+        boolean parsed = tmts.makeTMLMapping(spec, RESOURCES_DIR);
+        assertTrue(parsed);
+        System.out.println("executing: checking syntax " + s);
+
+        // Checking syntax
+        TMLMapping tmap = tmts.getTMLMapping();
+        TMLSyntaxChecking syntax = new TMLSyntaxChecking(tmap);
+        syntax.checkSyntax();
+        assertTrue(syntax.hasErrors() == 0);
+
+        // Generate C code
+        System.out.println("executing: sim code gen for " + s);
+        final IDiploSimulatorCodeGenerator tml2systc;
+        List<EBRDD> al = new ArrayList<EBRDD>();
+        List<TEPE> alTepe = new ArrayList<TEPE>();
+        tml2systc = DiploSimulatorFactory.INSTANCE.createCodeGenerator(tmap, al, alTepe);
+        tml2systc.setModelName(s);
+        String error = tml2systc.generateSystemC(false, true);
+        assertTrue(error == null);
+        File directory = new File(SIM_DIR);
+        if (! directory.exists()){
+            directory.mkdirs();
+        }
+
+        // Putting sim files
+        System.out.println("SIM executing: sim lib code copying for " + s);
+        ConfigurationTTool.SystemCCodeDirectory = getBaseResourcesDir() +  "../../../../simulators/c++2/";
+        boolean simFiles = SpecConfigTTool.checkAndCreateSystemCDir(SIM_DIR);
+        System.out.println("SIM executing: sim lib code copying done with result " + simFiles);
+        assertTrue(simFiles);
+        System.out.println("SIM Saving file in: " + SIM_DIR);
+        tml2systc.saveFile(SIM_DIR, "appmodel");
+
+        // Compile it
+        System.out.println("executing: compile");
+        Process proc;
+        BufferedReader proc_in;
+        String str;
+        boolean mustRecompileAll;
+        Penalties penalty = new Penalties(SIM_DIR + File.separator + "src_simulator");
+        int changed = penalty.handlePenalties(false);
+        if (changed == 1) {
+            mustRecompileAll = true;
+        } else {
+            mustRecompileAll = false;
+        }
+
+        if (mustRecompileAll) {
+            System.out.println("executing: " + "make -C " + SIM_DIR + " clean");
+            try {
+                proc = Runtime.getRuntime().exec("make -C " + SIM_DIR + " clean");
+                proc_in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+                while ((str = proc_in.readLine()) != null) {
+                    System.out.println("executing: " + str);
+                }
+            } catch (Exception e) {
+                // probably make is not installed
+                System.out.println("FAILED: executing: " + "make -C " + SIM_DIR + " clean");
+                return;
+            }
+        }
+
+        System.out.println("executing: " + "make -C " + SIM_DIR);
+        try {
+            proc = Runtime.getRuntime().exec("make -C " + SIM_DIR + "");
+            proc_in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+            monitorError(proc);
+            while ((str = proc_in.readLine()) != null) {
+                System.out.println("executing: " + str);
+            }
+        } catch (Exception e) {
+            // Probably make is not installed
+            System.out.println("FAILED: executing: " + "make -C " + SIM_DIR);
+            return;
+        }
+        System.out.println("SUCCESS: executing: " + "make -C " + SIM_DIR);
+
+        // Run the simulator
+        String graphPath = SIM_DIR + "testgraph_" + s;
+        try {
+
+            String[] params = new String[3];
+
+            params[0] = "./" + SIM_DIR + "run.x";
+            params[1] = "-cmd";
+            params[2] = "1 0; 1 7 100 100 " + graphPath;
+            proc = Runtime.getRuntime().exec(params);
+            //proc = Runtime.getRuntime().exec("./" + SIM_DIR + "run.x -explo -gname testgraph_" + s);
+            proc_in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+
+            monitorError(proc);
+
+            boolean simulationTime = false;
+            while ((str = proc_in.readLine()) != null) {
+                System.out.println("executing: " + str);
+                if (str.startsWith(SIM_KEYWORD_TIME_BEG)) {
+                    String str1 = str.substring(SIM_KEYWORD_TIME_BEG.length());
+                    int index = str1.indexOf(SIM_KEYWORD_TIME_END);
+                    System.out.println("executing: str1=" + str1);
+                    if (index != -1) {
+                        String str2 = str1.substring(0, index).trim();
+                        System.out.println("executing: str2=" + str2);
+                        simulationTime = str2.compareTo(SIMUL_END_TIME) == 0;
+                    }
+                }
+            }
+            assertTrue(simulationTime);
+        } catch (Exception e) {
+            // Probably make is not installed
+            System.out.println("FAILED: executing simulation");
+            return;
+        }
+    }
 }
-- 
GitLab