aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/string/mempcpy_test.cpp
blob: 7351a829f5c44f109119eca874c53b3320b3d056 (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
//===-- Unittests for mempcpy ---------------------------------------------===//
//
// 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 "hdr/signal_macros.h"
#include "src/string/mempcpy.h"
#include "test/UnitTest/Test.h"

// Since this function just calls out to memcpy, and memcpy has its own unit
// tests, it is assumed that memcpy works. These tests are just for the specific
// mempcpy behavior (returning the end of what was copied).
TEST(LlvmLibcMempcpyTest, Simple) {
  const char *src = "12345";
  char dest[10] = {};
  void *result = LIBC_NAMESPACE::mempcpy(dest, src, 6);
  ASSERT_EQ(static_cast<char *>(result), dest + 6);
  ASSERT_STREQ(src, dest);
}

TEST(LlvmLibcMempcpyTest, ZeroCount) {
  const char *src = "12345";
  char dest[10];
  void *result = LIBC_NAMESPACE::mempcpy(dest, src, 0);
  ASSERT_EQ(static_cast<char *>(result), dest + 0);
}

#if defined(LIBC_ADD_NULL_CHECKS)

TEST(LlvmLibcMempcpyTest, CrashOnNullPtr) {
  ASSERT_DEATH([]() { LIBC_NAMESPACE::mempcpy(nullptr, nullptr, 1); },
               WITH_SIGNAL(-1));
}

#endif // defined(LIBC_ADD_NULL_CHECKS)