1 #include <vector>
2 
3 #include "../src/benchmark_register.h"
4 #include "gmock/gmock.h"
5 #include "gtest/gtest.h"
6 
7 namespace benchmark {
8 namespace internal {
9 namespace {
10 
TEST(AddRangeTest,Simple)11 TEST(AddRangeTest, Simple) {
12   std::vector<int> dst;
13   AddRange(&dst, 1, 2, 2);
14   EXPECT_THAT(dst, testing::ElementsAre(1, 2));
15 }
16 
TEST(AddRangeTest,Simple64)17 TEST(AddRangeTest, Simple64) {
18   std::vector<int64_t> dst;
19   AddRange(&dst, static_cast<int64_t>(1), static_cast<int64_t>(2), 2);
20   EXPECT_THAT(dst, testing::ElementsAre(1, 2));
21 }
22 
TEST(AddRangeTest,Advanced)23 TEST(AddRangeTest, Advanced) {
24   std::vector<int> dst;
25   AddRange(&dst, 5, 15, 2);
26   EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15));
27 }
28 
TEST(AddRangeTest,Advanced64)29 TEST(AddRangeTest, Advanced64) {
30   std::vector<int64_t> dst;
31   AddRange(&dst, static_cast<int64_t>(5), static_cast<int64_t>(15), 2);
32   EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15));
33 }
34 
TEST(AddRangeTest,FullRange8)35 TEST(AddRangeTest, FullRange8) {
36   std::vector<int8_t> dst;
37   AddRange(&dst, int8_t{1}, std::numeric_limits<int8_t>::max(), 8);
38   EXPECT_THAT(dst, testing::ElementsAre(1, 8, 64, 127));
39 }
40 
TEST(AddRangeTest,FullRange64)41 TEST(AddRangeTest, FullRange64) {
42   std::vector<int64_t> dst;
43   AddRange(&dst, int64_t{1}, std::numeric_limits<int64_t>::max(), 1024);
44   EXPECT_THAT(
45       dst, testing::ElementsAre(1LL, 1024LL, 1048576LL, 1073741824LL,
46                                 1099511627776LL, 1125899906842624LL,
47                                 1152921504606846976LL, 9223372036854775807LL));
48 }
49 
TEST(AddRangeTest,NegativeRanges)50 TEST(AddRangeTest, NegativeRanges) {
51   std::vector<int> dst;
52   AddRange(&dst, -8, 0, 2);
53   EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1, 0));
54 }
55 
TEST(AddRangeTest,StrictlyNegative)56 TEST(AddRangeTest, StrictlyNegative) {
57   std::vector<int> dst;
58   AddRange(&dst, -8, -1, 2);
59   EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1));
60 }
61 
TEST(AddRangeTest,SymmetricNegativeRanges)62 TEST(AddRangeTest, SymmetricNegativeRanges) {
63   std::vector<int> dst;
64   AddRange(&dst, -8, 8, 2);
65   EXPECT_THAT(dst, testing::ElementsAre(-8, -4, -2, -1, 0, 1, 2, 4, 8));
66 }
67 
TEST(AddRangeTest,SymmetricNegativeRangesOddMult)68 TEST(AddRangeTest, SymmetricNegativeRangesOddMult) {
69   std::vector<int> dst;
70   AddRange(&dst, -30, 32, 5);
71   EXPECT_THAT(dst, testing::ElementsAre(-30, -25, -5, -1, 0, 1, 5, 25, 32));
72 }
73 
TEST(AddRangeTest,NegativeRangesAsymmetric)74 TEST(AddRangeTest, NegativeRangesAsymmetric) {
75   std::vector<int> dst;
76   AddRange(&dst, -3, 5, 2);
77   EXPECT_THAT(dst, testing::ElementsAre(-3, -2, -1, 0, 1, 2, 4, 5));
78 }
79 
TEST(AddRangeTest,NegativeRangesLargeStep)80 TEST(AddRangeTest, NegativeRangesLargeStep) {
81   // Always include -1, 0, 1 when crossing zero.
82   std::vector<int> dst;
83   AddRange(&dst, -8, 8, 10);
84   EXPECT_THAT(dst, testing::ElementsAre(-8, -1, 0, 1, 8));
85 }
86 
TEST(AddRangeTest,ZeroOnlyRange)87 TEST(AddRangeTest, ZeroOnlyRange) {
88   std::vector<int> dst;
89   AddRange(&dst, 0, 0, 2);
90   EXPECT_THAT(dst, testing::ElementsAre(0));
91 }
92 
TEST(AddRangeTest,ZeroStartingRange)93 TEST(AddRangeTest, ZeroStartingRange) {
94   std::vector<int> dst;
95   AddRange(&dst, 0, 2, 2);
96   EXPECT_THAT(dst, testing::ElementsAre(0, 1, 2));
97 }
98 
TEST(AddRangeTest,NegativeRange64)99 TEST(AddRangeTest, NegativeRange64) {
100   std::vector<int64_t> dst;
101   AddRange<int64_t>(&dst, -4, 4, 2);
102   EXPECT_THAT(dst, testing::ElementsAre(-4, -2, -1, 0, 1, 2, 4));
103 }
104 
TEST(AddRangeTest,NegativeRangePreservesExistingOrder)105 TEST(AddRangeTest, NegativeRangePreservesExistingOrder) {
106   // If elements already exist in the range, ensure we don't change
107   // their ordering by adding negative values.
108   std::vector<int64_t> dst = {1, 2, 3};
109   AddRange<int64_t>(&dst, -2, 2, 2);
110   EXPECT_THAT(dst, testing::ElementsAre(1, 2, 3, -2, -1, 0, 1, 2));
111 }
112 
TEST(AddRangeTest,FullNegativeRange64)113 TEST(AddRangeTest, FullNegativeRange64) {
114   std::vector<int64_t> dst;
115   const auto min = std::numeric_limits<int64_t>::min();
116   const auto max = std::numeric_limits<int64_t>::max();
117   AddRange(&dst, min, max, 1024);
118   EXPECT_THAT(
119       dst, testing::ElementsAreArray(std::vector<int64_t>{
120                min, -1152921504606846976LL, -1125899906842624LL,
121                -1099511627776LL, -1073741824LL, -1048576LL, -1024LL, -1LL, 0LL,
122                1LL, 1024LL, 1048576LL, 1073741824LL, 1099511627776LL,
123                1125899906842624LL, 1152921504606846976LL, max}));
124 }
125 
TEST(AddRangeTest,Simple8)126 TEST(AddRangeTest, Simple8) {
127   std::vector<int8_t> dst;
128   AddRange<int8_t>(&dst, 1, 8, 2);
129   EXPECT_THAT(dst, testing::ElementsAre(1, 2, 4, 8));
130 }
131 
132 }  // namespace
133 }  // namespace internal
134 }  // namespace benchmark
135