brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 3c796c8 Raw
108 lines · cpp
1//===-- Benchmark Memory Test ---------------------------------------------===//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 "LibcMemoryBenchmark.h"10#include "llvm/Support/Alignment.h"11#include "gmock/gmock.h"12#include "gtest/gtest.h"13#include <optional>14 15using testing::AllOf;16using testing::AnyOf;17using testing::ElementsAre;18using testing::Ge;19using testing::Gt;20using testing::Le;21using testing::Lt;22 23namespace llvm {24namespace libc_benchmarks {25namespace {26 27TEST(AlignedBuffer, IsAligned) {28  AlignedBuffer AB(0);29  EXPECT_TRUE(isAddrAligned(Align(AlignedBuffer::Alignment), AB.begin()));30}31 32TEST(AlignedBuffer, Empty) {33  AlignedBuffer AB(0);34  EXPECT_EQ(std::distance(AB.begin(), AB.end()), 0U);35}36 37TEST(OffsetDistribution, AlignToBegin) {38  const size_t BufferSize = 8192;39  OffsetDistribution OD(BufferSize, 1024, std::nullopt);40  std::default_random_engine Gen;41  for (size_t i = 0; i <= 10; ++i)42    EXPECT_EQ(OD(Gen), 0U);43}44 45TEST(OffsetDistribution, NoAlignment) {46  const size_t BufferSize = 8192;47  OffsetDistribution OD(BufferSize, 1, Align(1));48  std::default_random_engine Gen;49  for (size_t i = 0; i <= 10; ++i)50    EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U)));51}52 53MATCHER_P(IsDivisibleBy, n, "") {54  *result_listener << "where the remainder is " << (arg % n);55  return (arg % n) == 0;56}57 58TEST(OffsetDistribution, Aligned) {59  const size_t BufferSize = 8192;60  OffsetDistribution OD(BufferSize, 1, Align(16));61  std::default_random_engine Gen;62  for (size_t i = 0; i <= 10; ++i)63    EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U), IsDivisibleBy(16U)));64}65 66TEST(MismatchOffsetDistribution, EqualBufferDisablesDistribution) {67  const size_t BufferSize = 8192;68  const uint32_t MismatchAt = 0; // buffer are equal.69 70  MismatchOffsetDistribution MOD(BufferSize, 1024, MismatchAt);71  EXPECT_FALSE(MOD);72}73 74TEST(MismatchOffsetDistribution, DifferentBufferDisablesDistribution) {75  const size_t BufferSize = 8192;76  const uint32_t MismatchAt = 1; // buffer are different.77 78  MismatchOffsetDistribution MOD(BufferSize, 1024, MismatchAt);79  EXPECT_FALSE(MOD);80}81 82TEST(MismatchOffsetDistribution, MismatchAt2) {83  const size_t BufferSize = 16;84  const uint32_t MismatchAt = 2; // buffer are different at position 2.85  const uint32_t MaxSize = 4;86 87  MismatchOffsetDistribution MOD(BufferSize, MaxSize, MismatchAt);88  EXPECT_TRUE(MOD);89  // We test equality up to MaxSize (=4) so we need spans of 4 equal bytes90  // spaced by one mismatch.91  EXPECT_THAT(MOD.getMismatchIndices(), ElementsAre(5, 9, 13));92  std::default_random_engine Gen;93  for (size_t Iterations = 0; Iterations <= 10; ++Iterations) {94    for (size_t Size = 0; Size <= MaxSize; ++Size) {95      if (Size >= MismatchAt)96        EXPECT_THAT(MOD(Gen, Size),97                    AnyOf(5 - MismatchAt, 9 - MismatchAt, 13 - MismatchAt));98      else99        EXPECT_THAT(MOD(Gen, Size),100                    AnyOf(5 - Size - 1, 9 - Size - 1, 13 - Size - 1));101    }102  }103}104 105} // namespace106} // namespace libc_benchmarks107} // namespace llvm108