summaryrefslogtreecommitdiffstats
path: root/base/string_util_unittest.cc
diff options
context:
space:
mode:
authormmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 17:15:41 +0000
committermmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 17:15:41 +0000
commitd9023abb77c05d411835658b1091bdbb592b45c2 (patch)
tree0fb92b1786b59c2c1a42a0d29d58df697e1b7ec0 /base/string_util_unittest.cc
parent9f16de03664224cc286851ca03369cf699dd7c3c (diff)
downloadchromium_src-d9023abb77c05d411835658b1091bdbb592b45c2.zip
chromium_src-d9023abb77c05d411835658b1091bdbb592b45c2.tar.gz
chromium_src-d9023abb77c05d411835658b1091bdbb592b45c2.tar.bz2
Cross-platform portability fixes for JSONReader. Adds generic string-to-double parsing and tests in string_util.
There is one behavior change here: numbers which "look" like integers by virtue of being free of '.', 'e', and 'E' are no longer rejected if they're not within the [INT_MIN .. INT_MAX] range. Instead, they'll be parsed and stored internally as doubles. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@519 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util_unittest.cc')
-rw-r--r--base/string_util_unittest.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index 1aa4043..dcebddf 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -735,6 +735,8 @@ TEST(StringUtilTest, StringToInt64) {
{"99999999999", GG_INT64_C(99999999999), true},
{"9223372036854775807", kint64max, true},
{"-9223372036854775808", kint64min, true},
+ {"09", 9, true},
+ {"-09", -9, true},
{"", 0, false},
{" 42", 42, false},
{"\t\n\v\f\r 42", 42, false},
@@ -801,6 +803,8 @@ TEST(StringUtilTest, HexStringToInt) {
{"0x80000000", INT_MIN, true},
{"0xffffffff", -1, true},
{"0XDeadBeef", 0xdeadbeef, true},
+ {"0x0f", 15, true},
+ {"0f", 15, true},
{" 45", 0x45, false},
{"\t\n\v\f\r 0x45", 0x45, false},
{"efgh", 0xef, false},
@@ -836,6 +840,65 @@ TEST(StringUtilTest, HexStringToInt) {
EXPECT_EQ(0xc0ffee, output);
}
+TEST(StringUtilTest, StringToDouble) {
+ static const struct {
+ std::string input;
+ double output;
+ bool success;
+ } cases[] = {
+ {"0", 0.0, true},
+ {"42", 42.0, true},
+ {"-42", -42.0, true},
+ {"123.45", 123.45, true},
+ {"-123.45", -123.45, true},
+ {"+123.45", 123.45, true},
+ {"2.99792458e8", 299792458.0, true},
+ {"149597870.691E+3", 149597870691.0, true},
+ {"6.", 6.0, true},
+ {"9e99999999999999999999", HUGE_VAL, false},
+ {"-9e99999999999999999999", -HUGE_VAL, false},
+ {"1e-2", 0.01, true},
+ {"-1E-7", -0.0000001, true},
+ {"01e02", 100, true},
+ {"2.3e15", 2.3e15, true},
+ {"\t\n\v\f\r -123.45e2", -12345.0, false},
+ {"+123 e4", 123.0, false},
+ {"123e ", 123.0, false},
+ {"123e", 123.0, false},
+ {" 2.99", 2.99, false},
+ {"1e3.4", 1000.0, false},
+ {"nothing", 0.0, false},
+ {"-", 0.0, false},
+ {"+", 0.0, false},
+ {"", 0.0, false},
+ };
+
+ for (int i = 0; i < arraysize(cases); ++i) {
+ EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(cases[i].input));
+ double output;
+ EXPECT_EQ(cases[i].success, StringToDouble(cases[i].input, &output));
+ EXPECT_DOUBLE_EQ(cases[i].output, output);
+
+ std::wstring wide_input = ASCIIToWide(cases[i].input);
+ EXPECT_DOUBLE_EQ(cases[i].output, StringToDouble(wide_input));
+ EXPECT_EQ(cases[i].success, StringToDouble(wide_input, &output));
+ EXPECT_DOUBLE_EQ(cases[i].output, output);
+ }
+
+ // One additional test to verify that conversion of numbers in strings with
+ // embedded NUL characters. The NUL and extra data after it should be
+ // interpreted as junk after the number.
+ const char input[] = "3.14\0159";
+ std::string input_string(input, arraysize(input) - 1);
+ double output;
+ EXPECT_FALSE(StringToDouble(input_string, &output));
+ EXPECT_DOUBLE_EQ(3.14, output);
+
+ std::wstring wide_input = ASCIIToWide(input_string);
+ EXPECT_FALSE(StringToDouble(wide_input, &output));
+ EXPECT_DOUBLE_EQ(3.14, output);
+}
+
// This checks where we can use the assignment operator for a va_list. We need
// a way to do this since Visual C doesn't support va_copy, but assignment on
// va_list is not guaranteed to be a copy. See StringAppendVT which uses this