1 | #include <map> |
2 | #include <string> |
3 | #include <vector> |
4 | |
5 | #include "../src/benchmark_register.h" |
6 | #include "benchmark/benchmark.h" |
7 | #include "gmock/gmock.h" |
8 | #include "gtest/gtest.h" |
9 | |
10 | namespace benchmark { |
11 | namespace internal { |
12 | |
13 | namespace { |
14 | |
15 | TEST(AddRangeTest, Simple) { |
16 | std::vector<int> dst; |
17 | AddRange(&dst, 1, 2, 2); |
18 | EXPECT_THAT(dst, testing::ElementsAre(1, 2)); |
19 | } |
20 | |
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)); |
25 | } |
26 | |
27 | TEST(AddRangeTest, Advanced) { |
28 | std::vector<int> dst; |
29 | AddRange(&dst, 5, 15, 2); |
30 | EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15)); |
31 | } |
32 | |
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)); |
37 | } |
38 | |
39 | TEST(AddRangeTest, FullRange8) { |
40 | std::vector<int8_t> dst; |
41 | AddRange(&dst, int8_t{1}, std::numeric_limits<int8_t>::max(), int8_t{8}); |
42 | EXPECT_THAT( |
43 | dst, testing::ElementsAre(int8_t{1}, int8_t{8}, int8_t{64}, int8_t{127})); |
44 | } |
45 | |
46 | TEST(AddRangeTest, FullRange64) { |
47 | std::vector<int64_t> dst; |
48 | AddRange(&dst, int64_t{1}, std::numeric_limits<int64_t>::max(), 1024); |
49 | EXPECT_THAT( |
50 | dst, testing::ElementsAre(1LL, 1024LL, 1048576LL, 1073741824LL, |
51 | 1099511627776LL, 1125899906842624LL, |
52 | 1152921504606846976LL, 9223372036854775807LL)); |
53 | } |
54 | |
55 | TEST(AddRangeTest, NegativeRanges) { |
56 | std::vector<int> dst; |
57 | AddRange(&dst, -8, 0, 2); |
58 | EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1, 0)); |
59 | } |
60 | |
61 | TEST(AddRangeTest, StrictlyNegative) { |
62 | std::vector<int> dst; |
63 | AddRange(&dst, -8, -1, 2); |
64 | EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1)); |
65 | } |
66 | |
67 | TEST(AddRangeTest, SymmetricNegativeRanges) { |
68 | std::vector<int> dst; |
69 | AddRange(&dst, -8, 8, 2); |
70 | EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1, 0, 1, 2, 4, 8)); |
71 | } |
72 | |
73 | TEST(AddRangeTest, SymmetricNegativeRangesOddMult) { |
74 | std::vector<int> dst; |
75 | AddRange(&dst, -30, 32, 5); |
76 | EXPECT_THAT(dst, testing::ElementsAre(-30, -25, -5, -1, 0, 1, 5, 25, 32)); |
77 | } |
78 | |
79 | TEST(AddRangeTest, NegativeRangesAsymmetric) { |
80 | std::vector<int> dst; |
81 | AddRange(&dst, -3, 5, 2); |
82 | EXPECT_THAT(dst, testing::ElementsAre(-3, -2, -1, 0, 1, 2, 4, 5)); |
83 | } |
84 | |
85 | TEST(AddRangeTest, NegativeRangesLargeStep) { |
86 | // Always include -1, 0, 1 when crossing zero. |
87 | std::vector<int> dst; |
88 | AddRange(&dst, -8, 8, 10); |
89 | EXPECT_THAT(dst, testing::ElementsAre(-8, -1, 0, 1, 8)); |
90 | } |
91 | |
92 | TEST(AddRangeTest, ZeroOnlyRange) { |
93 | std::vector<int> dst; |
94 | AddRange(&dst, 0, 0, 2); |
95 | EXPECT_THAT(dst, testing::ElementsAre(0)); |
96 | } |
97 | |
98 | TEST(AddRangeTest, ZeroStartingRange) { |
99 | std::vector<int> dst; |
100 | AddRange(&dst, 0, 2, 2); |
101 | EXPECT_THAT(dst, testing::ElementsAre(0, 1, 2)); |
102 | } |
103 | |
104 | TEST(AddRangeTest, NegativeRange64) { |
105 | std::vector<int64_t> dst; |
106 | AddRange<int64_t>(&dst, -4, 4, 2); |
107 | EXPECT_THAT(dst, testing::ElementsAre(-4, -2, -1, 0, 1, 2, 4)); |
108 | } |
109 | |
110 | TEST(AddRangeTest, NegativeRangePreservesExistingOrder) { |
111 | // If elements already exist in the range, ensure we don't change |
112 | // their ordering by adding negative values. |
113 | std::vector<int64_t> dst = {1, 2, 3}; |
114 | AddRange<int64_t>(&dst, -2, 2, 2); |
115 | EXPECT_THAT(dst, testing::ElementsAre(1, 2, 3, -2, -1, 0, 1, 2)); |
116 | } |
117 | |
118 | TEST(AddRangeTest, FullNegativeRange64) { |
119 | std::vector<int64_t> dst; |
120 | const auto min = std::numeric_limits<int64_t>::min(); |
121 | const auto max = std::numeric_limits<int64_t>::max(); |
122 | AddRange(&dst, min, max, 1024); |
123 | EXPECT_THAT( |
124 | dst, testing::ElementsAreArray(std::vector<int64_t>{ |
125 | min, -1152921504606846976LL, -1125899906842624LL, |
126 | -1099511627776LL, -1073741824LL, -1048576LL, -1024LL, -1LL, 0LL, |
127 | 1LL, 1024LL, 1048576LL, 1073741824LL, 1099511627776LL, |
128 | 1125899906842624LL, 1152921504606846976LL, max})); |
129 | } |
130 | |
131 | TEST(AddRangeTest, Simple8) { |
132 | std::vector<int8_t> dst; |
133 | AddRange<int8_t>(&dst, int8_t{1}, int8_t{8}, int8_t{2}); |
134 | EXPECT_THAT(dst, |
135 | testing::ElementsAre(int8_t{1}, int8_t{2}, int8_t{4}, int8_t{8})); |
136 | } |
137 | |
138 | TEST(AddCustomContext, Simple) { |
139 | std::map<std::string, std::string> *&global_context = GetGlobalContext(); |
140 | EXPECT_THAT(global_context, nullptr); |
141 | |
142 | AddCustomContext("foo" , "bar" ); |
143 | AddCustomContext("baz" , "qux" ); |
144 | |
145 | EXPECT_THAT(*global_context, |
146 | testing::UnorderedElementsAre(testing::Pair("foo" , "bar" ), |
147 | testing::Pair("baz" , "qux" ))); |
148 | |
149 | delete global_context; |
150 | global_context = nullptr; |
151 | } |
152 | |
153 | TEST(AddCustomContext, DuplicateKey) { |
154 | std::map<std::string, std::string> *&global_context = GetGlobalContext(); |
155 | EXPECT_THAT(global_context, nullptr); |
156 | |
157 | AddCustomContext("foo" , "bar" ); |
158 | AddCustomContext("foo" , "qux" ); |
159 | |
160 | EXPECT_THAT(*global_context, |
161 | testing::UnorderedElementsAre(testing::Pair("foo" , "bar" ))); |
162 | |
163 | delete global_context; |
164 | global_context = nullptr; |
165 | } |
166 | |
167 | } // namespace |
168 | } // namespace internal |
169 | } // namespace benchmark |
170 | |