|
| 1 | +//===-- Unittests for mbsetowcs -------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "hdr/types/mbstate_t.h" |
| 10 | +#include "hdr/types/wchar_t.h" |
| 11 | +#include "src/__support/libc_errno.h" |
| 12 | +#include "src/__support/wchar/mbstate.h" |
| 13 | +#include "src/string/memset.h" |
| 14 | +#include "src/wchar/mbsrtowcs.h" |
| 15 | +#include "test/UnitTest/ErrnoCheckingTest.h" |
| 16 | +#include "test/UnitTest/Test.h" |
| 17 | + |
| 18 | +using LlvmLibcMBSRToWCSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 19 | + |
| 20 | +TEST_F(LlvmLibcMBSRToWCSTest, OneByteOneChar) { |
| 21 | + const char *ch = "A"; |
| 22 | + wchar_t dest[2]; |
| 23 | + mbstate_t *mb; |
| 24 | + LIBC_NAMESPACE::memset(&mb, 0, sizeof(mbstate_t)); |
| 25 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &ch, 1, mb); |
| 26 | + ASSERT_EQ(static_cast<char>(*dest), 'A'); |
| 27 | + ASSERT_EQ(static_cast<int>(n), 1); |
| 28 | + ASSERT_ERRNO_SUCCESS(); |
| 29 | + |
| 30 | + n = LIBC_NAMESPACE::mbsrtowcs(dest + 1, &ch, 1, mb); |
| 31 | + ASSERT_EQ(static_cast<char>(dest[1]), '\0'); |
| 32 | + // Should not include null terminator |
| 33 | + ASSERT_EQ(static_cast<int>(n), 0); |
| 34 | + ASSERT_ERRNO_SUCCESS(); |
| 35 | +} |
| 36 | + |
| 37 | +// TEST_F(LlvmLibcMBSRToWCSTest, FourByteOneChar) { |
| 38 | +// const char *src = "\xf0\x9f\x98\xb9"; // laughing cat emoji 😹 |
| 39 | +// wchar_t dest[2]; |
| 40 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 2); |
| 41 | +// ASSERT_ERRNO_SUCCESS(); |
| 42 | +// ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 43 | +// ASSERT_TRUE(dest[1] == L'\0'); |
| 44 | +// // Should not count null terminator in number |
| 45 | +// ASSERT_EQ(static_cast<int>(n), 1); |
| 46 | +// } |
| 47 | + |
| 48 | +// TEST_F(LlvmLibcMBSRToWCSTest, MultiByteTwoCharacters) { |
| 49 | +// // Two laughing cat emojis "😹😹" |
| 50 | +// const char *src = "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 51 | +// wchar_t dest[3]; |
| 52 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 3); |
| 53 | +// ASSERT_ERRNO_SUCCESS(); |
| 54 | +// ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 55 | +// ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 56 | +// ASSERT_TRUE(dest[2] == L'\0'); |
| 57 | +// // Should not count null terminator in number |
| 58 | +// ASSERT_EQ(static_cast<int>(n), 2); |
| 59 | +// } |
| 60 | + |
| 61 | +// TEST_F(LlvmLibcMBSRToWCSTest, MixedNumberOfBytes) { |
| 62 | +// // 'A', sigma symbol 'Σ', recycling symbol '♻', laughing cat emoji '😹' |
| 63 | +// const char *src = "A\xce\xa3\xe2\x99\xbb\xf0\x9f\x98\xb9"; |
| 64 | +// wchar_t dest[5]; |
| 65 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 5); |
| 66 | +// ASSERT_ERRNO_SUCCESS(); |
| 67 | +// ASSERT_EQ(static_cast<char>(dest[0]), 'A'); |
| 68 | +// ASSERT_EQ(static_cast<int>(dest[1]), 931); |
| 69 | +// ASSERT_EQ(static_cast<int>(dest[2]), 9851); |
| 70 | +// ASSERT_EQ(static_cast<int>(dest[3]), 128569); |
| 71 | +// ASSERT_TRUE(dest[4] == L'\0'); |
| 72 | +// // Should not count null terminator in number |
| 73 | +// ASSERT_EQ(static_cast<int>(n), 4); |
| 74 | +// } |
| 75 | + |
| 76 | +// TEST_F(LlvmLibcMBSRToWCSTest, ReadLessThanStringLength) { |
| 77 | +// // Four laughing cat emojis "😹😹😹😹" |
| 78 | +// const char *src = |
| 79 | +// "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 80 | +// wchar_t dest[5] = {L'a', L'b', L'c', L'd', L'e'}; |
| 81 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 3); |
| 82 | +// ASSERT_ERRNO_SUCCESS(); |
| 83 | +// // Should have read 3 emojis |
| 84 | +// ASSERT_EQ(static_cast<int>(n), 3); |
| 85 | +// ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 86 | +// ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 87 | +// ASSERT_EQ(static_cast<int>(dest[2]), 128569); |
| 88 | +// ASSERT_TRUE(dest[3] == L'd'); |
| 89 | +// ASSERT_TRUE(dest[4] == L'e'); |
| 90 | +// } |
| 91 | + |
| 92 | +// TEST_F(LlvmLibcMBSRToWCSTest, InvalidFirstByte) { |
| 93 | +// // 0x80 is invalid first byte of mb character |
| 94 | +// const char *src = |
| 95 | +// "\x80\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 96 | +// wchar_t dest[3]; |
| 97 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 3); |
| 98 | +// // Should return error and set errno |
| 99 | +// ASSERT_EQ(static_cast<int>(n), -1); |
| 100 | +// ASSERT_ERRNO_EQ(EILSEQ); |
| 101 | +// } |
| 102 | + |
| 103 | +// TEST_F(LlvmLibcMBSRToWCSTest, InvalidMiddleByte) { |
| 104 | +// // The 7th byte is invalid for a 4 byte character |
| 105 | +// const char *src = |
| 106 | +// "\xf0\x9f\x98\xb9\xf0\x9f\xf0\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 107 | +// wchar_t dest[3]; |
| 108 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 5); |
| 109 | +// // Should return error and set errno |
| 110 | +// ASSERT_EQ(static_cast<int>(n), -1); |
| 111 | +// ASSERT_ERRNO_EQ(EILSEQ); |
| 112 | +// } |
| 113 | + |
| 114 | +// TEST_F(LlvmLibcMBSRToWCSTest, NullDestination) { |
| 115 | +// // Four laughing cat emojis "😹😹😹😹" |
| 116 | +// const char *src = |
| 117 | +// "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 118 | +// size_t n = LIBC_NAMESPACE::mbstowcs(nullptr, src, 2); |
| 119 | +// ASSERT_ERRNO_SUCCESS(); |
| 120 | +// // Null destination should ignore len and read till end of string |
| 121 | +// ASSERT_EQ(static_cast<int>(n), 4); |
| 122 | +// } |
| 123 | + |
| 124 | +// TEST_F(LlvmLibcMBSRToWCSTest, ErrnoChecks) { |
| 125 | +// // Two laughing cat emojis and invalid 3rd mb char (3rd byte of it) |
| 126 | +// const char *src = |
| 127 | +// "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\xf0\xb9\xf0\x9f\x98\xb9"; |
| 128 | +// wchar_t dest[5]; |
| 129 | +// // First two bytes are valid --> should not set errno |
| 130 | +// size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 2); |
| 131 | +// ASSERT_ERRNO_SUCCESS(); |
| 132 | +// ASSERT_EQ(static_cast<int>(n), 2); |
| 133 | +// ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 134 | +// ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 135 | +// // Trying to read the 3rd byte should set errno |
| 136 | +// n = LIBC_NAMESPACE::mbstowcs(dest, src, 2); |
| 137 | +// ASSERT_ERRNO_EQ(EILSEQ); |
| 138 | +// ASSERT_EQ(static_cast<int>(n), -1); |
| 139 | +// } |
0 commit comments