52 lines · cpp
1//===-- Unittests for strndup ---------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/string/strndup.h"10#include "test/UnitTest/Test.h"11 12TEST(LlvmLibcstrndupTest, EmptyString) {13 const char *empty = "";14 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);19 ::free(result);20}21 22TEST(LlvmLibcstrndupTest, AnyString) {23 const char *abc = "abc";24 25 char *result = LIBC_NAMESPACE::strndup(abc, 3);26 27 ASSERT_NE(result, static_cast<char *>(nullptr));28 ASSERT_NE(abc, const_cast<const char *>(result));29 ASSERT_STREQ(abc, result);30 ::free(result);31 32 result = LIBC_NAMESPACE::strndup(abc, 1);33 34 ASSERT_NE(result, static_cast<char *>(nullptr));35 ASSERT_NE(abc, const_cast<const char *>(result));36 ASSERT_STREQ("a", result);37 ::free(result);38 39 result = LIBC_NAMESPACE::strndup(abc, 10);40 41 ASSERT_NE(result, static_cast<char *>(nullptr));42 ASSERT_NE(abc, const_cast<const char *>(result));43 ASSERT_STREQ(abc, result);44 ::free(result);45}46 47TEST(LlvmLibcstrndupTest, NullPtr) {48 char *result = LIBC_NAMESPACE::strndup(nullptr, 0);49 50 ASSERT_EQ(result, static_cast<char *>(nullptr));51}52