1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #include "test_precomp.hpp"
9 #include <ade/graph.hpp>
10 #include "compiler/transactions.hpp"
17 bool contains(const ade::Graph& graph, const ade::NodeHandle& node)
19 auto nodes = graph.nodes();
20 return nodes.end() != std::find(nodes.begin(), nodes.end(), node);
23 bool connected(const ade::NodeHandle& src_node, const ade::NodeHandle& dst_node)
25 auto nodes = src_node->outNodes();
26 return nodes.end() != std::find(nodes.begin(), nodes.end(), dst_node);
31 // ehs[0] ehs[1] ehs[2] ehs[3]
32 // nhs[0] -- > nhs[1] --> nhs[2] --> nhs[3] --> nhs[4]
34 enum { node_nums = 5 };
36 ade::NodeHandle fused_nh; /* For check that fusion node is connected to the
37 inputs of the prod and the outputs of the cons */
38 std::array<ade::NodeHandle, node_nums> nhs;
39 std::array<ade::EdgeHandle, node_nums - 1> ehs;
44 nhs[0] = graph.createNode();
45 for (int i = 1; i < node_nums; ++i)
47 nhs[i ] = graph.createNode();
48 ehs[i - 1] = graph.link(nhs[i - 1], nhs[i]);
54 // nhs[0] --> fused_nh --> nhs[4]
56 fused_nh = graph.createNode();
57 changes.enqueue<Change::NodeCreated>(fused_nh);
58 changes.enqueue<Change::NewLink> (graph, nhs[0], fused_nh);
59 changes.enqueue<Change::DropLink>(graph, nhs[1], ehs[0]);
60 changes.enqueue<Change::NewLink> (graph, fused_nh, nhs[4]);
61 changes.enqueue<Change::DropLink>(graph, nhs[3], ehs[3]);
62 changes.enqueue<Change::DropLink>(graph, nhs[1], ehs[1]);
63 changes.enqueue<Change::DropLink>(graph, nhs[2], ehs[2]);
64 changes.enqueue<Change::DropNode>(nhs[1]);
65 changes.enqueue<Change::DropNode>(nhs[2]);
66 changes.enqueue<Change::DropNode>(nhs[3]);
69 void commit() { changes.commit(graph); }
70 void rollback() { changes.rollback(graph); }
74 struct Transactions: public ::testing::Test, public SimpleGraph {};
76 } // anonymous namespace
78 TEST_F(Transactions, NodeCreated_Create)
80 auto new_nh = graph.createNode();
81 Change::NodeCreated node_created(new_nh);
83 EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
84 EXPECT_TRUE(contains(graph, new_nh));
87 TEST_F(Transactions, NodeCreated_RollBack)
89 auto new_nh = graph.createNode();
90 Change::NodeCreated node_created(new_nh);
92 node_created.rollback(graph);
94 EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));
95 EXPECT_FALSE(contains(graph, new_nh));
98 TEST_F(Transactions, NodeCreated_Commit)
100 auto new_nh = graph.createNode();
101 Change::NodeCreated node_created(new_nh);
103 node_created.commit(graph);
105 EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
106 EXPECT_TRUE(contains(graph, new_nh));
109 TEST_F(Transactions, DropLink_Create)
111 Change::DropLink drop_link(graph, nhs[0], ehs[0]);
113 EXPECT_FALSE(connected(nhs[0], nhs[1]));
116 TEST_F(Transactions, DropLink_RollBack)
118 Change::DropLink drop_link(graph, nhs[0], ehs[0]);
120 drop_link.rollback(graph);
122 EXPECT_TRUE(connected(nhs[0], nhs[1]));
125 TEST_F(Transactions, DropLink_Commit)
127 Change::DropLink drop_link(graph, nhs[0], ehs[0]);
129 drop_link.commit(graph);
131 EXPECT_FALSE(connected(nhs[0], nhs[1]));
134 TEST_F(Transactions, NewLink_Create)
136 auto new_nh = graph.createNode();
137 Change::NewLink new_link(graph, new_nh, nhs[0]);
139 EXPECT_TRUE(connected(new_nh, nhs[0]));
142 TEST_F(Transactions, NewLink_RollBack)
144 auto new_nh = graph.createNode();
145 Change::NewLink new_link(graph, new_nh, nhs[0]);
147 new_link.rollback(graph);
149 EXPECT_FALSE(connected(new_nh, nhs[0]));
152 TEST_F(Transactions, NewLink_Commit)
154 auto new_nh = graph.createNode();
155 Change::NewLink new_link(graph, new_nh, nhs[0]);
157 new_link.commit(graph);
159 EXPECT_TRUE(connected(new_nh, nhs[0]));
162 TEST_F(Transactions, DropNode_Create)
164 auto new_nh = graph.createNode();
165 Change::DropNode drop_node(new_nh);
167 EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
168 EXPECT_TRUE(contains(graph, new_nh));
171 TEST_F(Transactions, DropNode_RollBack)
173 auto new_nh = graph.createNode();
174 Change::DropNode drop_node(new_nh);
176 drop_node.rollback(graph);
178 EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
179 EXPECT_TRUE(contains(graph, new_nh));
182 TEST_F(Transactions, DropNode_Commit)
184 auto new_nh = graph.createNode();
185 Change::DropNode drop_node(new_nh);
187 drop_node.commit(graph);
189 EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));
190 EXPECT_FALSE(contains(graph, new_nh));
193 TEST_F(Transactions, Fusion_Commit)
195 namespace C = Change;
200 EXPECT_EQ(3u, static_cast<std::size_t>(graph.nodes().size()));
201 EXPECT_TRUE(connected(nhs[0] , fused_nh));
202 EXPECT_TRUE(connected(fused_nh, nhs[4]));
205 TEST_F(Transactions, Fusion_RollBack)
207 namespace C = Change;
212 EXPECT_EQ(static_cast<std::size_t>(node_nums),
213 static_cast<std::size_t>(graph.nodes().size()));
214 EXPECT_FALSE(contains(graph, fused_nh));
216 for (int i = 0; i < static_cast<int>(node_nums) - 1; ++i)
218 EXPECT_TRUE(connected(nhs[i], nhs[i + 1]));