[Reland][Runtimes] Merge 'compile_commands.json' files from runtimes build (#116303)
[llvm-project.git] / third-party / benchmark / test / string_util_gtest.cc
blob67b4bc0c24f2620d07303163f4837bee514173ad
1 //===---------------------------------------------------------------------===//
2 // string_util_test - Unit tests for src/string_util.cc
3 //===---------------------------------------------------------------------===//
5 #include <tuple>
7 #include "../src/internal_macros.h"
8 #include "../src/string_util.h"
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
12 namespace {
13 TEST(StringUtilTest, stoul) {
15 size_t pos = 0;
16 EXPECT_EQ(0ul, benchmark::stoul("0", &pos));
17 EXPECT_EQ(1ul, pos);
20 size_t pos = 0;
21 EXPECT_EQ(7ul, benchmark::stoul("7", &pos));
22 EXPECT_EQ(1ul, pos);
25 size_t pos = 0;
26 EXPECT_EQ(135ul, benchmark::stoul("135", &pos));
27 EXPECT_EQ(3ul, pos);
29 #if ULONG_MAX == 0xFFFFFFFFul
31 size_t pos = 0;
32 EXPECT_EQ(0xFFFFFFFFul, benchmark::stoul("4294967295", &pos));
33 EXPECT_EQ(10ul, pos);
35 #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
37 size_t pos = 0;
38 EXPECT_EQ(0xFFFFFFFFFFFFFFFFul,
39 benchmark::stoul("18446744073709551615", &pos));
40 EXPECT_EQ(20ul, pos);
42 #endif
44 size_t pos = 0;
45 EXPECT_EQ(10ul, benchmark::stoul("1010", &pos, 2));
46 EXPECT_EQ(4ul, pos);
49 size_t pos = 0;
50 EXPECT_EQ(520ul, benchmark::stoul("1010", &pos, 8));
51 EXPECT_EQ(4ul, pos);
54 size_t pos = 0;
55 EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos, 10));
56 EXPECT_EQ(4ul, pos);
59 size_t pos = 0;
60 EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos, 16));
61 EXPECT_EQ(4ul, pos);
64 size_t pos = 0;
65 EXPECT_EQ(0xBEEFul, benchmark::stoul("BEEF", &pos, 16));
66 EXPECT_EQ(4ul, pos);
68 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
70 ASSERT_THROW(std::ignore = benchmark::stoul("this is a test"),
71 std::invalid_argument);
73 #endif
76 TEST(StringUtilTest, stoi){{size_t pos = 0;
77 EXPECT_EQ(0, benchmark::stoi("0", &pos));
78 EXPECT_EQ(1ul, pos);
79 } // namespace
81 size_t pos = 0;
82 EXPECT_EQ(-17, benchmark::stoi("-17", &pos));
83 EXPECT_EQ(3ul, pos);
86 size_t pos = 0;
87 EXPECT_EQ(1357, benchmark::stoi("1357", &pos));
88 EXPECT_EQ(4ul, pos);
91 size_t pos = 0;
92 EXPECT_EQ(10, benchmark::stoi("1010", &pos, 2));
93 EXPECT_EQ(4ul, pos);
96 size_t pos = 0;
97 EXPECT_EQ(520, benchmark::stoi("1010", &pos, 8));
98 EXPECT_EQ(4ul, pos);
101 size_t pos = 0;
102 EXPECT_EQ(1010, benchmark::stoi("1010", &pos, 10));
103 EXPECT_EQ(4ul, pos);
106 size_t pos = 0;
107 EXPECT_EQ(4112, benchmark::stoi("1010", &pos, 16));
108 EXPECT_EQ(4ul, pos);
111 size_t pos = 0;
112 EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos, 16));
113 EXPECT_EQ(4ul, pos);
115 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
117 ASSERT_THROW(std::ignore = benchmark::stoi("this is a test"),
118 std::invalid_argument);
120 #endif
123 TEST(StringUtilTest, stod){{size_t pos = 0;
124 EXPECT_EQ(0.0, benchmark::stod("0", &pos));
125 EXPECT_EQ(1ul, pos);
128 size_t pos = 0;
129 EXPECT_EQ(-84.0, benchmark::stod("-84", &pos));
130 EXPECT_EQ(3ul, pos);
133 size_t pos = 0;
134 EXPECT_EQ(1234.0, benchmark::stod("1234", &pos));
135 EXPECT_EQ(4ul, pos);
138 size_t pos = 0;
139 EXPECT_EQ(1.5, benchmark::stod("1.5", &pos));
140 EXPECT_EQ(3ul, pos);
143 size_t pos = 0;
144 /* Note: exactly representable as double */
145 EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos));
146 EXPECT_EQ(8ul, pos);
148 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
150 ASSERT_THROW(std::ignore = benchmark::stod("this is a test"),
151 std::invalid_argument);
153 #endif
156 TEST(StringUtilTest, StrSplit) {
157 EXPECT_EQ(benchmark::StrSplit("", ','), std::vector<std::string>{});
158 EXPECT_EQ(benchmark::StrSplit("hello", ','),
159 std::vector<std::string>({"hello"}));
160 EXPECT_EQ(benchmark::StrSplit("hello,there,is,more", ','),
161 std::vector<std::string>({"hello", "there", "is", "more"}));
164 using HumanReadableFixture = ::testing::TestWithParam<
165 std::tuple<double, benchmark::Counter::OneK, std::string>>;
167 INSTANTIATE_TEST_SUITE_P(
168 HumanReadableTests, HumanReadableFixture,
169 ::testing::Values(
170 std::make_tuple(0.0, benchmark::Counter::kIs1024, "0"),
171 std::make_tuple(999.0, benchmark::Counter::kIs1024, "999"),
172 std::make_tuple(1000.0, benchmark::Counter::kIs1024, "1000"),
173 std::make_tuple(1024.0, benchmark::Counter::kIs1024, "1Ki"),
174 std::make_tuple(1000 * 1000.0, benchmark::Counter::kIs1024,
175 "976\\.56.Ki"),
176 std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1024, "1Mi"),
177 std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1024,
178 "953\\.674Mi"),
179 std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1024,
180 "1Gi"),
181 std::make_tuple(0.0, benchmark::Counter::kIs1000, "0"),
182 std::make_tuple(999.0, benchmark::Counter::kIs1000, "999"),
183 std::make_tuple(1000.0, benchmark::Counter::kIs1000, "1k"),
184 std::make_tuple(1024.0, benchmark::Counter::kIs1000, "1.024k"),
185 std::make_tuple(1000 * 1000.0, benchmark::Counter::kIs1000, "1M"),
186 std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1000,
187 "1\\.04858M"),
188 std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1000,
189 "1G"),
190 std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1000,
191 "1\\.07374G")));
193 TEST_P(HumanReadableFixture, HumanReadableNumber) {
194 std::string str = benchmark::HumanReadableNumber(std::get<0>(GetParam()),
195 std::get<1>(GetParam()));
196 ASSERT_THAT(str, ::testing::MatchesRegex(std::get<2>(GetParam())));
199 } // end namespace