|
| 1 | +//===-- Unittests for mbtowc ---------------------------------------------===// |
| 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/wchar/mbtowc.h" |
| 11 | +#include "test/UnitTest/Test.h" |
| 12 | + |
| 13 | +TEST(LlvmLibcMBToWC, OneByte) { |
| 14 | + const char *ch = "A"; |
| 15 | + wchar_t dest[2]; |
| 16 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 1); |
| 17 | + ASSERT_EQ(static_cast<char>(*dest), 'A'); |
| 18 | + ASSERT_EQ(n, 1); |
| 19 | + |
| 20 | + // Should fail since we have not read enough |
| 21 | + n = LIBC_NAMESPACE::mbtowc(dest, ch, 0); |
| 22 | + ASSERT_EQ(n, -1); |
| 23 | +} |
| 24 | + |
| 25 | +TEST(LlvmLibcMBToWC, TwoByte) { |
| 26 | + const char ch[2] = {static_cast<char>(0xC2), |
| 27 | + static_cast<char>(0x8E)}; // car symbol |
| 28 | + wchar_t dest[2]; |
| 29 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 2); |
| 30 | + ASSERT_EQ(static_cast<int>(*dest), 142); |
| 31 | + ASSERT_EQ(n, 2); |
| 32 | + |
| 33 | + // Should fail since we have not read enough |
| 34 | + n = LIBC_NAMESPACE::mbtowc(dest, ch, 1); |
| 35 | + ASSERT_EQ(n, -1); |
| 36 | + // Should fail after trying to read next byte too |
| 37 | + n = LIBC_NAMESPACE::mbtowc(dest, ch + 1, 1); |
| 38 | + ASSERT_EQ(n, -1); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(LlvmLibcMBToWC, ThreeByte) { |
| 42 | + const char ch[3] = {static_cast<char>(0xE2), static_cast<char>(0x88), |
| 43 | + static_cast<char>(0x91)}; // ∑ sigma symbol |
| 44 | + wchar_t dest[2]; |
| 45 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 3); |
| 46 | + ASSERT_EQ(static_cast<int>(*dest), 8721); |
| 47 | + ASSERT_EQ(n, 3); |
| 48 | + |
| 49 | + // Should fail since we have not read enough |
| 50 | + n = LIBC_NAMESPACE::mbtowc(dest, ch, 2); |
| 51 | + ASSERT_EQ(n, -1); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(LlvmLibcMBToWC, FourByte) { |
| 55 | + const char ch[4] = {static_cast<char>(0xF0), static_cast<char>(0x9F), |
| 56 | + static_cast<char>(0xA4), |
| 57 | + static_cast<char>(0xA1)}; // 🤡 clown emoji |
| 58 | + wchar_t dest[2]; |
| 59 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 4); |
| 60 | + ASSERT_EQ(static_cast<int>(*dest), 129313); |
| 61 | + ASSERT_EQ(n, 4); |
| 62 | + |
| 63 | + // Should fail since we have not read enough |
| 64 | + n = LIBC_NAMESPACE::mbtowc(dest, ch, 2); |
| 65 | + ASSERT_EQ(n, -1); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(LlvmLibcMBToWC, InvalidByte) { |
| 69 | + const char ch[1] = {static_cast<char>(0x80)}; |
| 70 | + wchar_t dest[2]; |
| 71 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 1); |
| 72 | + ASSERT_EQ(n, -1); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(LlvmLibcMBToWC, InvalidMultiByte) { |
| 76 | + const char ch[4] = {static_cast<char>(0x80), static_cast<char>(0x00), |
| 77 | + static_cast<char>(0x80), |
| 78 | + static_cast<char>(0x00)}; // invalid sequence of bytes |
| 79 | + wchar_t dest[2]; |
| 80 | + // Trying to push all 4 should error |
| 81 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 4); |
| 82 | + ASSERT_EQ(n, -1); |
| 83 | + // Trying to push the second and third should correspond to null wc |
| 84 | + n = LIBC_NAMESPACE::mbtowc(dest, ch + 1, 2); |
| 85 | + ASSERT_EQ(n, 0); |
| 86 | + ASSERT_TRUE(*dest == L'\0'); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(LlvmLibcMBToWC, InvalidLastByte) { |
| 90 | + // Last byte is invalid since it does not have correct starting sequence. |
| 91 | + // 0xC0 --> 11000000 starting sequence should be 10xxxxxx |
| 92 | + const char ch[4] = {static_cast<char>(0xF1), static_cast<char>(0x80), |
| 93 | + static_cast<char>(0x80), static_cast<char>(0xC0)}; |
| 94 | + wchar_t dest[2]; |
| 95 | + // Trying to push all 4 should error |
| 96 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 4); |
| 97 | + ASSERT_EQ(n, -1); |
| 98 | +} |
| 99 | + |
| 100 | +TEST(LlvmLibcMBToWC, ValidTwoByteWithExtraRead) { |
| 101 | + const char ch[3] = {static_cast<char>(0xC2), static_cast<char>(0x8E), |
| 102 | + static_cast<char>(0x80)}; |
| 103 | + wchar_t dest[2]; |
| 104 | + // Trying to push all 3 should return valid 2 byte |
| 105 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 3); |
| 106 | + ASSERT_EQ(n, 2); |
| 107 | + ASSERT_EQ(static_cast<int>(*dest), 142); |
| 108 | +} |
| 109 | + |
| 110 | +TEST(LlvmLibcMBToWC, TwoValidTwoBytes) { |
| 111 | + const char ch[4] = {static_cast<char>(0xC2), static_cast<char>(0x8E), |
| 112 | + static_cast<char>(0xC7), static_cast<char>(0x8C)}; |
| 113 | + wchar_t dest[2]; |
| 114 | + int n = LIBC_NAMESPACE::mbtowc(dest, ch, 2); |
| 115 | + ASSERT_EQ(n, 2); |
| 116 | + ASSERT_EQ(static_cast<int>(*dest), 142); |
| 117 | + n = LIBC_NAMESPACE::mbtowc(dest + 1, ch + 2, 2); |
| 118 | + ASSERT_EQ(n, 2); |
| 119 | + ASSERT_EQ(static_cast<int>(*(dest + 1)), 460); |
| 120 | +} |
| 121 | + |
| 122 | +TEST(LlvmLibcMBToWC, NullString) { |
| 123 | + wchar_t dest[2] = {L'O', L'K'}; |
| 124 | + // reading on nullptr should return 0 |
| 125 | + int n = LIBC_NAMESPACE::mbtowc(dest, nullptr, 2); |
| 126 | + ASSERT_EQ(n, 0); |
| 127 | + ASSERT_TRUE(dest[0] == L'O'); |
| 128 | + // reading a null terminator should return 0 |
| 129 | + const char *ch = "\0"; |
| 130 | + n = LIBC_NAMESPACE::mbtowc(dest, ch, 1); |
| 131 | + ASSERT_EQ(n, 0); |
| 132 | +} |
0 commit comments