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