| 1 | //===-- Unittests for wcsncpy ---------------------------------------------===// |
| 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/wcsncpy.h" |
| 11 | #include "test/UnitTest/Test.h" |
| 12 | |
| 13 | TEST(LlvmLibcWCSNCpyTest, CopyZero) { |
| 14 | // Dest should remain unchanged. |
| 15 | wchar_t dest[3] = {L'a', L'b', L'\0'}; |
| 16 | const wchar_t *src = L"x" ; |
| 17 | LIBC_NAMESPACE::wcsncpy(dest, src, 0); |
| 18 | ASSERT_TRUE(dest[0] == L'a'); |
| 19 | ASSERT_TRUE(dest[1] == L'b'); |
| 20 | ASSERT_TRUE(dest[2] == L'\0'); |
| 21 | } |
| 22 | |
| 23 | TEST(LlvmLibcWCSNCpyTest, CopyFullIntoEmpty) { |
| 24 | // Dest should be the exact same as src. |
| 25 | wchar_t dest[15]; |
| 26 | const wchar_t *src = L"aaaaabbbbccccc" ; |
| 27 | LIBC_NAMESPACE::wcsncpy(dest, src, 15); |
| 28 | for (int i = 0; i < 15; i++) |
| 29 | ASSERT_TRUE(dest[i] == src[i]); |
| 30 | } |
| 31 | |
| 32 | TEST(LlvmLibcWCSNCpyTest, CopyPartial) { |
| 33 | // First two characters of dest should be the first two characters of src. |
| 34 | wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'}; |
| 35 | const wchar_t *src = L"1234" ; |
| 36 | LIBC_NAMESPACE::wcsncpy(dest, src, 2); |
| 37 | ASSERT_TRUE(dest[0] == L'1'); |
| 38 | ASSERT_TRUE(dest[1] == L'2'); |
| 39 | ASSERT_TRUE(dest[2] == L'c'); |
| 40 | ASSERT_TRUE(dest[3] == L'd'); |
| 41 | ASSERT_TRUE(dest[4] == L'\0'); |
| 42 | } |
| 43 | |
| 44 | TEST(LlvmLibcWCSNCpyTest, CopyNullTerminator) { |
| 45 | // Null terminator should copy into dest. |
| 46 | wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'}; |
| 47 | const wchar_t src[] = {L'\0', L'y'}; |
| 48 | LIBC_NAMESPACE::wcsncpy(dest, src, 1); |
| 49 | ASSERT_TRUE(dest[0] == L'\0'); |
| 50 | ASSERT_TRUE(dest[1] == L'b'); |
| 51 | ASSERT_TRUE(dest[2] == L'c'); |
| 52 | ASSERT_TRUE(dest[3] == L'd'); |
| 53 | ASSERT_TRUE(dest[4] == L'\0'); |
| 54 | } |
| 55 | |
| 56 | TEST(LlvmLibcWCSNCpyTest, CopyPastSrc) { |
| 57 | // Copying past src should fill with null terminator. |
| 58 | wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'}; |
| 59 | const wchar_t src[] = {L'x', L'\0'}; |
| 60 | LIBC_NAMESPACE::wcsncpy(dest, src, 4); |
| 61 | ASSERT_TRUE(dest[0] == L'x'); |
| 62 | ASSERT_TRUE(dest[1] == L'\0'); |
| 63 | ASSERT_TRUE(dest[2] == L'\0'); |
| 64 | ASSERT_TRUE(dest[3] == L'\0'); |
| 65 | ASSERT_TRUE(dest[4] == L'\0'); |
| 66 | } |
| 67 | |