diff options
author | zverre@yandex-team.ru <zverre@yandex-team.ru@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-23 13:37:04 +0000 |
---|---|---|
committer | zverre@yandex-team.ru <zverre@yandex-team.ru@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-23 13:37:04 +0000 |
commit | 3fb0169853cbeca7abcd266fbf56310a02d85508 (patch) | |
tree | ccdd22c64906b935704daa3c62474bbd82908357 /base/strings/string_number_conversions_unittest.cc | |
parent | ea83c63fad527e6bdee803bb1e5ae54e32af036d (diff) | |
download | chromium_src-3fb0169853cbeca7abcd266fbf56310a02d85508.zip chromium_src-3fb0169853cbeca7abcd266fbf56310a02d85508.tar.gz chromium_src-3fb0169853cbeca7abcd266fbf56310a02d85508.tar.bz2 |
Add support for unsigned int32 in string_number_conversions
BUG=
Review URL: https://codereview.chromium.org/26131003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@230396 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/strings/string_number_conversions_unittest.cc')
-rw-r--r-- | base/strings/string_number_conversions_unittest.cc | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/base/strings/string_number_conversions_unittest.cc b/base/strings/string_number_conversions_unittest.cc index b861964..b6e5f96 100644 --- a/base/strings/string_number_conversions_unittest.cc +++ b/base/strings/string_number_conversions_unittest.cc @@ -458,6 +458,66 @@ TEST(StringNumberConversionsTest, HexStringToInt) { EXPECT_EQ(0xc0ffee, output); } +TEST(StringNumberConversionsTest, HexStringToUInt) { + static const struct { + std::string input; + uint32 output; + bool success; + } cases[] = { + {"0", 0, true}, + {"42", 0x42, true}, + {"-42", 0, false}, + {"+42", 0x42, true}, + {"7fffffff", INT_MAX, true}, + {"-80000000", 0, false}, + {"ffffffff", 0xffffffff, true}, + {"DeadBeef", 0xdeadbeef, true}, + {"0x42", 0x42, true}, + {"-0x42", 0, false}, + {"+0x42", 0x42, true}, + {"0x7fffffff", INT_MAX, true}, + {"-0x80000000", 0, false}, + {"0xffffffff", kuint32max, true}, + {"0XDeadBeef", 0xdeadbeef, true}, + {"0x7fffffffffffffff", kuint32max, false}, // Overflow test. + {"-0x8000000000000000", 0, false}, + {"0x8000000000000000", kuint32max, false}, // Overflow test. + {"-0x8000000000000001", 0, false}, + {"0xFFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test. + {"FFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test. + {"0x0000000000000000", 0, true}, + {"0000000000000000", 0, true}, + {"1FFFFFFFFFFFFFFFF", kuint32max, false}, // Overflow test. + {"0x0f", 0x0f, true}, + {"0f", 0x0f, true}, + {" 45", 0x45, false}, + {"\t\n\v\f\r 0x45", 0x45, false}, + {" 45", 0x45, false}, + {"45 ", 0x45, false}, + {"45:", 0x45, false}, + {"efgh", 0xef, false}, + {"0xefgh", 0xef, false}, + {"hgfe", 0, false}, + {"-", 0, false}, + {"", 0, false}, + {"0x", 0, false}, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + uint32 output = 0; + EXPECT_EQ(cases[i].success, HexStringToUInt(cases[i].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[] = "0xc0ffee\09"; + std::string input_string(input, arraysize(input) - 1); + uint32 output; + EXPECT_FALSE(HexStringToUInt(input_string, &output)); + EXPECT_EQ(0xc0ffeeU, output); +} + TEST(StringNumberConversionsTest, HexStringToInt64) { static const struct { std::string input; |