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_NE(__llvm_libc::bcmp(lhs, rhs, 2), 0); 28 } 29 30 TEST(LlvmLibcBcmpTest, LhsAfterRhsLexically) { 31 const char *lhs = "ac"; 32 const char *rhs = "ab"; 33 EXPECT_NE(__llvm_libc::bcmp(lhs, rhs, 2), 0); 34 } 35 36 TEST(LlvmLibcBcmpTest, Sweep) { 37 static constexpr size_t K_MAX_SIZE = 1024; 38 char lhs[K_MAX_SIZE]; 39 char rhs[K_MAX_SIZE]; 40 41 const auto reset = [](char *const ptr) { 42 for (size_t i = 0; i < K_MAX_SIZE; ++i) 43 ptr[i] = 'a'; 44 }; 45 46 reset(lhs); 47 reset(rhs); 48 for (size_t i = 0; i < K_MAX_SIZE; ++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 < K_MAX_SIZE; ++i) { 54 rhs[i] = 'b'; 55 EXPECT_NE(__llvm_libc::bcmp(lhs, rhs, K_MAX_SIZE), 0); 56 rhs[i] = 'a'; 57 } 58 } 59