benchmark_gtest.cc 4.9 KB

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