-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathct_string.cpp
More file actions
194 lines (160 loc) · 6.05 KB
/
ct_string.cpp
File metadata and controls
194 lines (160 loc) · 6.05 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
#include <stdx/ct_string.hpp>
#include <stdx/utility.hpp>
#include <catch2/catch_test_macros.hpp>
#include <iterator>
#include <string_view>
#include <type_traits>
namespace {
template <typename T, T...> struct string_constant {};
} // namespace
TEST_CASE("construction", "[ct_string]") {
[[maybe_unused]] constexpr auto s = stdx::ct_string{"ABC"};
}
TEST_CASE("UDL", "[ct_string]") {
using namespace stdx::ct_string_literals;
[[maybe_unused]] constexpr auto s = "ABC"_cts;
}
TEST_CASE("empty", "[ct_string]") {
constexpr auto s1 = stdx::ct_string{""};
STATIC_REQUIRE(s1.empty());
constexpr auto s2 = stdx::ct_string{"A"};
STATIC_REQUIRE(not s2.empty());
}
TEST_CASE("size", "[ct_string]") {
constexpr auto s = stdx::ct_string{"ABC"};
STATIC_REQUIRE(s.size() == 3u);
STATIC_REQUIRE(not s.empty());
}
TEST_CASE("equality", "[ct_string]") {
constexpr auto s1 = stdx::ct_string{"ABC"};
constexpr auto s2 = stdx::ct_string{"ABC"};
constexpr auto s3 = stdx::ct_string{"ABD"};
STATIC_REQUIRE(s1 == s2);
STATIC_REQUIRE(s1 != s3);
}
TEST_CASE("explicit length construction", "[ct_string]") {
constexpr auto s = stdx::ct_string<3u>{"ABC", 2};
STATIC_REQUIRE(s == stdx::ct_string{"AB"});
}
TEST_CASE("from type", "[ct_string]") {
using T = string_constant<char, 'A', 'B', 'C'>;
constexpr auto s = stdx::ct_string_from_type(T{});
STATIC_REQUIRE(s == stdx::ct_string{"ABC"});
}
TEST_CASE("to type", "[ct_string]") {
constexpr auto s = stdx::ct_string{"ABC"};
constexpr auto sc = stdx::ct_string_to_type<s, string_constant>();
STATIC_REQUIRE(std::is_same_v<decltype(sc),
string_constant<char, 'A', 'B', 'C'> const>);
}
TEST_CASE("to type_t", "[ct_string]") {
constexpr auto s = stdx::ct_string{"ABC"};
using sc = stdx::ct_string_to_type_t<s, string_constant>;
STATIC_REQUIRE(std::is_same_v<sc, string_constant<char, 'A', 'B', 'C'>>);
}
TEST_CASE("to string_view", "[ct_string]") {
constexpr auto s = stdx::ct_string{"ABC"};
auto const sv = static_cast<std::string_view>(s);
CHECK(sv == "ABC");
}
TEST_CASE("string split (character present)", "[ct_string]") {
constexpr auto s = stdx::ct_string{"A.B"};
constexpr auto p = stdx::split<s, '.'>();
STATIC_REQUIRE(p.first == stdx::ct_string{"A"});
STATIC_REQUIRE(p.second == stdx::ct_string{"B"});
}
TEST_CASE("string split (character not present)", "[ct_string]") {
constexpr auto s = stdx::ct_string{"A"};
constexpr auto p = stdx::split<s, '.'>();
STATIC_REQUIRE(p.first == stdx::ct_string{"A"});
STATIC_REQUIRE(p.second.empty());
}
TEST_CASE("string concat (lhs empty)", "[ct_string]") {
constexpr auto s1 = stdx::ct_string{""};
constexpr auto s2 = stdx::ct_string{"def"};
STATIC_REQUIRE(s1 + s2 == stdx::ct_string{"def"});
}
TEST_CASE("string concat (rhs empty)", "[ct_string]") {
constexpr auto s1 = stdx::ct_string{"abc"};
constexpr auto s2 = stdx::ct_string{""};
STATIC_REQUIRE(s1 + s2 == stdx::ct_string{"abc"});
}
TEST_CASE("string concat", "[ct_string]") {
constexpr auto s1 = stdx::ct_string{"abc"};
constexpr auto s2 = stdx::ct_string{"def"};
STATIC_REQUIRE(s1 + s2 == stdx::ct_string{"abcdef"});
}
TEST_CASE("ct_string as iterable", "[ct_string]") {
constexpr auto s = stdx::ct_string{"abc"};
STATIC_REQUIRE(std::next(std::begin(s), std::size(s)) == std::end(s));
auto it = std::cbegin(s);
CHECK(*it++ == 'a');
CHECK(*it++ == 'b');
CHECK(*it++ == 'c');
CHECK(it == std::cend(s));
}
TEST_CASE("ct_string as reverse iterable", "[ct_string]") {
constexpr auto s = stdx::ct_string{"abc"};
STATIC_REQUIRE(std::next(std::rbegin(s), std::size(s)) == std::rend(s));
auto it = std::crbegin(s);
CHECK(*it++ == 'c');
CHECK(*it++ == 'b');
CHECK(*it++ == 'a');
CHECK(it == std::crend(s));
}
namespace {
template <stdx::ct_string S> constexpr auto to_cx_value() {
return CX_VALUE(S);
}
} // namespace
TEST_CASE("template argument as CX_VALUE", "[ct_string]") {
using namespace stdx::ct_string_literals;
constexpr auto s = to_cx_value<"Hello">();
STATIC_REQUIRE(s() == "Hello"_cts);
}
TEST_CASE("wrap ct_string in type", "[ct_string]") {
using namespace stdx::ct_string_literals;
using S = stdx::cts_t<"Hello">;
STATIC_REQUIRE(S::value == "Hello"_cts);
}
TEST_CASE("ct (ct_string)", "[ct_string]") {
using namespace stdx::ct_string_literals;
constexpr auto v1 = stdx::ct<"Hello">();
STATIC_REQUIRE(v1 == "Hello"_ctst);
constexpr auto v2 = stdx::ct<"Hello"_cts>();
STATIC_REQUIRE(v2 == "Hello"_ctst);
}
namespace {
template <stdx::ct_string> constexpr auto conversion_success = true;
} // namespace
TEST_CASE("cts_t can implicitly convert to ct_string", "[ct_string]") {
using namespace stdx::ct_string_literals;
STATIC_REQUIRE(conversion_success<"Hello"_ctst>);
}
TEST_CASE("operator+ works to concat cts_t and ct_string", "[ct_string]") {
using namespace stdx::ct_string_literals;
STATIC_REQUIRE("Hello"_ctst + " world"_cts == "Hello world"_cts);
STATIC_REQUIRE("Hello"_cts + " world"_ctst == "Hello world"_cts);
}
TEST_CASE("is_ct (ct_string)", "[ct_string]") {
using namespace stdx::ct_string_literals;
constexpr auto v1 = stdx::ct<"Hello">();
STATIC_REQUIRE(stdx::is_ct_v<decltype(v1)>);
}
TEST_CASE("CT_WRAP", "[ct_string]") {
using namespace stdx::ct_string_literals;
auto x1 = "hello"_cts;
STATIC_REQUIRE(std::is_same_v<decltype(CT_WRAP(x1)), stdx::ct_string<6>>);
CHECK(CT_WRAP(x1) == "hello"_cts);
auto x2 = "hello"_ctst;
STATIC_REQUIRE(std::is_same_v<decltype(CT_WRAP(x2)), stdx::cts_t<"hello">>);
STATIC_REQUIRE(CT_WRAP(x2) == "hello"_ctst);
constexpr static auto x3 = "hello"_cts;
STATIC_REQUIRE(std::is_same_v<decltype(CT_WRAP(x3)), stdx::cts_t<"hello">>);
STATIC_REQUIRE(CT_WRAP(x3) == "hello"_ctst);
[]<stdx::ct_string X>() {
STATIC_REQUIRE(
std::is_same_v<decltype(CT_WRAP(X)), stdx::cts_t<"hello">>);
STATIC_REQUIRE(CT_WRAP(X) == "hello"_ctst);
}.template operator()<"hello">();
}