1 | //===-- Unittests for strdup ----------------------------------------------===// |
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 "src/errno/libc_errno.h" |
10 | #include "src/string/strdup.h" |
11 | #include "test/UnitTest/Test.h" |
12 | |
13 | #include <stdlib.h> |
14 | |
15 | TEST(LlvmLibcStrDupTest, EmptyString) { |
16 | const char *empty = "" ; |
17 | |
18 | LIBC_NAMESPACE::libc_errno = 0; |
19 | char *result = LIBC_NAMESPACE::strdup(src: empty); |
20 | ASSERT_ERRNO_SUCCESS(); |
21 | |
22 | ASSERT_NE(result, static_cast<char *>(nullptr)); |
23 | ASSERT_NE(empty, const_cast<const char *>(result)); |
24 | ASSERT_STREQ(empty, result); |
25 | ::free(ptr: result); |
26 | } |
27 | |
28 | TEST(LlvmLibcStrDupTest, AnyString) { |
29 | const char *abc = "abc" ; |
30 | |
31 | LIBC_NAMESPACE::libc_errno = 0; |
32 | char *result = LIBC_NAMESPACE::strdup(src: abc); |
33 | ASSERT_ERRNO_SUCCESS(); |
34 | |
35 | ASSERT_NE(result, static_cast<char *>(nullptr)); |
36 | ASSERT_NE(abc, const_cast<const char *>(result)); |
37 | ASSERT_STREQ(abc, result); |
38 | ::free(ptr: result); |
39 | } |
40 | |
41 | TEST(LlvmLibcStrDupTest, NullPtr) { |
42 | LIBC_NAMESPACE::libc_errno = 0; |
43 | char *result = LIBC_NAMESPACE::strdup(src: nullptr); |
44 | ASSERT_ERRNO_SUCCESS(); |
45 | |
46 | ASSERT_EQ(result, static_cast<char *>(nullptr)); |
47 | } |
48 | |