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 1c8d99fc4cb3b29f2d5d8a433144d7924b509bf6..950f2de4be17de09c483d1278cb15e509931bd34 100644 |
--- a/base/strings/string_number_conversions_unittest.cc |
+++ b/base/strings/string_number_conversions_unittest.cc |
@@ -3,11 +3,15 @@ |
// found in the LICENSE file. |
#include <errno.h> |
-#include <math.h> |
+#include <stdint.h> |
+#include <stdio.h> |
+#include <cmath> |
#include <limits> |
+#include "base/format_macros.h" |
#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
#include "base/strings/utf_string_conversions.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -136,6 +140,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 +269,149 @@ 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_UINT64_C(2147483648), true}, |
+ {"99999999999", GG_UINT64_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", GG_UINT64_C(9223372036854775808), 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) { |
+ |
+ size_t size_t_max = std::numeric_limits<size_t>::max(); |
+ size_t digits = static_cast<size_t>(ceil(std::log10( |
+ static_cast<double>(size_t_max)))); |
+ const unsigned int cstr_len = digits + 1; |
+ char *size_t_max_cstr = new char[cstr_len]; |
+ base::snprintf(size_t_max_cstr, cstr_len, "%" PRIuS, size_t_max); |
jar (doing other things)
2013/06/12 18:24:27
There is a method for sprint'ing into a std::strin
Mostyn Bramley-Moore
2013/06/12 20:29:16
Done.
This also allows us to drop the string size
|
+ |
+ 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", size_t_max, false}, |
+ {size_t_max_cstr, size_t_max, true}, |
+ }; |
+ |
+ 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); |
+ } |
+ |
+ delete[] size_t_max_cstr; |
+ |
+ // 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; |