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