[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / string / strndup_test.cpp
blob3e7129b2d1f00593b71d8b9ba610334c4536448f
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 //===----------------------------------------------------------------------===//
9 #include "src/string/strndup.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcstrndupTest, EmptyString) {
13 const char *empty = "";
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);
22 TEST(LlvmLibcstrndupTest, AnyString) {
23 const char *abc = "abc";
25 char *result = LIBC_NAMESPACE::strndup(abc, 3);
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);
32 result = LIBC_NAMESPACE::strndup(abc, 1);
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);
39 result = LIBC_NAMESPACE::strndup(abc, 10);
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);
47 TEST(LlvmLibcstrndupTest, NullPtr) {
48 char *result = LIBC_NAMESPACE::strndup(nullptr, 0);
50 ASSERT_EQ(result, static_cast<char *>(nullptr));