|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
| 9 | +#include "hdr/types/wchar_t.h" |
9 | 10 | #include "src/stdio/printf_core/converter.h" |
10 | 11 | #include "src/stdio/printf_core/core_structs.h" |
11 | 12 | #include "src/stdio/printf_core/writer.h" |
12 | | - |
13 | 13 | #include "test/UnitTest/Test.h" |
14 | 14 |
|
15 | 15 | class LlvmLibcPrintfConverterTest : public LIBC_NAMESPACE::testing::Test { |
@@ -255,3 +255,83 @@ TEST_F(LlvmLibcPrintfConverterTest, OctConversion) { |
255 | 255 | ASSERT_STREQ(str, "1234"); |
256 | 256 | ASSERT_EQ(writer.get_chars_written(), size_t{4}); |
257 | 257 | } |
| 258 | + |
| 259 | +TEST_F(LlvmLibcPrintfConverterTest, WideCharConversion) { |
| 260 | + LIBC_NAMESPACE::printf_core::FormatSection section; |
| 261 | + section.has_conv = true; |
| 262 | + section.raw_string = "%lc"; |
| 263 | + section.conv_name = 'c'; |
| 264 | + section.length_modifier = LIBC_NAMESPACE::printf_core::LengthModifier::l; |
| 265 | + section.conv_val_raw = static_cast<wchar_t>(L'€'); |
| 266 | + |
| 267 | + LIBC_NAMESPACE::printf_core::convert(&writer, section); |
| 268 | + |
| 269 | + wb.buff[wb.buff_cur] = '\0'; |
| 270 | + |
| 271 | + ASSERT_STREQ(str, "€"); |
| 272 | + ASSERT_EQ(writer.get_chars_written(), size_t{1}); |
| 273 | +} |
| 274 | + |
| 275 | +TEST_F(LlvmLibcPrintfConverterTest, WideCharConversionLeftJustified) { |
| 276 | + LIBC_NAMESPACE::printf_core::FormatSection left_justified_conv; |
| 277 | + left_justified_conv.has_conv = true; |
| 278 | + left_justified_conv.raw_string = "%-4lc"; |
| 279 | + left_justified_conv.conv_name = 'c'; |
| 280 | + left_justified_conv.length_modifier = |
| 281 | + LIBC_NAMESPACE::printf_core::LengthModifier::l; |
| 282 | + left_justified_conv.flags = |
| 283 | + LIBC_NAMESPACE::printf_core::FormatFlags::LEFT_JUSTIFIED; |
| 284 | + left_justified_conv.min_width = 4; |
| 285 | + left_justified_conv.conv_val_raw = static_cast<wchar_t>(L'€'); |
| 286 | + |
| 287 | + LIBC_NAMESPACE::printf_core::convert(&writer, left_justified_conv); |
| 288 | + wb.buff[wb.buff_cur] = '\0'; |
| 289 | + |
| 290 | + ASSERT_STREQ(str, "€ "); |
| 291 | + ASSERT_EQ(writer.get_chars_written(), size_t{4}); |
| 292 | +} |
| 293 | + |
| 294 | +TEST_F(LlvmLibcPrintfConverterTest, WideCharConversionRightJustified) { |
| 295 | + LIBC_NAMESPACE::printf_core::FormatSection right_justified_conv; |
| 296 | + right_justified_conv.has_conv = true; |
| 297 | + right_justified_conv.raw_string = "%4lc"; |
| 298 | + right_justified_conv.conv_name = 'c'; |
| 299 | + right_justified_conv.length_modifier = |
| 300 | + LIBC_NAMESPACE::printf_core::LengthModifier::l; |
| 301 | + right_justified_conv.min_width = 4; |
| 302 | + right_justified_conv.conv_val_raw = static_cast<wchar_t>(L'€'); |
| 303 | + |
| 304 | + LIBC_NAMESPACE::printf_core::convert(&writer, right_justified_conv); |
| 305 | + wb.buff[wb.buff_cur] = '\0'; |
| 306 | + |
| 307 | + ASSERT_STREQ(str, " €"); |
| 308 | + ASSERT_EQ(writer.get_chars_written(), size_t{4}); |
| 309 | +} |
| 310 | + |
| 311 | +TEST_F(LlvmLibcPrintfConverterTest, WideCharConversionInvalid) { |
| 312 | + LIBC_NAMESPACE::printf_core::FormatSection section; |
| 313 | + section.has_conv = true; |
| 314 | + section.raw_string = "%lc"; |
| 315 | + section.conv_name = 'c'; |
| 316 | + section.length_modifier = LIBC_NAMESPACE::printf_core::LengthModifier::l; |
| 317 | + // An invalid wide character. |
| 318 | + section.conv_val_raw = static_cast<wchar_t>(0xFFFFFFFF); |
| 319 | + |
| 320 | + int ret = LIBC_NAMESPACE::printf_core::convert(&writer, section); |
| 321 | + |
| 322 | + ASSERT_EQ(ret, -1); |
| 323 | +} |
| 324 | + |
| 325 | +TEST_F(LlvmLibcPrintfConverterTest, WideCharWEOFConversion) { |
| 326 | + LIBC_NAMESPACE::printf_core::FormatSection section; |
| 327 | + section.has_conv = true; |
| 328 | + section.raw_string = "%lc"; |
| 329 | + section.conv_name = 'c'; |
| 330 | + section.length_modifier = LIBC_NAMESPACE::printf_core::LengthModifier::l; |
| 331 | + // WEOF value. |
| 332 | + section.conv_val_raw = static_cast<wchar_t>(WEOF); |
| 333 | + |
| 334 | + int ret = LIBC_NAMESPACE::printf_core::convert(&writer, section); |
| 335 | + |
| 336 | + ASSERT_EQ(ret, -1); |
| 337 | +} |
0 commit comments