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/__support/macros/config.h" 10 #include "src/strings/bcmp.h" 11 #include "test/UnitTest/Test.h" 12 #include "test/UnitTest/TestLogger.h" 13 #include "test/src/string/memory_utils/memory_check_utils.h" 14 15 namespace LIBC_NAMESPACE_DECL { 16 17 TEST(LlvmLibcBcmpTest, CmpZeroByte) { 18 const char *lhs = "ab"; 19 const char *rhs = "bc"; 20 ASSERT_EQ(LIBC_NAMESPACE::bcmp(lhs, rhs, 0), 0); 21 } 22 23 TEST(LlvmLibcBcmpTest, LhsRhsAreTheSame) { 24 const char *lhs = "ab"; 25 const char *rhs = "ab"; 26 ASSERT_EQ(LIBC_NAMESPACE::bcmp(lhs, rhs, 2), 0); 27 } 28 29 TEST(LlvmLibcBcmpTest, LhsBeforeRhsLexically) { 30 const char *lhs = "ab"; 31 const char *rhs = "ac"; 32 ASSERT_NE(LIBC_NAMESPACE::bcmp(lhs, rhs, 2), 0); 33 } 34 35 TEST(LlvmLibcBcmpTest, LhsAfterRhsLexically) { 36 const char *lhs = "ac"; 37 const char *rhs = "ab"; 38 ASSERT_NE(LIBC_NAMESPACE::bcmp(lhs, rhs, 2), 0); 39 } 40 41 // Adapt CheckBcmp signature to bcmp. 42 static inline int Adaptor(cpp::span<char> p1, cpp::span<char> p2, size_t size) { 43 return LIBC_NAMESPACE::bcmp(p1.begin(), p2.begin(), size); 44 } 45 46 TEST(LlvmLibcBcmpTest, SizeSweep) { 47 static constexpr size_t kMaxSize = 400; 48 Buffer Buffer1(kMaxSize); 49 Buffer Buffer2(kMaxSize); 50 Randomize(Buffer1.span()); 51 for (size_t size = 0; size < kMaxSize; ++size) { 52 auto span1 = Buffer1.span().subspan(0, size); 53 auto span2 = Buffer2.span().subspan(0, size); 54 const bool OK = CheckBcmp<Adaptor>(span1, span2, size); 55 if (!OK) 56 testing::tlog << "Failed at size=" << size << '\n'; 57 ASSERT_TRUE(OK); 58 } 59 } 60 61 } // namespace LIBC_NAMESPACE_DECL 62