Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/test/internal/gapi_transactions_test.cpp
16345 views
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.
4
//
5
// Copyright (C) 2018 Intel Corporation
6
7
8
#include "test_precomp.hpp"
9
#include <ade/graph.hpp>
10
#include "compiler/transactions.hpp"
11
12
namespace opencv_test
13
{
14
namespace
15
{
16
17
bool contains(const ade::Graph& graph, const ade::NodeHandle& node)
18
{
19
auto nodes = graph.nodes();
20
return nodes.end() != std::find(nodes.begin(), nodes.end(), node);
21
}
22
23
bool connected(const ade::NodeHandle& src_node, const ade::NodeHandle& dst_node)
24
{
25
auto nodes = src_node->outNodes();
26
return nodes.end() != std::find(nodes.begin(), nodes.end(), dst_node);
27
}
28
29
struct SimpleGraph
30
{
31
// ehs[0] ehs[1] ehs[2] ehs[3]
32
// nhs[0] -- > nhs[1] --> nhs[2] --> nhs[3] --> nhs[4]
33
34
enum { node_nums = 5 };
35
ade::Graph graph;
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;
40
Change::List changes;
41
42
SimpleGraph()
43
{
44
nhs[0] = graph.createNode();
45
for (int i = 1; i < node_nums; ++i)
46
{
47
nhs[i ] = graph.createNode();
48
ehs[i - 1] = graph.link(nhs[i - 1], nhs[i]);
49
}
50
}
51
52
void fuse()
53
{
54
// nhs[0] --> fused_nh --> nhs[4]
55
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]);
67
}
68
69
void commit() { changes.commit(graph); }
70
void rollback() { changes.rollback(graph); }
71
72
};
73
74
struct Transactions: public ::testing::Test, public SimpleGraph {};
75
76
} // anonymous namespace
77
78
TEST_F(Transactions, NodeCreated_Create)
79
{
80
auto new_nh = graph.createNode();
81
Change::NodeCreated node_created(new_nh);
82
83
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
84
EXPECT_TRUE(contains(graph, new_nh));
85
}
86
87
TEST_F(Transactions, NodeCreated_RollBack)
88
{
89
auto new_nh = graph.createNode();
90
Change::NodeCreated node_created(new_nh);
91
92
node_created.rollback(graph);
93
94
EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));
95
EXPECT_FALSE(contains(graph, new_nh));
96
}
97
98
TEST_F(Transactions, NodeCreated_Commit)
99
{
100
auto new_nh = graph.createNode();
101
Change::NodeCreated node_created(new_nh);
102
103
node_created.commit(graph);
104
105
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
106
EXPECT_TRUE(contains(graph, new_nh));
107
}
108
109
TEST_F(Transactions, DropLink_Create)
110
{
111
Change::DropLink drop_link(graph, nhs[0], ehs[0]);
112
113
EXPECT_FALSE(connected(nhs[0], nhs[1]));
114
}
115
116
TEST_F(Transactions, DropLink_RollBack)
117
{
118
Change::DropLink drop_link(graph, nhs[0], ehs[0]);
119
120
drop_link.rollback(graph);
121
122
EXPECT_TRUE(connected(nhs[0], nhs[1]));
123
}
124
125
TEST_F(Transactions, DropLink_Commit)
126
{
127
Change::DropLink drop_link(graph, nhs[0], ehs[0]);
128
129
drop_link.commit(graph);
130
131
EXPECT_FALSE(connected(nhs[0], nhs[1]));
132
}
133
134
TEST_F(Transactions, NewLink_Create)
135
{
136
auto new_nh = graph.createNode();
137
Change::NewLink new_link(graph, new_nh, nhs[0]);
138
139
EXPECT_TRUE(connected(new_nh, nhs[0]));
140
}
141
142
TEST_F(Transactions, NewLink_RollBack)
143
{
144
auto new_nh = graph.createNode();
145
Change::NewLink new_link(graph, new_nh, nhs[0]);
146
147
new_link.rollback(graph);
148
149
EXPECT_FALSE(connected(new_nh, nhs[0]));
150
}
151
152
TEST_F(Transactions, NewLink_Commit)
153
{
154
auto new_nh = graph.createNode();
155
Change::NewLink new_link(graph, new_nh, nhs[0]);
156
157
new_link.commit(graph);
158
159
EXPECT_TRUE(connected(new_nh, nhs[0]));
160
}
161
162
TEST_F(Transactions, DropNode_Create)
163
{
164
auto new_nh = graph.createNode();
165
Change::DropNode drop_node(new_nh);
166
167
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
168
EXPECT_TRUE(contains(graph, new_nh));
169
}
170
171
TEST_F(Transactions, DropNode_RollBack)
172
{
173
auto new_nh = graph.createNode();
174
Change::DropNode drop_node(new_nh);
175
176
drop_node.rollback(graph);
177
178
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
179
EXPECT_TRUE(contains(graph, new_nh));
180
}
181
182
TEST_F(Transactions, DropNode_Commit)
183
{
184
auto new_nh = graph.createNode();
185
Change::DropNode drop_node(new_nh);
186
187
drop_node.commit(graph);
188
189
EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));
190
EXPECT_FALSE(contains(graph, new_nh));
191
}
192
193
TEST_F(Transactions, Fusion_Commit)
194
{
195
namespace C = Change;
196
197
fuse();
198
commit();
199
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]));
203
}
204
205
TEST_F(Transactions, Fusion_RollBack)
206
{
207
namespace C = Change;
208
209
fuse();
210
rollback();
211
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));
215
216
for (int i = 0; i < static_cast<int>(node_nums) - 1; ++i)
217
{
218
EXPECT_TRUE(connected(nhs[i], nhs[i + 1]));
219
}
220
}
221
222
} // opencv_test
223
224