xref: /llvm-project/libc/benchmarks/LibcMemoryBenchmarkTest.cpp (revision deae7e982a3b08996455e2cdfdc5062bf37895a3)
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   const size_t BufferSize = 8192;
38   OffsetDistribution OD(BufferSize, 1024, None);
39   std::default_random_engine Gen;
40   for (size_t I = 0; I <= 10; ++I)
41     EXPECT_EQ(OD(Gen), 0U);
42 }
43 
44 TEST(OffsetDistribution, NoAlignment) {
45   const size_t BufferSize = 8192;
46   OffsetDistribution OD(BufferSize, 1, Align(1));
47   std::default_random_engine Gen;
48   for (size_t I = 0; I <= 10; ++I)
49     EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U)));
50 }
51 
52 MATCHER_P(IsDivisibleBy, n, "") {
53   *result_listener << "where the remainder is " << (arg % n);
54   return (arg % n) == 0;
55 }
56 
57 TEST(OffsetDistribution, Aligned) {
58   const size_t BufferSize = 8192;
59   OffsetDistribution OD(BufferSize, 1, Align(16));
60   std::default_random_engine Gen;
61   for (size_t I = 0; I <= 10; ++I)
62     EXPECT_THAT(OD(Gen), AllOf(Ge(0U), Lt(8192U), IsDivisibleBy(16U)));
63 }
64 
65 TEST(MismatchOffsetDistribution, EqualBufferDisablesDistribution) {
66   const size_t BufferSize = 8192;
67   const uint32_t MismatchAt = 0; // buffer are equal.
68 
69   MismatchOffsetDistribution MOD(BufferSize, 1024, MismatchAt);
70   EXPECT_FALSE(MOD);
71 }
72 
73 TEST(MismatchOffsetDistribution, DifferentBufferDisablesDistribution) {
74   const size_t BufferSize = 8192;
75   const uint32_t MismatchAt = 1; // buffer are different.
76 
77   MismatchOffsetDistribution MOD(BufferSize, 1024, MismatchAt);
78   EXPECT_FALSE(MOD);
79 }
80 
81 TEST(MismatchOffsetDistribution, MismatchAt2) {
82   const size_t BufferSize = 16;
83   const uint32_t MismatchAt = 2; // buffer are different at position 2.
84   const uint32_t MaxSize = 4;
85 
86   MismatchOffsetDistribution MOD(BufferSize, MaxSize, MismatchAt);
87   EXPECT_TRUE(MOD);
88   // We test equality up to MaxSize (=4) so we need spans of 4 equal bytes
89   // spaced by one mismatch.
90   EXPECT_THAT(MOD.getMismatchIndices(), ElementsAre(5, 9, 13));
91   std::default_random_engine Gen;
92   for (size_t Iterations = 0; Iterations <= 10; ++Iterations) {
93     for (size_t Size = 0; Size <= MaxSize; ++Size) {
94       if (Size >= MismatchAt)
95         EXPECT_THAT(MOD(Gen, Size),
96                     AnyOf(5 - MismatchAt, 9 - MismatchAt, 13 - MismatchAt));
97       else
98         EXPECT_THAT(MOD(Gen, Size),
99                     AnyOf(5 - Size - 1, 9 - Size - 1, 13 - Size - 1));
100     }
101   }
102 }
103 
104 } // namespace
105 } // namespace libc_benchmarks
106 } // namespace llvm
107