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 | #include <optional> |
14 | |
15 | using testing::AllOf; |
16 | using testing::AnyOf; |
17 | using testing::ElementsAre; |
18 | using testing::Ge; |
19 | using testing::Gt; |
20 | using testing::Le; |
21 | using testing::Lt; |
22 | |
23 | namespace llvm { |
24 | namespace libc_benchmarks { |
25 | namespace { |
26 | |
27 | TEST(AlignedBuffer, IsAligned) { |
28 | AlignedBuffer AB(0); |
29 | EXPECT_TRUE(isAddrAligned(Align(AlignedBuffer::Alignment), AB.begin())); |
30 | } |
31 | |
32 | TEST(AlignedBuffer, Empty) { |
33 | AlignedBuffer AB(0); |
34 | EXPECT_EQ(std::distance(first: AB.begin(), last: AB.end()), 0U); |
35 | } |
36 | |
37 | TEST(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 | |
45 | TEST(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 | |
53 | MATCHER_P(IsDivisibleBy, n, "" ) { |
54 | *result_listener << "where the remainder is " << (arg % n); |
55 | return (arg % n) == 0; |
56 | } |
57 | |
58 | TEST(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 | |
66 | TEST(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 | |
74 | TEST(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 | |
82 | TEST(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 bytes |
90 | // 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 | else |
99 | EXPECT_THAT(MOD(Gen, Size), |
100 | AnyOf(5 - Size - 1, 9 - Size - 1, 13 - Size - 1)); |
101 | } |
102 | } |
103 | } |
104 | |
105 | } // namespace |
106 | } // namespace libc_benchmarks |
107 | } // namespace llvm |
108 | |