1 //===-- Unittests for memcmp ----------------------------------------------===// 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/string/memcmp.h" 10 #include "utils/UnitTest/Test.h" 11 #include <cstring> 12 13 TEST(LlvmLibcMemcmpTest, CmpZeroByte) { 14 const char *lhs = "ab"; 15 const char *rhs = "bc"; 16 EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 0), 0); 17 } 18 19 TEST(LlvmLibcMemcmpTest, LhsRhsAreTheSame) { 20 const char *lhs = "ab"; 21 const char *rhs = "ab"; 22 EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 2), 0); 23 } 24 25 TEST(LlvmLibcMemcmpTest, LhsBeforeRhsLexically) { 26 const char *lhs = "ab"; 27 const char *rhs = "ac"; 28 EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 2), -1); 29 } 30 31 TEST(LlvmLibcMemcmpTest, LhsAfterRhsLexically) { 32 const char *lhs = "ac"; 33 const char *rhs = "ab"; 34 EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 2), 1); 35 } 36 37 TEST(LlvmLibcMemcmpTest, Sweep) { 38 static constexpr size_t kMaxSize = 1024; 39 char lhs[kMaxSize]; 40 char rhs[kMaxSize]; 41 42 memset(lhs, 'a', sizeof(lhs)); 43 memset(rhs, 'a', sizeof(rhs)); 44 for (int i = 0; i < kMaxSize; ++i) 45 EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, i), 0); 46 47 memset(lhs, 'a', sizeof(lhs)); 48 memset(rhs, 'a', sizeof(rhs)); 49 for (int i = 0; i < kMaxSize; ++i) { 50 rhs[i] = 'b'; 51 EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, kMaxSize), -1); 52 rhs[i] = 'a'; 53 } 54 } 55