diff options
author | Daniel Bertalan <dani@danielbertalan.dev> | 2021-10-04 16:59:13 +0200 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-10-15 21:50:19 -0700 |
commit | c8367df74639a399a2195a2a07ff7ac74b88d426 (patch) | |
tree | dd9febcec72643bec3138f47e43f9a5f364fde4f /Tests | |
parent | 9c29e6cde7615cdb0dc8037dd67df422ecf064a6 (diff) | |
download | serenity-c8367df74639a399a2195a2a07ff7ac74b88d426.zip |
LibC: Implement wcrtomb
This function converts a single wide character into its multibyte
representation (UTF-8 in our case). It is called from libc++'s
`std::basic_ostream<wchar_t>::flush`, which gets called at program exit
from a global destructor in order to flush `std::wcout`.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/LibC/TestWchar.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp index ea3bcd5eda..f893969a62 100644 --- a/Tests/LibC/TestWchar.cpp +++ b/Tests/LibC/TestWchar.cpp @@ -285,3 +285,34 @@ TEST_CASE(mbrtowc) EXPECT_EQ(ret, -1ul); EXPECT_EQ(errno, EILSEQ); } + +TEST_CASE(wcrtomb) +{ + char buf[MB_LEN_MAX]; + size_t ret = 0; + + // Ensure that `wc` is ignored when buf is a nullptr. + ret = wcrtomb(nullptr, L'a', nullptr); + EXPECT_EQ(ret, 1ul); + + ret = wcrtomb(nullptr, L'\U0001F41E', nullptr); + EXPECT_EQ(ret, 1ul); + + // When the buffer is non-null, the multibyte representation is written into it. + ret = wcrtomb(buf, L'a', nullptr); + EXPECT_EQ(ret, 1ul); + EXPECT_EQ(memcmp(buf, "a", ret), 0); + + ret = wcrtomb(buf, L'\U0001F41E', nullptr); + EXPECT_EQ(ret, 4ul); + EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e", ret), 0); + + // When the wide character is invalid, -1 is returned and errno is set to EILSEQ. + ret = wcrtomb(buf, 0x110000, nullptr); + EXPECT_EQ(ret, (size_t)-1); + EXPECT_EQ(errno, EILSEQ); + + // Replacement characters and conversion errors are not confused. + ret = wcrtomb(buf, L'\uFFFD', nullptr); + EXPECT_NE(ret, (size_t)-1); +} |