1*3bbbec1aSMichael Jones //===-- Unittests for strndup ---------------------------------------------===//
2*3bbbec1aSMichael Jones //
3*3bbbec1aSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3bbbec1aSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5*3bbbec1aSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3bbbec1aSMichael Jones //
7*3bbbec1aSMichael Jones //===----------------------------------------------------------------------===//
8*3bbbec1aSMichael Jones
9*3bbbec1aSMichael Jones #include "src/string/strndup.h"
10*3bbbec1aSMichael Jones #include "utils/UnitTest/Test.h"
11*3bbbec1aSMichael Jones #include <stdlib.h>
12*3bbbec1aSMichael Jones
TEST(LlvmLibcstrndupTest,EmptyString)13*3bbbec1aSMichael Jones TEST(LlvmLibcstrndupTest, EmptyString) {
14*3bbbec1aSMichael Jones const char *empty = "";
15*3bbbec1aSMichael Jones
16*3bbbec1aSMichael Jones char *result = __llvm_libc::strndup(empty, 1);
17*3bbbec1aSMichael Jones ASSERT_NE(result, static_cast<char *>(nullptr));
18*3bbbec1aSMichael Jones ASSERT_NE(empty, const_cast<const char *>(result));
19*3bbbec1aSMichael Jones ASSERT_STREQ(empty, result);
20*3bbbec1aSMichael Jones ::free(result);
21*3bbbec1aSMichael Jones }
22*3bbbec1aSMichael Jones
TEST(LlvmLibcstrndupTest,AnyString)23*3bbbec1aSMichael Jones TEST(LlvmLibcstrndupTest, AnyString) {
24*3bbbec1aSMichael Jones const char *abc = "abc";
25*3bbbec1aSMichael Jones
26*3bbbec1aSMichael Jones char *result = __llvm_libc::strndup(abc, 3);
27*3bbbec1aSMichael Jones
28*3bbbec1aSMichael Jones ASSERT_NE(result, static_cast<char *>(nullptr));
29*3bbbec1aSMichael Jones ASSERT_NE(abc, const_cast<const char *>(result));
30*3bbbec1aSMichael Jones ASSERT_STREQ(abc, result);
31*3bbbec1aSMichael Jones ::free(result);
32*3bbbec1aSMichael Jones
33*3bbbec1aSMichael Jones result = __llvm_libc::strndup(abc, 1);
34*3bbbec1aSMichael Jones
35*3bbbec1aSMichael Jones ASSERT_NE(result, static_cast<char *>(nullptr));
36*3bbbec1aSMichael Jones ASSERT_NE(abc, const_cast<const char *>(result));
37*3bbbec1aSMichael Jones ASSERT_STREQ("a", result);
38*3bbbec1aSMichael Jones ::free(result);
39*3bbbec1aSMichael Jones
40*3bbbec1aSMichael Jones result = __llvm_libc::strndup(abc, 10);
41*3bbbec1aSMichael Jones
42*3bbbec1aSMichael Jones ASSERT_NE(result, static_cast<char *>(nullptr));
43*3bbbec1aSMichael Jones ASSERT_NE(abc, const_cast<const char *>(result));
44*3bbbec1aSMichael Jones ASSERT_STREQ(abc, result);
45*3bbbec1aSMichael Jones ::free(result);
46*3bbbec1aSMichael Jones }
47*3bbbec1aSMichael Jones
TEST(LlvmLibcstrndupTest,NullPtr)48*3bbbec1aSMichael Jones TEST(LlvmLibcstrndupTest, NullPtr) {
49*3bbbec1aSMichael Jones char *result = __llvm_libc::strndup(nullptr, 0);
50*3bbbec1aSMichael Jones
51*3bbbec1aSMichael Jones ASSERT_EQ(result, static_cast<char *>(nullptr));
52*3bbbec1aSMichael Jones }
53