summaryrefslogtreecommitdiff
path: root/Tests/LibC
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2021-09-19 14:11:53 +0200
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-10-03 11:13:50 +0000
commite7f99edefa50b1014880d44b4683e5570a05f987 (patch)
tree9be8fd2012766b3e40ba7605eb191d43dc1f4683 /Tests/LibC
parent212f9308d4b4a3fcbdd801e8e7917aa5c2034b78 (diff)
downloadserenity-e7f99edefa50b1014880d44b4683e5570a05f987.zip
Tests: Add a test for mbsinit
Diffstat (limited to 'Tests/LibC')
-rw-r--r--Tests/LibC/TestWchar.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp
index 1a165f8c08..fdb156472a 100644
--- a/Tests/LibC/TestWchar.cpp
+++ b/Tests/LibC/TestWchar.cpp
@@ -198,3 +198,31 @@ TEST_CASE(wcscoll)
EXPECT(wcscoll(L"a", L"z") < 0);
EXPECT(wcscoll(L"z", L"a") > 0);
}
+
+TEST_CASE(mbsinit)
+{
+ // Ensure that nullptr is considered an initial state.
+ EXPECT(mbsinit(nullptr) != 0);
+
+ // Ensure that a zero-initialized state is recognized as initial state.
+ mbstate_t state = {};
+ EXPECT(mbsinit(&state) != 0);
+
+ // Read a partial multibyte sequence (0b11011111 / 0xdf).
+ size_t ret = mbrtowc(nullptr, "\xdf", 1, &state);
+
+ if (ret != -2ul)
+ FAIL(String::formatted("mbrtowc accepted partial multibyte sequence with return code {} (expected -2)", static_cast<ssize_t>(ret)));
+
+ // Ensure that we are not in an initial state.
+ EXPECT(mbsinit(&state) == 0);
+
+ // Read the remaining multibyte sequence (0b10111111 / 0xbf).
+ ret = mbrtowc(nullptr, "\xbf", 1, &state);
+
+ if (ret != 1ul)
+ FAIL(String::formatted("mbrtowc did not consume the expected number of bytes (1), returned {} instead", static_cast<ssize_t>(ret)));
+
+ // Ensure that we are in an initial state again.
+ EXPECT(mbsinit(&state) != 0);
+}