-
Notifications
You must be signed in to change notification settings - Fork 47
/
lgtuple_test.cpp
288 lines (208 loc) · 7.33 KB
/
lgtuple_test.cpp
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
// This file is distributed under the BSD 3-Clause License. See LICENSE for details.
#include "lgtuple.hpp"
#include <set>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "lbench.hpp"
#include "lgraph.hpp"
#include "lrand.hpp"
class Lgtuple_test : public ::testing::Test {
protected:
std::vector<Node_pin> dpin;
std::set<std::string> name_set;
void SetUp() override {
auto* lg = Lgraph::create("lgdb_lgtest", "constants", "-");
for (int i = 0; i < 100; ++i) {
dpin.emplace_back(lg->create_node_const(i).get_driver_pin());
}
name_set.clear();
}
std::string create_rand_name(int levels) {
std::vector<std::string> pool
= {"a", "xx", " ", "not_a_dot", "long_name", "zz", "not here", "foo33", "potato", "__b", "nothing", "x", "Zoom"};
Lrand<uint8_t> rng;
std::string name;
while (name.empty() || name_set.count(name)) {
name.clear();
for (int i = 0; i < levels; ++i) {
std::string part = pool[rng.max(pool.size())];
part = part + std::to_string(rng.max(32));
if (name.empty()) {
name = part;
} else {
name = name + "." + part;
}
}
}
name_set.insert(name);
return name;
}
void TearDown() override {}
};
// FIXME
#if 0
TEST_F(Lgtuple_test, flat1) {
Lbench b("lgtuple_test.FLAT1");
std::vector<std::string> names;
Lgtuple tup("flat");
for (auto i = 0u; i < dpin.size(); ++i) {
auto name = create_rand_name(1);
names.emplace_back(name);
if (names[i] == "not here1")
fmt::print("DEBUG {} got deleted by {}\n", names[1], names[i]);
tup.add(name, dpin[i]);
if (i > 0u) {
if (!tup.has_dpin(names[1])) {
fmt::print("{} got deleted by [{}]\n", names[1], names[i]);
}
EXPECT_TRUE(tup.has_dpin(names[1]));
}
}
for (auto i = 0u; i < dpin.size(); ++i) {
EXPECT_TRUE(tup.has_dpin(names[i]));
EXPECT_EQ(tup.get_dpin(names[i]), dpin[i]);
}
}
TEST_F(Lgtuple_test, flat2) {
Lbench b("lgtuple_test.FLAT2");
std::vector<std::string> names;
Lgtuple tup("flat");
for (auto i = 0u; i < dpin.size(); ++i) {
tup.add(std::to_string(i), dpin[i]);
}
for (auto i = 0u; i < dpin.size(); ++i) {
EXPECT_TRUE(tup.has_dpin(std::to_string(i)));
EXPECT_EQ(tup.get_dpin(std::to_string(i)), dpin[i]);
}
}
TEST_F(Lgtuple_test, flat3) {
Lbench b("lgtuple_test.FLAT3");
std::vector<std::string> names;
Lgtuple tup("flat");
for (auto i = 0u; i < dpin.size(); ++i) {
auto name = create_rand_name(1);
names.emplace_back(name);
auto pname = absl::StrCat(":", std::to_string(i), ":", name);
tup.add(pname, dpin[i]);
}
for (auto i = 0u; i < dpin.size(); ++i) {
EXPECT_TRUE(tup.has_dpin(names[i]));
EXPECT_TRUE(tup.has_dpin(std::to_string(i)));
EXPECT_EQ(tup.get_dpin(names[i]), dpin[i]);
EXPECT_EQ(tup.get_dpin(std::to_string(i)), dpin[i]);
auto pname = absl::StrCat(":", std::to_string(i), ":", names[i]);
EXPECT_EQ(tup.get_dpin(pname), dpin[i]);
}
}
TEST_F(Lgtuple_test, hier1) {
Lbench b("lgtuple_test.HIER1");
std::vector<std::string> names;
Lgtuple tup("hier");
for (auto i = 0u; i < dpin.size(); ++i) {
auto name = create_rand_name(3);
names.emplace_back(name);
tup.add(name, dpin[i]);
}
for (auto i = 0u; i < dpin.size(); ++i) {
EXPECT_TRUE(tup.has_dpin(names[i]));
EXPECT_EQ(tup.get_dpin(names[i]), dpin[i]);
}
}
TEST_F(Lgtuple_test, hier2) {
Lbench b("lgtuple_test.HIER2");
std::vector<std::string> names;
Lgtuple tup("hier");
for (auto i = 0u; i < dpin.size(); ++i) {
auto name = create_rand_name(6);
names.emplace_back(name);
auto xx = absl::StrCat(":", std::to_string(i), ":", name);
tup.add(xx, dpin[i]);
}
for (auto i = 0u; i < dpin.size(); ++i) {
EXPECT_TRUE(tup.has_dpin(names[i]));
EXPECT_EQ(tup.get_dpin(names[i]), dpin[i]);
auto xx = absl::StrCat(":", std::to_string(i), ":", names[i]);
EXPECT_EQ(tup.get_dpin(xx), dpin[i]);
}
}
TEST_F(Lgtuple_test, nested1) {
Lbench b("lgtuple_test.NESTED1");
auto top = std::make_shared<Lgtuple>("top");
auto ch1 = std::make_shared<Lgtuple>("ch1");
auto ch2 = std::make_shared<Lgtuple>("ch2");
// Tuples can have local fields
ch1->add("c1_method", dpin[1]);
top->add("ch1", ch1);
EXPECT_EQ(top->get_dpin("ch1.c1_method"), dpin[1]);
EXPECT_EQ(ch1->get_dpin("c1_method"), dpin[1]);
// You can add a tuple to a subfield
top->add("local_2", dpin[2]);
ch1->add("local_3", dpin[3]);
EXPECT_TRUE(top->has_dpin("local_2"));
EXPECT_TRUE(ch1->has_dpin("local_3"));
EXPECT_FALSE(top->has_dpin("local_3")); // copied before
// Hierarchy of tuples can be added directly
ch2->add("p3", dpin[3]);
ch2->add("foo.p4", dpin[4]);
ch2->add("foo.p5", dpin[5]);
top->add("xxx", ch2);
EXPECT_EQ(top->get_dpin("local_2"), dpin[2]);
EXPECT_EQ(top->get_dpin("ch1.c1_method"), dpin[1]);
EXPECT_EQ(top->get_dpin("xxx.p3"), dpin[3]);
EXPECT_EQ(top->get_dpin("xxx.foo.p4"), dpin[4]);
EXPECT_EQ(top->get_dpin("xxx.foo.p5"), dpin[5]);
EXPECT_FALSE(top->has_dpin("xxx.foo")); // No dpin at foo
// Overwriting a method, removes all the children
top->add("xxx.foo", dpin[6]);
EXPECT_EQ(top->get_dpin("local_2"), dpin[2]);
EXPECT_EQ(top->get_dpin("ch1.c1_method"), dpin[1]);
EXPECT_EQ(top->get_dpin("xxx.p3"), dpin[3]);
EXPECT_NE(top->get_dpin("xxx.foo.p4"), dpin[4]);
EXPECT_NE(top->get_dpin("xxx.foo.p5"), dpin[5]);
EXPECT_TRUE(top->has_dpin("xxx.foo")); // dpin at foo
EXPECT_FALSE(top->has_dpin("xxx.foo.p4"));
EXPECT_FALSE(top->has_dpin("xxx.foo.p5"));
}
TEST_F(Lgtuple_test, internal_test) {
auto top = std::make_shared<Lgtuple>("top");
// EXPECT_EQ(top->get_next_free_pos(""), 0);
top->add(":3:foo", dpin[1]);
top->add("foo.:2:bar", dpin[2]);
EXPECT_TRUE(top->has_dpin("foo"));
EXPECT_TRUE(top->has_dpin("3"));
EXPECT_TRUE(top->has_dpin("3.bar"));
EXPECT_TRUE(top->has_dpin("3.2"));
top->add("foo.xxx", dpin[3]);
top->add("foo.:5:xxx.potato", dpin[4]); // It should learn from this
top->add("foo.5.jojojo", dpin[5]);
// EXPECT_EQ(top->get_next_free_pos(""), 4);
// EXPECT_EQ(top->get_next_free_pos("foo"), 2);
EXPECT_EQ(top->get_first_level_pos("lalala.bar"), -1);
EXPECT_EQ(top->get_first_level_pos("5.bar"), 5);
EXPECT_EQ(top->get_first_level_pos(":55:jejeje.bar"), 55);
EXPECT_EQ(top->get_dpin("3"), dpin[1]);
EXPECT_EQ(top->get_dpin("foo"), dpin[1]);
EXPECT_EQ(top->get_dpin("3.bar"), dpin[2]);
EXPECT_EQ(top->get_dpin("3.2"), dpin[2]);
EXPECT_EQ(top->get_dpin("foo.xxx.potato"), dpin[4]);
EXPECT_EQ(top->get_dpin("foo.5.potato"), dpin[4]);
EXPECT_EQ(top->get_dpin("foo.:5:xxx.potato"), dpin[4]);
EXPECT_EQ(top->get_dpin("3.:5:xxx.potato"), dpin[4]);
// EXPECT_EQ(top->get_dpin("3.:5:xxx.potato.0.0.0"), dpin[4]); // 0.0.0 legal
EXPECT_EQ(top->get_dpin("foo.5.jojojo"), dpin[5]);
EXPECT_EQ(top->get_dpin("foo.xxx.jojojo"), dpin[5]);
}
#endif
TEST_F(Lgtuple_test, sort) {
auto top = std::make_shared<Lgtuple>("top");
const std::vector<std::string>& names = {":0:a", ":0:a.__sbits", ":1:b", ":2:c", ":2:c.__ubits", "3", "3.__sbits", "4"};
for (const auto& name : names) {
top->add(name, dpin[1]);
}
const auto& sorted_map = top->get_sort_map();
EXPECT_EQ(sorted_map.size(), names.size());
for (size_t i = 0; i < sorted_map.size(); ++i) {
EXPECT_EQ(sorted_map[i].first, names[i]);
}
}