16ca54e01SGuillaume Chatelet //===-- Unittests for bzero -----------------------------------------------===//
26ca54e01SGuillaume Chatelet //
36ca54e01SGuillaume Chatelet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46ca54e01SGuillaume Chatelet // See https://llvm.org/LICENSE.txt for license information.
56ca54e01SGuillaume Chatelet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66ca54e01SGuillaume Chatelet //
76ca54e01SGuillaume Chatelet //===----------------------------------------------------------------------===//
86ca54e01SGuillaume Chatelet 
9f362aea4SSiva Chandra Reddy #include "src/__support/CPP/ArrayRef.h"
106ca54e01SGuillaume Chatelet #include "src/string/bzero.h"
116ca54e01SGuillaume Chatelet #include "utils/UnitTest/Test.h"
126ca54e01SGuillaume Chatelet 
136ca54e01SGuillaume Chatelet using __llvm_libc::cpp::Array;
146ca54e01SGuillaume Chatelet using __llvm_libc::cpp::ArrayRef;
156ca54e01SGuillaume Chatelet using Data = Array<char, 2048>;
166ca54e01SGuillaume Chatelet 
17*25226f3eSMichael Jones static const ArrayRef<char> k_deadcode("DEADC0DE", 8);
186ca54e01SGuillaume Chatelet 
196ca54e01SGuillaume Chatelet // Returns a Data object filled with a repetition of `filler`.
get_data(ArrayRef<char> filler)20*25226f3eSMichael Jones Data get_data(ArrayRef<char> filler) {
216ca54e01SGuillaume Chatelet   Data out;
226ca54e01SGuillaume Chatelet   for (size_t i = 0; i < out.size(); ++i)
236ca54e01SGuillaume Chatelet     out[i] = filler[i % filler.size()];
246ca54e01SGuillaume Chatelet   return out;
256ca54e01SGuillaume Chatelet }
266ca54e01SGuillaume Chatelet 
TEST(LlvmLibcBzeroTest,Thorough)271df0dbfcSMichael Jones TEST(LlvmLibcBzeroTest, Thorough) {
28*25226f3eSMichael Jones   const Data dirty = get_data(k_deadcode);
296ca54e01SGuillaume Chatelet   for (size_t count = 0; count < 1024; ++count) {
306ca54e01SGuillaume Chatelet     for (size_t align = 0; align < 64; ++align) {
316ca54e01SGuillaume Chatelet       auto buffer = dirty;
326ca54e01SGuillaume Chatelet       char *const dst = &buffer[align];
336ca54e01SGuillaume Chatelet       __llvm_libc::bzero(dst, count);
346ca54e01SGuillaume Chatelet       // Everything before copy is untouched.
356ca54e01SGuillaume Chatelet       for (size_t i = 0; i < align; ++i)
366ca54e01SGuillaume Chatelet         ASSERT_EQ(buffer[i], dirty[i]);
376ca54e01SGuillaume Chatelet       // Everything in between is copied.
386ca54e01SGuillaume Chatelet       for (size_t i = 0; i < count; ++i)
396ca54e01SGuillaume Chatelet         ASSERT_EQ(buffer[align + i], char(0));
406ca54e01SGuillaume Chatelet       // Everything after copy is untouched.
416ca54e01SGuillaume Chatelet       for (size_t i = align + count; i < dirty.size(); ++i)
426ca54e01SGuillaume Chatelet         ASSERT_EQ(buffer[i], dirty[i]);
436ca54e01SGuillaume Chatelet     }
446ca54e01SGuillaume Chatelet   }
456ca54e01SGuillaume Chatelet }
466ca54e01SGuillaume Chatelet 
476ca54e01SGuillaume Chatelet // FIXME: Add tests with reads and writes on the boundary of a read/write
486ca54e01SGuillaume Chatelet // protected page to check we're not reading nor writing prior/past the allowed
496ca54e01SGuillaume Chatelet // regions.
50