forked from intel/cpp-std-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathct_format.cpp
More file actions
303 lines (254 loc) · 11.5 KB
/
ct_format.cpp
File metadata and controls
303 lines (254 loc) · 11.5 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <stdx/ct_format.hpp>
#include <stdx/utility.hpp>
#include <catch2/catch_test_macros.hpp>
using namespace stdx::ct_string_literals;
TEST_CASE("detect string format specifiers", "[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(stdx::detail::count_specifiers("{}"sv) == 1u);
STATIC_REQUIRE(stdx::detail::count_specifiers("{} {}"sv) == 2u);
STATIC_REQUIRE(stdx::detail::count_specifiers("{"sv) == 0u);
STATIC_REQUIRE(stdx::detail::count_specifiers("{{"sv) == 0u);
STATIC_REQUIRE(stdx::detail::count_specifiers("{{{}"sv) == 1u);
STATIC_REQUIRE(stdx::detail::count_specifiers("{{}}"sv) == 0u);
STATIC_REQUIRE(stdx::detail::count_specifiers("{{{{"sv) == 0u);
}
TEST_CASE("split format string by specifiers", "[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(stdx::detail::split_specifiers<1>("hello"sv) ==
std::array{"hello"sv});
STATIC_REQUIRE(stdx::detail::split_specifiers<2>("{}"sv) ==
std::array{"{}"sv, ""sv});
STATIC_REQUIRE(stdx::detail::split_specifiers<3>("{} {}"sv) ==
std::array{"{}"sv, " {}"sv, ""sv});
STATIC_REQUIRE(stdx::detail::split_specifiers<2>("{{{}"sv) ==
std::array{"{{{}"sv, ""sv});
STATIC_REQUIRE(stdx::detail::split_specifiers<2>("{} hello"sv) ==
std::array{"{}"sv, " hello"sv});
}
TEST_CASE("format a static string", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello">() == "Hello"_fmt_res);
}
TEST_CASE("format a compile-time stringish argument (CX_VALUE)",
"[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE("world"sv)) ==
"Hello world"_fmt_res);
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE("world"_cts)) ==
"Hello world"_fmt_res);
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE("world")) ==
"Hello world"_fmt_res);
}
TEST_CASE("format a compile-time stringish argument (ct)", "[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(stdx::ct_format<"Hello {}">("world"_ctst) ==
"Hello world"_fmt_res);
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(stdx::ct<"world">()) ==
"Hello world"_fmt_res);
}
TEST_CASE("format a compile-time integral argument (CX_VALUE)", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE(42)) ==
"Hello 42"_fmt_res);
}
TEST_CASE("format a compile-time integral argument (ct)", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(stdx::ct<42>()) ==
"Hello 42"_fmt_res);
}
TEST_CASE("format a type argument (CX_VALUE)", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE(int)) ==
"Hello int"_fmt_res);
}
TEST_CASE("format a type argument (ct)", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(stdx::ct<int>()) ==
"Hello int"_fmt_res);
}
TEST_CASE("format a compile-time argument with fmt spec", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {:*>#6x}">(CX_VALUE(42)) ==
"Hello **0x2a"_fmt_res);
}
namespace {
enum struct E { A };
}
TEST_CASE("format a compile-time enum argument (CX_VALUE)", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE(E::A)) ==
"Hello A"_fmt_res);
}
TEST_CASE("format a compile-time enum argument (ct)", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(stdx::ct<E::A>()) ==
"Hello A"_fmt_res);
}
TEST_CASE("format a runtime argument", "[ct_format]") {
constexpr auto x = 17;
constexpr auto expected =
stdx::format_result{"Hello {}"_ctst, stdx::make_tuple(x)};
CHECK(stdx::ct_format<"Hello {}">(x) == expected);
STATIC_REQUIRE(stdx::ct_format<"Hello {}">(x) == expected);
}
TEST_CASE("format a compile-time and a runtime argument (1)", "[ct_format]") {
constexpr auto x = 17;
constexpr auto expected =
stdx::format_result{"Hello int {}"_ctst, stdx::make_tuple(x)};
CHECK(stdx::ct_format<"Hello {} {}">(CX_VALUE(int), x) == expected);
STATIC_REQUIRE(stdx::ct_format<"Hello {} {}">(CX_VALUE(int), x) ==
expected);
}
TEST_CASE("format a compile-time and a runtime argument (2)", "[ct_format]") {
STATIC_REQUIRE(
stdx::ct_format<"Hello {} {}">(42, CX_VALUE(int)) ==
stdx::format_result{"Hello {} int"_ctst, stdx::make_tuple(42)});
}
TEST_CASE("format multiple runtime arguments", "[ct_format]") {
STATIC_REQUIRE(
stdx::ct_format<"Hello {} {}">(42, 17) ==
stdx::format_result{"Hello {} {}"_ctst, stdx::make_tuple(42, 17)});
}
TEST_CASE("format multiple mixed arguments", "[ct_format]") {
using namespace std::string_view_literals;
auto b = "B"sv;
CHECK(stdx::ct_format<"Hello {} {} {} {} world">(42, CX_VALUE("A"sv), b,
CX_VALUE(int)) ==
stdx::format_result{"Hello {} A {} int world"_ctst,
stdx::make_tuple(42, "B"sv)});
STATIC_REQUIRE(stdx::ct_format<"Hello {} {} {} {} world">(
42, CX_VALUE("A"sv), "B"sv, CX_VALUE(int)) ==
stdx::format_result{"Hello {} A {} int world"_ctst,
stdx::make_tuple(42, "B"sv)});
}
TEST_CASE("format a format result", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"The value is {}.">(
stdx::ct_format<"(year={})">(2022)) ==
stdx::format_result{"The value is (year={})."_ctst,
stdx::make_tuple(2022)});
}
namespace {
template <stdx::ct_string> constexpr auto conversion_success = true;
} // namespace
TEST_CASE("empty format_result can implicitly convert to ct_string",
"[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(
stdx::detail::format_convertible<decltype(stdx::ct_format<"Hello">())>);
STATIC_REQUIRE(stdx::detail::format_convertible<
decltype(stdx::ct_format<"Hello {}">("world"_ctst))>);
STATIC_REQUIRE(not stdx::detail::format_convertible<
decltype(stdx::ct_format<"Hello {}">(42))>);
STATIC_REQUIRE(conversion_success<stdx::ct_format<"Hello">()>);
}
TEST_CASE("empty format_result can explicitly convert to ct_string",
"[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(+stdx::ct_format<"Hello">() == "Hello"_cts);
}
namespace {
template <typename T, T...> struct string_constant {
private:
friend constexpr auto operator==(string_constant const &,
string_constant const &) -> bool = default;
};
template <class T, T... Ls, T... Rs>
[[nodiscard]] constexpr auto operator+(string_constant<T, Ls...>,
string_constant<T, Rs...>) noexcept
-> string_constant<T, Ls..., Rs...> {
return {};
}
} // namespace
TEST_CASE("format_to a different type", "[ct_format]") {
using namespace std::string_view_literals;
STATIC_REQUIRE(stdx::ct_format<"{}", string_constant>(CX_VALUE("A"sv)) ==
stdx::format_result{string_constant<char, 'A'>{}});
auto x = 17;
CHECK(stdx::ct_format<"{}", string_constant>(x) ==
stdx::format_result{string_constant<char, '{', '}'>{},
stdx::make_tuple(17)});
STATIC_REQUIRE(stdx::ct_format<"{}", string_constant>(17) ==
stdx::format_result{string_constant<char, '{', '}'>{},
stdx::make_tuple(17)});
}
TEST_CASE("format a string-type argument", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"Hello {}!">(string_constant<char, 'A'>{}) ==
"Hello A!"_fmt_res);
}
TEST_CASE("format a format result with different type", "[ct_format]") {
STATIC_REQUIRE(stdx::ct_format<"A{}D", string_constant>(
stdx::ct_format<"B{}C", string_constant>(2022)) ==
stdx::format_result{
string_constant<char, 'A', 'B', '{', '}', 'C', 'D'>{},
stdx::make_tuple(2022)});
}
TEST_CASE("format multiple mixed arguments with different type",
"[ct_format]") {
using namespace std::string_view_literals;
auto b = "B"sv;
CHECK(stdx::ct_format<"Hello {} {} {} {} world", string_constant>(
42, CX_VALUE("A"sv), b, CX_VALUE(int)) ==
stdx::format_result{
stdx::ct_string_to_type<"Hello {} A {} int world"_cts,
string_constant>(),
stdx::make_tuple(42, "B"sv)});
STATIC_REQUIRE(stdx::ct_format<"Hello {} {} {} {} world", string_constant>(
42, CX_VALUE("A"sv), "B"sv, CX_VALUE(int)) ==
stdx::format_result{
stdx::ct_string_to_type<"Hello {} A {} int world"_cts,
string_constant>(),
stdx::make_tuple(42, "B"sv)});
}
TEST_CASE("num fmt specifiers", "[ct_format]") {
STATIC_REQUIRE(stdx::num_fmt_specifiers<"{}"> == 1u);
STATIC_REQUIRE(stdx::num_fmt_specifiers<"{} {}"> == 2u);
}
namespace user {
struct S {
int x;
};
constexpr auto ct_format_as(S const &s) {
return stdx::ct_format<"S: {}">(s.x);
}
} // namespace user
TEST_CASE("user-defined formatting", "[ct_format]") {
auto r = stdx::ct_format<"Hello {}">(user::S{17});
CHECK(r == stdx::format_result{"Hello S: {}"_ctst, stdx::make_tuple(17)});
}
TEST_CASE("FORMAT with no arguments", "[ct_format]") {
STATIC_REQUIRE(STDX_CT_FORMAT("Hello") == "Hello"_fmt_res);
}
TEST_CASE("FORMAT a compile-time string argument", "[ct_format]") {
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", "world") ==
"Hello world"_fmt_res);
}
TEST_CASE("FORMAT a compile-time int argument", "[ct_format]") {
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", 17) == "Hello 17"_fmt_res);
}
TEST_CASE("FORMAT a type argument", "[ct_format]") {
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", int) == "Hello int"_fmt_res);
}
TEST_CASE("FORMAT a constexpr ct_string argument", "[ct_format]") {
constexpr static auto S = "world"_cts;
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
}
TEST_CASE("FORMAT a cts_t argument", "[ct_format]") {
auto S = "world"_ctst;
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
}
TEST_CASE("FORMAT a format_result argument", "[ct_format]") {
auto S = "world"_fmt_res;
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
}
TEST_CASE("FORMAT a constexpr int argument", "[ct_format]") {
constexpr static auto I = 17;
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", I) == "Hello 17"_fmt_res);
}
#ifdef __clang__
TEST_CASE("FORMAT a constexpr nonstatic string_view argument", "[ct_format]") {
constexpr auto S = std::string_view{"world"};
constexpr auto expected =
stdx::format_result{"Hello {}"_ctst, stdx::make_tuple(S)};
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == expected);
}
#endif
TEST_CASE("FORMAT a constexpr string_view argument", "[ct_format]") {
constexpr static auto S = std::string_view{"world"};
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
}
TEST_CASE("FORMAT an integral_constant argument", "[ct_format]") {
auto I = std::integral_constant<int, 17>{};
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", I) == "Hello 17"_fmt_res);
}