diff --git a/unit_tests/graph/Test_GraphView.cpp b/unit_tests/graph/Test_GraphView.cpp index 2fa06cf23b3b681211208a3e5bbea9226f0930b8..a2a348c47093c9fffd841774650fcbeba0508756 100644 --- a/unit_tests/graph/Test_GraphView.cpp +++ b/unit_tests/graph/Test_GraphView.cpp @@ -15,6 +15,7 @@ #include <memory> #include <set> #include <string> +#include <fstream> #include <catch2/catch_test_macros.hpp> #include <catch2/generators/catch_generators_random.hpp> @@ -31,6 +32,32 @@ #include "aidge/graph/OpArgs.hpp" #include "aidge/operator/GenericOperator.hpp" #include "aidge/operator/Producer.hpp" +#include "aidge/utils/Log.hpp" + + +bool removeFile(std::string i_strFileName) +{ + return remove(i_strFileName.c_str()); +} + +bool LoadTextFile(std::string i_strFileName, std::vector<std::string> & output) +{ + std::ifstream file(i_strFileName.c_str()); + if ( file ) { + std::string line; + while ( std::getline( file, line) ) { + output.push_back(line); + } + } + else { + return false; + } + return true;; +} + + + + using namespace Aidge; @@ -816,6 +843,127 @@ TEST_CASE("[core/graph] GraphView(replace)", "[GraphView][replace]") { } } +///////////////////////////////////////// +// AIDGE_TEST_0108000 +///////////////////////////////////////// +// Requirements tested: +// AIDGE_CORE_REQ_0108000 +// AIDGE_CORE_REQ_0108090 +////////////////////////////////////////// +// Initial conditions for each case: +// -log file initialized with 0 log +////////////////////////////////////////// +// test case 1: one input, one output +// 1/ graphview= other_input --> other1 --> old1 --> old2 --> other2 +// 2/ replace ooldGraph= old1 --> old2 by newGraph = new1 --> new2 +// 3/ verify : +// - output value of replace == TRUE +// - graphview = other_input --> other1 --> new1 --> new2 --> other2 +// - old1 -and old2 are remove from graphview +////////////////////////////////////////// +// test case 2 multiple input, multiple output +// 1/ graphview= other_input1 --> other11 --> old11 --> old12 --> other21 +// other_input2 --> other12 --> old121 --> old22 --> other22 +// 2/ replace oldGraph= old11 --> old12 by newGraph= new11 --> new12 +// old12 --> old22 new21 --> new22 +// 3/ verify : +// - output value of replace == TRUE +// - graphview = other_input1 --> other11 --> new11 --> new12 --> other21 +// other_input2 --> other12 --> new21 --> new22 --> other22 +// - old1, old2, old21 and old22 are remove from graphview +////////////////////////////////////////// +// test case 3 none input, none output ? +// +TEST_CASE("[core/graph] AIDGE_TEST_0108000: Replacing a set of nodes, same inputs and output", "[GraphView][replace]") +{ + SECTION("test case 1 one input, one output") { + removeFile("my_log.txt"); + Log::setFileName("my_log.txt"); + Log::setFileLevel(Log::Level::Warn); + std::vector<std::string> logs; + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput = GenericOperator("Producer", {}, 1, "other_input"); + auto other1 = GenericOperator("Other", {InputCategory::Data}, 1, "other1"); + auto myOld1 = GenericOperator("Old", {InputCategory::Data}, 1, "old1"); + auto myOld2 = GenericOperator("Old", {InputCategory::Data}, 1, "old2"); + auto other2 = GenericOperator("Other", {InputCategory::Data}, 1, "other2"); + otherInput->addChild(other1); + other1->addChild(myOld1); + myOld1->addChild(myOld2); + myOld2->addChild(other2); + graphTest->add({other1, myOld1, myOld2, other2}); + + auto myNew1 = GenericOperator("New", {InputCategory::Data}, 1, "new1"); + auto myNew2 = GenericOperator("New", {InputCategory::Data}, 1, "new2"); + myNew1->addChild(myNew2); + + bool retValue = GraphView::replace({myOld1, myOld2}, {myNew1, myNew2}); + CHECK(LoadTextFile("my_log.txt", logs)); + CHECK(logs.size() == 0); + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other1, myNew1, myNew2, other2})); + graphTest->save("myGraph",true,true); + CHECK(retValue); + CHECK(myNew1->input(0).first == other1); + CHECK(myNew2->output(0).at(0).first == other2); + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + CHECK(graphTest->getNode("new1") == myNew1); + CHECK(graphTest->getNode("new2") == myNew2); + } + SECTION("test case 2 multiple input, multiple output") { + removeFile("my_log.txt"); + Log::setFileName("my_log.txt"); + Log::setFileLevel(Log::Level::Warn); + std::vector<std::string> logs; + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", {}, 1, "other_input1"); + auto otherInput2 = GenericOperator("Producer", {}, 1, "other_input2"); + auto other11 = GenericOperator("Other", {InputCategory::Data}, 1, "other11"); + auto other12 = GenericOperator("Other", {InputCategory::Data}, 1, "other12"); + auto myOld11 = GenericOperator("Old", {InputCategory::Data}, 1, "old11"); + auto myOld12 = GenericOperator("Old", {InputCategory::Data}, 1, "old12"); + auto myOld21 = GenericOperator("Old", {InputCategory::Data}, 1, "old21"); + auto myOld22 = GenericOperator("Old", {InputCategory::Data}, 1, "old22"); + auto other21 = GenericOperator("Other", {InputCategory::Data}, 1, "other21"); + auto other22 = GenericOperator("Other", {InputCategory::Data}, 1, "other22"); + otherInput1->addChild(other11); + other11->addChild(myOld11); + myOld11->addChild(myOld12); + myOld12->addChild(other12); + otherInput2->addChild(other11); + other21->addChild(myOld21); + myOld21->addChild(myOld22); + myOld22->addChild(other22); + graphTest->add({other11, myOld11, myOld12, other12, other21, myOld21, myOld22, other22}); + + auto myNew11 = GenericOperator("New", {InputCategory::Data}, 1, "new11"); + auto myNew12 = GenericOperator("New", {InputCategory::Data}, 1, "new12"); + auto myNew21 = GenericOperator("New", {InputCategory::Data}, 1, "new21"); + auto myNew22 = GenericOperator("New", {InputCategory::Data}, 1, "new22"); + myNew11->addChild(myNew12); + myNew21->addChild(myNew22); + + bool retValue = GraphView::replace({myOld11, myOld12, myOld21, myOld22}, {myNew11, myNew12, myNew21, myNew22}); + CHECK(LoadTextFile("my_log.txt", logs)); + CHECK(logs.size() == 0); + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, myNew11, myNew12, other12, other21, myNew21, myNew22, other22})); + graphTest->save("myGraph",true,true); + CHECK(retValue); + CHECK(myNew11->input(0).first == other11); + CHECK(myNew12->output(0).at(0).first == other12); + CHECK(myNew21->input(0).first == other21); + CHECK(myNew22->output(0).at(0).first == other22); + CHECK(graphTest->getNode("old11") == nullptr); + CHECK(graphTest->getNode("old21") == nullptr); + CHECK(graphTest->getNode("old21") == nullptr); + CHECK(graphTest->getNode("old22") == nullptr); + CHECK(graphTest->getNode("new11") == myNew11); + CHECK(graphTest->getNode("new12") == myNew12); + CHECK(graphTest->getNode("new21") == myNew21); + CHECK(graphTest->getNode("new22") == myNew22); + } +} + TEST_CASE("[GraphView] clone") { auto dataProvider = Producer({16, 3, 224, 224}, "dataProvider"); auto conv1 = Conv(3, 32, {3, 3}, "conv1");