Skip to content

Commit ada3f1d

Browse files
committed
Add tests for format_to and format_to_n with wchar_t, char16_t, and char32_t
1 parent 1dcff42 commit ada3f1d

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

test/xchar-test.cc

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <algorithm>
1111
#include <complex>
1212
#include <cwchar>
13+
#include <string>
1314
#include <vector>
1415

1516
#include "fmt/chrono.h"
@@ -97,11 +98,23 @@ TEST(xchar_test, compile_time_string) {
9798
#endif
9899
}
99100

100-
TEST(xchar_test, format_to) {
101-
auto buf = std::vector<wchar_t>();
102-
fmt::format_to(std::back_inserter(buf), L"{}{}", 42, L'\0');
103-
EXPECT_STREQ(buf.data(), L"42");
104-
}
101+
#define WLIT(x) L##x
102+
#define U16LIT(x) u##x
103+
#define U32LIT(x) U##x
104+
105+
#define DEFINE_FORMAT_TO_TEST(SUITE, NAME, CHAR_TYPE, LIT) \
106+
TEST(SUITE, NAME) { \
107+
auto buf = std::vector<CHAR_TYPE>(); \
108+
fmt::format_to(std::back_inserter(buf), LIT("{}{}"), 42, LIT('\0')); \
109+
auto expected = std::vector<CHAR_TYPE>{LIT('4'), LIT('2'), LIT('\0')}; \
110+
EXPECT_EQ(buf, expected); \
111+
}
112+
113+
DEFINE_FORMAT_TO_TEST(xchar_test, wchar_format_to, wchar_t, WLIT)
114+
DEFINE_FORMAT_TO_TEST(xchar_test, char16_format_to, char16_t, U16LIT)
115+
DEFINE_FORMAT_TO_TEST(xchar_test, char32_format_to, char32_t, U32LIT)
116+
117+
#undef DEFINE_FORMAT_TO_TEST
105118

106119
TEST(xchar_test, compile_time_string_format_to) {
107120
std::wstring ws;
@@ -126,27 +139,41 @@ TEST(xchar_test, format_as) {
126139
EXPECT_EQ(fmt::format(L"{}", test::struct_as_wstring_view()), L"foo");
127140
}
128141

129-
TEST(format_test, wide_format_to_n) {
130-
wchar_t buffer[4];
131-
buffer[3] = L'x';
132-
auto result = fmt::format_to_n(buffer, 3, L"{}", 12345);
133-
EXPECT_EQ(5u, result.size);
134-
EXPECT_EQ(buffer + 3, result.out);
135-
EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
136-
buffer[0] = L'x';
137-
buffer[1] = L'x';
138-
buffer[2] = L'x';
139-
result = fmt::format_to_n(buffer, 3, L"{}", L'A');
140-
EXPECT_EQ(1u, result.size);
141-
EXPECT_EQ(buffer + 1, result.out);
142-
EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4));
143-
result = fmt::format_to_n(buffer, 3, L"{}{} ", L'B', L'C');
144-
EXPECT_EQ(3u, result.size);
145-
EXPECT_EQ(buffer + 3, result.out);
146-
EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
147-
}
142+
#define DEFINE_FORMAT_TO_N_TEST(SUITE, NAME, CHAR_TYPE, LIT) \
143+
TEST(SUITE, NAME) { \
144+
CHAR_TYPE buffer[4]; \
145+
buffer[3] = LIT('x'); \
146+
auto result = fmt::format_to_n(buffer, 3, LIT("{}"), 12345); \
147+
EXPECT_EQ(5u, result.size); \
148+
EXPECT_EQ(buffer + 3, result.out); \
149+
EXPECT_EQ(std::basic_string<CHAR_TYPE>(LIT("123x")), \
150+
std::basic_string<CHAR_TYPE>(buffer, 4)); \
151+
buffer[0] = LIT('x'); \
152+
buffer[1] = LIT('x'); \
153+
buffer[2] = LIT('x'); \
154+
result = fmt::format_to_n(buffer, 3, LIT("{}"), LIT('A')); \
155+
EXPECT_EQ(1u, result.size); \
156+
EXPECT_EQ(buffer + 1, result.out); \
157+
EXPECT_EQ(std::basic_string<CHAR_TYPE>(LIT("Axxx")), \
158+
std::basic_string<CHAR_TYPE>(buffer, 4)); \
159+
result = fmt::format_to_n(buffer, 3, LIT("{}{} "), LIT('B'), LIT('C')); \
160+
EXPECT_EQ(3u, result.size); \
161+
EXPECT_EQ(buffer + 3, result.out); \
162+
EXPECT_EQ(std::basic_string<CHAR_TYPE>(LIT("BC x")), \
163+
std::basic_string<CHAR_TYPE>(buffer, 4)); \
164+
}
165+
166+
DEFINE_FORMAT_TO_N_TEST(xchar_test, wchar_format_to_n, wchar_t, WLIT)
167+
DEFINE_FORMAT_TO_N_TEST(xchar_test, char16_format_to_n, char16_t, U16LIT)
168+
DEFINE_FORMAT_TO_N_TEST(xchar_test, char32_format_to_n, char32_t, U32LIT)
169+
170+
#undef DEFINE_FORMAT_TO_N_TEST
171+
172+
#undef U32LIT
173+
#undef U16LIT
174+
#undef WLIT
148175

149-
TEST(format_test, wide_format_to_n_runtime) {
176+
TEST(xchar_test, wchar_format_to_n_runtime) {
150177
wchar_t buffer[4];
151178
buffer[3] = L'x';
152179
auto result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}"), 12345);

0 commit comments

Comments
 (0)