1//===-- Unittests for wcslcpy ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "hdr/types/size_t.h"
10#include "hdr/types/wchar_t.h"
11#include "src/wchar/wcslcpy.h"
12#include "test/UnitTest/Test.h"
13
14TEST(LlvmLibcWCSLCpyTest, BiggerSource) {
15 const wchar_t *src = L"abcde";
16 wchar_t dst[3];
17 size_t res = LIBC_NAMESPACE::wcslcpy(dst, src, 3);
18 ASSERT_TRUE(dst[0] == L'a');
19 ASSERT_TRUE(dst[1] == L'b');
20 // Should append null terminator
21 ASSERT_TRUE(dst[2] == L'\0');
22 // Should still return length of src
23 ASSERT_EQ(res, size_t(5));
24}
25
26TEST(LlvmLibcWCSLCpyTest, CopyZero) {
27 const wchar_t *src = L"abcde";
28 wchar_t dst = L'f';
29 // Copying zero should not change destination
30 size_t res = LIBC_NAMESPACE::wcslcpy(&dst, src, 0);
31 ASSERT_TRUE(dst == L'f');
32 // Should still return length of src
33 ASSERT_EQ(res, size_t(5));
34}
35
36TEST(LlvmLibcWCSLCpyTest, SmallerSource) {
37 const wchar_t *src = L"abc";
38 wchar_t dst[7]{L"123456"};
39 size_t res = LIBC_NAMESPACE::wcslcpy(dst, src, 7);
40 ASSERT_TRUE(dst[0] == L'a');
41 ASSERT_TRUE(dst[1] == L'b');
42 ASSERT_TRUE(dst[2] == L'c');
43 // Should append null terminator after copying source
44 ASSERT_TRUE(dst[3] == L'\0');
45 // Should not change following characters
46 ASSERT_TRUE(dst[4] == L'5');
47 ASSERT_TRUE(dst[5] == L'6');
48 ASSERT_TRUE(dst[6] == L'\0');
49 // Should still return length of src
50 ASSERT_EQ(res, size_t(3));
51}
52
53TEST(LlvmLibcWCSLCpyTest, DoesNotCopyAfterNull) {
54 const wchar_t src[5] = {L'a', L'b', L'\0', L'c', L'd'};
55 wchar_t dst[5]{L"1234"};
56 size_t res = LIBC_NAMESPACE::wcslcpy(dst, src, 5);
57 ASSERT_TRUE(dst[0] == L'a');
58 ASSERT_TRUE(dst[1] == L'b');
59 ASSERT_TRUE(dst[2] == L'\0');
60 // Should not change following characters
61 ASSERT_TRUE(dst[3] == L'4');
62 ASSERT_TRUE(dst[4] == L'\0');
63 // Should still return length of src
64 ASSERT_EQ(res, size_t(2));
65}
66

source code of libc/test/src/wchar/wcslcpy_test.cpp