@@ -98,51 +98,23 @@ TEST(xchar_test, compile_time_string) {
9898#endif
9999}
100100
101- template <typename Char> struct xchar_helpers ;
102-
103- #define DEFINE_XCHAR_HELPERS (CHAR_TYPE, LIT ) \
104- template <> struct xchar_helpers <CHAR_TYPE> { \
105- static constexpr const CHAR_TYPE* fmt1 () { return LIT (" {}" ); } \
106- static constexpr const CHAR_TYPE* fmt2 () { return LIT (" {}{}" ); } \
107- static constexpr const CHAR_TYPE* fmt2sp () { return LIT (" {}{} " ); } \
108- static constexpr const CHAR_TYPE* s123x () { return LIT (" 123x" ); } \
109- static constexpr const CHAR_TYPE* sAxxx () { return LIT (" Axxx" ); } \
110- static constexpr const CHAR_TYPE* sBCx () { return LIT (" BC x" ); } \
111- }
112-
113101#define WLIT (x ) L##x
114102#define U16LIT (x ) u##x
115103#define U32LIT (x ) U##x
116104
117- DEFINE_XCHAR_HELPERS (wchar_t , WLIT);
118- DEFINE_XCHAR_HELPERS (char16_t , U16LIT);
119- DEFINE_XCHAR_HELPERS (char32_t , U32LIT);
120-
121- #undef U32LIT
122- #undef U16LIT
123- #undef WLIT
124- #undef DEFINE_XCHAR_HELPERS
125-
126- template <typename Char> class xchar_format_test : public testing ::Test {};
127-
128- using wide_char_types = testing::Types<wchar_t , char16_t , char32_t >;
129- TYPED_TEST_SUITE (xchar_format_test, wide_char_types);
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+ }
130112
131- TYPED_TEST (xchar_format_test, format_to) {
132- using H = xchar_helpers<TypeParam>;
133- auto buf = std::vector<TypeParam>();
134- fmt::format_to (std::back_inserter (buf), H::fmt2 (), 42 , TypeParam (' \0 ' ));
135- auto expected =
136- std::vector<TypeParam>{TypeParam (' 4' ), TypeParam (' 2' ), TypeParam (' \0 ' )};
137- EXPECT_EQ (buf, expected);
138- }
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)
139116
140- TEST (xchar_test, wchar_format_to_runtime) {
141- auto buf = std::vector<wchar_t >();
142- fmt::format_to (std::back_inserter (buf), fmt::runtime (L" {}{}" ), 42 , L' \0 ' );
143- auto expected = std::vector<wchar_t >{L' 4' , L' 2' , L' \0 ' };
144- EXPECT_EQ (buf, expected);
145- }
117+ #undef DEFINE_FORMAT_TO_TEST
146118
147119TEST (xchar_test, compile_time_string_format_to) {
148120 std::wstring ws;
@@ -167,31 +139,39 @@ TEST(xchar_test, format_as) {
167139 EXPECT_EQ (fmt::format (L" {}" , test::struct_as_wstring_view ()), L" foo" );
168140}
169141
170- TYPED_TEST (xchar_format_test, format_to_n) {
171- using H = xchar_helpers<TypeParam>;
172- TypeParam buffer[4 ];
173- buffer[3 ] = TypeParam (' x' );
174- auto result = fmt::format_to_n (buffer, 3 , H::fmt1 (), 12345 );
175- EXPECT_EQ (5u , result.size );
176- EXPECT_EQ (buffer + 3 , result.out );
177- EXPECT_EQ (std::basic_string<TypeParam>(H::s123x ()),
178- std::basic_string<TypeParam>(buffer, 4 ));
179- buffer[0 ] = TypeParam (' x' );
180- buffer[1 ] = TypeParam (' x' );
181- buffer[2 ] = TypeParam (' x' );
182- result = fmt::format_to_n (buffer, 3 , H::fmt1 (), TypeParam (' A' ));
183- EXPECT_EQ (1u , result.size );
184- EXPECT_EQ (buffer + 1 , result.out );
185- EXPECT_EQ (std::basic_string<TypeParam>(H::sAxxx ()),
186- std::basic_string<TypeParam>(buffer, 4 ));
187- result =
188- fmt::format_to_n (buffer, 3 , H::fmt2sp (), TypeParam (' B' ), TypeParam (' C' ));
189- EXPECT_EQ (3u , result.size );
190- EXPECT_EQ (buffer + 3 , result.out );
191- EXPECT_EQ (std::basic_string<TypeParam>(H::sBCx ()),
192- std::basic_string<TypeParam>(buffer, 4 ));
193- }
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
194171
172+ #undef U32LIT
173+ #undef U16LIT
174+ #undef WLIT
195175
196176TEST (xchar_test, wchar_format_to_n_runtime) {
197177 wchar_t buffer[4 ];
0 commit comments