29 #include <gtest/gtest-spi.h>
30 #include <gtest/gtest.h>
39 TEST(StatsGroupTest, ConstructNoParent)
41 Stats::Group root(
nullptr);
42 ASSERT_EQ(root.getStatGroups().size(), 0);
46 TEST(StatsGroupTest, AddGetSingleStatGroup)
48 Stats::Group root(
nullptr);
49 Stats::Group node1(
nullptr);
50 root.addStatGroup(
"Node1", &node1);
52 const auto root_map = root.getStatGroups();
53 ASSERT_EQ(root_map.size(), 1);
54 ASSERT_NE(root_map.find(
"Node1"), root_map.end());
56 ASSERT_EQ(node1.getStatGroups().size(), 0);
60 TEST(StatsGroupDeathTest, AddUniqueNameStatGroup)
62 Stats::Group root(
nullptr);
63 Stats::Group node1(
nullptr);
64 Stats::Group node2(
nullptr);
65 root.addStatGroup(
"Node1", &node1);
66 ASSERT_ANY_THROW(root.addStatGroup(
"Node1", &node2));
70 TEST(StatsGroupTest, AddNotUniqueNameAmongGroups)
72 Stats::Group root(
nullptr);
73 Stats::Group node1(
nullptr);
74 Stats::Group node2(
nullptr);
75 Stats::Group node1_1(
nullptr);
76 root.addStatGroup(
"Node1", &node1);
77 node1.addStatGroup(
"Node1_1", &node1_1);
78 ASSERT_NO_THROW(node1.addStatGroup(
"Node1", &node2));
82 TEST(StatsGroupDeathTest, AddNull)
84 Stats::Group root(
nullptr);
85 ASSERT_ANY_THROW(root.addStatGroup(
"Node1",
nullptr));
89 TEST(StatsGroupDeathTest, AddItself)
91 Stats::Group root(
nullptr);
92 ASSERT_ANY_THROW(root.addStatGroup(
"Node1", &root));
96 TEST(StatsGroupDeathTest, DISABLED_AddCycle)
98 Stats::Group root(
nullptr);
99 Stats::Group node1(
nullptr);
100 Stats::Group node1_1(
nullptr);
101 root.addStatGroup(
"Node1", &node1);
102 node1.addStatGroup(
"Node1_1", &node1_1);
103 ASSERT_ANY_THROW(node1_1.addStatGroup(
"Root", &root));
107 TEST(StatsGroupTest, AddGetMultipleStatGroup)
109 Stats::Group root(
nullptr);
110 Stats::Group node1(
nullptr);
111 Stats::Group node2(
nullptr);
112 root.addStatGroup(
"Node1", &node1);
113 root.addStatGroup(
"Node2", &node2);
115 const auto root_map = root.getStatGroups();
116 ASSERT_EQ(root_map.size(), 2);
117 ASSERT_NE(root_map.find(
"Node1"), root_map.end());
118 ASSERT_NE(root_map.find(
"Node2"), root_map.end());
120 ASSERT_EQ(node1.getStatGroups().size(), 0);
121 ASSERT_EQ(node2.getStatGroups().size(), 0);
125 TEST(StatsGroupTest, ConstructCorrectlyAssigned)
127 Stats::Group root(
nullptr);
128 Stats::Group node1(
nullptr);
129 Stats::Group node1_1(
nullptr);
130 Stats::Group node1_1_1(
nullptr);
131 root.addStatGroup(
"Node1", &node1);
132 node1.addStatGroup(
"Node1_1", &node1_1);
133 node1_1.addStatGroup(
"Node1_1_1", &node1_1_1);
135 ASSERT_EQ(node1.getStatGroups().find(
"Node1_1")->second->getStatGroups(),
136 node1_1.getStatGroups());
147 TEST(StatsGroupTest, ConstructOneLevelLinear)
149 Stats::Group root(
nullptr);
150 Stats::Group node1(&root,
"Node1");
152 const auto root_map = root.getStatGroups();
153 ASSERT_EQ(root_map.size(), 1);
154 ASSERT_NE(root_map.find(
"Node1"), root_map.end());
156 ASSERT_EQ(node1.getStatGroups().size(), 0);
167 TEST(StatsGroupTest, ConstructOneLevelOfTwoNodes)
169 Stats::Group root(
nullptr);
170 Stats::Group node1(&root,
"Node1");
171 Stats::Group node2(&root,
"Node2");
173 const auto root_map = root.getStatGroups();
174 ASSERT_EQ(root_map.size(), 2);
175 ASSERT_NE(root_map.find(
"Node1"), root_map.end());
176 ASSERT_NE(root_map.find(
"Node2"), root_map.end());
178 ASSERT_EQ(node1.getStatGroups().size(), 0);
179 ASSERT_EQ(node2.getStatGroups().size(), 0);
192 TEST(StatsGroupTest, ConstructTwoLevelsLinear)
194 Stats::Group root(
nullptr);
195 Stats::Group node1(&root,
"Node1");
196 Stats::Group node1_1(&node1,
"Node1_1");
198 const auto root_map = root.getStatGroups();
199 ASSERT_EQ(root_map.size(), 1);
200 ASSERT_NE(root_map.find(
"Node1"), root_map.end());
201 ASSERT_EQ(root_map.find(
"Node1_1"), root_map.end());
203 ASSERT_EQ(node1.getStatGroups().size(), 1);
204 ASSERT_NE(node1.getStatGroups().find(
"Node1_1"),
205 node1.getStatGroups().end());
207 ASSERT_EQ(node1_1.getStatGroups().size(), 0);
220 TEST(StatsGroupTest, ConstructTwoLevelsUnbalancedTree)
222 Stats::Group root(
nullptr);
223 Stats::Group node1(&root,
"Node1");
224 Stats::Group node2(&root,
"Node2");
225 Stats::Group node1_1(&node1,
"Node1_1");
226 Stats::Group node2_1(&node2,
"Node2_1");
227 Stats::Group node2_2(&node2,
"Node2_2");
229 const auto root_map = root.getStatGroups();
230 ASSERT_EQ(root_map.size(), 2);
231 ASSERT_NE(root_map.find(
"Node1"), root_map.end());
232 ASSERT_NE(root_map.find(
"Node2"), root_map.end());
233 ASSERT_EQ(root_map.find(
"Node1_1"), root_map.end());
234 ASSERT_EQ(root_map.find(
"Node2_1"), root_map.end());
235 ASSERT_EQ(root_map.find(
"Node2_2"), root_map.end());
237 ASSERT_EQ(node1.getStatGroups().size(), 1);
238 ASSERT_NE(node1.getStatGroups().find(
"Node1_1"),
239 node1.getStatGroups().end());
240 ASSERT_EQ(node1.getStatGroups().find(
"Node2_1"),
241 node1.getStatGroups().end());
242 ASSERT_EQ(node1.getStatGroups().find(
"Node2_2"),
243 node1.getStatGroups().end());
245 ASSERT_EQ(node2.getStatGroups().size(), 2);
246 ASSERT_EQ(node2.getStatGroups().find(
"Node1_1"),
247 node2.getStatGroups().end());
248 ASSERT_NE(node2.getStatGroups().find(
"Node2_1"),
249 node2.getStatGroups().end());
250 ASSERT_NE(node2.getStatGroups().find(
"Node2_2"),
251 node2.getStatGroups().end());
253 ASSERT_EQ(node1_1.getStatGroups().size(), 0);
254 ASSERT_EQ(node2_1.getStatGroups().size(), 0);
255 ASSERT_EQ(node2_2.getStatGroups().size(), 0);
261 using Stats::Info::Info;
265 bool check()
const override {
return true; }
267 void reset()
override { value = 0; }
268 bool zero()
const override {
return false; }
269 void visit(Stats::Output &visitor)
override {}
273 TEST(StatsGroupTest, AddGetStat)
275 Stats::Group root(
nullptr);
276 auto info_vec = root.getStats();
277 ASSERT_EQ(info_vec.size(), 0);
280 info.setName(
"InfoAddGetStat");
282 info_vec = root.getStats();
283 ASSERT_EQ(info_vec.size(), 1);
284 ASSERT_EQ(info_vec[0]->
name,
"InfoAddGetStat");
287 info2.setName(
"InfoAddGetStat2");
288 root.addStat(&info2);
289 info_vec = root.getStats();
290 ASSERT_EQ(info_vec.size(), 2);
291 ASSERT_EQ(info_vec[0]->
name,
"InfoAddGetStat");
292 ASSERT_EQ(info_vec[1]->
name,
"InfoAddGetStat2");
296 TEST(StatsGroupDeathTest, MergeStatGroupNoGroup)
298 Stats::Group root(
nullptr);
299 ASSERT_ANY_THROW(root.mergeStatGroup(
nullptr));
303 TEST(StatsGroupDeathTest, MergeStatGroupItself)
305 Stats::Group root(
nullptr);
306 ASSERT_ANY_THROW(root.mergeStatGroup(&root));
310 TEST(StatsGroupTest, MergeStatGroup)
312 Stats::Group root(
nullptr);
313 Stats::Group node1(
nullptr);
316 info.setName(
"InfoMergeStatGroup");
317 node1.addStat(&info);
319 info2.setName(
"InfoMergeStatGroup2");
320 node1.addStat(&info2);
322 root.mergeStatGroup(&node1);
323 auto info_vec = root.getStats();
324 ASSERT_EQ(info_vec.size(), 2);
325 ASSERT_EQ(info_vec[0]->
name,
"InfoMergeStatGroup");
326 ASSERT_EQ(info_vec[1]->
name,
"InfoMergeStatGroup2");
330 TEST(StatsGroupDeathTest, MergeStatGroupMergedParent)
332 Stats::Group root(
nullptr);
333 Stats::Group node1(
nullptr);
334 Stats::Group node2(
nullptr);
335 root.mergeStatGroup(&node2);
336 ASSERT_ANY_THROW(node1.mergeStatGroup(&node2));
343 TEST(StatsGroupTest, AddStatMergedParent)
345 Stats::Group root(
nullptr);
346 Stats::Group node1(
nullptr);
347 Stats::Group node1_1(
nullptr);
349 root.mergeStatGroup(&node1);
350 root.mergeStatGroup(&node1_1);
353 info.setName(
"AddStatMergedParent");
354 node1_1.addStat(&info);
356 auto info_vec = root.getStats();
357 ASSERT_EQ(info_vec.size(), 1);
358 ASSERT_EQ(info_vec[0]->
name,
"AddStatMergedParent");
359 info_vec = node1.getStats();
360 ASSERT_EQ(info_vec.size(), 0);
361 info_vec = node1_1.getStats();
362 ASSERT_EQ(info_vec.size(), 1);
363 ASSERT_EQ(info_vec[0]->
name,
"AddStatMergedParent");
370 TEST(StatsGroupTest, AddStatMergedParentMain)
372 Stats::Group root(
nullptr);
373 Stats::Group node1(
nullptr);
375 root.mergeStatGroup(&node1);
378 info.setName(
"AddStatMergedParentMain");
381 auto info_vec = root.getStats();
382 ASSERT_EQ(info_vec.size(), 1);
383 ASSERT_EQ(info_vec[0]->
name,
"AddStatMergedParentMain");
384 info_vec = node1.getStats();
385 ASSERT_EQ(info_vec.size(), 0);
392 TEST(StatsGroupTest, ConstructNoName)
394 Stats::Group root(
nullptr);
395 Stats::Group node1(&root);
398 info.setName(
"InfoConstructNoName");
399 node1.addStat(&info);
401 auto info_vec = root.getStats();
402 ASSERT_EQ(info_vec.size(), 1);
403 ASSERT_EQ(info_vec[0]->
name,
"InfoConstructNoName");
412 class TestGroup :
public Stats::Group
415 using Stats::Group::Group;
423 Stats::Group::regStats();
427 TestGroup root(
nullptr);
429 TestGroup node1(&root,
"Node1");
431 TestGroup node1_1(&node1,
"Node1_1");
433 TestGroup node1_2(&node1_1);
437 ASSERT_EQ(root.value, 1);
438 ASSERT_EQ(node1.value, 3);
439 ASSERT_EQ(node1_1.value, 4);
440 ASSERT_EQ(node1_2.value, 5);
448 TEST(StatsGroupTest, ResetStats)
450 Stats::Group root(
nullptr);
451 Stats::Group node1(&root,
"Node1");
452 Stats::Group node1_1(&node1,
"Node1_1");
453 Stats::Group node1_2(&node1_1);
456 info.setName(
"InfoResetStats");
461 info2.setName(
"InfoResetStats2");
463 node1.addStat(&info2);
466 info3.setName(
"InfoResetStats3");
468 node1_1.addStat(&info3);
471 info4.setName(
"InfoResetStats4");
473 node1_1.addStat(&info4);
476 info5.setName(
"InfoResetStats5");
478 node1_2.addStat(&info5);
481 ASSERT_EQ(info.
value, 1);
482 ASSERT_EQ(info2.
value, 0);
483 ASSERT_EQ(info3.
value, 0);
484 ASSERT_EQ(info4.
value, 0);
485 ASSERT_EQ(info5.
value, 0);
492 TEST(StatsGroupTest, PreDumpStats)
494 class TestGroup :
public Stats::Group
497 using Stats::Group::Group;
502 preDumpStats()
override
505 Stats::Group::preDumpStats();
509 TestGroup root(
nullptr);
511 TestGroup node1(&root,
"Node1");
513 TestGroup node1_1(&node1,
"Node1_1");
515 TestGroup node1_2(&node1_1);
518 node1.preDumpStats();
519 ASSERT_EQ(root.value, 1);
520 ASSERT_EQ(node1.value, 3);
521 ASSERT_EQ(node1_1.value, 4);
522 ASSERT_EQ(node1_2.value, 5);
526 TEST(StatsGroupTest, ResolveStatNone)
528 Stats::Group root(
nullptr);
531 info.setName(
"InfoResolveStatNone");
534 auto info_found = root.resolveStat(
"InfoResolveStatAny");
535 ASSERT_EQ(info_found,
nullptr);
539 TEST(StatsGroupTest, ResolveStatSelf)
541 Stats::Group root(
nullptr);
544 info.setName(
"InfoResolveStatSelf");
548 info2.setName(
"InfoResolveStatSelf2");
549 root.addStat(&info2);
552 info3.setName(
"InfoResolveStatSelf3");
553 root.addStat(&info3);
555 auto info_found = root.resolveStat(
"InfoResolveStatSelf");
556 ASSERT_NE(info_found,
nullptr);
557 ASSERT_EQ(info_found->name,
"InfoResolveStatSelf");
559 info_found = root.resolveStat(
"InfoResolveStatSelf2");
560 ASSERT_NE(info_found,
nullptr);
561 ASSERT_EQ(info_found->name,
"InfoResolveStatSelf2");
563 info_found = root.resolveStat(
"InfoResolveStatSelf3");
564 ASSERT_NE(info_found,
nullptr);
565 ASSERT_EQ(info_found->name,
"InfoResolveStatSelf3");
569 TEST(StatsGroupTest, ResolveSubGroupStatFromParent)
571 Stats::Group root(
nullptr);
572 Stats::Group node1(&root,
"Node1");
573 Stats::Group node1_1(&node1,
"Node1_1");
574 Stats::Group node1_1_1(&node1_1,
"Node1_1_1");
577 info.setName(
"InfoResolveSubGroupStatFromParent");
578 node1.addStat(&info);
581 info2.setName(
"InfoResolveSubGroupStatFromParent2");
582 node1_1.addStat(&info2);
585 info3.setName(
"InfoResolveSubGroupStatFromParent3");
586 node1_1_1.addStat(&info3);
589 root.resolveStat(
"Node1.InfoResolveSubGroupStatFromParent");
590 ASSERT_NE(info_found,
nullptr);
591 ASSERT_EQ(info_found->name,
"InfoResolveSubGroupStatFromParent");
593 info_found = root.resolveStat(
594 "Node1.Node1_1.InfoResolveSubGroupStatFromParent2");
595 ASSERT_NE(info_found,
nullptr);
596 ASSERT_EQ(info_found->name,
"InfoResolveSubGroupStatFromParent2");
598 info_found = root.resolveStat(
599 "Node1.Node1_1.Node1_1_1.InfoResolveSubGroupStatFromParent3");
600 ASSERT_NE(info_found,
nullptr);
601 ASSERT_EQ(info_found->name,
"InfoResolveSubGroupStatFromParent3");
605 TEST(StatsGroupTest, ResolveStatSubGroupOnSubGroup)
607 Stats::Group root(
nullptr);
608 Stats::Group node1(&root,
"Node1");
611 info.setName(
"InfoResolveStatSubGroupOnSubGroup");
614 auto info_found = node1.resolveStat(
"InfoResolveStatSubGroupOnSubGroup");
615 ASSERT_EQ(info_found,
nullptr);
619 TEST(StatsGroupTest, ResolveStatMerged)
621 Stats::Group root(
nullptr);
622 Stats::Group node1(
nullptr);
625 info.setName(
"InfoResolveStatMerged");
626 node1.addStat(&info);
628 info2.setName(
"InfoResolveStatMerged2");
629 node1.addStat(&info2);
631 root.mergeStatGroup(&node1);
633 auto info_found = root.resolveStat(
"InfoResolveStatMerged");
634 ASSERT_NE(info_found,
nullptr);
635 ASSERT_EQ(info_found->name,
"InfoResolveStatMerged");
637 info_found = root.resolveStat(
"InfoResolveStatMerged2");
638 ASSERT_NE(info_found,
nullptr);
639 ASSERT_EQ(info_found->name,
"InfoResolveStatMerged2");
643 TEST(StatsGroupTest, ResolveStatMergedSubGroup)
645 Stats::Group root(
nullptr);
646 Stats::Group node1(
nullptr);
647 Stats::Group node2(
nullptr);
650 info.setName(
"InfoResolveStatMergedSubGroup");
651 node2.addStat(&info);
653 root.addStatGroup(
"Node1", &node1);
654 node1.mergeStatGroup(&node2);
656 auto info_found = root.resolveStat(
"Node1.InfoResolveStatMergedSubGroup");
657 ASSERT_NE(info_found,
nullptr);
658 ASSERT_EQ(info_found->name,
"InfoResolveStatMergedSubGroup");