string_util_gtest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //===---------------------------------------------------------------------===//
  2. // string_util_test - Unit tests for src/string_util.cc
  3. //===---------------------------------------------------------------------===//
  4. #include <tuple>
  5. #include "../src/internal_macros.h"
  6. #include "../src/string_util.h"
  7. #include "gmock/gmock.h"
  8. #include "gtest/gtest.h"
  9. namespace {
  10. TEST(StringUtilTest, stoul) {
  11. {
  12. size_t pos = 0;
  13. EXPECT_EQ(0ul, benchmark::stoul("0", &pos));
  14. EXPECT_EQ(1ul, pos);
  15. }
  16. {
  17. size_t pos = 0;
  18. EXPECT_EQ(7ul, benchmark::stoul("7", &pos));
  19. EXPECT_EQ(1ul, pos);
  20. }
  21. {
  22. size_t pos = 0;
  23. EXPECT_EQ(135ul, benchmark::stoul("135", &pos));
  24. EXPECT_EQ(3ul, pos);
  25. }
  26. #if ULONG_MAX == 0xFFFFFFFFul
  27. {
  28. size_t pos = 0;
  29. EXPECT_EQ(0xFFFFFFFFul, benchmark::stoul("4294967295", &pos));
  30. EXPECT_EQ(10ul, pos);
  31. }
  32. #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
  33. {
  34. size_t pos = 0;
  35. EXPECT_EQ(0xFFFFFFFFFFFFFFFFul,
  36. benchmark::stoul("18446744073709551615", &pos));
  37. EXPECT_EQ(20ul, pos);
  38. }
  39. #endif
  40. {
  41. size_t pos = 0;
  42. EXPECT_EQ(10ul, benchmark::stoul("1010", &pos, 2));
  43. EXPECT_EQ(4ul, pos);
  44. }
  45. {
  46. size_t pos = 0;
  47. EXPECT_EQ(520ul, benchmark::stoul("1010", &pos, 8));
  48. EXPECT_EQ(4ul, pos);
  49. }
  50. {
  51. size_t pos = 0;
  52. EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos, 10));
  53. EXPECT_EQ(4ul, pos);
  54. }
  55. {
  56. size_t pos = 0;
  57. EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos, 16));
  58. EXPECT_EQ(4ul, pos);
  59. }
  60. {
  61. size_t pos = 0;
  62. EXPECT_EQ(0xBEEFul, benchmark::stoul("BEEF", &pos, 16));
  63. EXPECT_EQ(4ul, pos);
  64. }
  65. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  66. {
  67. ASSERT_THROW(std::ignore = benchmark::stoul("this is a test"),
  68. std::invalid_argument);
  69. }
  70. #endif
  71. }
  72. TEST(StringUtilTest, stoi){{size_t pos = 0;
  73. EXPECT_EQ(0, benchmark::stoi("0", &pos));
  74. EXPECT_EQ(1ul, pos);
  75. } // namespace
  76. {
  77. size_t pos = 0;
  78. EXPECT_EQ(-17, benchmark::stoi("-17", &pos));
  79. EXPECT_EQ(3ul, pos);
  80. }
  81. {
  82. size_t pos = 0;
  83. EXPECT_EQ(1357, benchmark::stoi("1357", &pos));
  84. EXPECT_EQ(4ul, pos);
  85. }
  86. {
  87. size_t pos = 0;
  88. EXPECT_EQ(10, benchmark::stoi("1010", &pos, 2));
  89. EXPECT_EQ(4ul, pos);
  90. }
  91. {
  92. size_t pos = 0;
  93. EXPECT_EQ(520, benchmark::stoi("1010", &pos, 8));
  94. EXPECT_EQ(4ul, pos);
  95. }
  96. {
  97. size_t pos = 0;
  98. EXPECT_EQ(1010, benchmark::stoi("1010", &pos, 10));
  99. EXPECT_EQ(4ul, pos);
  100. }
  101. {
  102. size_t pos = 0;
  103. EXPECT_EQ(4112, benchmark::stoi("1010", &pos, 16));
  104. EXPECT_EQ(4ul, pos);
  105. }
  106. {
  107. size_t pos = 0;
  108. EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos, 16));
  109. EXPECT_EQ(4ul, pos);
  110. }
  111. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  112. {
  113. ASSERT_THROW(std::ignore = benchmark::stoi("this is a test"),
  114. std::invalid_argument);
  115. }
  116. #endif
  117. }
  118. TEST(StringUtilTest, stod){{size_t pos = 0;
  119. EXPECT_EQ(0.0, benchmark::stod("0", &pos));
  120. EXPECT_EQ(1ul, pos);
  121. }
  122. {
  123. size_t pos = 0;
  124. EXPECT_EQ(-84.0, benchmark::stod("-84", &pos));
  125. EXPECT_EQ(3ul, pos);
  126. }
  127. {
  128. size_t pos = 0;
  129. EXPECT_EQ(1234.0, benchmark::stod("1234", &pos));
  130. EXPECT_EQ(4ul, pos);
  131. }
  132. {
  133. size_t pos = 0;
  134. EXPECT_EQ(1.5, benchmark::stod("1.5", &pos));
  135. EXPECT_EQ(3ul, pos);
  136. }
  137. {
  138. size_t pos = 0;
  139. /* Note: exactly representable as double */
  140. EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos));
  141. EXPECT_EQ(8ul, pos);
  142. }
  143. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  144. {
  145. ASSERT_THROW(std::ignore = benchmark::stod("this is a test"),
  146. std::invalid_argument);
  147. }
  148. #endif
  149. }
  150. TEST(StringUtilTest, StrSplit) {
  151. EXPECT_EQ(benchmark::StrSplit("", ','), std::vector<std::string>{});
  152. EXPECT_EQ(benchmark::StrSplit("hello", ','),
  153. std::vector<std::string>({"hello"}));
  154. EXPECT_EQ(benchmark::StrSplit("hello,there,is,more", ','),
  155. std::vector<std::string>({"hello", "there", "is", "more"}));
  156. }
  157. using HumanReadableFixture = ::testing::TestWithParam<
  158. std::tuple<double, benchmark::Counter::OneK, std::string>>;
  159. INSTANTIATE_TEST_SUITE_P(
  160. HumanReadableTests, HumanReadableFixture,
  161. ::testing::Values(
  162. std::make_tuple(0.0, benchmark::Counter::kIs1024, "0"),
  163. std::make_tuple(999.0, benchmark::Counter::kIs1024, "999"),
  164. std::make_tuple(1000.0, benchmark::Counter::kIs1024, "1000"),
  165. std::make_tuple(1024.0, benchmark::Counter::kIs1024, "1Ki"),
  166. std::make_tuple(1000 * 1000.0, benchmark::Counter::kIs1024,
  167. "976\\.56.Ki"),
  168. std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1024, "1Mi"),
  169. std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1024,
  170. "953\\.674Mi"),
  171. std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1024,
  172. "1Gi"),
  173. std::make_tuple(0.0, benchmark::Counter::kIs1000, "0"),
  174. std::make_tuple(999.0, benchmark::Counter::kIs1000, "999"),
  175. std::make_tuple(1000.0, benchmark::Counter::kIs1000, "1k"),
  176. std::make_tuple(1024.0, benchmark::Counter::kIs1000, "1.024k"),
  177. std::make_tuple(1000 * 1000.0, benchmark::Counter::kIs1000, "1M"),
  178. std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1000,
  179. "1\\.04858M"),
  180. std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1000,
  181. "1G"),
  182. std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1000,
  183. "1\\.07374G")));
  184. TEST_P(HumanReadableFixture, HumanReadableNumber) {
  185. std::string str = benchmark::HumanReadableNumber(std::get<0>(GetParam()),
  186. std::get<1>(GetParam()));
  187. ASSERT_THAT(str, ::testing::MatchesRegex(std::get<2>(GetParam())));
  188. }
  189. } // end namespace