1 //===-- Unittests for memcmp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "memory_utils/memory_check_utils.h"
10 #include "src/__support/macros/config.h"
11 #include "src/string/memcmp.h"
12 #include "test/UnitTest/Test.h"
13 #include "test/UnitTest/TestLogger.h"
15 namespace LIBC_NAMESPACE_DECL
{
17 TEST(LlvmLibcMemcmpTest
, CmpZeroByte
) {
18 const char *lhs
= "ab";
19 const char *rhs
= "yz";
20 EXPECT_EQ(LIBC_NAMESPACE::memcmp(lhs
, rhs
, 0), 0);
23 TEST(LlvmLibcMemcmpTest
, LhsRhsAreTheSame
) {
24 const char *lhs
= "ab";
25 const char *rhs
= "ab";
26 EXPECT_EQ(LIBC_NAMESPACE::memcmp(lhs
, rhs
, 2), 0);
29 TEST(LlvmLibcMemcmpTest
, LhsBeforeRhsLexically
) {
30 const char *lhs
= "ab";
31 const char *rhs
= "az";
32 EXPECT_LT(LIBC_NAMESPACE::memcmp(lhs
, rhs
, 2), 0);
35 TEST(LlvmLibcMemcmpTest
, LhsAfterRhsLexically
) {
36 const char *lhs
= "az";
37 const char *rhs
= "ab";
38 EXPECT_GT(LIBC_NAMESPACE::memcmp(lhs
, rhs
, 2), 0);
41 TEST(LlvmLibcMemcmpTest
, Issue77080
) {
42 // https://github.com/llvm/llvm-project/issues/77080
43 constexpr char lhs
[35] = "1.069cd68bbe76eb2143a3284d27ebe220";
44 constexpr char rhs
[35] = "1.0500185b5d966a544e2d0fa40701b0f3";
45 ASSERT_GE(LIBC_NAMESPACE::memcmp(lhs
, rhs
, 34), 1);
48 // Adapt CheckMemcmp signature to memcmp.
49 static inline int Adaptor(cpp::span
<char> p1
, cpp::span
<char> p2
, size_t size
) {
50 return LIBC_NAMESPACE::memcmp(p1
.begin(), p2
.begin(), size
);
53 TEST(LlvmLibcMemcmpTest
, SizeSweep
) {
54 static constexpr size_t kMaxSize
= 400;
55 Buffer
Buffer1(kMaxSize
);
56 Buffer
Buffer2(kMaxSize
);
57 Randomize(Buffer1
.span());
58 for (size_t size
= 0; size
< kMaxSize
; ++size
) {
59 auto span1
= Buffer1
.span().subspan(0, size
);
60 auto span2
= Buffer2
.span().subspan(0, size
);
61 const bool OK
= CheckMemcmp
<Adaptor
>(span1
, span2
, size
);
63 testing::tlog
<< "Failed at size=" << size
<< '\n';
68 } // namespace LIBC_NAMESPACE_DECL