aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/string/strdup_test.cpp
blob: b1d4df31603c4b211eb5a546fa37411a018d3f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//===-- Unittests for strdup ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/string/strdup.h"
#include "test/UnitTest/Test.h"

#include <stdlib.h>

TEST(LlvmLibcStrDupTest, EmptyString) {
  const char *empty = "";

  libc_errno = 0;
  char *result = LIBC_NAMESPACE::strdup(empty);
  ASSERT_EQ(libc_errno, 0);

  ASSERT_NE(result, static_cast<char *>(nullptr));
  ASSERT_NE(empty, const_cast<const char *>(result));
  ASSERT_STREQ(empty, result);
  ::free(result);
}

TEST(LlvmLibcStrDupTest, AnyString) {
  const char *abc = "abc";

  libc_errno = 0;
  char *result = LIBC_NAMESPACE::strdup(abc);
  ASSERT_EQ(libc_errno, 0);

  ASSERT_NE(result, static_cast<char *>(nullptr));
  ASSERT_NE(abc, const_cast<const char *>(result));
  ASSERT_STREQ(abc, result);
  ::free(result);
}

TEST(LlvmLibcStrDupTest, NullPtr) {
  libc_errno = 0;
  char *result = LIBC_NAMESPACE::strdup(nullptr);
  ASSERT_EQ(libc_errno, 0);

  ASSERT_EQ(result, static_cast<char *>(nullptr));
}