1 //===---------------------------------------------------------------------===//
2 // statistics_test - Unit tests for src/statistics.cc
3 //===---------------------------------------------------------------------===//
4 
5 #include "../src/string_util.h"
6 #include "gtest/gtest.h"
7 
8 namespace {
TEST(StringUtilTest,stoul)9 TEST(StringUtilTest, stoul) {
10   {
11     size_t pos = 0;
12     EXPECT_EQ(0ul, benchmark::stoul("0", &pos));
13     EXPECT_EQ(1ul, pos);
14   }
15   {
16     size_t pos = 0;
17     EXPECT_EQ(7ul, benchmark::stoul("7", &pos));
18     EXPECT_EQ(1ul, pos);
19   }
20   {
21     size_t pos = 0;
22     EXPECT_EQ(135ul, benchmark::stoul("135", &pos));
23     EXPECT_EQ(3ul, pos);
24   }
25 #if ULONG_MAX == 0xFFFFFFFFul
26   {
27     size_t pos = 0;
28     EXPECT_EQ(0xFFFFFFFFul, benchmark::stoul("4294967295", &pos));
29     EXPECT_EQ(10ul, pos);
30   }
31 #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
32   {
33     size_t pos = 0;
34     EXPECT_EQ(0xFFFFFFFFFFFFFFFFul, benchmark::stoul("18446744073709551615", &pos));
35     EXPECT_EQ(20ul, pos);
36   }
37 #endif
38   {
39     size_t pos = 0;
40     EXPECT_EQ(10ul, benchmark::stoul("1010", &pos, 2));
41     EXPECT_EQ(4ul, pos);
42   }
43   {
44     size_t pos = 0;
45     EXPECT_EQ(520ul, benchmark::stoul("1010", &pos, 8));
46     EXPECT_EQ(4ul, pos);
47   }
48   {
49     size_t pos = 0;
50     EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos, 10));
51     EXPECT_EQ(4ul, pos);
52   }
53   {
54     size_t pos = 0;
55     EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos, 16));
56     EXPECT_EQ(4ul, pos);
57   }
58   {
59     size_t pos = 0;
60     EXPECT_EQ(0xBEEFul, benchmark::stoul("BEEF", &pos, 16));
61     EXPECT_EQ(4ul, pos);
62   }
63   {
64     ASSERT_THROW(benchmark::stoul("this is a test"), std::invalid_argument);
65   }
66 }
67 
TEST(StringUtilTest,stoi)68 TEST(StringUtilTest, stoi) {
69   {
70     size_t pos = 0;
71     EXPECT_EQ(0, benchmark::stoi("0", &pos));
72     EXPECT_EQ(1ul, pos);
73   }
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   {
110     ASSERT_THROW(benchmark::stoi("this is a test"), std::invalid_argument);
111   }
112 }
113 
TEST(StringUtilTest,stod)114 TEST(StringUtilTest, stod) {
115   {
116     size_t pos = 0;
117     EXPECT_EQ(0.0, benchmark::stod("0", &pos));
118     EXPECT_EQ(1ul, pos);
119   }
120   {
121     size_t pos = 0;
122     EXPECT_EQ(-84.0, benchmark::stod("-84", &pos));
123     EXPECT_EQ(3ul, pos);
124   }
125   {
126     size_t pos = 0;
127     EXPECT_EQ(1234.0, benchmark::stod("1234", &pos));
128     EXPECT_EQ(4ul, pos);
129   }
130   {
131     size_t pos = 0;
132     EXPECT_EQ(1.5, benchmark::stod("1.5", &pos));
133     EXPECT_EQ(3ul, pos);
134   }
135   {
136     size_t pos = 0;
137     /* Note: exactly representable as double */
138     EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos));
139     EXPECT_EQ(8ul, pos);
140   }
141   {
142     ASSERT_THROW(benchmark::stod("this is a test"), std::invalid_argument);
143   }
144 }
145 
146 }  // end namespace
147