diff options
author | Tim Schumacher <timschumi@gmx.de> | 2021-10-10 21:34:00 +0200 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-10-15 21:50:19 -0700 |
commit | 420bdccf0b528152b29aa3a1cdfc1abf889655ac (patch) | |
tree | 5caca9b611ac720c670a3a3941472403843299b2 /Tests/LibC/TestWchar.cpp | |
parent | b0babd062eda9e18fcc2450317f83912e3b84c43 (diff) | |
download | serenity-420bdccf0b528152b29aa3a1cdfc1abf889655ac.zip |
LibC: Implement mbsrtowcs
Diffstat (limited to 'Tests/LibC/TestWchar.cpp')
-rw-r--r-- | Tests/LibC/TestWchar.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp index b96c432ab3..a158f33748 100644 --- a/Tests/LibC/TestWchar.cpp +++ b/Tests/LibC/TestWchar.cpp @@ -363,3 +363,54 @@ TEST_CASE(wcsrtombs) EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e", 9), 0); EXPECT_EQ(src, nullptr); } + +TEST_CASE(mbsrtowcs) +{ + mbstate_t state = {}; + wchar_t buf[4]; + const char good_chars[] = "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e"; + const char bad_chars[] = "\xf0\x9f\x90\x9e\xf0\xff\x90\x9e"; + const char* src; + size_t ret = 0; + + // Convert normal and valid multibyte sequences. + src = good_chars; + ret = mbsrtowcs(buf, &src, 3, &state); + EXPECT_EQ(ret, 2ul); + EXPECT_EQ(buf[0], L'\U0001F41E'); + EXPECT_EQ(buf[1], L'\U0001F41E'); + EXPECT_EQ(buf[2], L'\0'); + EXPECT_EQ(src, nullptr); + EXPECT_NE(mbsinit(&state), 0); + + // Stop on invalid multibyte sequences. + src = bad_chars; + ret = mbsrtowcs(buf, &src, 3, &state); + EXPECT_EQ(ret, -1ul); + EXPECT_EQ(buf[0], L'\U0001F41E'); + EXPECT_EQ(errno, EILSEQ); + EXPECT_EQ(src, bad_chars + 4); + + // Valid sequence but not enough space. + src = good_chars; + ret = mbsrtowcs(buf, &src, 1, &state); + EXPECT_EQ(ret, 1ul); + EXPECT_EQ(buf[0], L'\U0001F41E'); + EXPECT_EQ(src, good_chars + 4); + + // Try a conversion with no destination and too short length. + src = good_chars; + ret = mbsrtowcs(nullptr, &src, 1, &state); + EXPECT_EQ(ret, 2ul); + EXPECT_EQ(src, nullptr); + EXPECT_NE(mbsinit(&state), 0); + + // Try a conversion using the internal anonymous state. + src = good_chars; + ret = mbsrtowcs(buf, &src, 3, nullptr); + EXPECT_EQ(ret, 2ul); + EXPECT_EQ(buf[0], L'\U0001F41E'); + EXPECT_EQ(buf[1], L'\U0001F41E'); + EXPECT_EQ(buf[2], L'\0'); + EXPECT_EQ(src, nullptr); +} |