5 #include "../src/benchmark_register.h"
6 #include "gmock/gmock.h"
7 #include "gtest/gtest.h"
11 extern std::map
<std::string
, std::string
>* global_context
;
15 TEST(AddRangeTest
, Simple
) {
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
) {
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);
49 dst
, testing::ElementsAre(1LL, 1024LL, 1048576LL, 1073741824LL,
50 1099511627776LL, 1125899906842624LL,
51 1152921504606846976LL, 9223372036854775807LL));
54 TEST(AddRangeTest
, NegativeRanges
) {
56 AddRange(&dst
, -8, 0, 2);
57 EXPECT_THAT(dst
, testing::ElementsAre(-8, -4, -2, -1, 0));
60 TEST(AddRangeTest
, StrictlyNegative
) {
62 AddRange(&dst
, -8, -1, 2);
63 EXPECT_THAT(dst
, testing::ElementsAre(-8, -4, -2, -1));
66 TEST(AddRangeTest
, SymmetricNegativeRanges
) {
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
) {
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
) {
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.
87 AddRange(&dst
, -8, 8, 10);
88 EXPECT_THAT(dst
, testing::ElementsAre(-8, -1, 0, 1, 8));
91 TEST(AddRangeTest
, ZeroOnlyRange
) {
93 AddRange(&dst
, 0, 0, 2);
94 EXPECT_THAT(dst
, testing::ElementsAre(0));
97 TEST(AddRangeTest
, ZeroStartingRange
) {
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);
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;
164 } // namespace internal
165 } // namespace benchmark