Commit fc979660 authored by Nigel Tao's avatar Nigel Tao Committed by Commit Bot

clang-format string_number_conversions_unittest.cc

This will minimize the diff for a follow-up commit.

Change-Id: I79bfaf5913550acb9c579e3e2037012241c6639b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2310050
Commit-Queue: Nico Weber <thakis@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790650}
parent 979cb165
...@@ -44,7 +44,8 @@ TEST(StringNumberConversionsTest, NumberToString) { ...@@ -44,7 +44,8 @@ TEST(StringNumberConversionsTest, NumberToString) {
{0, "0", "0"}, {0, "0", "0"},
{-1, "-1", "18446744073709551615"}, {-1, "-1", "18446744073709551615"},
{ {
std::numeric_limits<int64_t>::max(), "9223372036854775807", std::numeric_limits<int64_t>::max(),
"9223372036854775807",
"9223372036854775807", "9223372036854775807",
}, },
{std::numeric_limits<int64_t>::min(), "-9223372036854775808", {std::numeric_limits<int64_t>::min(), "-9223372036854775808",
...@@ -111,30 +112,32 @@ TEST(StringNumberConversionsTest, StringToInt) { ...@@ -111,30 +112,32 @@ TEST(StringNumberConversionsTest, StringToInt) {
int output; int output;
bool success; bool success;
} cases[] = { } cases[] = {
{"0", 0, true}, {"0", 0, true},
{"42", 42, true}, {"42", 42, true},
{"42\x99", 42, false}, {"42\x99", 42, false},
{"\x99" "42\x99", 0, false}, {"\x99"
{"-2147483648", INT_MIN, true}, "42\x99",
{"2147483647", INT_MAX, true}, 0, false},
{"", 0, false}, {"-2147483648", INT_MIN, true},
{" 42", 42, false}, {"2147483647", INT_MAX, true},
{"42 ", 42, false}, {"", 0, false},
{"\t\n\v\f\r 42", 42, false}, {" 42", 42, false},
{"blah42", 0, false}, {"42 ", 42, false},
{"42blah", 42, false}, {"\t\n\v\f\r 42", 42, false},
{"blah42blah", 0, false}, {"blah42", 0, false},
{"-273.15", -273, false}, {"42blah", 42, false},
{"+98.6", 98, false}, {"blah42blah", 0, false},
{"--123", 0, false}, {"-273.15", -273, false},
{"++123", 0, false}, {"+98.6", 98, false},
{"-+123", 0, false}, {"--123", 0, false},
{"+-123", 0, false}, {"++123", 0, false},
{"-", 0, false}, {"-+123", 0, false},
{"-2147483649", INT_MIN, false}, {"+-123", 0, false},
{"-99999999999", INT_MIN, false}, {"-", 0, false},
{"2147483648", INT_MAX, false}, {"-2147483649", INT_MIN, false},
{"99999999999", INT_MAX, false}, {"-99999999999", INT_MIN, false},
{"2147483648", INT_MAX, false},
{"99999999999", INT_MAX, false},
}; };
for (const auto& i : cases) { for (const auto& i : cases) {
...@@ -163,7 +166,7 @@ TEST(StringNumberConversionsTest, StringToInt) { ...@@ -163,7 +166,7 @@ TEST(StringNumberConversionsTest, StringToInt) {
EXPECT_EQ(6, output); EXPECT_EQ(6, output);
output = 0; output = 0;
const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; const char16 negative_wide_input[] = {0xFF4D, '4', '2', 0};
EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output));
EXPECT_EQ(0, output); EXPECT_EQ(0, output);
} }
...@@ -174,31 +177,33 @@ TEST(StringNumberConversionsTest, StringToUint) { ...@@ -174,31 +177,33 @@ TEST(StringNumberConversionsTest, StringToUint) {
unsigned output; unsigned output;
bool success; bool success;
} cases[] = { } cases[] = {
{"0", 0, true}, {"0", 0, true},
{"42", 42, true}, {"42", 42, true},
{"42\x99", 42, false}, {"42\x99", 42, false},
{"\x99" "42\x99", 0, false}, {"\x99"
{"-2147483648", 0, false}, "42\x99",
{"2147483647", INT_MAX, true}, 0, false},
{"", 0, false}, {"-2147483648", 0, false},
{" 42", 42, false}, {"2147483647", INT_MAX, true},
{"42 ", 42, false}, {"", 0, false},
{"\t\n\v\f\r 42", 42, false}, {" 42", 42, false},
{"blah42", 0, false}, {"42 ", 42, false},
{"42blah", 42, false}, {"\t\n\v\f\r 42", 42, false},
{"blah42blah", 0, false}, {"blah42", 0, false},
{"-273.15", 0, false}, {"42blah", 42, false},
{"+98.6", 98, false}, {"blah42blah", 0, false},
{"--123", 0, false}, {"-273.15", 0, false},
{"++123", 0, false}, {"+98.6", 98, false},
{"-+123", 0, false}, {"--123", 0, false},
{"+-123", 0, false}, {"++123", 0, false},
{"-", 0, false}, {"-+123", 0, false},
{"-2147483649", 0, false}, {"+-123", 0, false},
{"-99999999999", 0, false}, {"-", 0, false},
{"4294967295", UINT_MAX, true}, {"-2147483649", 0, false},
{"4294967296", UINT_MAX, false}, {"-99999999999", 0, false},
{"99999999999", UINT_MAX, false}, {"4294967295", UINT_MAX, true},
{"4294967296", UINT_MAX, false},
{"99999999999", UINT_MAX, false},
}; };
for (const auto& i : cases) { for (const auto& i : cases) {
...@@ -227,7 +232,7 @@ TEST(StringNumberConversionsTest, StringToUint) { ...@@ -227,7 +232,7 @@ TEST(StringNumberConversionsTest, StringToUint) {
EXPECT_EQ(6U, output); EXPECT_EQ(6U, output);
output = 0; output = 0;
const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; const char16 negative_wide_input[] = {0xFF4D, '4', '2', 0};
EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output)); EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output));
EXPECT_EQ(0U, output); EXPECT_EQ(0U, output);
} }
...@@ -439,35 +444,35 @@ TEST(StringNumberConversionsTest, HexStringToInt) { ...@@ -439,35 +444,35 @@ TEST(StringNumberConversionsTest, HexStringToInt) {
int64_t output; int64_t output;
bool success; bool success;
} cases[] = { } cases[] = {
{"0", 0, true}, {"0", 0, true},
{"42", 66, true}, {"42", 66, true},
{"-42", -66, true}, {"-42", -66, true},
{"+42", 66, true}, {"+42", 66, true},
{"7fffffff", INT_MAX, true}, {"7fffffff", INT_MAX, true},
{"-80000000", INT_MIN, true}, {"-80000000", INT_MIN, true},
{"80000000", INT_MAX, false}, // Overflow test. {"80000000", INT_MAX, false}, // Overflow test.
{"-80000001", INT_MIN, false}, // Underflow test. {"-80000001", INT_MIN, false}, // Underflow test.
{"0x42", 66, true}, {"0x42", 66, true},
{"-0x42", -66, true}, {"-0x42", -66, true},
{"+0x42", 66, true}, {"+0x42", 66, true},
{"0x7fffffff", INT_MAX, true}, {"0x7fffffff", INT_MAX, true},
{"-0x80000000", INT_MIN, true}, {"-0x80000000", INT_MIN, true},
{"-80000000", INT_MIN, true}, {"-80000000", INT_MIN, true},
{"80000000", INT_MAX, false}, // Overflow test. {"80000000", INT_MAX, false}, // Overflow test.
{"-80000001", INT_MIN, false}, // Underflow test. {"-80000001", INT_MIN, false}, // Underflow test.
{"0x0f", 15, true}, {"0x0f", 15, true},
{"0f", 15, true}, {"0f", 15, true},
{" 45", 0x45, false}, {" 45", 0x45, false},
{"\t\n\v\f\r 0x45", 0x45, false}, {"\t\n\v\f\r 0x45", 0x45, false},
{" 45", 0x45, false}, {" 45", 0x45, false},
{"45 ", 0x45, false}, {"45 ", 0x45, false},
{"45:", 0x45, false}, {"45:", 0x45, false},
{"efgh", 0xef, false}, {"efgh", 0xef, false},
{"0xefgh", 0xef, false}, {"0xefgh", 0xef, false},
{"hgfe", 0, false}, {"hgfe", 0, false},
{"-", 0, false}, {"-", 0, false},
{"", 0, false}, {"", 0, false},
{"0x", 0, false}, {"0x", 0, false},
}; };
for (const auto& i : cases) { for (const auto& i : cases) {
...@@ -478,7 +483,9 @@ TEST(StringNumberConversionsTest, HexStringToInt) { ...@@ -478,7 +483,9 @@ TEST(StringNumberConversionsTest, HexStringToInt) {
// One additional test to verify that conversion of numbers in strings with // One additional test to verify that conversion of numbers in strings with
// embedded NUL characters. The NUL and extra data after it should be // embedded NUL characters. The NUL and extra data after it should be
// interpreted as junk after the number. // interpreted as junk after the number.
const char input[] = "0xc0ffee\0" "9"; const char input[] =
"0xc0ffee\0"
"9";
std::string input_string(input, base::size(input) - 1); std::string input_string(input, base::size(input) - 1);
int output; int output;
EXPECT_FALSE(HexStringToInt(input_string, &output)); EXPECT_FALSE(HexStringToInt(input_string, &output));
...@@ -543,7 +550,9 @@ TEST(StringNumberConversionsTest, HexStringToUInt) { ...@@ -543,7 +550,9 @@ TEST(StringNumberConversionsTest, HexStringToUInt) {
// One additional test to verify that conversion of numbers in strings with // One additional test to verify that conversion of numbers in strings with
// embedded NUL characters. The NUL and extra data after it should be // embedded NUL characters. The NUL and extra data after it should be
// interpreted as junk after the number. // interpreted as junk after the number.
const char input[] = "0xc0ffee\0" "9"; const char input[] =
"0xc0ffee\0"
"9";
std::string input_string(input, base::size(input) - 1); std::string input_string(input, base::size(input) - 1);
uint32_t output; uint32_t output;
EXPECT_FALSE(HexStringToUInt(input_string, &output)); EXPECT_FALSE(HexStringToUInt(input_string, &output));
...@@ -602,7 +611,9 @@ TEST(StringNumberConversionsTest, HexStringToInt64) { ...@@ -602,7 +611,9 @@ TEST(StringNumberConversionsTest, HexStringToInt64) {
// One additional test to verify that conversion of numbers in strings with // One additional test to verify that conversion of numbers in strings with
// embedded NUL characters. The NUL and extra data after it should be // embedded NUL characters. The NUL and extra data after it should be
// interpreted as junk after the number. // interpreted as junk after the number.
const char input[] = "0xc0ffee\0" "9"; const char input[] =
"0xc0ffee\0"
"9";
std::string input_string(input, base::size(input) - 1); std::string input_string(input, base::size(input) - 1);
int64_t output; int64_t output;
EXPECT_FALSE(HexStringToInt64(input_string, &output)); EXPECT_FALSE(HexStringToInt64(input_string, &output));
...@@ -665,7 +676,9 @@ TEST(StringNumberConversionsTest, HexStringToUInt64) { ...@@ -665,7 +676,9 @@ TEST(StringNumberConversionsTest, HexStringToUInt64) {
// One additional test to verify that conversion of numbers in strings with // One additional test to verify that conversion of numbers in strings with
// embedded NUL characters. The NUL and extra data after it should be // embedded NUL characters. The NUL and extra data after it should be
// interpreted as junk after the number. // interpreted as junk after the number.
const char input[] = "0xc0ffee\0" "9"; const char input[] =
"0xc0ffee\0"
"9";
std::string input_string(input, base::size(input) - 1); std::string input_string(input, base::size(input) - 1);
uint64_t output; uint64_t output;
EXPECT_FALSE(HexStringToUInt64(input_string, &output)); EXPECT_FALSE(HexStringToUInt64(input_string, &output));
...@@ -680,23 +693,23 @@ TEST(StringNumberConversionsTest, HexStringToBytesStringSpan) { ...@@ -680,23 +693,23 @@ TEST(StringNumberConversionsTest, HexStringToBytesStringSpan) {
size_t output_len; size_t output_len;
bool success; bool success;
} cases[] = { } cases[] = {
{"0", "", 0, false}, // odd number of characters fails {"0", "", 0, false}, // odd number of characters fails
{"00", "\0", 1, true}, {"00", "\0", 1, true},
{"42", "\x42", 1, true}, {"42", "\x42", 1, true},
{"-42", "", 0, false}, // any non-hex value fails {"-42", "", 0, false}, // any non-hex value fails
{"+42", "", 0, false}, {"+42", "", 0, false},
{"7fffffff", "\x7f\xff\xff\xff", 4, true}, {"7fffffff", "\x7f\xff\xff\xff", 4, true},
{"80000000", "\x80\0\0\0", 4, true}, {"80000000", "\x80\0\0\0", 4, true},
{"deadbeef", "\xde\xad\xbe\xef", 4, true}, {"deadbeef", "\xde\xad\xbe\xef", 4, true},
{"DeadBeef", "\xde\xad\xbe\xef", 4, true}, {"DeadBeef", "\xde\xad\xbe\xef", 4, true},
{"0x42", "", 0, false}, // leading 0x fails (x is not hex) {"0x42", "", 0, false}, // leading 0x fails (x is not hex)
{"0f", "\xf", 1, true}, {"0f", "\xf", 1, true},
{"45 ", "\x45", 1, false}, {"45 ", "\x45", 1, false},
{"efgh", "\xef", 1, false}, {"efgh", "\xef", 1, false},
{"", "", 0, false}, {"", "", 0, false},
{"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true}, {"0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8, true},
{"0123456789ABCDEF012345", {"0123456789ABCDEF012345", "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45",
"\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45", 11, true}, 11, true},
}; };
for (size_t test_i = 0; test_i < base::size(cases); ++test_i) { for (size_t test_i = 0; test_i < base::size(cases); ++test_i) {
...@@ -767,77 +780,77 @@ TEST(StringNumberConversionsTest, StringToDouble) { ...@@ -767,77 +780,77 @@ TEST(StringNumberConversionsTest, StringToDouble) {
double output; double output;
bool success; bool success;
} cases[] = { } cases[] = {
// Test different forms of zero. // Test different forms of zero.
{"0", 0.0, true}, {"0", 0.0, true},
{"+0", 0.0, true}, {"+0", 0.0, true},
{"-0", 0.0, true}, {"-0", 0.0, true},
{"0.0", 0.0, true}, {"0.0", 0.0, true},
{"000000000000000000000000000000.0", 0.0, true}, {"000000000000000000000000000000.0", 0.0, true},
{"0.000000000000000000000000000", 0.0, true}, {"0.000000000000000000000000000", 0.0, true},
// Test the answer. // Test the answer.
{"42", 42.0, true}, {"42", 42.0, true},
{"-42", -42.0, true}, {"-42", -42.0, true},
// Test variances of an ordinary number. // Test variances of an ordinary number.
{"123.45", 123.45, true}, {"123.45", 123.45, true},
{"-123.45", -123.45, true}, {"-123.45", -123.45, true},
{"+123.45", 123.45, true}, {"+123.45", 123.45, true},
// Test different forms of representation. // Test different forms of representation.
{"2.99792458e8", 299792458.0, true}, {"2.99792458e8", 299792458.0, true},
{"149597870.691E+3", 149597870691.0, true}, {"149597870.691E+3", 149597870691.0, true},
{"6.", 6.0, true}, {"6.", 6.0, true},
// Test around the largest/smallest value that a double can represent. // Test around the largest/smallest value that a double can represent.
{"9e307", 9e307, true}, {"9e307", 9e307, true},
{"1.7976e308", 1.7976e308, true}, {"1.7976e308", 1.7976e308, true},
{"1.7977e308", HUGE_VAL, false}, {"1.7977e308", HUGE_VAL, false},
{"1.797693134862315807e+308", HUGE_VAL, true}, {"1.797693134862315807e+308", HUGE_VAL, true},
{"1.797693134862315808e+308", HUGE_VAL, false}, {"1.797693134862315808e+308", HUGE_VAL, false},
{"9e308", HUGE_VAL, false}, {"9e308", HUGE_VAL, false},
{"9e309", HUGE_VAL, false}, {"9e309", HUGE_VAL, false},
{"9e999", HUGE_VAL, false}, {"9e999", HUGE_VAL, false},
{"9e1999", HUGE_VAL, false}, {"9e1999", HUGE_VAL, false},
{"9e19999", HUGE_VAL, false}, {"9e19999", HUGE_VAL, false},
{"9e99999999999999999999", HUGE_VAL, false}, {"9e99999999999999999999", HUGE_VAL, false},
{"-9e307", -9e307, true}, {"-9e307", -9e307, true},
{"-1.7976e308", -1.7976e308, true}, {"-1.7976e308", -1.7976e308, true},
{"-1.7977e308", -HUGE_VAL, false}, {"-1.7977e308", -HUGE_VAL, false},
{"-1.797693134862315807e+308", -HUGE_VAL, true}, {"-1.797693134862315807e+308", -HUGE_VAL, true},
{"-1.797693134862315808e+308", -HUGE_VAL, false}, {"-1.797693134862315808e+308", -HUGE_VAL, false},
{"-9e308", -HUGE_VAL, false}, {"-9e308", -HUGE_VAL, false},
{"-9e309", -HUGE_VAL, false}, {"-9e309", -HUGE_VAL, false},
{"-9e999", -HUGE_VAL, false}, {"-9e999", -HUGE_VAL, false},
{"-9e1999", -HUGE_VAL, false}, {"-9e1999", -HUGE_VAL, false},
{"-9e19999", -HUGE_VAL, false}, {"-9e19999", -HUGE_VAL, false},
{"-9e99999999999999999999", -HUGE_VAL, false}, {"-9e99999999999999999999", -HUGE_VAL, false},
// Test more exponents. // Test more exponents.
{"1e-2", 0.01, true}, {"1e-2", 0.01, true},
{"42 ", 42.0, false}, {"42 ", 42.0, false},
{" 1e-2", 0.01, false}, {" 1e-2", 0.01, false},
{"1e-2 ", 0.01, false}, {"1e-2 ", 0.01, false},
{"-1E-7", -0.0000001, true}, {"-1E-7", -0.0000001, true},
{"01e02", 100, true}, {"01e02", 100, true},
{"2.3e15", 2.3e15, true}, {"2.3e15", 2.3e15, true},
{"100e-309", 100e-309, true}, {"100e-309", 100e-309, true},
// Test some invalid cases. // Test some invalid cases.
{"\t\n\v\f\r -123.45e2", -12345.0, false}, {"\t\n\v\f\r -123.45e2", -12345.0, false},
{"+123 e4", 123.0, false}, {"+123 e4", 123.0, false},
{"123e ", 123.0, false}, {"123e ", 123.0, false},
{"123e", 123.0, false}, {"123e", 123.0, false},
{" 2.99", 2.99, false}, {" 2.99", 2.99, false},
{"1e3.4", 1000.0, false}, {"1e3.4", 1000.0, false},
{"nothing", 0.0, false}, {"nothing", 0.0, false},
{"-", 0.0, false}, {"-", 0.0, false},
{"+", 0.0, false}, {"+", 0.0, false},
{"", 0.0, false}, {"", 0.0, false},
// crbug.org/588726 // crbug.org/588726
{"-0.0010000000000000000000000000000000000000001e-256", {"-0.0010000000000000000000000000000000000000001e-256",
-1.0000000000000001e-259, true}, -1.0000000000000001e-259, true},
}; };
for (size_t i = 0; i < base::size(cases); ++i) { for (size_t i = 0; i < base::size(cases); ++i) {
...@@ -854,7 +867,9 @@ TEST(StringNumberConversionsTest, StringToDouble) { ...@@ -854,7 +867,9 @@ TEST(StringNumberConversionsTest, StringToDouble) {
// One additional test to verify that conversion of numbers in strings with // One additional test to verify that conversion of numbers in strings with
// embedded NUL characters. The NUL and extra data after it should be // embedded NUL characters. The NUL and extra data after it should be
// interpreted as junk after the number. // interpreted as junk after the number.
const char input[] = "3.14\0" "159"; const char input[] =
"3.14\0"
"159";
std::string input_string(input, base::size(input) - 1); std::string input_string(input, base::size(input) - 1);
double output; double output;
EXPECT_FALSE(StringToDouble(input_string, &output)); EXPECT_FALSE(StringToDouble(input_string, &output));
...@@ -886,8 +901,8 @@ TEST(StringNumberConversionsTest, DoubleToString) { ...@@ -886,8 +901,8 @@ TEST(StringNumberConversionsTest, DoubleToString) {
double input = 0; double input = 0;
memcpy(&input, input_bytes, base::size(input_bytes)); memcpy(&input, input_bytes, base::size(input_bytes));
EXPECT_EQ("1.335179083776e+12", NumberToString(input)); EXPECT_EQ("1.335179083776e+12", NumberToString(input));
const char input_bytes2[8] = const char input_bytes2[8] = {0, 0, 0, '\xa0',
{0, 0, 0, '\xa0', '\xda', '\x6c', '\x73', '\x42'}; '\xda', '\x6c', '\x73', '\x42'};
input = 0; input = 0;
memcpy(&input, input_bytes2, base::size(input_bytes2)); memcpy(&input, input_bytes2, base::size(input_bytes2));
EXPECT_EQ("1.33489033216e+12", NumberToString(input)); EXPECT_EQ("1.33489033216e+12", NumberToString(input));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment