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

source code of third-party/benchmark/test/string_util_gtest.cc