Index: base/strings/string_number_conversions_unittest.cc |
diff --git a/base/strings/string_number_conversions_unittest.cc b/base/strings/string_number_conversions_unittest.cc |
index 5ae59d304108a865fb994143f58aa091b03b2b5d..a3470226941a497e6b32d5ab4f1e81a04aacb83b 100644 |
--- a/base/strings/string_number_conversions_unittest.cc |
+++ b/base/strings/string_number_conversions_unittest.cc |
@@ -4,6 +4,7 @@ |
#include <errno.h> |
#include <math.h> |
+#include <stdint.h> |
#include <limits> |
@@ -136,6 +137,70 @@ TEST(StringNumberConversionsTest, StringToInt) { |
EXPECT_EQ(0, output); |
} |
+TEST(StringNumberConversionsTest, StringToUint) { |
+ static const struct { |
+ std::string input; |
+ unsigned output; |
+ bool success; |
+ } cases[] = { |
+ {"0", 0, true}, |
+ {"42", 42, true}, |
+ {"42\x99", 42, false}, |
+ {"\x99" "42\x99", 0, false}, |
+ {"-2147483648", 0, false}, |
+ {"2147483647", INT_MAX, true}, |
+ {"", 0, false}, |
+ {" 42", 42, false}, |
+ {"42 ", 42, false}, |
+ {"\t\n\v\f\r 42", 42, false}, |
+ {"blah42", 0, false}, |
+ {"42blah", 42, false}, |
+ {"blah42blah", 0, false}, |
+ {"-273.15", 0, false}, |
+ {"+98.6", 98, false}, |
+ {"--123", 0, false}, |
+ {"++123", 0, false}, |
+ {"-+123", 0, false}, |
+ {"+-123", 0, false}, |
+ {"-", 0, false}, |
+ {"-2147483649", 0, false}, |
+ {"-99999999999", 0, false}, |
+ {"4294967295", UINT_MAX, true}, |
+ {"4294967296", UINT_MAX, false}, |
+ {"99999999999", UINT_MAX, false}, |
+ }; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
+ unsigned output = 0; |
+ EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output)); |
+ EXPECT_EQ(cases[i].output, output); |
+ |
+ string16 utf16_input = UTF8ToUTF16(cases[i].input); |
+ output = 0; |
+ EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output)); |
+ EXPECT_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[] = "6\06"; |
+ std::string input_string(input, arraysize(input) - 1); |
+ unsigned output; |
+ EXPECT_FALSE(StringToUint(input_string, &output)); |
+ EXPECT_EQ(6U, output); |
+ |
+ string16 utf16_input = UTF8ToUTF16(input_string); |
+ output = 0; |
+ EXPECT_FALSE(StringToUint(utf16_input, &output)); |
+ EXPECT_EQ(6U, output); |
+ |
+ output = 0; |
+ const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; |
+ EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); |
+ EXPECT_EQ(0U, output); |
+} |
+ |
TEST(StringNumberConversionsTest, StringToInt64) { |
static const struct { |
std::string input; |
@@ -201,6 +266,138 @@ TEST(StringNumberConversionsTest, StringToInt64) { |
EXPECT_EQ(6, output); |
} |
+TEST(StringNumberConversionsTest, StringToUint64) { |
+ static const struct { |
+ std::string input; |
+ uint64 output; |
+ bool success; |
+ } cases[] = { |
+ {"0", 0, true}, |
+ {"42", 42, true}, |
+ {"-2147483648", 0, false}, |
+ {"2147483647", INT_MAX, true}, |
+ {"-2147483649", 0, false}, |
+ {"-99999999999", 0, false}, |
+ {"2147483648", GG_INT64_C(2147483648), true}, |
+ {"99999999999", GG_INT64_C(99999999999), true}, |
+ {"9223372036854775807", kint64max, true}, |
+ {"-9223372036854775808", 0, false}, |
+ {"09", 9, true}, |
+ {"-09", 0, false}, |
+ {"", 0, false}, |
+ {" 42", 42, false}, |
+ {"42 ", 42, false}, |
+ {"0x42", 0, false}, |
+ {"\t\n\v\f\r 42", 42, false}, |
+ {"blah42", 0, false}, |
+ {"42blah", 42, false}, |
+ {"blah42blah", 0, false}, |
+ {"-273.15", 0, false}, |
+ {"+98.6", 98, false}, |
+ {"--123", 0, false}, |
+ {"++123", 0, false}, |
+ {"-+123", 0, false}, |
+ {"+-123", 0, false}, |
+ {"-", 0, false}, |
+ {"-9223372036854775809", 0, false}, |
+ {"-99999999999999999999", 0, false}, |
+ {"9223372036854775808", 9223372036854775808U, true}, |
+ {"99999999999999999999", kuint64max, false}, |
+ {"18446744073709551615", kuint64max, true}, |
+ {"18446744073709551616", kuint64max, false}, |
+ }; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
+ uint64 output = 0; |
+ EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); |
+ EXPECT_EQ(cases[i].output, output); |
+ |
+ string16 utf16_input = UTF8ToUTF16(cases[i].input); |
+ output = 0; |
+ EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); |
+ EXPECT_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[] = "6\06"; |
+ std::string input_string(input, arraysize(input) - 1); |
+ uint64 output; |
+ EXPECT_FALSE(StringToUint64(input_string, &output)); |
+ EXPECT_EQ(6U, output); |
+ |
+ string16 utf16_input = UTF8ToUTF16(input_string); |
+ output = 0; |
+ EXPECT_FALSE(StringToUint64(utf16_input, &output)); |
+ EXPECT_EQ(6U, output); |
+} |
+ |
+TEST(StringNumberConversionsTest, StringToSizeT) { |
+ static const struct { |
+ std::string input; |
+ size_t output; |
+ bool success; |
+ } cases[] = { |
+ {"0", 0, true}, |
+ {"42", 42, true}, |
+ {"-2147483648", 0, false}, |
+ {"2147483647", INT_MAX, true}, |
+ {"-2147483649", 0, false}, |
+ {"-99999999999", 0, false}, |
+ {"2147483648", 2147483648U, true}, |
+#if SIZE_MAX > 4294967295U |
+ {"99999999999", 99999999999U, true}, |
+#endif |
+ {"-9223372036854775808", 0, false}, |
+ {"09", 9, true}, |
+ {"-09", 0, false}, |
+ {"", 0, false}, |
+ {" 42", 42, false}, |
+ {"42 ", 42, false}, |
+ {"0x42", 0, false}, |
+ {"\t\n\v\f\r 42", 42, false}, |
+ {"blah42", 0, false}, |
+ {"42blah", 42, false}, |
+ {"blah42blah", 0, false}, |
+ {"-273.15", 0, false}, |
+ {"+98.6", 98, false}, |
+ {"--123", 0, false}, |
+ {"++123", 0, false}, |
+ {"-+123", 0, false}, |
+ {"+-123", 0, false}, |
+ {"-", 0, false}, |
+ {"-9223372036854775809", 0, false}, |
+ {"-99999999999999999999", 0, false}, |
+ {"999999999999999999999999", std::numeric_limits<size_t>::max(), false}, |
+ }; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
+ size_t output = 0; |
+ EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output)); |
+ EXPECT_EQ(cases[i].output, output); |
+ |
+ string16 utf16_input = UTF8ToUTF16(cases[i].input); |
+ output = 0; |
+ EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output)); |
+ EXPECT_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[] = "6\06"; |
+ std::string input_string(input, arraysize(input) - 1); |
+ size_t output; |
+ EXPECT_FALSE(StringToSizeT(input_string, &output)); |
+ EXPECT_EQ(6U, output); |
+ |
+ string16 utf16_input = UTF8ToUTF16(input_string); |
+ output = 0; |
+ EXPECT_FALSE(StringToSizeT(utf16_input, &output)); |
+ EXPECT_EQ(6U, output); |
+} |
+ |
TEST(StringNumberConversionsTest, HexStringToInt) { |
static const struct { |
std::string input; |