Chromium Code Reviews| 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..34dd1776705fb3d8137b31a9f1c4290638662993 100644 |
| --- a/base/strings/string_number_conversions_unittest.cc |
| +++ b/base/strings/string_number_conversions_unittest.cc |
| @@ -136,6 +136,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 +265,151 @@ 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", static_cast<size_t>(2147483648), 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}, |
| + {"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); |
| + } |
| + |
| + if (sizeof(size_t) == 8) { |
| + // there is some implementation flexibility in the size of size_t |
| + // so let's have a special case for a 64bit value that is greater |
| + // than 32bit UINT_MAX. |
| + size_t output; |
| + EXPECT_EQ(true, StringToSizeT("99999999999", &output)); |
| +#if defined(OS_WIN) |
| +#pragma warning(disable:4309) |
| +#endif |
|
Vitaly Buka corp
2013/05/20 19:42:49
Can you put this tests back to array and
use #if
Mostyn Bramley-Moore
2013/05/20 20:13:34
I found SIZE_MAX in stdint.h which should be more
|
| + EXPECT_EQ( static_cast<size_t>(99999999999), output); |
| +#if defined(OS_WIN) |
| +#pragma warning(default:4309) |
| +#endif |
| + } |
| + |
| + |
| + // 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; |