You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.7 KiB

2 months ago
  1. //===---------------------------------------------------------------------===//
  2. // statistics_test - Unit tests for src/statistics.cc
  3. //===---------------------------------------------------------------------===//
  4. #include <tuple>
  5. #include "../src/internal_macros.h"
  6. #include "../src/string_util.h"
  7. #include "gtest/gtest.h"
  8. namespace {
  9. TEST(StringUtilTest, stoul) {
  10. {
  11. size_t pos = 0;
  12. EXPECT_EQ(0ul, benchmark::stoul("0", &pos));
  13. EXPECT_EQ(1ul, pos);
  14. }
  15. {
  16. size_t pos = 0;
  17. EXPECT_EQ(7ul, benchmark::stoul("7", &pos));
  18. EXPECT_EQ(1ul, pos);
  19. }
  20. {
  21. size_t pos = 0;
  22. EXPECT_EQ(135ul, benchmark::stoul("135", &pos));
  23. EXPECT_EQ(3ul, pos);
  24. }
  25. #if ULONG_MAX == 0xFFFFFFFFul
  26. {
  27. size_t pos = 0;
  28. EXPECT_EQ(0xFFFFFFFFul, benchmark::stoul("4294967295", &pos));
  29. EXPECT_EQ(10ul, pos);
  30. }
  31. #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
  32. {
  33. size_t pos = 0;
  34. EXPECT_EQ(0xFFFFFFFFFFFFFFFFul,
  35. benchmark::stoul("18446744073709551615", &pos));
  36. EXPECT_EQ(20ul, pos);
  37. }
  38. #endif
  39. {
  40. size_t pos = 0;
  41. EXPECT_EQ(10ul, benchmark::stoul("1010", &pos, 2));
  42. EXPECT_EQ(4ul, pos);
  43. }
  44. {
  45. size_t pos = 0;
  46. EXPECT_EQ(520ul, benchmark::stoul("1010", &pos, 8));
  47. EXPECT_EQ(4ul, pos);
  48. }
  49. {
  50. size_t pos = 0;
  51. EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos, 10));
  52. EXPECT_EQ(4ul, pos);
  53. }
  54. {
  55. size_t pos = 0;
  56. EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos, 16));
  57. EXPECT_EQ(4ul, pos);
  58. }
  59. {
  60. size_t pos = 0;
  61. EXPECT_EQ(0xBEEFul, benchmark::stoul("BEEF", &pos, 16));
  62. EXPECT_EQ(4ul, pos);
  63. }
  64. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  65. {
  66. ASSERT_THROW(std::ignore = benchmark::stoul("this is a test"),
  67. std::invalid_argument);
  68. }
  69. #endif
  70. }
  71. TEST(StringUtilTest, stoi){{size_t pos = 0;
  72. EXPECT_EQ(0, benchmark::stoi("0", &pos));
  73. EXPECT_EQ(1ul, pos);
  74. } // namespace
  75. {
  76. size_t pos = 0;
  77. EXPECT_EQ(-17, benchmark::stoi("-17", &pos));
  78. EXPECT_EQ(3ul, pos);
  79. }
  80. {
  81. size_t pos = 0;
  82. EXPECT_EQ(1357, benchmark::stoi("1357", &pos));
  83. EXPECT_EQ(4ul, pos);
  84. }
  85. {
  86. size_t pos = 0;
  87. EXPECT_EQ(10, benchmark::stoi("1010", &pos, 2));
  88. EXPECT_EQ(4ul, pos);
  89. }
  90. {
  91. size_t pos = 0;
  92. EXPECT_EQ(520, benchmark::stoi("1010", &pos, 8));
  93. EXPECT_EQ(4ul, pos);
  94. }
  95. {
  96. size_t pos = 0;
  97. EXPECT_EQ(1010, benchmark::stoi("1010", &pos, 10));
  98. EXPECT_EQ(4ul, pos);
  99. }
  100. {
  101. size_t pos = 0;
  102. EXPECT_EQ(4112, benchmark::stoi("1010", &pos, 16));
  103. EXPECT_EQ(4ul, pos);
  104. }
  105. {
  106. size_t pos = 0;
  107. EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos, 16));
  108. EXPECT_EQ(4ul, pos);
  109. }
  110. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  111. {
  112. ASSERT_THROW(std::ignore = benchmark::stoi("this is a test"),
  113. std::invalid_argument);
  114. }
  115. #endif
  116. }
  117. TEST(StringUtilTest, stod){{size_t pos = 0;
  118. EXPECT_EQ(0.0, benchmark::stod("0", &pos));
  119. EXPECT_EQ(1ul, pos);
  120. }
  121. {
  122. size_t pos = 0;
  123. EXPECT_EQ(-84.0, benchmark::stod("-84", &pos));
  124. EXPECT_EQ(3ul, pos);
  125. }
  126. {
  127. size_t pos = 0;
  128. EXPECT_EQ(1234.0, benchmark::stod("1234", &pos));
  129. EXPECT_EQ(4ul, pos);
  130. }
  131. {
  132. size_t pos = 0;
  133. EXPECT_EQ(1.5, benchmark::stod("1.5", &pos));
  134. EXPECT_EQ(3ul, pos);
  135. }
  136. {
  137. size_t pos = 0;
  138. /* Note: exactly representable as double */
  139. EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos));
  140. EXPECT_EQ(8ul, pos);
  141. }
  142. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  143. {
  144. ASSERT_THROW(std::ignore = benchmark::stod("this is a test"),
  145. std::invalid_argument);
  146. }
  147. #endif
  148. }
  149. TEST(StringUtilTest, StrSplit) {
  150. EXPECT_EQ(benchmark::StrSplit("", ','), std::vector<std::string>{});
  151. EXPECT_EQ(benchmark::StrSplit("hello", ','),
  152. std::vector<std::string>({"hello"}));
  153. EXPECT_EQ(benchmark::StrSplit("hello,there,is,more", ','),
  154. std::vector<std::string>({"hello", "there", "is", "more"}));
  155. }
  156. } // end namespace