1 //===-- Unittests for strncat ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/string/strncat.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcStrNCatTest
, EmptyDest
) {
13 const char *abc
= "abc";
18 // Start by copying nothing
19 char *result
= LIBC_NAMESPACE::strncat(dest
, abc
, 0);
20 ASSERT_EQ(dest
, result
);
21 ASSERT_EQ(dest
[0], '\0');
23 // Then copy part of it.
24 result
= LIBC_NAMESPACE::strncat(dest
, abc
, 1);
25 ASSERT_EQ(dest
, result
);
26 ASSERT_STREQ(dest
, "a");
28 // Reset for the last test.
31 // Then copy all of it.
32 result
= LIBC_NAMESPACE::strncat(dest
, abc
, 3);
33 ASSERT_EQ(dest
, result
);
34 ASSERT_STREQ(dest
, result
);
35 ASSERT_STREQ(dest
, abc
);
38 TEST(LlvmLibcStrNCatTest
, NonEmptyDest
) {
39 const char *abc
= "abc";
47 // Copy only part of the string onto the end
48 char *result
= LIBC_NAMESPACE::strncat(dest
, abc
, 1);
49 ASSERT_EQ(dest
, result
);
50 ASSERT_STREQ(dest
, "xyza");
52 // Copy a bit more, but without resetting.
53 result
= LIBC_NAMESPACE::strncat(dest
, abc
, 2);
54 ASSERT_EQ(dest
, result
);
55 ASSERT_STREQ(dest
, "xyzaab");
57 // Set just the end marker, to make sure it overwrites properly.
60 result
= LIBC_NAMESPACE::strncat(dest
, abc
, 3);
61 ASSERT_EQ(dest
, result
);
62 ASSERT_STREQ(dest
, "xyzabc");
64 // Check that copying still works when count > src length
66 // And that it doesn't write beyond what is necessary.
68 result
= LIBC_NAMESPACE::strncat(dest
, abc
, 4);
69 ASSERT_EQ(dest
, result
);
70 ASSERT_STREQ(dest
, "abc");
71 ASSERT_EQ(dest
[4], 'Z');
73 result
= LIBC_NAMESPACE::strncat(dest
, abc
, 5);
74 ASSERT_EQ(dest
, result
);
75 ASSERT_STREQ(dest
, "abcabc");