From 89afd4d063b79ef2eba81d1bfc7857c388679c5e Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Fri, 22 Oct 2021 01:38:29 +0200 Subject: LibC: Implement mbsnrtowcs --- Tests/LibC/TestWchar.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'Tests/LibC/TestWchar.cpp') diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp index 4ac7282504..c8b6f71f30 100644 --- a/Tests/LibC/TestWchar.cpp +++ b/Tests/LibC/TestWchar.cpp @@ -441,6 +441,45 @@ TEST_CASE(mbsrtowcs) EXPECT_EQ(src, nullptr); } +TEST_CASE(mbsnrtowcs) +{ + mbstate_t state = {}; + const char good_chars[] = "\xf0\x9f\x90\x9e\xf0\x9f\x90\x9e"; + const char* src; + size_t ret = 0; + + // Convert nothing. + src = good_chars; + ret = mbsnrtowcs(nullptr, &src, 0, 0, &state); + EXPECT_EQ(ret, 0ul); + EXPECT_EQ(src, good_chars); + + // Convert one full wide character. + src = good_chars; + ret = mbsnrtowcs(nullptr, &src, 4, 0, &state); + EXPECT_EQ(ret, 1ul); + EXPECT_EQ(src, good_chars + 4); + + // Encounter a null character. + src = good_chars; + ret = mbsnrtowcs(nullptr, &src, 10, 0, &state); + EXPECT_EQ(ret, 2ul); + EXPECT_EQ(src, nullptr); + + // Convert an incomplete character. + // Make sure that we point past the last processed byte. + src = good_chars; + ret = mbsnrtowcs(nullptr, &src, 6, 0, &state); + EXPECT_EQ(ret, 1ul); + EXPECT_EQ(src, good_chars + 6); + EXPECT_EQ(mbsinit(&state), 0); + + // Finish converting the incomplete character. + ret = mbsnrtowcs(nullptr, &src, 2, 0, &state); + EXPECT_EQ(ret, 1ul); + EXPECT_EQ(src, good_chars + 8); +} + TEST_CASE(wcslcpy) { auto buf = static_cast(malloc(8 * sizeof(wchar_t))); -- cgit v1.2.3