-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstl_compatibility.cpp
152 lines (143 loc) · 5.56 KB
/
stl_compatibility.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
// #define CATCH_CONFIG_MAIN
#include <numeric> // std::accumulate
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include "dict.hpp"
using Dict = cppdict::Dict<int, double, std::string>;
TEST_CASE("can get size", "[cppdict::Dict<int, double, std::string> stl_compat]")
{
Dict dict;
SECTION("an empty dic has size == 0")
{
REQUIRE(std::size(dict) == 0);
}
dict["first"] = 3.14;
dict["second"] = 1;
dict["third"]["level2"] = std::string{"hello"};
SECTION("A dic with 3 elements has size == 3")
{
REQUIRE(std::size(dict) == 3);
}
}
TEST_CASE("iterate over children", "[cppdict::Dict<int, double, std::string> stl_compat]")
{
Dict dict;
dict["first"] = 3.14;
dict["second"] = 1;
dict["third"]["level2"] = std::string{"hello"};
SECTION("A dict with 3 elements has size == 3")
{
REQUIRE(std::accumulate(std::cbegin(dict), std::cend(dict), 0UL,
[](std::size_t i, const auto&) { return i + 1; })
== 3UL);
}
SECTION("Can't iterate a leaf")
{
REQUIRE_THROWS_WITH(std::begin(dict["first"]), "cppdict: can't iterate this node");
}
SECTION("Can't iterate an empty node")
{
REQUIRE_THROWS_WITH(std::begin(dict["this node is empty"]),
"cppdict: can't iterate this node");
}
}
TEST_CASE("Visit node's children", "[cppdict::Dict<int, double, std::string> stl_compat>]")
{
Dict dict;
dict["first"] = 3.14;
dict["second"] = 1;
dict["third"]["level2"] = std::string{"hello"};
dict["fourth"] = std::string{"world"};
dict["empty"];
SECTION("Can visit in read only mode")
{
auto node_count = 0UL;
auto empty_count = 0UL;
auto int_count = 0UL;
auto double_count = 0UL;
auto string_count = 0UL;
dict.visit(
cppdict::visit_all_nodes,
[&node_count](const std::string&, const auto&) { node_count++; },
[&empty_count](const std::string&, const Dict::empty_leaf_t&) { empty_count++; },
[&double_count](const std::string&, double) { double_count++; },
[&int_count](const std::string&, int) { int_count++; },
[&string_count](const std::string&, const std::string&) { string_count++; });
REQUIRE(node_count == 1UL);
REQUIRE(empty_count == 1UL);
REQUIRE(int_count == 1UL);
REQUIRE(double_count == 1UL);
REQUIRE(string_count == 1UL);
}
SECTION("Can visit values only, IE filter out nodes")
{
auto node_count = 0UL;
auto int_count = 0UL;
auto double_count = 0UL;
auto string_count = 0UL;
dict.visit(
cppdict::visit_values_only,
[&node_count](const std::string&, const auto&) { node_count++; },
[&double_count](const std::string&, double) { double_count++; },
[&int_count](const std::string&, int) { int_count++; },
[&string_count](const std::string&, const std::string&) { string_count++; });
REQUIRE(node_count == 0UL);
REQUIRE(int_count == 1UL);
REQUIRE(double_count == 1UL);
REQUIRE(string_count == 1UL);
}
SECTION("Can visit values only(default visit), IE filter out nodes")
{
auto node_count = 0UL;
auto int_count = 0UL;
auto double_count = 0UL;
auto string_count = 0UL;
dict.visit([&node_count](const std::string&, const auto&) { node_count++; },
[&double_count](const std::string&, double) { double_count++; },
[&int_count](const std::string&, int) { int_count++; },
[&string_count](const std::string&, const std::string&) { string_count++; });
REQUIRE(node_count == 0UL);
REQUIRE(int_count == 1UL);
REQUIRE(double_count == 1UL);
REQUIRE(string_count == 1UL);
}
SECTION("Visitor can modify values")
{
dict.visit(
cppdict::visit_all_nodes, [](const std::string&, const auto&) {},
[](const std::string&, double) {}, [](const std::string&, int& value) { value = 42; },
[](const std::string&, const std::string&) {});
REQUIRE(dict["second"].to<int>() == 42);
}
SECTION("Visiting leaves is forbiden")
{
REQUIRE_THROWS_WITH(dict["first"].visit([](const std::string&, const auto&) {}),
"cppdict: can only visit node");
}
}
TEST_CASE("Visit leaves", "[cppdict::Dict<int, double, std::string> stl_compat>]")
{
Dict dict;
dict["first"] = 3.14;
dict["second"] = 1;
dict["third"]["level2"] = std::string{"hello"};
dict["third"]["level2_2"] = .2;
dict["third"]["level2_3"] = 55;
dict["third"]["level2_4"]["level3"] = 33;
SECTION("Can visit in read only mode")
{
auto node_count = 0UL;
auto int_count = 0UL;
auto double_count = 0UL;
auto string_count = 0UL;
dict.visit_leaves(
[&node_count](const std::string&, const auto&) { node_count++; },
[&double_count](const std::string&, double) { double_count++; },
[&int_count](const std::string&, int) { int_count++; },
[&string_count](const std::string&, const std::string&) { string_count++; });
REQUIRE(node_count == 0UL);
REQUIRE(int_count == 3UL);
REQUIRE(double_count == 2UL);
REQUIRE(string_count == 1UL);
}
}