| 1 | //===-- Unittests for strcat ----------------------------------------------===// |
| 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/signal_macros.h" |
| 10 | #include "src/string/strcat.h" |
| 11 | #include "test/UnitTest/Test.h" |
| 12 | |
| 13 | TEST(LlvmLibcStrCatTest, EmptyDest) { |
| 14 | const char *abc = "abc" ; |
| 15 | char dest[4]; |
| 16 | |
| 17 | dest[0] = '\0'; |
| 18 | |
| 19 | char *result = LIBC_NAMESPACE::strcat(dest, abc); |
| 20 | ASSERT_EQ(dest, result); |
| 21 | ASSERT_STREQ(dest, result); |
| 22 | ASSERT_STREQ(dest, abc); |
| 23 | } |
| 24 | |
| 25 | TEST(LlvmLibcStrCatTest, NonEmptyDest) { |
| 26 | const char *abc = "abc" ; |
| 27 | char dest[7]; |
| 28 | |
| 29 | dest[0] = 'x'; |
| 30 | dest[1] = 'y'; |
| 31 | dest[2] = 'z'; |
| 32 | dest[3] = '\0'; |
| 33 | |
| 34 | char *result = LIBC_NAMESPACE::strcat(dest, abc); |
| 35 | ASSERT_EQ(dest, result); |
| 36 | ASSERT_STREQ(dest, result); |
| 37 | ASSERT_STREQ(dest, "xyzabc" ); |
| 38 | } |
| 39 | |
| 40 | #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 41 | |
| 42 | TEST(LlvmLibcStrCatTest, CrashOnNullPtr) { |
| 43 | ASSERT_DEATH([]() { LIBC_NAMESPACE::strcat(nullptr, nullptr); }, |
| 44 | WITH_SIGNAL(-1)); |
| 45 | } |
| 46 | |
| 47 | #endif // defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 48 | |