gapi_transactions_test.cpp
9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2018 - 2020 Intel Corporation
#include "../test_precomp.hpp"
#include <ade/graph.hpp>
#include <ade/typed_graph.hpp>
#include "compiler/transactions.hpp"
namespace opencv_test
{
namespace
{
bool contains(const ade::Graph& graph, const ade::NodeHandle& node)
{
auto nodes = graph.nodes();
return nodes.end() != std::find(nodes.begin(), nodes.end(), node);
}
bool connected(const ade::NodeHandle& src_node, const ade::NodeHandle& dst_node)
{
auto nodes = src_node->outNodes();
return nodes.end() != std::find(nodes.begin(), nodes.end(), dst_node);
}
struct SimpleGraph
{
// ehs[0] ehs[1] ehs[2] ehs[3]
// nhs[0] -- > nhs[1] --> nhs[2] --> nhs[3] --> nhs[4]
enum { node_nums = 5 };
ade::Graph graph;
ade::NodeHandle fused_nh; // For check that fusion node is connected to the
// inputs of the prod and the outputs of the cons
std::array<ade::NodeHandle, node_nums> nhs;
std::array<ade::EdgeHandle, node_nums - 1> ehs;
using Change = ChangeT<>;
Change::List changes;
SimpleGraph()
{
nhs[0] = graph.createNode();
for (int i = 1; i < node_nums; ++i)
{
nhs[i ] = graph.createNode();
ehs[i - 1] = graph.link(nhs[i - 1], nhs[i]);
}
}
void fuse()
{
// nhs[0] --> fused_nh --> nhs[4]
fused_nh = graph.createNode();
changes.enqueue<Change::NodeCreated>(fused_nh);
changes.enqueue<Change::NewLink> (graph, nhs[0], fused_nh);
changes.enqueue<Change::DropLink>(graph, nhs[1], ehs[0]);
changes.enqueue<Change::NewLink> (graph, fused_nh, nhs[4]);
changes.enqueue<Change::DropLink>(graph, nhs[3], ehs[3]);
changes.enqueue<Change::DropLink>(graph, nhs[1], ehs[1]);
changes.enqueue<Change::DropLink>(graph, nhs[2], ehs[2]);
changes.enqueue<Change::DropNode>(nhs[1]);
changes.enqueue<Change::DropNode>(nhs[2]);
changes.enqueue<Change::DropNode>(nhs[3]);
}
void commit() { changes.commit(graph); }
void rollback() { changes.rollback(graph); }
};
struct Transactions: public ::testing::Test, public SimpleGraph {};
} // anonymous namespace
TEST_F(Transactions, NodeCreated_Create)
{
auto new_nh = graph.createNode();
Change::NodeCreated node_created(new_nh);
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_TRUE(contains(graph, new_nh));
}
TEST_F(Transactions, NodeCreated_RollBack)
{
auto new_nh = graph.createNode();
Change::NodeCreated node_created(new_nh);
node_created.rollback(graph);
EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_FALSE(contains(graph, new_nh));
}
TEST_F(Transactions, NodeCreated_Commit)
{
auto new_nh = graph.createNode();
Change::NodeCreated node_created(new_nh);
node_created.commit(graph);
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_TRUE(contains(graph, new_nh));
}
TEST_F(Transactions, DropLink_Create)
{
Change::DropLink drop_link(graph, nhs[0], ehs[0]);
EXPECT_FALSE(connected(nhs[0], nhs[1]));
}
TEST_F(Transactions, DropLink_RollBack)
{
Change::DropLink drop_link(graph, nhs[0], ehs[0]);
drop_link.rollback(graph);
EXPECT_TRUE(connected(nhs[0], nhs[1]));
}
TEST_F(Transactions, DropLink_Commit)
{
Change::DropLink drop_link(graph, nhs[0], ehs[0]);
drop_link.commit(graph);
EXPECT_FALSE(connected(nhs[0], nhs[1]));
}
TEST_F(Transactions, NewLink_Create)
{
auto new_nh = graph.createNode();
Change::NewLink new_link(graph, new_nh, nhs[0]);
EXPECT_TRUE(connected(new_nh, nhs[0]));
}
TEST_F(Transactions, NewLink_RollBack)
{
auto new_nh = graph.createNode();
Change::NewLink new_link(graph, new_nh, nhs[0]);
new_link.rollback(graph);
EXPECT_FALSE(connected(new_nh, nhs[0]));
}
TEST_F(Transactions, NewLink_Commit)
{
auto new_nh = graph.createNode();
Change::NewLink new_link(graph, new_nh, nhs[0]);
new_link.commit(graph);
EXPECT_TRUE(connected(new_nh, nhs[0]));
}
TEST_F(Transactions, DropNode_Create)
{
auto new_nh = graph.createNode();
Change::DropNode drop_node(new_nh);
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_TRUE(contains(graph, new_nh));
}
TEST_F(Transactions, DropNode_RollBack)
{
auto new_nh = graph.createNode();
Change::DropNode drop_node(new_nh);
drop_node.rollback(graph);
EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_TRUE(contains(graph, new_nh));
}
TEST_F(Transactions, DropNode_Commit)
{
auto new_nh = graph.createNode();
Change::DropNode drop_node(new_nh);
drop_node.commit(graph);
EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_FALSE(contains(graph, new_nh));
}
TEST_F(Transactions, Fusion_Commit)
{
fuse();
commit();
EXPECT_EQ(3u, static_cast<std::size_t>(graph.nodes().size()));
EXPECT_TRUE(connected(nhs[0] , fused_nh));
EXPECT_TRUE(connected(fused_nh, nhs[4]));
}
TEST_F(Transactions, Fusion_RollBack)
{
fuse();
rollback();
EXPECT_EQ(static_cast<std::size_t>(node_nums),
static_cast<std::size_t>(graph.nodes().size()));
EXPECT_FALSE(contains(graph, fused_nh));
for (int i = 0; i < static_cast<int>(node_nums) - 1; ++i)
{
EXPECT_TRUE(connected(nhs[i], nhs[i + 1]));
}
}
namespace
{
struct MetaInt {
static const char *name() { return "int_meta"; }
int x;
};
struct MetaStr {
static const char *name() { return "string_meta"; }
std::string s;
};
}
TEST(PreservedMeta, TestMetaCopy_Full)
{
ade::Graph g;
ade::TypedGraph<MetaInt, MetaStr> tg(g);
auto src_nh = tg.createNode();
tg.metadata(src_nh).set(MetaInt{42});
tg.metadata(src_nh).set(MetaStr{"hi"});
auto dst_nh = tg.createNode();
EXPECT_FALSE(tg.metadata(dst_nh).contains<MetaInt>());
EXPECT_FALSE(tg.metadata(dst_nh).contains<MetaStr>());
// Here we specify all the meta types we know about the src node
// Assume Preserved copies its all for us
Preserved<ade::NodeHandle, MetaInt, MetaStr>(g, src_nh).copyTo(g, dst_nh);
ASSERT_TRUE(tg.metadata(dst_nh).contains<MetaInt>());
ASSERT_TRUE(tg.metadata(dst_nh).contains<MetaStr>());
EXPECT_EQ(42, tg.metadata(dst_nh).get<MetaInt>().x);
EXPECT_EQ("hi", tg.metadata(dst_nh).get<MetaStr>().s);
}
TEST(PreservedMeta, TestMetaCopy_Partial_Dst)
{
ade::Graph g;
ade::TypedGraph<MetaInt, MetaStr> tg(g);
auto tmp_nh1 = tg.createNode();
auto tmp_nh2 = tg.createNode();
auto src_eh = tg.link(tmp_nh1, tmp_nh2);
tg.metadata(src_eh).set(MetaInt{42});
tg.metadata(src_eh).set(MetaStr{"hi"});
auto tmp_nh3 = tg.createNode();
auto tmp_nh4 = tg.createNode();
auto dst_eh = tg.link(tmp_nh3, tmp_nh4);
EXPECT_FALSE(tg.metadata(dst_eh).contains<MetaInt>());
EXPECT_FALSE(tg.metadata(dst_eh).contains<MetaStr>());
// Here we specify just a single meta type for the src node
// Assume Preserved copies only this type and nothing else
Preserved<ade::EdgeHandle, MetaStr>(g, src_eh).copyTo(g, dst_eh);
ASSERT_FALSE(tg.metadata(dst_eh).contains<MetaInt>());
ASSERT_TRUE (tg.metadata(dst_eh).contains<MetaStr>());
EXPECT_EQ("hi", tg.metadata(dst_eh).get<MetaStr>().s);
}
TEST(PreservedMeta, TestMetaCopy_Partial_Src)
{
ade::Graph g;
ade::TypedGraph<MetaInt, MetaStr> tg(g);
auto src_nh = tg.createNode();
tg.metadata(src_nh).set(MetaInt{42});
auto dst_nh = tg.createNode();
EXPECT_FALSE(tg.metadata(dst_nh).contains<MetaInt>());
EXPECT_FALSE(tg.metadata(dst_nh).contains<MetaStr>());
// Here we specify all the meta types we know about the src node
// but the src node has just one of them.
// A valid situation, only MetaInt to be copied.
Preserved<ade::NodeHandle, MetaInt, MetaStr>(g, src_nh).copyTo(g, dst_nh);
ASSERT_TRUE (tg.metadata(dst_nh).contains<MetaInt>());
ASSERT_FALSE(tg.metadata(dst_nh).contains<MetaStr>());
EXPECT_EQ(42, tg.metadata(dst_nh).get<MetaInt>().x);
}
TEST(PreservedMeta, TestMetaCopy_Nothing)
{
ade::Graph g;
ade::TypedGraph<MetaInt, MetaStr> tg(g);
auto src_nh = tg.createNode();
auto dst_nh = tg.createNode();
EXPECT_FALSE(tg.metadata(src_nh).contains<MetaInt>());
EXPECT_FALSE(tg.metadata(src_nh).contains<MetaStr>());
EXPECT_FALSE(tg.metadata(dst_nh).contains<MetaInt>());
EXPECT_FALSE(tg.metadata(dst_nh).contains<MetaStr>());
// Here we specify all the meta types we know about the src node
// but the src node has none of those. See how it works now
Preserved<ade::NodeHandle, MetaInt, MetaStr>(g, src_nh).copyTo(g, dst_nh);
ASSERT_FALSE(tg.metadata(dst_nh).contains<MetaInt>());
ASSERT_FALSE(tg.metadata(dst_nh).contains<MetaStr>());
}
TEST(PreservedMeta, DropEdge)
{
ade::Graph g;
ade::TypedGraph<MetaInt, MetaStr> tg(g);
auto nh1 = tg.createNode();
auto nh2 = tg.createNode();
auto eh = tg.link(nh1, nh2);
tg.metadata(eh).set(MetaInt{42});
tg.metadata(eh).set(MetaStr{"hi"});
// Drop an edge using the transaction API
using Change = ChangeT<MetaInt, MetaStr>;
Change::List changes;
changes.enqueue<Change::DropLink>(g, nh1, eh);
EXPECT_EQ(0u, nh1->outNodes().size());
EXPECT_EQ(nullptr, eh);
// Now restore the edge and check if it's meta was restored
changes.rollback(g);
ASSERT_EQ(1u, nh1->outNodes().size());
eh = *nh1->outEdges().begin();
ASSERT_TRUE(tg.metadata(eh).contains<MetaInt>());
ASSERT_TRUE(tg.metadata(eh).contains<MetaStr>());
EXPECT_EQ(42, tg.metadata(eh).get<MetaInt>().x);
EXPECT_EQ("hi", tg.metadata(eh).get<MetaStr>().s);
}
} // opencv_test