aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/wchar/wmemcpy_test.cpp
blob: 5533eef5a9abc7c3a4e0f504a8e2a00a3b84ae73 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//===-- Unittests for wmemcpy ---------------------------------------------===//
//
// 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/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/wchar/wmemcpy.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWMemcpyTest, CopyIntoEmpty) {
  wchar_t dest[10] = {};
  const wchar_t *src = L"abcde";
  LIBC_NAMESPACE::wmemcpy(dest, src, 6);
  ASSERT_TRUE(src[0] == dest[0]);
  ASSERT_TRUE(src[1] == dest[1]);
  ASSERT_TRUE(src[2] == dest[2]);
  ASSERT_TRUE(src[3] == dest[3]);
  ASSERT_TRUE(src[4] == dest[4]);
  ASSERT_TRUE(src[5] == dest[5]);
}

TEST(LlvmLibcWMemcpyTest, CopyFullString) {
  // After copying, strings should be the same.
  wchar_t dest[10] = {};
  const wchar_t *src = L"abcde";
  LIBC_NAMESPACE::wmemcpy(dest, src, 6);
  ASSERT_TRUE(src[0] == dest[0]);
  ASSERT_TRUE(src[1] == dest[1]);
  ASSERT_TRUE(src[2] == dest[2]);
  ASSERT_TRUE(src[3] == dest[3]);
  ASSERT_TRUE(src[4] == dest[4]);
  ASSERT_TRUE(src[5] == dest[5]);
}

TEST(LlvmLibcWMemcpyTest, CopyPartialString) {
  // After copying, only first two characters should be the same.
  wchar_t dest[10] = {};
  const wchar_t *src = L"abcde";
  LIBC_NAMESPACE::wmemcpy(dest, src, 2);
  ASSERT_TRUE(src[0] == dest[0]);
  ASSERT_TRUE(src[1] == dest[1]);
  ASSERT_TRUE(src[2] != dest[2]);
  ASSERT_TRUE(src[3] != dest[3]);
  ASSERT_TRUE(src[4] != dest[4]);
}

TEST(LlvmLibcWMemcpyTest, CopyZeroCharacters) {
  // Copying 0 characters should not change the string
  wchar_t dest[10] = {L'1', L'2', L'3', L'4', L'5', L'\0'};
  const wchar_t *src = L"abcde";
  LIBC_NAMESPACE::wmemcpy(dest, src, 0);
  ASSERT_TRUE(L'1' == dest[0]);
  ASSERT_TRUE(L'2' == dest[1]);
  ASSERT_TRUE(L'3' == dest[2]);
  ASSERT_TRUE(L'4' == dest[3]);
  ASSERT_TRUE(L'5' == dest[4]);
}