Index: base/string_number_conversions_unittest.cc |
diff --git a/base/string_number_conversions_unittest.cc b/base/string_number_conversions_unittest.cc |
index 30b9b8c06ea416ee82820521f9b6f69ec5e16b1e..af4b75897aa81262cd7f21267a73e3d715903012 100644 |
--- a/base/string_number_conversions_unittest.cc |
+++ b/base/string_number_conversions_unittest.cc |
@@ -252,6 +252,63 @@ TEST(StringNumberConversionsTest, HexStringToInt) { |
EXPECT_EQ(0xc0ffee, output); |
} |
+TEST(StringNumberConversionsTest, HexStringToInt64) { |
+ static const struct { |
+ std::string input; |
+ int64 output; |
+ bool success; |
+ } cases[] = { |
+ {"0", 0, true}, |
+ {"42", 66, true}, |
+ {"-42", -66, true}, |
+ {"+42", 66, true}, |
+ {"7fffffff", INT_MAX, true}, |
+ {"80000000", 0x80000000, true}, |
+ {"ffffffff", 0xffffffff, true}, |
+ {"DeadBeef", 0xdeadbeef, true}, |
+ {"0x40acd88557b", 0x40acd88557b, true}, |
+ {"0x42", 66, true}, |
+ {"-0x42", -66, true}, |
+ {"+0x42", 66, true}, |
+ {"0x7fffffff", INT_MAX, true}, |
+ {"0x80000000", 0x80000000, true}, |
+ {"0xffffffff", 0xffffffff, true}, |
+ {"0XDeadBeef", 0xdeadbeef, true}, |
+ {"100000000", 0x100000000, true}, |
+ {"0xffffffffffffffff", -1, true}, |
+ {"0x7fffffffffffffff", kint64max, true}, |
+ {"0x8000000000000000", kint64min, true}, |
+ {"0x0f", 15, true}, |
+ {"0f", 15, 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}, |
+ {"10000000000000000", -1, false}, |
+ {"-", 0, false}, |
+ {"", 0, false}, |
+ {"0x", 0, false}, |
+ }; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
+ int64 output = 0; |
+ EXPECT_EQ(cases[i].success, HexStringToInt64(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); |
+ int64 output; |
+ EXPECT_FALSE(HexStringToInt64(input_string, &output)); |
+ EXPECT_EQ(0xc0ffee, output); |
+} |
+ |
TEST(StringNumberConversionsTest, HexStringToBytes) { |
static const struct { |
const std::string input; |