xref: /llvm-project/libc/test/src/string/strndup_test.cpp (revision 33bdb53d864e3e244d8fd5649062f17b7d4c958d)
13bbbec1aSMichael Jones //===-- Unittests for strndup ---------------------------------------------===//
23bbbec1aSMichael Jones //
33bbbec1aSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43bbbec1aSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
53bbbec1aSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63bbbec1aSMichael Jones //
73bbbec1aSMichael Jones //===----------------------------------------------------------------------===//
83bbbec1aSMichael Jones 
93bbbec1aSMichael Jones #include "src/string/strndup.h"
10af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
113bbbec1aSMichael Jones 
123bbbec1aSMichael Jones TEST(LlvmLibcstrndupTest, EmptyString) {
133bbbec1aSMichael Jones   const char *empty = "";
143bbbec1aSMichael Jones 
15*b6bc9d72SGuillaume Chatelet   char *result = LIBC_NAMESPACE::strndup(empty, 1);
163bbbec1aSMichael Jones   ASSERT_NE(result, static_cast<char *>(nullptr));
173bbbec1aSMichael Jones   ASSERT_NE(empty, const_cast<const char *>(result));
183bbbec1aSMichael Jones   ASSERT_STREQ(empty, result);
193bbbec1aSMichael Jones   ::free(result);
203bbbec1aSMichael Jones }
213bbbec1aSMichael Jones 
223bbbec1aSMichael Jones TEST(LlvmLibcstrndupTest, AnyString) {
233bbbec1aSMichael Jones   const char *abc = "abc";
243bbbec1aSMichael Jones 
25*b6bc9d72SGuillaume Chatelet   char *result = LIBC_NAMESPACE::strndup(abc, 3);
263bbbec1aSMichael Jones 
273bbbec1aSMichael Jones   ASSERT_NE(result, static_cast<char *>(nullptr));
283bbbec1aSMichael Jones   ASSERT_NE(abc, const_cast<const char *>(result));
293bbbec1aSMichael Jones   ASSERT_STREQ(abc, result);
303bbbec1aSMichael Jones   ::free(result);
313bbbec1aSMichael Jones 
32*b6bc9d72SGuillaume Chatelet   result = LIBC_NAMESPACE::strndup(abc, 1);
333bbbec1aSMichael Jones 
343bbbec1aSMichael Jones   ASSERT_NE(result, static_cast<char *>(nullptr));
353bbbec1aSMichael Jones   ASSERT_NE(abc, const_cast<const char *>(result));
363bbbec1aSMichael Jones   ASSERT_STREQ("a", result);
373bbbec1aSMichael Jones   ::free(result);
383bbbec1aSMichael Jones 
39*b6bc9d72SGuillaume Chatelet   result = LIBC_NAMESPACE::strndup(abc, 10);
403bbbec1aSMichael Jones 
413bbbec1aSMichael Jones   ASSERT_NE(result, static_cast<char *>(nullptr));
423bbbec1aSMichael Jones   ASSERT_NE(abc, const_cast<const char *>(result));
433bbbec1aSMichael Jones   ASSERT_STREQ(abc, result);
443bbbec1aSMichael Jones   ::free(result);
453bbbec1aSMichael Jones }
463bbbec1aSMichael Jones 
473bbbec1aSMichael Jones TEST(LlvmLibcstrndupTest, NullPtr) {
48*b6bc9d72SGuillaume Chatelet   char *result = LIBC_NAMESPACE::strndup(nullptr, 0);
493bbbec1aSMichael Jones 
503bbbec1aSMichael Jones   ASSERT_EQ(result, static_cast<char *>(nullptr));
513bbbec1aSMichael Jones }
52