1*6ca54e01SGuillaume Chatelet //===-- Unittests for bzero -----------------------------------------------===//
2*6ca54e01SGuillaume Chatelet //
3*6ca54e01SGuillaume Chatelet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*6ca54e01SGuillaume Chatelet // See https://llvm.org/LICENSE.txt for license information.
5*6ca54e01SGuillaume Chatelet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*6ca54e01SGuillaume Chatelet //
7*6ca54e01SGuillaume Chatelet //===----------------------------------------------------------------------===//
8*6ca54e01SGuillaume Chatelet 
9*6ca54e01SGuillaume Chatelet #include "src/string/bzero.h"
10*6ca54e01SGuillaume Chatelet #include "utils/CPP/ArrayRef.h"
11*6ca54e01SGuillaume Chatelet #include "utils/UnitTest/Test.h"
12*6ca54e01SGuillaume Chatelet 
13*6ca54e01SGuillaume Chatelet using __llvm_libc::cpp::Array;
14*6ca54e01SGuillaume Chatelet using __llvm_libc::cpp::ArrayRef;
15*6ca54e01SGuillaume Chatelet using Data = Array<char, 2048>;
16*6ca54e01SGuillaume Chatelet 
17*6ca54e01SGuillaume Chatelet static const ArrayRef<char> kDeadcode("DEADC0DE", 8);
18*6ca54e01SGuillaume Chatelet 
19*6ca54e01SGuillaume Chatelet // Returns a Data object filled with a repetition of `filler`.
20*6ca54e01SGuillaume Chatelet Data getData(ArrayRef<char> filler) {
21*6ca54e01SGuillaume Chatelet   Data out;
22*6ca54e01SGuillaume Chatelet   for (size_t i = 0; i < out.size(); ++i)
23*6ca54e01SGuillaume Chatelet     out[i] = filler[i % filler.size()];
24*6ca54e01SGuillaume Chatelet   return out;
25*6ca54e01SGuillaume Chatelet }
26*6ca54e01SGuillaume Chatelet 
27*6ca54e01SGuillaume Chatelet TEST(BzeroTest, Thorough) {
28*6ca54e01SGuillaume Chatelet   const Data dirty = getData(kDeadcode);
29*6ca54e01SGuillaume Chatelet   for (size_t count = 0; count < 1024; ++count) {
30*6ca54e01SGuillaume Chatelet     for (size_t align = 0; align < 64; ++align) {
31*6ca54e01SGuillaume Chatelet       auto buffer = dirty;
32*6ca54e01SGuillaume Chatelet       char *const dst = &buffer[align];
33*6ca54e01SGuillaume Chatelet       __llvm_libc::bzero(dst, count);
34*6ca54e01SGuillaume Chatelet       // Everything before copy is untouched.
35*6ca54e01SGuillaume Chatelet       for (size_t i = 0; i < align; ++i)
36*6ca54e01SGuillaume Chatelet         ASSERT_EQ(buffer[i], dirty[i]);
37*6ca54e01SGuillaume Chatelet       // Everything in between is copied.
38*6ca54e01SGuillaume Chatelet       for (size_t i = 0; i < count; ++i)
39*6ca54e01SGuillaume Chatelet         ASSERT_EQ(buffer[align + i], char(0));
40*6ca54e01SGuillaume Chatelet       // Everything after copy is untouched.
41*6ca54e01SGuillaume Chatelet       for (size_t i = align + count; i < dirty.size(); ++i)
42*6ca54e01SGuillaume Chatelet         ASSERT_EQ(buffer[i], dirty[i]);
43*6ca54e01SGuillaume Chatelet     }
44*6ca54e01SGuillaume Chatelet   }
45*6ca54e01SGuillaume Chatelet }
46*6ca54e01SGuillaume Chatelet 
47*6ca54e01SGuillaume Chatelet // FIXME: Add tests with reads and writes on the boundary of a read/write
48*6ca54e01SGuillaume Chatelet // protected page to check we're not reading nor writing prior/past the allowed
49*6ca54e01SGuillaume Chatelet // regions.
50