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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "LibcMemoryBenchmark.h" 10 #include "llvm/Support/Alignment.h" 11 #include "gmock/gmock.h" 12 #include "gtest/gtest.h" 13 14 using testing::AllOf; 15 using testing::AnyOf; 16 using testing::ElementsAre; 17 using testing::Ge; 18 using testing::Gt; 19 using testing::Le; 20 using testing::Lt; 21 22 namespace llvm { 23 namespace libc_benchmarks { 24 namespace { 25 26 TEST(AlignedBuffer, IsAligned) { 27 AlignedBuffer AB(0); 28 EXPECT_TRUE(isAddrAligned(Align(AlignedBuffer::Alignment), AB.begin())); 29 } 30 31 TEST(AlignedBuffer, Empty) { 32 AlignedBuffer AB(0); 33 EXPECT_EQ(std::distance(AB.begin(), AB.end()), 0U); 34 } 35 36 TEST(OffsetDistribution, AlignToBegin) { 37 StudyConfiguration Conf; 38 Conf.BufferSize = 8192; 39 Conf.AddressAlignment = None; 40 41 OffsetDistribution OD(Conf); 42 std::default_random_engine Gen; 43 for (size_t I = 0; I <= 10; ++I) 44 EXPECT_EQ(OD(Gen), 0U); 45 } 46 47 TEST(OffsetDistribution, NoAlignment) { 48 StudyConfiguration Conf; 49 Conf.BufferSize = 8192; 50 Conf.Size.To = 1; 51 52 OffsetDistribution OD(Conf); 53 std::default_random_engine Gen; 54 for (size_t I = 0; I <= 10; ++I) 55 EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U))); 56 } 57 58 MATCHER_P(IsDivisibleBy, n, "") { 59 *result_listener << "where the remainder is " << (arg % n); 60 return (arg % n) == 0; 61 } 62 63 TEST(OffsetDistribution, Aligned) { 64 StudyConfiguration Conf; 65 Conf.BufferSize = 8192; 66 Conf.AddressAlignment = Align(16); 67 Conf.Size.To = 1; 68 69 OffsetDistribution OD(Conf); 70 std::default_random_engine Gen; 71 for (size_t I = 0; I <= 10; ++I) 72 EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U), IsDivisibleBy(16U))); 73 } 74 75 TEST(MismatchOffsetDistribution, EqualBufferDisablesDistribution) { 76 StudyConfiguration Conf; 77 Conf.MemcmpMismatchAt = 0; // buffer are equal. 78 79 MismatchOffsetDistribution MOD(Conf); 80 EXPECT_FALSE(MOD); 81 } 82 83 TEST(MismatchOffsetDistribution, DifferentBufferDisablesDistribution) { 84 StudyConfiguration Conf; 85 Conf.MemcmpMismatchAt = 1; // buffer are different. 86 87 MismatchOffsetDistribution MOD(Conf); 88 EXPECT_FALSE(MOD); 89 } 90 91 TEST(MismatchOffsetDistribution, MismatchAt2) { 92 const uint32_t MismatchAt = 2; 93 const uint32_t ToSize = 4; 94 StudyConfiguration Conf; 95 Conf.BufferSize = 16; 96 Conf.MemcmpMismatchAt = MismatchAt; // buffer are different at position 2. 97 Conf.Size.To = ToSize; 98 99 MismatchOffsetDistribution MOD(Conf); 100 EXPECT_TRUE(MOD); 101 // We test equality up to ToSize (=4) so we need spans of 4 equal bytes spaced 102 // by one mismatch. 103 EXPECT_THAT(MOD.getMismatchIndices(), ElementsAre(5, 9, 13)); 104 std::default_random_engine Gen; 105 for (size_t Iterations = 0; Iterations <= 10; ++Iterations) { 106 for (size_t Size = Conf.Size.From; Size <= ToSize; ++Size) { 107 if (Size >= MismatchAt) 108 EXPECT_THAT(MOD(Gen, Size), 109 AnyOf(5 - MismatchAt, 9 - MismatchAt, 13 - MismatchAt)); 110 else 111 EXPECT_THAT(MOD(Gen, Size), 112 AnyOf(5 - Size - 1, 9 - Size - 1, 13 - Size - 1)); 113 } 114 } 115 } 116 117 } // namespace 118 } // namespace libc_benchmarks 119 } // namespace llvm 120