1 //===-- Unittests for memmove ---------------------------------------------===// 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/memmove.h" 11 #include "utils/UnitTest/Test.h" 12 13 class LlvmLibcMemmoveTest : public __llvm_libc::testing::Test { 14 public: 15 void check_memmove(void *dst, const void *src, size_t count, 16 const unsigned char *str, 17 const __llvm_libc::cpp::ArrayRef<unsigned char> expected) { 18 void *result = __llvm_libc::memmove(dst, src, count); 19 // Making sure the pointer returned is same with `dst`. 20 EXPECT_EQ(result, dst); 21 // `expected` is designed according to `str`. 22 // `dst` and `src` might be part of `str`. 23 // Making sure `str` is same with `expected`. 24 for (size_t i = 0; i < expected.size(); ++i) 25 EXPECT_EQ(str[i], expected[i]); 26 } 27 }; 28 29 TEST_F(LlvmLibcMemmoveTest, MoveZeroByte) { 30 unsigned char dst[] = {'a', 'b'}; 31 const unsigned char src[] = {'y', 'z'}; 32 const unsigned char expected[] = {'a', 'b'}; 33 check_memmove(dst, src, 0, dst, expected); 34 } 35 36 TEST_F(LlvmLibcMemmoveTest, OverlapThatDstAndSrcPointToSameAddress) { 37 unsigned char str[] = {'a', 'b'}; 38 const unsigned char expected[] = {'a', 'b'}; 39 check_memmove(str, str, 1, str, expected); 40 } 41 42 TEST_F(LlvmLibcMemmoveTest, OverlapThatDstStartsBeforeSrc) { 43 // Set boundary at beginning and end for not overstepping when 44 // copy forward or backward. 45 unsigned char str[] = {'z', 'a', 'b', 'c', 'z'}; 46 const unsigned char expected[] = {'z', 'b', 'c', 'c', 'z'}; 47 // `dst` is `&str[1]`. 48 check_memmove(&str[1], &str[2], 2, str, expected); 49 } 50 51 TEST_F(LlvmLibcMemmoveTest, OverlapThatDstStartsAfterSrc) { 52 unsigned char str[] = {'z', 'a', 'b', 'c', 'z'}; 53 const unsigned char expected[] = {'z', 'a', 'a', 'b', 'z'}; 54 check_memmove(&str[2], &str[1], 2, str, expected); 55 } 56 57 // e.g. `dst` follow `src`. 58 // str: [abcdefghij] 59 // [__src_____] 60 // [_____dst__] 61 TEST_F(LlvmLibcMemmoveTest, SrcFollowDst) { 62 unsigned char str[] = {'z', 'a', 'b', 'z'}; 63 const unsigned char expected[] = {'z', 'b', 'b', 'z'}; 64 check_memmove(&str[1], &str[2], 1, str, expected); 65 } 66 67 TEST_F(LlvmLibcMemmoveTest, DstFollowSrc) { 68 unsigned char str[] = {'z', 'a', 'b', 'z'}; 69 const unsigned char expected[] = {'z', 'a', 'a', 'z'}; 70 check_memmove(&str[2], &str[1], 1, str, expected); 71 } 72