|
| 1 | +//===-- Unittests for mbstowcs --------------------------------------------===// |
| 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/wchar_t.h" |
| 10 | +#include "src/__support/libc_errno.h" |
| 11 | +#include "src/wchar/mbstowcs.h" |
| 12 | +#include "test/UnitTest/ErrnoCheckingTest.h" |
| 13 | +#include "test/UnitTest/Test.h" |
| 14 | + |
| 15 | +using LlvmLibcMBSToWCSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 16 | + |
| 17 | +TEST_F(LlvmLibcMBSToWCSTest, OneByteOneChar) { |
| 18 | + const char *ch = "A"; |
| 19 | + wchar_t dest[2]; |
| 20 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, ch, 1); |
| 21 | + ASSERT_EQ(static_cast<char>(*dest), 'A'); |
| 22 | + ASSERT_EQ(static_cast<int>(n), 1); |
| 23 | + ASSERT_ERRNO_SUCCESS(); |
| 24 | + |
| 25 | + n = LIBC_NAMESPACE::mbstowcs(dest + 1, ch + 1, 1); |
| 26 | + ASSERT_EQ(static_cast<char>(dest[1]), '\0'); |
| 27 | + // Should not include null terminator |
| 28 | + ASSERT_EQ(static_cast<int>(n), 0); |
| 29 | + ASSERT_ERRNO_SUCCESS(); |
| 30 | +} |
| 31 | + |
| 32 | +TEST_F(LlvmLibcMBSToWCSTest, FourByteOneChar) { |
| 33 | + const char *src = "\xf0\x9f\x98\xb9"; // laughing cat emoji 😹 |
| 34 | + wchar_t dest[2]; |
| 35 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 2); |
| 36 | + ASSERT_ERRNO_SUCCESS(); |
| 37 | + ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 38 | + ASSERT_TRUE(dest[1] == L'\0'); |
| 39 | + // Should not count null terminator in number |
| 40 | + ASSERT_EQ(static_cast<int>(n), 1); |
| 41 | +} |
| 42 | + |
| 43 | +TEST_F(LlvmLibcMBSToWCSTest, MultiByteTwoCharacters) { |
| 44 | + // Two laughing cat emojis "😹😹" |
| 45 | + const char *src = "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 46 | + wchar_t dest[3]; |
| 47 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 3); |
| 48 | + ASSERT_ERRNO_SUCCESS(); |
| 49 | + ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 50 | + ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 51 | + ASSERT_TRUE(dest[2] == L'\0'); |
| 52 | + // Should not count null terminator in number |
| 53 | + ASSERT_EQ(static_cast<int>(n), 2); |
| 54 | +} |
| 55 | + |
| 56 | +TEST_F(LlvmLibcMBSToWCSTest, MixedNumberOfBytes) { |
| 57 | + // 'A', sigma symbol 'Σ', recycling symbol '♻', laughing cat emoji '😹' |
| 58 | + const char *src = "A\xce\xa3\xe2\x99\xbb\xf0\x9f\x98\xb9"; |
| 59 | + wchar_t dest[5]; |
| 60 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 5); |
| 61 | + ASSERT_ERRNO_SUCCESS(); |
| 62 | + ASSERT_EQ(static_cast<char>(dest[0]), 'A'); |
| 63 | + ASSERT_EQ(static_cast<int>(dest[1]), 931); |
| 64 | + ASSERT_EQ(static_cast<int>(dest[2]), 9851); |
| 65 | + ASSERT_EQ(static_cast<int>(dest[3]), 128569); |
| 66 | + ASSERT_TRUE(dest[4] == L'\0'); |
| 67 | + // Should not count null terminator in number |
| 68 | + ASSERT_EQ(static_cast<int>(n), 4); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(LlvmLibcMBSToWCSTest, ReadLessThanStringLength) { |
| 72 | + // Four laughing cat emojis "😹😹😹😹" |
| 73 | + const char *src = |
| 74 | + "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 75 | + wchar_t dest[5] = {L'a', L'b', L'c', L'd', L'e'}; |
| 76 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 3); |
| 77 | + ASSERT_ERRNO_SUCCESS(); |
| 78 | + // Should have read 3 emojis |
| 79 | + ASSERT_EQ(static_cast<int>(n), 3); |
| 80 | + ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 81 | + ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 82 | + ASSERT_EQ(static_cast<int>(dest[2]), 128569); |
| 83 | + ASSERT_TRUE(dest[3] == L'd'); |
| 84 | + ASSERT_TRUE(dest[4] == L'e'); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_F(LlvmLibcMBSToWCSTest, InvalidFirstByte) { |
| 88 | + // 0x80 is invalid first byte of mb character |
| 89 | + const char *src = |
| 90 | + "\x80\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 91 | + wchar_t dest[3]; |
| 92 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 3); |
| 93 | + // Should return error and set errno |
| 94 | + ASSERT_EQ(static_cast<int>(n), -1); |
| 95 | + ASSERT_ERRNO_EQ(EILSEQ); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(LlvmLibcMBSToWCSTest, InvalidMiddleByte) { |
| 99 | + // The 7th byte is invalid for a 4 byte character |
| 100 | + const char *src = |
| 101 | + "\xf0\x9f\x98\xb9\xf0\x9f\xf0\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 102 | + wchar_t dest[3]; |
| 103 | + size_t n = LIBC_NAMESPACE::mbstowcs(dest, src, 5); |
| 104 | + // Should return error and set errno |
| 105 | + ASSERT_EQ(static_cast<int>(n), -1); |
| 106 | + ASSERT_ERRNO_EQ(EILSEQ); |
| 107 | +} |
| 108 | + |
| 109 | +TEST_F(LlvmLibcMBSToWCSTest, NullDestination) { |
| 110 | + // Four laughing cat emojis "😹😹😹😹" |
| 111 | + const char *src = |
| 112 | + "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 113 | + size_t n = LIBC_NAMESPACE::mbstowcs(nullptr, src, 2); |
| 114 | + ASSERT_ERRNO_SUCCESS(); |
| 115 | + // Null destination should ignore len and read till end of string |
| 116 | + ASSERT_EQ(static_cast<int>(n), 4); |
| 117 | +} |
0 commit comments