summaryrefslogtreecommitdiff
path: root/Tests/LibC/TestWchar.cpp
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2021-09-21 23:27:17 +0200
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-10-03 11:13:50 +0000
commit4302e7ac266f5f526f1cd9e7f6961817bdbe7482 (patch)
tree87dca7c4f46e9ef5f87fab1b3e0ff1ddf1230fa2 /Tests/LibC/TestWchar.cpp
parent67a579aab01b605f08d0d457efff2f847c7b0a9a (diff)
downloadserenity-4302e7ac266f5f526f1cd9e7f6961817bdbe7482.zip
Tests: Add tests for mbrtowc
Diffstat (limited to 'Tests/LibC/TestWchar.cpp')
-rw-r--r--Tests/LibC/TestWchar.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp
index fdb156472a..f7928fe9a9 100644
--- a/Tests/LibC/TestWchar.cpp
+++ b/Tests/LibC/TestWchar.cpp
@@ -6,6 +6,7 @@
#include <LibTest/TestCase.h>
+#include <errno.h>
#include <string.h>
#include <wchar.h>
@@ -226,3 +227,61 @@ TEST_CASE(mbsinit)
// Ensure that we are in an initial state again.
EXPECT(mbsinit(&state) != 0);
}
+
+TEST_CASE(mbrtowc)
+{
+ size_t ret = 0;
+ mbstate_t state = {};
+ wchar_t wc = 0;
+
+ // Ensure that we can parse normal ASCII characters.
+ ret = mbrtowc(&wc, "Hello", 5, &state);
+ EXPECT_EQ(ret, 1ul);
+ EXPECT_EQ(wc, 'H');
+
+ // Try two three-byte codepoints (™™), only one of which should be consumed.
+ ret = mbrtowc(&wc, "\xe2\x84\xa2\xe2\x84\xa2", 6, &state);
+ EXPECT_EQ(ret, 3ul);
+ EXPECT_EQ(wc, 0x2122);
+
+ // Try a null character, which should return 0 and reset the state to the initial state.
+ ret = mbrtowc(&wc, "\x00\x00", 2, &state);
+ EXPECT_EQ(ret, 0ul);
+ EXPECT_EQ(wc, 0);
+ EXPECT_NE(mbsinit(&state), 0);
+
+ // Try an incomplete multibyte character.
+ ret = mbrtowc(&wc, "\xe2\x84", 2, &state);
+ EXPECT_EQ(ret, -2ul);
+ EXPECT_EQ(mbsinit(&state), 0);
+
+ mbstate_t incomplete_state = state;
+
+ // Finish the previous multibyte character.
+ ret = mbrtowc(&wc, "\xa2", 1, &state);
+ EXPECT_EQ(ret, 1ul);
+ EXPECT_EQ(wc, 0x2122);
+
+ // Try an invalid multibyte sequence.
+ // Reset the state afterwards because the effects are undefined.
+ ret = mbrtowc(&wc, "\xff", 1, &state);
+ EXPECT_EQ(ret, -1ul);
+ EXPECT_EQ(errno, EILSEQ);
+ state = {};
+
+ // Try a successful conversion, but without target address.
+ ret = mbrtowc(nullptr, "\xe2\x84\xa2\xe2\x84\xa2", 6, &state);
+ EXPECT_EQ(ret, 3ul);
+
+ // Test the "null byte shorthand". Ensure that wc is ignored.
+ state = {};
+ wchar_t old_wc = wc;
+ ret = mbrtowc(&wc, nullptr, 0, &state);
+ EXPECT_EQ(ret, 0ul);
+ EXPECT_EQ(wc, old_wc);
+
+ // Test recognition of incomplete multibyte sequences.
+ ret = mbrtowc(nullptr, nullptr, 0, &incomplete_state);
+ EXPECT_EQ(ret, -1ul);
+ EXPECT_EQ(errno, EILSEQ);
+}