| 1 | //===-- Unittests for wcscat ---------------------------------------------===// |
| 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/wcsncat.h" |
| 11 | #include "test/UnitTest/Test.h" |
| 12 | |
| 13 | TEST(LlvmLibcWCSNCatTest, EmptyDest) { |
| 14 | wchar_t dest[4] = {L'\0'}; |
| 15 | const wchar_t *src = L"abc" ; |
| 16 | |
| 17 | // Start by copying nothing |
| 18 | LIBC_NAMESPACE::wcsncat(dest, src, 0); |
| 19 | ASSERT_TRUE(dest[0] == L'\0'); |
| 20 | |
| 21 | // Copying part of it. |
| 22 | LIBC_NAMESPACE::wcsncat(dest, src, 1); |
| 23 | ASSERT_TRUE(dest[0] == L'a'); |
| 24 | ASSERT_TRUE(dest[1] == L'\0'); |
| 25 | |
| 26 | // Resetting for the last test. |
| 27 | dest[0] = '\0'; |
| 28 | |
| 29 | // Copying all of it. |
| 30 | LIBC_NAMESPACE::wcsncat(dest, src, 3); |
| 31 | ASSERT_TRUE(dest[0] == L'a'); |
| 32 | ASSERT_TRUE(dest[1] == L'b'); |
| 33 | ASSERT_TRUE(dest[2] == L'c'); |
| 34 | ASSERT_TRUE(dest[3] == L'\0'); |
| 35 | } |
| 36 | |
| 37 | TEST(LlvmLibcWCSNCatTest, NonEmptyDest) { |
| 38 | wchar_t dest[7] = {L'x', L'y', L'z', L'\0'}; |
| 39 | const wchar_t *src = L"abc" ; |
| 40 | |
| 41 | // Adding on only part of the string |
| 42 | LIBC_NAMESPACE::wcsncat(dest, src, 1); |
| 43 | ASSERT_TRUE(dest[0] == L'x'); |
| 44 | ASSERT_TRUE(dest[1] == L'y'); |
| 45 | ASSERT_TRUE(dest[2] == L'z'); |
| 46 | ASSERT_TRUE(dest[3] == L'a'); |
| 47 | ASSERT_TRUE(dest[4] == L'\0'); |
| 48 | |
| 49 | // Copying more without resetting |
| 50 | LIBC_NAMESPACE::wcsncat(dest, src, 2); |
| 51 | ASSERT_TRUE(dest[0] == L'x'); |
| 52 | ASSERT_TRUE(dest[1] == L'y'); |
| 53 | ASSERT_TRUE(dest[2] == L'z'); |
| 54 | ASSERT_TRUE(dest[3] == L'a'); |
| 55 | ASSERT_TRUE(dest[4] == L'a'); |
| 56 | ASSERT_TRUE(dest[5] == L'b'); |
| 57 | ASSERT_TRUE(dest[6] == L'\0'); |
| 58 | |
| 59 | // Setting end marker to make sure it overwrites properly. |
| 60 | dest[3] = L'\0'; |
| 61 | |
| 62 | // Copying all of it. |
| 63 | LIBC_NAMESPACE::wcsncat(dest, src, 3); |
| 64 | ASSERT_TRUE(dest[0] == L'x'); |
| 65 | ASSERT_TRUE(dest[1] == L'y'); |
| 66 | ASSERT_TRUE(dest[2] == L'z'); |
| 67 | ASSERT_TRUE(dest[3] == L'a'); |
| 68 | ASSERT_TRUE(dest[4] == L'b'); |
| 69 | ASSERT_TRUE(dest[5] == L'c'); |
| 70 | ASSERT_TRUE(dest[6] == L'\0'); |
| 71 | |
| 72 | // Check that copying still works when count > src length. |
| 73 | dest[0] = L'\0'; |
| 74 | // And that it doesn't write beyond what is necessary. |
| 75 | dest[4] = L'Z'; |
| 76 | LIBC_NAMESPACE::wcsncat(dest, src, 4); |
| 77 | ASSERT_TRUE(dest[0] == L'a'); |
| 78 | ASSERT_TRUE(dest[1] == L'b'); |
| 79 | ASSERT_TRUE(dest[2] == L'c'); |
| 80 | ASSERT_TRUE(dest[3] == L'\0'); |
| 81 | ASSERT_TRUE(dest[4] == L'Z'); |
| 82 | } |
| 83 | |