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