19 #include <gtest/gtest.h>
20 #include <boost/cstdint.hpp>
21 #include <boost/lexical_cast.hpp>
27 using std::numeric_limits;
31 string space[] = {
"",
" ",
"\t\t\t",
"\n\n\n",
"\v\v\v",
"\f\f\f",
"\r\r\r"};
42 T val = StringParser::StringToInt<T>(str.data(), str.length(), &result);
43 EXPECT_EQ(exp_val, val) << str;
44 EXPECT_EQ(result, exp_result);
59 T val = StringParser::StringToInt<T>(str.data(), str.length(), base, &result);
60 EXPECT_EQ(exp_val, val) << str;
61 EXPECT_EQ(result, exp_result);
73 EXPECT_EQ(exp_val, val) << s;
74 EXPECT_EQ(result, exp_result);
83 T val = StringParser::StringToFloat<T>(s.data(), s.length(), &result);
84 EXPECT_EQ(exp_result, result);
87 T exp_val = strtod(s.c_str(), NULL);
88 EXPECT_EQ(exp_val, val);
95 T val = StringParser::StringToFloat<T>(s.data(), s.length(), &result);
96 EXPECT_EQ(exp_result, result);
99 EXPECT_TRUE(isnan(val));
106 string sign[] = {
"",
"+",
"-"};
109 for (
int k = 0; k < 3; ++k) {
111 string str =
space[i] + sign[k] + s +
space[j];
112 TestFloatValue<float>(str, exp_result);
113 TestFloatValue<double>(str, exp_result);
121 T min_val = numeric_limits<T>::min();
122 T max_val = numeric_limits<T>::max();
126 while (cur_val < max_val) {
127 string s = lexical_cast<
string>(cur_val);
134 while (cur_val > min_val) {
135 string s = lexical_cast<
string>(cur_val);
168 TEST(StringToInt, InvalidLeadingTrailing) {
195 TestIntValue<int64_t>(
"9223372036854775807", numeric_limits<int64_t>::max(),
197 TestIntValue<int64_t>(
"-9223372036854775808", numeric_limits<int64_t>::min(),
208 TestIntValue<int64_t>(
"9223372036854775808", 9223372036854775807LL,
210 TestIntValue<int64_t>(
"-9223372036854775809", numeric_limits<int64_t>::min(),
214 TEST(StringToInt, Int8_Exhaustive) {
216 for (
int i = -256; i <= 256; ++i) {
217 sprintf(buffer,
"%d", i);
221 }
else if (i < -128) {
224 TestIntValue<int8_t>(buffer, expected,
229 TEST(StringToIntWithBase, Basic) {
265 TEST(StringToIntWithBase, NonNumericCharacters) {
289 TEST(StringToIntWithBase, Limit) {
296 TestIntValue<int64_t>(
"9223372036854775807", 10, numeric_limits<int64_t>::max(),
298 TestIntValue<int64_t>(
"-9223372036854775808", 10, numeric_limits<int64_t>::min(),
302 TEST(StringToIntWithBase, Overflow) {
309 TestIntValue<int64_t>(
"9223372036854775808", 10, 9223372036854775807LL,
311 TestIntValue<int64_t>(
"-9223372036854775809", 10, numeric_limits<int64_t>::min(),
315 TEST(StringToIntWithBase, Int8_Exhaustive) {
317 for (
int i = -256; i <= 256; ++i) {
318 sprintf(buffer,
"%d", i);
322 }
else if (i < -128) {
325 TestIntValue<int8_t>(buffer, 10, expected,
355 string float_min = lexical_cast<
string>(numeric_limits<float>::min());
356 string float_max = lexical_cast<
string>(numeric_limits<float>::max());
359 string double_min = lexical_cast<
string>(numeric_limits<double>::min());
360 string double_max = lexical_cast<
string>(numeric_limits<double>::max());
430 TEST(StringToFloat, InvalidLeadingTrailing) {
450 TEST(StringToFloat, BruteForce) {
451 TestFloatBruteForce<float>();
452 TestFloatBruteForce<double>();
467 int main(
int argc,
char **argv) {
468 ::testing::InitGoogleTest(&argc, argv);
469 return RUN_ALL_TESTS();
void TestAllFloatVariants(const string &s, StringParser::ParseResult exp_result)
void TestBoolValue(const char *s, bool exp_val, StringParser::ParseResult exp_result)
void TestFloatBruteForce()
void TestIntValue(const char *s, T exp_val, StringParser::ParseResult exp_result)
static bool StringToBool(const char *s, int len, ParseResult *result)
Parses a string for 'true' or 'false', case insensitive.
int main(int argc, char **argv)
void TestFloatValue(const string &s, StringParser::ParseResult exp_result)
void TestFloatValueIsNan(const string &s, StringParser::ParseResult exp_result)