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

source code of libc/test/src/string/strndup_test.cpp