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