generated from cpp-best-practices/gui_starter_template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcpp17_tests.cpp
179 lines (139 loc) · 5.58 KB
/
cpp17_tests.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
#include <catch2/catch.hpp>
#include <lefticus/tools/flat_map.hpp>
#include <lefticus/tools/simple_stack_flat_map.hpp>
#include <lefticus/tools/simple_stack_string.hpp>
#include <lefticus/tools/simple_stack_vector.hpp>
#include <lefticus/tools/static_views.hpp>
#ifdef CATCH_CONFIG_RUNTIME_STATIC_REQUIRE
#define CONSTEXPR
#else
// NOLINTNEXTLINE
#define CONSTEXPR constexpr
#endif
#if __cpp_lib_constexpr_string >= 201907L
constexpr std::string make_string()
{
std::string result;
result = "Hello ";
result += "World";
result += " Test Long String";
return result;
}
TEST_CASE("to_string_view produces a std::string_view from std::string")
{
constexpr static auto result = lefticus::tools::to_string_view([]() { return make_string(); });
static_assert(std::is_same_v<decltype(result), const std::string_view>);
STATIC_REQUIRE(result == "Hello World Test Long String");
}
#endif
#if __cpp_lib_constexpr_vector >= 201907L
constexpr std::vector<double> make_vector()
{
std::vector result{ 1.2, 2.4 };
result.push_back(3.6);
return result;
}
TEST_CASE("to_span produces an std::span from std::vector")
{
constexpr static auto result = lefticus::tools::to_span([]() { return make_vector(); });
static_assert(std::is_same_v<decltype(result), const std::span<const double>>);
STATIC_REQUIRE(result[0] == 1.2);
STATIC_REQUIRE(result.size() == 3);
}
#endif
constexpr auto make_vector_like()
{
lefticus::tools::simple_stack_vector<double, 10> result{ 1.2, 2.4 };
result.push_back(3.6);
return result;
}
TEST_CASE("[to_span] produces an std::span from simple_stack_vector")
{
CONSTEXPR const auto result = lefticus::tools::to_span([]() { return make_vector_like(); });
static_assert(std::is_same_v<decltype(result), const std::span<const double>>);
STATIC_REQUIRE(result[0] == 1.2);
STATIC_REQUIRE(result.size() == 3);
}
TEST_CASE("[to_right_sized_array] produces an array of the correct size")
{
CONSTEXPR auto result = lefticus::tools::to_right_sized_array([]() { return make_vector_like(); });
STATIC_REQUIRE(result[0] == 1.2);
STATIC_REQUIRE(result.size() == 3);
STATIC_REQUIRE(sizeof(result) == 3 * sizeof(double));
}
constexpr auto make_string_like()
{
lefticus::tools::simple_stack_string<10> result{ "Hello" };
result.push_back('a');
return result;
}
TEST_CASE("[to_string_view] produces an std::string_view from simple_stack_string")
{
CONSTEXPR const auto result = lefticus::tools::to_string_view([]() { return make_string_like(); });
static_assert(std::is_same_v<decltype(result), const std::string_view>);
STATIC_REQUIRE(result[0] == 'H');
STATIC_REQUIRE(result.size() == 6);
}
TEST_CASE("[resize] can right-size a container level 1")// NOLINT (cognitive complexity)
{
constexpr lefticus::tools::simple_stack_vector<int, 16> vec{ 1, 2, 3, 4, 5 };
STATIC_REQUIRE(vec.size() == 5);
STATIC_REQUIRE(vec.max_size() == 16);
constexpr auto bigger = lefticus::tools::stackify<32>(vec);
// stackify should never grow something that's already stack-based
STATIC_REQUIRE(bigger.size() == 5);
STATIC_REQUIRE(bigger.max_size() == 16);
constexpr auto max_sizes = lefticus::tools::max_element_size(bigger);
STATIC_REQUIRE(max_sizes.first == 5);
STATIC_REQUIRE(max_sizes.second == 1);
constexpr auto minimized = lefticus::tools::resize<max_sizes>(bigger);
STATIC_REQUIRE(minimized.capacity() == 5);
STATIC_REQUIRE(sizeof(minimized) < sizeof(bigger));
}
TEST_CASE("[resize] can right-size a container level 2")// NOLINT (cognitive complexity)
{
constexpr lefticus::tools::simple_stack_vector<lefticus::tools::simple_stack_string<16>, 16> vec{
lefticus::tools::simple_stack_string<16>("Hello"), lefticus::tools::simple_stack_string<16>("long string")
};
constexpr auto max_sizes = lefticus::tools::max_element_size(vec);
STATIC_REQUIRE(max_sizes.first == 2);
STATIC_REQUIRE(max_sizes.second == 11);
constexpr auto minimized = lefticus::tools::resize<max_sizes>(vec);
STATIC_REQUIRE(minimized.capacity() == 2);
STATIC_REQUIRE(sizeof(minimized) < sizeof(vec));
}
template<typename T> using vector = lefticus::tools::simple_stack_vector<T, 16>;
template<typename Key, typename Value> using map = lefticus::tools::simple_stack_flat_map<Key, Value, 16>;
using string = lefticus::tools::simple_stack_string<16>;
TEST_CASE("[minimized_stackify] works")// NOLINT (cognitive complexity)
{
using namespace std::string_view_literals;
const auto make_data = []() {
map<string, map<string, vector<int>>> data;
data["hello"]["world"].push_back(42);
data["hello"]["jason"].push_back(72);
data["test"]["data"].push_back(84);
return data;
};
CONSTEXPR auto minimized = lefticus::tools::minimized_stackify<32>(make_data);
STATIC_REQUIRE(minimized.max_size() == 2);
STATIC_REQUIRE(minimized.at("hello").max_size() == 2);
STATIC_REQUIRE(minimized.at("hello").at("world").capacity() == 1);
}
#if __cpp_lib_constexpr_string >= 201907L && __cpp_lib_constexpr_vector >= 201907L
TEST_CASE("[minimized_stackify] works with std::vector, std::string")// NOLINT (cognitive complexity)
{
using namespace std::string_view_literals;
const auto make_data = []() {
lefticus::tools::flat_map<std::string, lefticus::tools::flat_map<std::string, std::vector<int>>> data;
data["hello"]["world"].push_back(42);
data["hello"]["jason"].push_back(72);
data["test"]["data"].push_back(84);
return data;
};
CONSTEXPR auto minimized = lefticus::tools::minimized_stackify<32>(make_data);
STATIC_REQUIRE(minimized.max_size() == 2);
STATIC_REQUIRE(minimized.at("hello").max_size() == 2);
STATIC_REQUIRE(minimized.at("hello").at("world").capacity() == 1);
}
#endif