1 //===---------------------------------------------------------------------===//
2 // string_util_test - Unit tests for src/string_util.cc
3 //===---------------------------------------------------------------------===//
7 #include "../src/internal_macros.h"
8 #include "../src/string_util.h"
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
13 TEST(StringUtilTest
, stoul
) {
16 EXPECT_EQ(0ul, benchmark::stoul("0", &pos
));
21 EXPECT_EQ(7ul, benchmark::stoul("7", &pos
));
26 EXPECT_EQ(135ul, benchmark::stoul("135", &pos
));
29 #if ULONG_MAX == 0xFFFFFFFFul
32 EXPECT_EQ(0xFFFFFFFFul
, benchmark::stoul("4294967295", &pos
));
35 #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
38 EXPECT_EQ(0xFFFFFFFFFFFFFFFFul
,
39 benchmark::stoul("18446744073709551615", &pos
));
45 EXPECT_EQ(10ul, benchmark::stoul("1010", &pos
, 2));
50 EXPECT_EQ(520ul, benchmark::stoul("1010", &pos
, 8));
55 EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos
, 10));
60 EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos
, 16));
65 EXPECT_EQ(0xBEEFul
, benchmark::stoul("BEEF", &pos
, 16));
68 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
70 ASSERT_THROW(std::ignore
= benchmark::stoul("this is a test"),
71 std::invalid_argument
);
76 TEST(StringUtilTest
, stoi
){{size_t pos
= 0;
77 EXPECT_EQ(0, benchmark::stoi("0", &pos
));
82 EXPECT_EQ(-17, benchmark::stoi("-17", &pos
));
87 EXPECT_EQ(1357, benchmark::stoi("1357", &pos
));
92 EXPECT_EQ(10, benchmark::stoi("1010", &pos
, 2));
97 EXPECT_EQ(520, benchmark::stoi("1010", &pos
, 8));
102 EXPECT_EQ(1010, benchmark::stoi("1010", &pos
, 10));
107 EXPECT_EQ(4112, benchmark::stoi("1010", &pos
, 16));
112 EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos
, 16));
115 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
117 ASSERT_THROW(std::ignore
= benchmark::stoi("this is a test"),
118 std::invalid_argument
);
123 TEST(StringUtilTest
, stod
){{size_t pos
= 0;
124 EXPECT_EQ(0.0, benchmark::stod("0", &pos
));
129 EXPECT_EQ(-84.0, benchmark::stod("-84", &pos
));
134 EXPECT_EQ(1234.0, benchmark::stod("1234", &pos
));
139 EXPECT_EQ(1.5, benchmark::stod("1.5", &pos
));
144 /* Note: exactly representable as double */
145 EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos
));
148 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
150 ASSERT_THROW(std::ignore
= benchmark::stod("this is a test"),
151 std::invalid_argument
);
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
,
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
,
176 std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1024
, "1Mi"),
177 std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1024
,
179 std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1024
,
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
,
188 std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1000
,
190 std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1000
,
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())));