diff --git a/unit_tests/graph/Test_GraphView.cpp b/unit_tests/graph/Test_GraphView.cpp index d2f41269eb46e6935f11fbf2ac0ab7dcf1232945..8462d50ee5805318cf4ac093ea0265fd544fba27 100644 --- a/unit_tests/graph/Test_GraphView.cpp +++ b/unit_tests/graph/Test_GraphView.cpp @@ -1087,3 +1087,806 @@ TEST_CASE("[core/graph] GraphView(insertParent)") { } } + +////////////////////////////////////////// +// test case 1: one input, one output +// 1/ graphview= other_input --> other1 --> old1 --> old2 --> other2 +// 2/ replace oldGraph= -->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 --> old12 --> 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 +// - old11, old12, old21 and old22 are remove from graphview +////////////////////////////////////////// +// test case 3 none input, multiple output +// 1/ graphview= old_input --> old1 --> other +// 2/ replace oldGraph= old_input --> old --> by newGraph = new_input --> new --> +// 3/ verify : +// - output value of replace == TRUE +// - graphview = new_input --> new --> other +// - old_input -and old are remove from graphview +////////////////////////////////////////// +TEST_CASE("[core/graph] Graph: replacing a set of nodes, same old/new inputs and same old/new outputs", "[GraphView][replace]") +{ + SECTION("test case 1 one input, one output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + // Create old graph + auto otherInput = GenericOperator("Producer", 0, 0, 1, "other_input"); + auto other1 = GenericOperator("Other", 1, 0, 1, "other1"); + auto myOld1 = GenericOperator("Old", 1, 0, 1, "old1"); + auto myOld2 = GenericOperator("Old", 1, 0, 1, "old2"); + auto other2 = GenericOperator("Other", 1, 0, 1, "other2"); + // Link old graph + otherInput->addChild(other1); + other1->addChild(myOld1); + myOld1->addChild(myOld2); + myOld2->addChild(other2); + graphTest->add({other1, myOld1, myOld2, other2}); + + // Create and link new graph + auto myNew1 = GenericOperator("New", 1, 0, 1, "new1"); + auto myNew2 = GenericOperator("New", 1, 0, 1, "new2"); + myNew1->addChild(myNew2); + + // Replace + bool retValue = GraphView::replace({myOld1, myOld2}, {myNew1, myNew2}); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other1, myNew1, myNew2, other2})); + graphTest->save("myGraph",true,true); + CHECK(retValue); + // Check links + CHECK(myNew1->input(0).first == other1); + CHECK(myNew2->output(0).at(0).first == other2); + // Check graph Nodes + 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") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + // Create old graph + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto otherInput2 = GenericOperator("Producer", 0, 0, 1, "other_input2"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto other12 = GenericOperator("Other", 1, 0, 1, "other12"); + auto myOld11 = GenericOperator("Old", 1, 0, 1, "old11"); + auto myOld12 = GenericOperator("Old", 1, 0, 1, "old12"); + auto myOld21 = GenericOperator("Old", 1, 0, 1, "old21"); + auto myOld22 = GenericOperator("Old", 1, 0, 1, "old22"); + auto other21 = GenericOperator("Other", 1, 0, 1, "other21"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + // Link old graph + otherInput1->addChild(other11); + other11->addChild(myOld11); + myOld11->addChild(myOld12); + myOld12->addChild(other12); + otherInput2->addChild(other21); + other21->addChild(myOld21); + myOld21->addChild(myOld22); + myOld22->addChild(other22); + graphTest->add({other11, myOld11, myOld12, other12, other21, myOld21, myOld22, other22}); + + //std::vector<std::pair<NodePtr, IOIndex_t>> orderInput; + //orderInput.push_back(std::pair<NodePtr, IOIndex_t>(other11,0)); + //orderInput.push_back(std::pair<NodePtr, IOIndex_t>(other21,0)); + //graphTest->setOrderedInputs(orderInput); + graphTest->save("myGraph",true,true); + // Create and link new graph + auto myNew11 = GenericOperator("New", 1, 0, 1, "new11"); + auto myNew12 = GenericOperator("New", 1, 0, 1, "new12"); + auto myNew21 = GenericOperator("New", 1, 0, 1, "new21"); + auto myNew22 = GenericOperator("New", 1, 0, 1, "new22"); + myNew11->addChild(myNew12); + myNew21->addChild(myNew22); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld11, myOld12, myOld21, myOld22}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld11,0), std::pair<NodePtr, IOIndex_t>(myOld21,0)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld12,0), std::pair<NodePtr, IOIndex_t>(myOld22,0)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + graphNew->add({myNew11, myNew12, myNew21, myNew22}); + graphNew->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myNew11,0), std::pair<NodePtr, IOIndex_t>(myNew21,0)}); + graphNew->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myNew12,0), std::pair<NodePtr, IOIndex_t>(myNew22,0)}); + + // Replace + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld11, myOld12, myOld21, myOld22}, {myNew11, myNew12, myNew21, myNew22}); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, myNew11, myNew12, other12, other21, myNew21, myNew22, other22})); + graphTest->save("myGraph2",true,true); + CHECK(retValue); + // Check links + 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 graph Nodes + CHECK(graphTest->getNode("old11") == nullptr); + CHECK(graphTest->getNode("old21") == nullptr); + CHECK(graphTest->getNode("old12") == 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); + } + SECTION("test case 3 none input, multiple output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto oldInput = GenericOperator("Producer", 0, 0, 1, "old_input"); + auto myOld = GenericOperator("Old", 1, 0, 1, "old"); + auto other = GenericOperator("Other", 1, 0, 1, "other"); + oldInput->addChild(myOld); + myOld->addChild(other); + graphTest->add({oldInput, myOld, other}); + + auto newInput = GenericOperator("Producer", 0, 0, 1, "newInput"); + auto myNew = GenericOperator("New", 1, 0, 1, "new"); + newInput->addChild(myNew); + + bool retValue = GraphView::replace({oldInput, myOld}, {newInput, myNew}); + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({newInput, myNew, other})); + graphTest->save("myGraph",true,true); + CHECK(retValue); + CHECK(myNew->output(0).at(0).first == other); + CHECK(graphTest->getNode("old_input") == nullptr); + CHECK(graphTest->getNode("old") == nullptr); + CHECK(graphTest->getNode("newInput") == newInput); + CHECK(graphTest->getNode("new") == myNew); + } +} + +TEST_CASE("[core/graph] Replacing a set of nodes, one old input, same number of outputs", "[GraphView][replace]") +{ +////////////////////////////////////////// +// test case 1 one old input several (2) new inputs, same number of outputs (1) +// 1/ graphview== other_input1 --> other1 --> old1 --> old2 --> other2 +// 2/ replace oldGraph= -->old1 --> old2--> by newGraph= -->new1 --> new3 --> +// -->new2 ---/ +// 3/ verify : +// - output value of replace == TRUE +// - graphview== other_input1 -->new1 --> new3 --> other2 +// \-->new2 ---/ +// - old1, old2 are removed from graphview +// - new1, new2,and new3 are added to graphview +////////////////////////////////////////// +// test case 2 one old input, several (2) new inputs, same number of outputs (0) +// 1/ graphview== other_input1 --> other1 --> old1 --> old2 +// 2/ replace oldGraph= -->old1 --> old2 by newGraph= -->new1 --> new3 +// -->new2 ---/ +// 3/ verify : +// - output value of replace == TRUE +// - graphview== other_input1 -->new1 --> new3 +// \-->new2 ---/ +// - old1, old2 are removed from graphview +// - new1, new2,and new3 are added to graphview +////////////////////////////////////////// +// test case 3 one old input, several (2) new input, same number of outputs (4) +// 1/ graphview== other_input1 --> other1 --> old1 --> old2 --> other4 +// \ \--> other2 +// \---------/ +// \-------> other3 +// 2/ replace oldGraph= -->old1 --> old2 by newGraph= -->new1 --> new3 --> +// -->new2 ---/ \-> +// 3/ verify : +// - output value of replace == TRUE +// /----------\ +// / /-----> other2 +// - graphview== other_input1 --> other1 -->new1 --> new3 --> other4 +// \-->new2 ---/------> other 3 +// - old1, old2 are removed from graphview +// - new1, new2,and new3 are added to graphview +////////////////////////////////////////// +// test case 4 one old input no (0) new inputs, same number of outputs (1) +// 1/ graphview== other_input1 --> other1 --> old1 --> old2 --> other2 +// 2/ replace oldGraph= -->old1 --> old2 by newGraph= new1 --> new3 --> +// new2 ---/ +// 3/ verify : +// - output value of replace == TRUE +// - graphview== other_input1 -->new1 --> new3 +// \-->new2 ---/ +// - old1, old2 are removed from graphview +// - new1, new2,and new3 are added to graphview +////////////////////////////////////////// + SECTION("test case 1 one old input several (2) new inputs, same number of outputs (1)") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("old_graph"); + std::shared_ptr<GraphView> graphNew = std::make_shared<GraphView>("new_graph"); + // Create old 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"); + // Link old graph + otherInput->addChild(other1); + other1->addChild(myOld1); + myOld1->addChild(myOld2); + myOld2->addChild(other2); + graphTest->add({other1, myOld1, myOld2, other2}); + graphOld->add({myOld1, myOld2}); + + // Create and link new graph + auto myNew1 = GenericOperator("New", {InputCategory::Data}, 1, "new1"); + auto myNew2 = GenericOperator("New", {InputCategory::Data}, 1, "new2"); + auto myNew3 = GenericOperator("New", {InputCategory::Data, InputCategory::Data}, 1, "new3"); + myNew1->addChild(myNew3); + myNew2->addChild(myNew3, 0, 1); + graphNew->add(std::set<Aidge::NodePtr>{myNew1, myNew2, myNew3}); + graphTest->save("myGraphBefore",true,true); + + // Replace + bool retValue = GraphView::replace(graphOld, graphNew); + graphTest->save("myGraphAfter",true,true); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other1, myNew1, myNew2, myNew3, other2})); + CHECK(retValue); + // Check links + CHECK(myNew1->input(0).first == other1); + CHECK(myNew2->input(0).first == other1); + CHECK(myNew3->output(0).at(0).first == other2); + // Check graph Nodes + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + CHECK(graphTest->getNode("new1") == myNew1); + CHECK(graphTest->getNode("new2") == myNew2); + CHECK(graphTest->getNode("new3") == myNew3); + } + SECTION("test case 2 one old input, several (2) new inputs, same number of outputs (0)") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("old_graph"); + std::shared_ptr<GraphView> graphNew = std::make_shared<GraphView>("new_graph"); + // Create old 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"); + // Link old graph + otherInput->addChild(other1); + other1->addChild(myOld1); + myOld1->addChild(myOld2); + graphTest->add({other1, myOld1, myOld2}); + graphOld->add({myOld1, myOld2}); + + // Create and link new graph + auto myNew1 = GenericOperator("New", {InputCategory::Data}, 1, "new1"); + auto myNew2 = GenericOperator("New", {InputCategory::Data}, 1, "new2"); + auto myNew3 = GenericOperator("New", {InputCategory::Data, InputCategory::Data}, 1, "new3"); + myNew1->addChild(myNew3); + myNew2->addChild(myNew3, 0, 1); + graphNew->add(std::set<Aidge::NodePtr>{myNew1, myNew2, myNew3}); + + // Replace + bool retValue = GraphView::replace(graphOld, graphNew); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other1, myNew1, myNew2, myNew3})); + CHECK(retValue); + // Check links + CHECK(myNew1->input(0).first == other1); + CHECK(myNew2->input(0).first == other1); + // Check graph Nodes + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + CHECK(graphTest->getNode("new1") == myNew1); + CHECK(graphTest->getNode("new2") == myNew2); + CHECK(graphTest->getNode("new3") == myNew3); + } + SECTION("test case 3 one old input, several (2) new input, same number of outputs (4)") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("old_graph"); + std::shared_ptr<GraphView> graphNew = std::make_shared<GraphView>("new_graph"); + // Create old 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}, 2, "old2"); + auto other2 = GenericOperator("Other", {InputCategory::Data, InputCategory::Data}, 1, "other2"); + auto other3 = GenericOperator("Other", {InputCategory::Data}, 1, "other3"); + auto other4 = GenericOperator("Other", {InputCategory::Data}, 1, "other4"); + // Link old graph + otherInput->addChild(other1); + other1->addChild(myOld1); + myOld1->addChild(myOld2, 0, 0); + myOld1->addChild(other2, 0, 1); + myOld1->addChild(other3, 0, 0); + myOld2->addChild(other2, 1, 0); + myOld2->addChild(other4, 0, 0); + graphTest->add({other1, myOld1, myOld2, other2}); + graphOld->add({myOld1, myOld2}); + std::vector<std::pair<NodePtr, IOIndex_t>> oldOutputs; + graphOld->setOrderedOutputs(std::vector<std::pair<NodePtr, IOIndex_t>> {{myOld1, 0}, {myOld1, 0}, {myOld2, 0}, {myOld2, 1}}); + // Create and link new graph + auto myNew1 = GenericOperator("New", {InputCategory::Data}, 1, "new1"); + auto myNew2 = GenericOperator("New", {InputCategory::Data}, 1, "new2"); + auto myNew3 = GenericOperator("New", {InputCategory::Data, InputCategory::Data}, 2, "new3"); + myNew1->addChild(myNew3); + myNew2->addChild(myNew3, 0, 1); + graphNew->add(std::set<Aidge::NodePtr>{myNew1, myNew2, myNew3}); + graphNew->setOrderedOutputs(std::vector<std::pair<NodePtr, IOIndex_t>> {{myNew1, 0}, {myNew2, 0}, {myNew3, 0}, {myNew3, 1}}); + + // Replace + bool retValue = GraphView::replace(graphOld, graphNew); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other1, myNew1, myNew2, myNew3, other2})); + CHECK(retValue); + // Check links + CHECK(myNew1->input(0).first == other1); + CHECK(myNew2->input(0).first == other1); + + CHECK(myNew3->output(0).at(0).first == other4); + CHECK(myNew3->output(1).at(0).first == other2); + + // TODO: check if the following conditions should be true (there aren't right now) + //CHECK(myNew2->output(0).at(0).first == other3); + //CHECK(myNew1->output(0).at(0).first == other2); + // Check graph Nodes + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + CHECK(graphTest->getNode("new1") == myNew1); + CHECK(graphTest->getNode("new2") == myNew2); + CHECK(graphTest->getNode("new3") == myNew3); + } + SECTION("test case 4 one old input no (0) new inputs, same number of outputs (1)") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("old_graph"); + std::shared_ptr<GraphView> graphNew = std::make_shared<GraphView>("new_graph"); + // Create old 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"); + // Link old graph + otherInput->addChild(other1); + other1->addChild(myOld1); + myOld1->addChild(myOld2); + myOld2->addChild(other2); + graphTest->add({other1, myOld1, myOld2, other2}); + graphOld->add({myOld1, myOld2}); + + // Create and link new graph + auto myNew1 = GenericOperator("Producer", {}, 1, "new1"); + auto myNew2 = GenericOperator("Producer", {}, 1, "new2"); + auto myNew3 = GenericOperator("New", {InputCategory::Data, InputCategory::Data}, 1, "new3"); + myNew1->addChild(myNew3); + myNew2->addChild(myNew3, 0, 1); + graphNew->add(std::set<Aidge::NodePtr>{myNew1, myNew2, myNew3}); + + // Replace + bool retValue = GraphView::replace(graphOld, graphNew); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other1, myNew1, myNew2, myNew3, other2})); + CHECK(retValue); + // Check links + CHECK(myNew3->output(0).at(0).first == other2); + // Check graph Nodes + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + CHECK(graphTest->getNode("new1") == myNew1); + CHECK(graphTest->getNode("new2") == myNew2); + CHECK(graphTest->getNode("new3") == myNew3); + } + SECTION("test case 5 one old input several (3) new inputs, same number of outputs (3) from same node output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("old_graph"); + std::shared_ptr<GraphView> graphNew = std::make_shared<GraphView>("new_graph"); + // Create old graph + auto otherInput = GenericOperator("Producer", {}, 1, "other_input"); + auto myOld1 = GenericOperator("Old", {InputCategory::Data}, 1, "old1"); + auto other1 = GenericOperator("Other", {InputCategory::Data}, 1, "other1"); + auto other2 = GenericOperator("Other", {InputCategory::Data}, 1, "other2"); + auto other3 = GenericOperator("Other", {InputCategory::Data}, 1, "other3"); + // Link old graph + otherInput->addChild(myOld1); + myOld1->addChild(other1); + myOld1->addChild(other2); + myOld1->addChild(other3); + graphTest->add({myOld1, other1, other2, other3}); + graphOld->add({myOld1}); + // Create and link new graph + auto myNew1 = GenericOperator("New", {InputCategory::Data}, 1, "new1"); + graphNew->add(std::set<Aidge::NodePtr>{myNew1}); + + // Replace + bool retValue = GraphView::replace(graphOld, graphNew); + + // Check outputs + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({ myNew1, other1, other2, other3})); + CHECK(retValue); + // Check links + CHECK(myNew1->input(0).first == otherInput); + + CHECK(myNew1->output(0).at(0).first == other1); + // Check graph Nodes + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("new1") == myNew1); + } +} + +////////////////////////////////////////// +// test case 1 same input, one new output +// 1/ graphview== other_input1 --> other11 --> old11 --> old12 --> other21 +// other_input2 --> other12 --> old12 --> old22 --> other22 +// 2/ replace oldGraph= -->old11 --> old12--> by newGraph= -->new --> +// -->old12 --> old22--> _/ +// 3/ verify : +// - output value of replace == FALSE +// - graphview== other_input1 --> other11 --> old11 --> old12 --> other21 +// other_input2 --> other12 --> old121 --> old22 --> other22 +// - old11, old12, old21 and old22 are not remove from graphview +// - new is not added to graphview +////////////////////////////////////////// +TEST_CASE("[core/graph] Graph: replacing a set of nodes, same old/new inputs and multiple old/one new output", "[GraphView][replace]") { + +SECTION("test case 1 same input, one new output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto otherInput2 = GenericOperator("Producer", 0, 0, 1, "other_input2"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto other12 = GenericOperator("Other", 1, 0, 1, "other12"); + auto myOld11 = GenericOperator("Old", 1, 0, 1, "old11"); + auto myOld12 = GenericOperator("Old", 1, 0, 1, "old12"); + auto myOld21 = GenericOperator("Old", 1, 0, 1, "old21"); + auto myOld22 = GenericOperator("Old", 1, 0, 1, "old22"); + auto other21 = GenericOperator("Other", 1, 0, 1, "other21"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + otherInput1->addChild(other11); + other11->addChild(myOld11); + myOld11->addChild(myOld12); + myOld12->addChild(other12); + otherInput2->addChild(other21); + other21->addChild(myOld21); + myOld21->addChild(myOld22); + myOld22->addChild(other22); + graphTest->add({other11, myOld11, myOld12, other12, other21, myOld21, myOld22, other22}); + + graphTest->save("myGraph",true,true); + auto myNew = GenericOperator("New", 2, 0, 1, "new"); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld11, myOld12, myOld21, myOld22}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld11,0), std::pair<NodePtr, IOIndex_t>(myOld21,0)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld12,0), std::pair<NodePtr, IOIndex_t>(myOld22,0)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + graphNew->add({myNew}); + graphNew->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myNew,0), std::pair<NodePtr, IOIndex_t>(myNew,1)}); + graphNew->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myNew,0)}); + + + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld11, myOld12, myOld21, myOld22}, {myNew11, myNew12, myNew21, myNew22}); + + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, myOld11, myOld12, other12, other21, myOld21, myOld22, other22})); + graphTest->save("myGraph2",true,true); + CHECK(retValue == false); + CHECK(graphTest->getNode("old11") == myOld11); + CHECK(graphTest->getNode("old12") == myOld12); + CHECK(graphTest->getNode("old21") == myOld21); + CHECK(graphTest->getNode("old22") == myOld22); + CHECK(graphTest->getNode("new") == nullptr); + } +} + +////////////////////////////////////////// +// test case 1 multiple new input, multiple old/one new output +// 1/ graphview== other_input1 --> other11 --> old --> other21 +// \--> other22 +// 2/ replace oldGraph= --> old --> by newGraph= -->new --> +// \--> --/ +// 3/ verify : +// - output value of replace == FALSE +// - graphview== other_input1 --> other11 --> old --> other21 +// \--> other22 +// - old is not remove from graphview +// - new is not added to graphview +////////////////////////////////////////// +TEST_CASE("[core/graph] Graph: replacing a set of nodes, one old/ multiple new inputs and multiple old/ one output output", "[GraphView][replace]") { + +SECTION("test case 1 multiple new input, multiple old/one new output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto myOld = GenericOperator("Old", 1, 0, 2, "old"); + auto other21 = GenericOperator("Other", 1, 0, 1, "other21"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + otherInput1->addChild(other11); + other11->addChild(myOld); + myOld->addChild(other21); + myOld->addChild(other22); + graphTest->add({other11, myOld, other21, other22}); + + graphTest->save("myGraph",true,true); + auto myNew = GenericOperator("New", 2, 0, 1, "new"); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld,0)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld,0), std::pair<NodePtr, IOIndex_t>(myOld,1)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + graphNew->add({myNew}); + graphNew->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myNew,0), std::pair<NodePtr, IOIndex_t>(myNew,1)}); + graphNew->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myNew,0)}); + + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld}, {myNew}); + + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, myOld, other21, other22})); + graphTest->save("myGraph2",true,true); + CHECK(retValue == false); + CHECK(graphTest->getNode("old") == myOld); + CHECK(graphTest->getNode("new") == nullptr); + } + +} + +////////////////////////////////////////// +// test case 1 multiple old input/output, not same new input/ same new output +// 1/ graphview== other_input1 --> other11 --> old1 --> other21 +// other_input2 --> other21 --> old2 --> other22 +// other_input3 --> other31 --/ +// 2/ replace oldGraph= --> old1 --> by newGraph= --> new1 --> +// --> old2 --> --> new2 --> +// --/ +// 3/ verify : +// - output value of replace == FALSE +// - graphview== other_input1 --> other11 --> old1 --> other12 +// other_input2 --> other21 --> old2 --> other22 +// other_input3 --> other31 --/ +// - old1, old2 are not remove from graphview +// - new1, new2 is not added to graphview +////////////////////////////////////////// +// test case 2 multiple old input/output, same new input/ not same new output +// 1/ graphview== other_input1 --> other11 --> old1 --> other12 +// other_input2 --> other21 --> old2 --> other22 +// \--> other32 +// 2/ replace oldGraph= --> old1 --> by newGraph= --> new1 --> +// --> old2 --> --> new2 --> +// \--> +// 3/ verify : +// - output value of replace == FALSE +// - graphview== other_input1 --> other11 --> old1 --> other12 +// other_input2 --> other21 --> old2 --> other22 +// \-> other32 +// - old1, old2 are not remove from graphview +// - new1, new2 is not added to graphview +TEST_CASE("[core/graph] Graph: replacing a set of nodes, not same number old/new inputs/outputs, failed", "[GraphView][replace]") { + +SECTION("test case 1 not same old/new input, same old/new output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto otherInput2 = GenericOperator("Producer", 0, 0, 1, "other_input2"); + auto otherInput3 = GenericOperator("Producer", 0, 0, 1, "other_input3"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto other21= GenericOperator("Other", 1, 0, 1, "other21"); + auto other31 = GenericOperator("Other", 1, 0, 1, "other31"); + auto myOld1 = GenericOperator("Old", 1, 0, 1, "old1"); + auto myOld2 = GenericOperator("Old", 2, 0, 1, "old2"); + auto other12 = GenericOperator("Other", 1, 0, 1, "other12"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + otherInput1->addChild(other11); + otherInput2->addChild(other21); + otherInput3->addChild(other31); + other11->addChild(myOld1); + other21->addChild(myOld2); + other31->addChild(myOld2); + myOld1->addChild(other12); + myOld2->addChild(other22); + graphTest->add({other11, other21, other31, myOld1, myOld2, other12, other22}); + + graphTest->save("myGraph",true,true); + auto myNew1 = GenericOperator("New", 1, 0, 1, "new1"); + auto myNew2 = GenericOperator("New", 1, 0, 1, "new2"); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld1, myOld2}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld1,0),std::pair<NodePtr, IOIndex_t>(myOld2,0), std::pair<NodePtr, IOIndex_t>(myOld2,1)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld1,0), std::pair<NodePtr, IOIndex_t>(myOld2,0)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + graphNew->add({myNew1}); + graphNew->add({myNew2}); + graphNew->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myNew1,0), std::pair<NodePtr, IOIndex_t>(myNew2,0)}); + graphNew->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myNew1,0), std::pair<NodePtr, IOIndex_t>(myNew2,0)}); + + + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld}, {myNew}); + + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, other21, other31, myOld1, myOld2, other12, other22})); + graphTest->save("myGraph2",true,true); + CHECK(retValue == false); + CHECK(graphTest->getNode("old1") == myOld1); + CHECK(graphTest->getNode("old2") == myOld2); + CHECK(graphTest->getNode("new1") == nullptr); + CHECK(graphTest->getNode("new2") == nullptr); + } + +SECTION("test case 2 same old/new input, not same old/new output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto otherInput2 = GenericOperator("Producer", 0, 0, 1, "other_input2"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto other21 = GenericOperator("Other", 1, 0, 1, "other21"); + auto myOld1 = GenericOperator("Old", 1, 0, 1, "old1"); + auto myOld2 = GenericOperator("Old", 1, 0, 2, "old2"); + auto other12 = GenericOperator("Other", 1, 0, 1, "other12"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + auto other32 = GenericOperator("Other", 1, 0, 1, "other32"); + otherInput1->addChild(other11); + otherInput2->addChild(other21); + other11->addChild(myOld1); + other21->addChild(myOld2); + myOld1->addChild(other12); + myOld2->addChild(other22); + myOld2->addChild(other32); + graphTest->add({other11, other21, myOld1, myOld2, other12, other22, other32}); + + graphTest->save("myGraph",true,true); + auto myNew1 = GenericOperator("New", 1, 0, 1, "new1"); + auto myNew2 = GenericOperator("New", 1, 0, 1, "new2"); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld1, myOld2}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld1,0), std::pair<NodePtr, IOIndex_t>(myOld2,0)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld1,0), std::pair<NodePtr, IOIndex_t>(myOld2,0), std::pair<NodePtr, IOIndex_t>(myOld2,1)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + graphNew->add({myNew1}); + graphNew->add({myNew2}); + graphNew->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myNew1,0), std::pair<NodePtr, IOIndex_t>(myNew2,0)}); + graphNew->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myNew1,0), std::pair<NodePtr, IOIndex_t>(myNew2,0)}); + + + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld}, {myNew}); + + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, other21, myOld1, myOld2, other12, other22, other32})); + graphTest->save("myGraph2",true,true); + CHECK(retValue==false); + CHECK(graphTest->getNode("old1") == myOld1); + CHECK(graphTest->getNode("old2") == myOld2); + CHECK(graphTest->getNode("new1") == nullptr); + CHECK(graphTest->getNode("new2") == nullptr); + } + +} + +////////////////////////////////////////// +// test case 1 replacing a set of nodes by empty set, same number of inputs and outputs +// 1/ graphview== other_input1 --> other11 --> old1 --> other12 +// other_input2 --> other21 --> old2 --> other22 +// other_input3 --> other31 --/ \--> other32 +// 2/ replace oldGraph= --> old1 --> by newGraph= "empty" +// --> old2 --> +// --/ \--> +// 3/ verify : +// - output value of replace == TRUE +// - graphview== other_input1 --> other11 --> other12 +// other_input2 --> other21 --> other22 +// other_input3 --> other31 --> other32 +// - old1, old2 are remove from graphview +TEST_CASE("[core/graph] Graph: replacing a set of nodes, new set of node is empty", "[GraphView][replace]") { + +SECTION("test case 1 multiple old input/output, not same new input/ same new output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto otherInput2 = GenericOperator("Producer", 0, 0, 1, "other_input2"); + auto otherInput3 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto other21 = GenericOperator("Other", 1, 0, 1, "other21"); + auto other31 = GenericOperator("Other", 1, 0, 1, "other31"); + auto myOld1 = GenericOperator("Old", 1, 0, 1, "old1"); + auto myOld2 = GenericOperator("Old", 2, 0, 2, "old2"); + auto other12 = GenericOperator("Other", 1, 0, 1, "other12"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + auto other32 = GenericOperator("Other", 1, 0, 1, "other32"); + otherInput1->addChild(other11); + otherInput2->addChild(other21); + otherInput3->addChild(other31); + other11->addChild(myOld1); + other21->addChild(myOld2); + other31->addChild(myOld2); + myOld1->addChild(other12); + myOld2->addChild(other22); + myOld2->addChild(other32); + graphTest->add({other11, other21, other31, myOld1, myOld2, other12, other22, other32}); + + graphTest->save("myGraph",true,true); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld1, myOld2}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld1,0), std::pair<NodePtr, IOIndex_t>(myOld2,0), std::pair<NodePtr, IOIndex_t>(myOld2,1)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld1,0), std::pair<NodePtr, IOIndex_t>(myOld2,0)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + + + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld}, {}); + + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, other21, other31, other12, other22, other32})); + graphTest->save("myGraph2",true,true); + CHECK(retValue ); + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + } +} + +////////////////////////////////////////// +// test case 1 replacing a set of nodes by empty set, there is a single input +// 1/ graphview== other_input1* -->*other11*-->*old1**--> *other12 +// | +// *old2**--> *other22 +// \--> *other32 +// 2/ replace oldGraph= --> *old1** --> by newGraph= "empty" +// | +// *old2** --> +// \--> +// 3/ verify : +// - output value of replace == TRUE +// - graphview== other_input1*--> *other11*-->*other12 +// \-->*other22 +// \-->*other32 +// - old1, old2 are remove from graphview +TEST_CASE("[core/graph] Graph: replacing a set of nodes, new set of node is empty and single output", "[GraphView][replace]") { + +SECTION("test case 1 multiple old input/output, not same new input / same new output") { + std::shared_ptr<GraphView> graphTest = std::make_shared<GraphView>("test_graph"); + auto otherInput1 = GenericOperator("Producer", 0, 0, 1, "other_input1"); + auto other11 = GenericOperator("Other", 1, 0, 1, "other11"); + auto myOld1 = GenericOperator("Old", 1, 0, 2, "old1"); + auto myOld2 = GenericOperator("Old", 1, 0, 2, "old2"); + auto other12 = GenericOperator("Other", 1, 0, 1, "other12"); + auto other22 = GenericOperator("Other", 1, 0, 1, "other22"); + auto other32 = GenericOperator("Other", 1, 0, 1, "other32"); + otherInput1->addChild(other11); + other11->addChild(myOld1); + myOld1->addChild(myOld2, 1, 0); + myOld1->addChild(other12, 0, 0); + myOld2->addChild(other22, 0, 0); + myOld2->addChild(other32,1, 0); + graphTest->add({other11, myOld1, myOld2, other12, other22, other32}); + + graphTest->save("myGraph",true,true); + // graphOld + std::shared_ptr<GraphView> graphOld = std::make_shared<GraphView>("oldGraph"); + graphOld->add({myOld1, myOld2}); + graphOld->setOrderedInputs({std::pair<NodePtr, IOIndex_t>(myOld1,0)}); + graphOld->setOrderedOutputs({std::pair<NodePtr, IOIndex_t>(myOld1,0), std::pair<NodePtr, IOIndex_t>(myOld2,0), std::pair<NodePtr, IOIndex_t>(myOld2,1)}); + // graphNew + std::shared_ptr<GraphView> graphNew= std::make_shared<GraphView>("newGraph"); + + + bool retValue = GraphView::replace(graphOld, graphNew); + //bool retValue = GraphView::replace({myOld}, {}); + + CHECK(graphTest->getNodes() == std::set<std::shared_ptr<Node>>({other11, other12, other22, other32})); + auto childs = other11->getChildren(0); + CHECK(childs.at(0)== other12); + CHECK(childs.at(1)== other22); + CHECK(childs.at(2)== other32); + graphTest->save("myGraph2",true,true); + CHECK(retValue ); + CHECK(graphTest->getNode("old1") == nullptr); + CHECK(graphTest->getNode("old2") == nullptr); + } +}