|
| 1 | +//===-- Unittests for wcstombs --------------------------------------------===// |
| 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 "src/wchar/wcstombs.h" |
| 10 | +#include "test/UnitTest/ErrnoCheckingTest.h" |
| 11 | +#include "test/UnitTest/Test.h" |
| 12 | + |
| 13 | +using LlvmLibcWcstombs = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 14 | + |
| 15 | +TEST_F(LlvmLibcWcstombs, AllMultibyteLengths) { |
| 16 | + /// clown emoji, sigma symbol, y with diaeresis, letter A |
| 17 | + const wchar_t src[] = {static_cast<wchar_t>(0x1f921), |
| 18 | + static_cast<wchar_t>(0x2211), |
| 19 | + static_cast<wchar_t>(0xff), static_cast<wchar_t>(0x41), |
| 20 | + static_cast<wchar_t>(0x0)}; |
| 21 | + char mbs[11]; |
| 22 | + |
| 23 | + ASSERT_EQ(wcstombs(mbs, src, 11), static_cast<size_t>(11)); |
| 24 | + ASSERT_ERRNO_SUCCESS(); |
| 25 | + ASSERT_EQ(mbs[0], '\xF0'); // clown begin |
| 26 | + ASSERT_EQ(mbs[1], '\x9F'); |
| 27 | + ASSERT_EQ(mbs[2], '\xA4'); |
| 28 | + ASSERT_EQ(mbs[3], '\xA1'); |
| 29 | + ASSERT_EQ(mbs[4], '\xE2'); // sigma begin |
| 30 | + ASSERT_EQ(mbs[5], '\x88'); |
| 31 | + ASSERT_EQ(mbs[6], '\x91'); |
| 32 | + ASSERT_EQ(mbs[7], '\xC3'); // y diaeresis begin |
| 33 | + ASSERT_EQ(mbs[8], '\xBF'); |
| 34 | + ASSERT_EQ(mbs[9], '\x41'); // A begin |
| 35 | + ASSERT_EQ(mbs[10], '\0'); // null terminator |
| 36 | +} |
| 37 | + |
| 38 | +TEST_F(LlvmLibcWcstombs, PartialConversion) { |
| 39 | + /// clown emoji, sigma symbol, y with diaeresis, letter A |
| 40 | + const wchar_t src[] = {static_cast<wchar_t>(0x1f921), |
| 41 | + static_cast<wchar_t>(0x2211), |
| 42 | + static_cast<wchar_t>(0xff), static_cast<wchar_t>(0x41), |
| 43 | + static_cast<wchar_t>(0x0)}; |
| 44 | + char mbs[11] = {0}; |
| 45 | + |
| 46 | + ASSERT_EQ(wcstombs(mbs, src, 6), static_cast<size_t>(4)); |
| 47 | + ASSERT_ERRNO_SUCCESS(); |
| 48 | + ASSERT_EQ(mbs[0], '\xF0'); // clown begin |
| 49 | + ASSERT_EQ(mbs[1], '\x9F'); |
| 50 | + ASSERT_EQ(mbs[2], '\xA4'); |
| 51 | + ASSERT_EQ(mbs[3], '\xA1'); |
| 52 | + ASSERT_EQ(mbs[4], '\0'); |
| 53 | + |
| 54 | + ASSERT_EQ(wcstombs(mbs, src, 6), static_cast<size_t>(4)); |
| 55 | + |
| 56 | + ASSERT_EQ(mbs[4], '\xE2'); // sigma begin |
| 57 | + ASSERT_EQ(mbs[5], '\x88'); |
| 58 | + ASSERT_EQ(mbs[6], '\x91'); |
| 59 | + ASSERT_EQ(mbs[7], '\xC3'); // y diaeresis begin |
| 60 | + ASSERT_EQ(mbs[8], '\xBF'); |
| 61 | + ASSERT_EQ(mbs[9], '\x41'); // A begin |
| 62 | + ASSERT_EQ(mbs[10], '\0'); // null terminator |
| 63 | +} |
0 commit comments