1 //===-- Unittests for memcpy ----------------------------------------------===//
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 "src/string/memcpy.h"
10 #include "utils/CPP/ArrayRef.h"
11 #include "utils/UnitTest/Test.h"
12
13 using __llvm_libc::cpp::Array;
14 using __llvm_libc::cpp::ArrayRef;
15 using Data = Array<char, 2048>;
16
17 static const ArrayRef<char> kNumbers("0123456789", 10);
18 static const ArrayRef<char> kDeadcode("DEADC0DE", 8);
19
20 // Returns a Data object filled with a repetition of `filler`.
getData(ArrayRef<char> filler)21 Data getData(ArrayRef<char> filler) {
22 Data out;
23 for (size_t i = 0; i < out.size(); ++i)
24 out[i] = filler[i % filler.size()];
25 return out;
26 }
27
TEST(MemcpyTest,Thorough)28 TEST(MemcpyTest, Thorough) {
29 const Data groundtruth = getData(kNumbers);
30 const Data dirty = getData(kDeadcode);
31 for (size_t count = 0; count < 1024; ++count) {
32 for (size_t align = 0; align < 64; ++align) {
33 auto buffer = dirty;
34 const char *const src = groundtruth.data();
35 void *const dst = &buffer[align];
36 void *const ret = __llvm_libc::memcpy(dst, src, count);
37 // Return value is `dst`.
38 ASSERT_EQ(ret, dst);
39 // Everything before copy is untouched.
40 for (size_t i = 0; i < align; ++i)
41 ASSERT_EQ(buffer[i], dirty[i]);
42 // Everything in between is copied.
43 for (size_t i = 0; i < count; ++i)
44 ASSERT_EQ(buffer[align + i], groundtruth[i]);
45 // Everything after copy is untouched.
46 for (size_t i = align + count; i < dirty.size(); ++i)
47 ASSERT_EQ(buffer[i], dirty[i]);
48 }
49 }
50 }
51
52 // FIXME: Add tests with reads and writes on the boundary of a read/write
53 // protected page to check we're not reading nor writing prior/past the allowed
54 // regions.
55