1 //===---------------------------------------------------------------------===//
2 // statistics_test - Unit tests for src/statistics.cc
3 //===---------------------------------------------------------------------===//
5 #include "../src/internal_macros.h"
6 #include "../src/string_util.h"
7 #include "gtest/gtest.h"
10 TEST(StringUtilTest
, stoul
) {
13 EXPECT_EQ(0ul, benchmark::stoul("0", &pos
));
18 EXPECT_EQ(7ul, benchmark::stoul("7", &pos
));
23 EXPECT_EQ(135ul, benchmark::stoul("135", &pos
));
26 #if ULONG_MAX == 0xFFFFFFFFul
29 EXPECT_EQ(0xFFFFFFFFul
, benchmark::stoul("4294967295", &pos
));
32 #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
35 EXPECT_EQ(0xFFFFFFFFFFFFFFFFul
,
36 benchmark::stoul("18446744073709551615", &pos
));
42 EXPECT_EQ(10ul, benchmark::stoul("1010", &pos
, 2));
47 EXPECT_EQ(520ul, benchmark::stoul("1010", &pos
, 8));
52 EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos
, 10));
57 EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos
, 16));
62 EXPECT_EQ(0xBEEFul
, benchmark::stoul("BEEF", &pos
, 16));
65 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
66 { ASSERT_THROW(benchmark::stoul("this is a test"), std::invalid_argument
); }
70 TEST(StringUtilTest
, stoi
){{size_t pos
= 0;
71 EXPECT_EQ(0, benchmark::stoi("0", &pos
));
76 EXPECT_EQ(-17, benchmark::stoi("-17", &pos
));
81 EXPECT_EQ(1357, benchmark::stoi("1357", &pos
));
86 EXPECT_EQ(10, benchmark::stoi("1010", &pos
, 2));
91 EXPECT_EQ(520, benchmark::stoi("1010", &pos
, 8));
96 EXPECT_EQ(1010, benchmark::stoi("1010", &pos
, 10));
101 EXPECT_EQ(4112, benchmark::stoi("1010", &pos
, 16));
106 EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos
, 16));
109 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
110 { ASSERT_THROW(benchmark::stoi("this is a test"), std::invalid_argument
); }
114 TEST(StringUtilTest
, stod
){{size_t pos
= 0;
115 EXPECT_EQ(0.0, benchmark::stod("0", &pos
));
120 EXPECT_EQ(-84.0, benchmark::stod("-84", &pos
));
125 EXPECT_EQ(1234.0, benchmark::stod("1234", &pos
));
130 EXPECT_EQ(1.5, benchmark::stod("1.5", &pos
));
135 /* Note: exactly representable as double */
136 EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos
));
139 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
140 { ASSERT_THROW(benchmark::stod("this is a test"), std::invalid_argument
); }
144 TEST(StringUtilTest
, StrSplit
) {
145 EXPECT_EQ(benchmark::StrSplit("", ','), std::vector
<std::string
>{});
146 EXPECT_EQ(benchmark::StrSplit("hello", ','),
147 std::vector
<std::string
>({"hello"}));
148 EXPECT_EQ(benchmark::StrSplit("hello,there,is,more", ','),
149 std::vector
<std::string
>({"hello", "there", "is", "more"}));