1 //===-- Unittests for strdup ----------------------------------------------===//
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/errno/libc_errno.h"
10 #include "src/string/strdup.h"
11 #include "test/UnitTest/Test.h"
13 TEST(LlvmLibcStrDupTest
, EmptyString
) {
14 const char *empty
= "";
16 LIBC_NAMESPACE::libc_errno
= 0;
17 char *result
= LIBC_NAMESPACE::strdup(empty
);
18 ASSERT_ERRNO_SUCCESS();
20 ASSERT_NE(result
, static_cast<char *>(nullptr));
21 ASSERT_NE(empty
, const_cast<const char *>(result
));
22 ASSERT_STREQ(empty
, result
);
26 TEST(LlvmLibcStrDupTest
, AnyString
) {
27 const char *abc
= "abc";
29 LIBC_NAMESPACE::libc_errno
= 0;
30 char *result
= LIBC_NAMESPACE::strdup(abc
);
31 ASSERT_ERRNO_SUCCESS();
33 ASSERT_NE(result
, static_cast<char *>(nullptr));
34 ASSERT_NE(abc
, const_cast<const char *>(result
));
35 ASSERT_STREQ(abc
, result
);
39 TEST(LlvmLibcStrDupTest
, NullPtr
) {
40 LIBC_NAMESPACE::libc_errno
= 0;
41 char *result
= LIBC_NAMESPACE::strdup(nullptr);
42 ASSERT_ERRNO_SUCCESS();
44 ASSERT_EQ(result
, static_cast<char *>(nullptr));