1 //===-- Unittests for strndup ---------------------------------------------===//
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/strndup.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcstrndupTest
, EmptyString
) {
13 const char *empty
= "";
15 char *result
= LIBC_NAMESPACE::strndup(empty
, 1);
16 ASSERT_NE(result
, static_cast<char *>(nullptr));
17 ASSERT_NE(empty
, const_cast<const char *>(result
));
18 ASSERT_STREQ(empty
, result
);
22 TEST(LlvmLibcstrndupTest
, AnyString
) {
23 const char *abc
= "abc";
25 char *result
= LIBC_NAMESPACE::strndup(abc
, 3);
27 ASSERT_NE(result
, static_cast<char *>(nullptr));
28 ASSERT_NE(abc
, const_cast<const char *>(result
));
29 ASSERT_STREQ(abc
, result
);
32 result
= LIBC_NAMESPACE::strndup(abc
, 1);
34 ASSERT_NE(result
, static_cast<char *>(nullptr));
35 ASSERT_NE(abc
, const_cast<const char *>(result
));
36 ASSERT_STREQ("a", result
);
39 result
= LIBC_NAMESPACE::strndup(abc
, 10);
41 ASSERT_NE(result
, static_cast<char *>(nullptr));
42 ASSERT_NE(abc
, const_cast<const char *>(result
));
43 ASSERT_STREQ(abc
, result
);
47 TEST(LlvmLibcstrndupTest
, NullPtr
) {
48 char *result
= LIBC_NAMESPACE::strndup(nullptr, 0);
50 ASSERT_EQ(result
, static_cast<char *>(nullptr));