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