brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · 99d08a4 Raw
79 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/signal_macros.h"10#include "memory_utils/memory_check_utils.h"11#include "src/__support/macros/config.h"12#include "src/string/memcmp.h"13#include "test/UnitTest/Test.h"14#include "test/UnitTest/TestLogger.h"15 16namespace LIBC_NAMESPACE_DECL {17 18TEST(LlvmLibcMemcmpTest, CmpZeroByte) {19  const char *lhs = "ab";20  const char *rhs = "yz";21  EXPECT_EQ(LIBC_NAMESPACE::memcmp(lhs, rhs, 0), 0);22}23 24TEST(LlvmLibcMemcmpTest, LhsRhsAreTheSame) {25  const char *lhs = "ab";26  const char *rhs = "ab";27  EXPECT_EQ(LIBC_NAMESPACE::memcmp(lhs, rhs, 2), 0);28}29 30TEST(LlvmLibcMemcmpTest, LhsBeforeRhsLexically) {31  const char *lhs = "ab";32  const char *rhs = "az";33  EXPECT_LT(LIBC_NAMESPACE::memcmp(lhs, rhs, 2), 0);34}35 36TEST(LlvmLibcMemcmpTest, LhsAfterRhsLexically) {37  const char *lhs = "az";38  const char *rhs = "ab";39  EXPECT_GT(LIBC_NAMESPACE::memcmp(lhs, rhs, 2), 0);40}41 42TEST(LlvmLibcMemcmpTest, Issue77080) {43  // https://github.com/llvm/llvm-project/issues/7708044  constexpr char lhs[35] = "1.069cd68bbe76eb2143a3284d27ebe220";45  constexpr char rhs[35] = "1.0500185b5d966a544e2d0fa40701b0f3";46  ASSERT_GE(LIBC_NAMESPACE::memcmp(lhs, rhs, 34), 1);47}48 49// Adapt CheckMemcmp signature to memcmp.50static inline int Adaptor(cpp::span<char> p1, cpp::span<char> p2, size_t size) {51  return LIBC_NAMESPACE::memcmp(p1.begin(), p2.begin(), size);52}53 54TEST(LlvmLibcMemcmpTest, SizeSweep) {55  static constexpr size_t kMaxSize = 400;56  Buffer Buffer1(kMaxSize);57  Buffer Buffer2(kMaxSize);58  Randomize(Buffer1.span());59  for (size_t size = 0; size < kMaxSize; ++size) {60    auto span1 = Buffer1.span().subspan(0, size);61    auto span2 = Buffer2.span().subspan(0, size);62    const bool OK = CheckMemcmp<Adaptor>(span1, span2, size);63    if (!OK)64      testing::tlog << "Failed at size=" << size << '\n';65    ASSERT_TRUE(OK);66  }67}68 69#if defined(LIBC_ADD_NULL_CHECKS)70 71TEST(LlvmLibcMemcmpTest, CrashOnNullPtr) {72  ASSERT_DEATH([]() { LIBC_NAMESPACE::memcmp(nullptr, nullptr, 1); },73               WITH_SIGNAL(-1));74}75 76#endif // defined(LIBC_ADD_NULL_CHECKS)77 78} // namespace LIBC_NAMESPACE_DECL79