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