1 //===-- Benchmark Memory Test ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "LibcMemoryBenchmark.h"
10 #include "llvm/Support/Alignment.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
16 using testing::ElementsAre
;
23 namespace libc_benchmarks
{
26 TEST(AlignedBuffer
, IsAligned
) {
28 EXPECT_TRUE(isAddrAligned(Align(AlignedBuffer::Alignment
), AB
.begin()));
31 TEST(AlignedBuffer
, Empty
) {
33 EXPECT_EQ(std::distance(AB
.begin(), AB
.end()), 0U);
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);
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)));
52 MATCHER_P(IsDivisibleBy
, n
, "") {
53 *result_listener
<< "where the remainder is " << (arg
% n
);
54 return (arg
% n
) == 0;
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)));
65 TEST(MismatchOffsetDistribution
, EqualBufferDisablesDistribution
) {
66 const size_t BufferSize
= 8192;
67 const uint32_t MismatchAt
= 0; // buffer are equal.
69 MismatchOffsetDistribution
MOD(BufferSize
, 1024, MismatchAt
);
73 TEST(MismatchOffsetDistribution
, DifferentBufferDisablesDistribution
) {
74 const size_t BufferSize
= 8192;
75 const uint32_t MismatchAt
= 1; // buffer are different.
77 MismatchOffsetDistribution
MOD(BufferSize
, 1024, MismatchAt
);
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;
86 MismatchOffsetDistribution
MOD(BufferSize
, MaxSize
, MismatchAt
);
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
));
98 EXPECT_THAT(MOD(Gen
, Size
),
99 AnyOf(5 - Size
- 1, 9 - Size
- 1, 13 - Size
- 1));
105 } // namespace libc_benchmarks