Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / test / benchmark_gtest.cc
blob14a885ba46da4da3a81d5561e6df5fe8532d8cab
1 #include <map>
2 #include <string>
3 #include <vector>
5 #include "../src/benchmark_register.h"
6 #include "gmock/gmock.h"
7 #include "gtest/gtest.h"
9 namespace benchmark {
10 namespace internal {
11 extern std::map<std::string, std::string>* global_context;
13 namespace {
15 TEST(AddRangeTest, Simple) {
16 std::vector<int> dst;
17 AddRange(&dst, 1, 2, 2);
18 EXPECT_THAT(dst, testing::ElementsAre(1, 2));
21 TEST(AddRangeTest, Simple64) {
22 std::vector<int64_t> dst;
23 AddRange(&dst, static_cast<int64_t>(1), static_cast<int64_t>(2), 2);
24 EXPECT_THAT(dst, testing::ElementsAre(1, 2));
27 TEST(AddRangeTest, Advanced) {
28 std::vector<int> dst;
29 AddRange(&dst, 5, 15, 2);
30 EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15));
33 TEST(AddRangeTest, Advanced64) {
34 std::vector<int64_t> dst;
35 AddRange(&dst, static_cast<int64_t>(5), static_cast<int64_t>(15), 2);
36 EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15));
39 TEST(AddRangeTest, FullRange8) {
40 std::vector<int8_t> dst;
41 AddRange(&dst, int8_t{1}, std::numeric_limits<int8_t>::max(), 8);
42 EXPECT_THAT(dst, testing::ElementsAre(1, 8, 64, 127));
45 TEST(AddRangeTest, FullRange64) {
46 std::vector<int64_t> dst;
47 AddRange(&dst, int64_t{1}, std::numeric_limits<int64_t>::max(), 1024);
48 EXPECT_THAT(
49 dst, testing::ElementsAre(1LL, 1024LL, 1048576LL, 1073741824LL,
50 1099511627776LL, 1125899906842624LL,
51 1152921504606846976LL, 9223372036854775807LL));
54 TEST(AddRangeTest, NegativeRanges) {
55 std::vector<int> dst;
56 AddRange(&dst, -8, 0, 2);
57 EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1, 0));
60 TEST(AddRangeTest, StrictlyNegative) {
61 std::vector<int> dst;
62 AddRange(&dst, -8, -1, 2);
63 EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1));
66 TEST(AddRangeTest, SymmetricNegativeRanges) {
67 std::vector<int> dst;
68 AddRange(&dst, -8, 8, 2);
69 EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1, 0, 1, 2, 4, 8));
72 TEST(AddRangeTest, SymmetricNegativeRangesOddMult) {
73 std::vector<int> dst;
74 AddRange(&dst, -30, 32, 5);
75 EXPECT_THAT(dst, testing::ElementsAre(-30, -25, -5, -1, 0, 1, 5, 25, 32));
78 TEST(AddRangeTest, NegativeRangesAsymmetric) {
79 std::vector<int> dst;
80 AddRange(&dst, -3, 5, 2);
81 EXPECT_THAT(dst, testing::ElementsAre(-3, -2, -1, 0, 1, 2, 4, 5));
84 TEST(AddRangeTest, NegativeRangesLargeStep) {
85 // Always include -1, 0, 1 when crossing zero.
86 std::vector<int> dst;
87 AddRange(&dst, -8, 8, 10);
88 EXPECT_THAT(dst, testing::ElementsAre(-8, -1, 0, 1, 8));
91 TEST(AddRangeTest, ZeroOnlyRange) {
92 std::vector<int> dst;
93 AddRange(&dst, 0, 0, 2);
94 EXPECT_THAT(dst, testing::ElementsAre(0));
97 TEST(AddRangeTest, ZeroStartingRange) {
98 std::vector<int> dst;
99 AddRange(&dst, 0, 2, 2);
100 EXPECT_THAT(dst, testing::ElementsAre(0, 1, 2));
103 TEST(AddRangeTest, NegativeRange64) {
104 std::vector<int64_t> dst;
105 AddRange<int64_t>(&dst, -4, 4, 2);
106 EXPECT_THAT(dst, testing::ElementsAre(-4, -2, -1, 0, 1, 2, 4));
109 TEST(AddRangeTest, NegativeRangePreservesExistingOrder) {
110 // If elements already exist in the range, ensure we don't change
111 // their ordering by adding negative values.
112 std::vector<int64_t> dst = {1, 2, 3};
113 AddRange<int64_t>(&dst, -2, 2, 2);
114 EXPECT_THAT(dst, testing::ElementsAre(1, 2, 3, -2, -1, 0, 1, 2));
117 TEST(AddRangeTest, FullNegativeRange64) {
118 std::vector<int64_t> dst;
119 const auto min = std::numeric_limits<int64_t>::min();
120 const auto max = std::numeric_limits<int64_t>::max();
121 AddRange(&dst, min, max, 1024);
122 EXPECT_THAT(
123 dst, testing::ElementsAreArray(std::vector<int64_t>{
124 min, -1152921504606846976LL, -1125899906842624LL,
125 -1099511627776LL, -1073741824LL, -1048576LL, -1024LL, -1LL, 0LL,
126 1LL, 1024LL, 1048576LL, 1073741824LL, 1099511627776LL,
127 1125899906842624LL, 1152921504606846976LL, max}));
130 TEST(AddRangeTest, Simple8) {
131 std::vector<int8_t> dst;
132 AddRange<int8_t>(&dst, 1, 8, 2);
133 EXPECT_THAT(dst, testing::ElementsAre(1, 2, 4, 8));
136 TEST(AddCustomContext, Simple) {
137 EXPECT_THAT(global_context, nullptr);
139 AddCustomContext("foo", "bar");
140 AddCustomContext("baz", "qux");
142 EXPECT_THAT(*global_context,
143 testing::UnorderedElementsAre(testing::Pair("foo", "bar"),
144 testing::Pair("baz", "qux")));
146 delete global_context;
147 global_context = nullptr;
150 TEST(AddCustomContext, DuplicateKey) {
151 EXPECT_THAT(global_context, nullptr);
153 AddCustomContext("foo", "bar");
154 AddCustomContext("foo", "qux");
156 EXPECT_THAT(*global_context,
157 testing::UnorderedElementsAre(testing::Pair("foo", "bar")));
159 delete global_context;
160 global_context = nullptr;
163 } // namespace
164 } // namespace internal
165 } // namespace benchmark