16ca54e01SGuillaume Chatelet //===-- Unittests for memset ----------------------------------------------===// 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 96ca54e01SGuillaume Chatelet #include "src/string/memset.h" 106ca54e01SGuillaume Chatelet #include "utils/CPP/ArrayRef.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 176ca54e01SGuillaume Chatelet static const ArrayRef<char> kDeadcode("DEADC0DE", 8); 186ca54e01SGuillaume Chatelet 196ca54e01SGuillaume Chatelet // Returns a Data object filled with a repetition of `filler`. 206ca54e01SGuillaume Chatelet Data getData(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 27*1df0dbfcSMichael Jones TEST(LlvmLibcMemsetTest, Thorough) { 286ca54e01SGuillaume Chatelet const Data dirty = getData(kDeadcode); 296ca54e01SGuillaume Chatelet for (int value = -1; value <= 1; ++value) { 306ca54e01SGuillaume Chatelet for (size_t count = 0; count < 1024; ++count) { 316ca54e01SGuillaume Chatelet for (size_t align = 0; align < 64; ++align) { 326ca54e01SGuillaume Chatelet auto buffer = dirty; 336ca54e01SGuillaume Chatelet void *const dst = &buffer[align]; 346ca54e01SGuillaume Chatelet void *const ret = __llvm_libc::memset(dst, value, count); 356ca54e01SGuillaume Chatelet // Return value is `dst`. 366ca54e01SGuillaume Chatelet ASSERT_EQ(ret, dst); 376ca54e01SGuillaume Chatelet // Everything before copy is untouched. 386ca54e01SGuillaume Chatelet for (size_t i = 0; i < align; ++i) 396ca54e01SGuillaume Chatelet ASSERT_EQ(buffer[i], dirty[i]); 406ca54e01SGuillaume Chatelet // Everything in between is copied. 416ca54e01SGuillaume Chatelet for (size_t i = 0; i < count; ++i) 426ca54e01SGuillaume Chatelet ASSERT_EQ(buffer[align + i], (char)value); 436ca54e01SGuillaume Chatelet // Everything after copy is untouched. 446ca54e01SGuillaume Chatelet for (size_t i = align + count; i < dirty.size(); ++i) 456ca54e01SGuillaume Chatelet ASSERT_EQ(buffer[i], dirty[i]); 466ca54e01SGuillaume Chatelet } 476ca54e01SGuillaume Chatelet } 486ca54e01SGuillaume Chatelet } 496ca54e01SGuillaume Chatelet } 506ca54e01SGuillaume Chatelet 516ca54e01SGuillaume Chatelet // FIXME: Add tests with reads and writes on the boundary of a read/write 526ca54e01SGuillaume Chatelet // protected page to check we're not reading nor writing prior/past the allowed 536ca54e01SGuillaume Chatelet // regions. 54