Chromium Code Reviews| Index: net/base/parse_number_unittest.cc |
| diff --git a/net/base/parse_number_unittest.cc b/net/base/parse_number_unittest.cc |
| index 79cac9f43ba211b26aa8d58d1c943154b387f911..cbbf0833d43435efa55eaa4b623deb308db8b635 100644 |
| --- a/net/base/parse_number_unittest.cc |
| +++ b/net/base/parse_number_unittest.cc |
| @@ -4,67 +4,132 @@ |
| #include "net/base/parse_number.h" |
| +#include <limits> |
| + |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace net { |
| namespace { |
| -TEST(ParseNumberTest, IntValidInputs) { |
| +// Common tests to run for each of the overloaded versions of |
| +// ParseNonNegativeInteger(). |
| +template <typename T> |
| +void TestParseNonNegativeInteger() { |
| + // Test valid inputs |
| const struct { |
| const char* input; |
| - int output; |
| - } kTests[] = { |
| + T output; |
| + } kValidTests[] = { |
| {"0", 0}, {"00000", 0}, {"003", 3}, {"003", 3}, {"1234566", 1234566}, |
| {"987", 987}, {"010", 10}, |
| }; |
| - for (const auto& test : kTests) { |
| - int result; |
| - ASSERT_TRUE(ParseNonNegativeDecimalInt(test.input, &result)) |
| + for (const auto& test : kValidTests) { |
| + T result; |
| + ASSERT_TRUE(ParseNonNegativeInteger(test.input, &result)) |
| << "Failed to parse: " << test.input; |
| EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; |
| } |
| + |
| + // Test invalid inputs. |
| + const char* kInvalidTests[] = { |
| + "", "-23", "+42", " 123", "123 ", "123\n", "0xFF", "0x11", "x11", "F11", |
|
eroman
2016/03/24 01:24:19
clang-format did something very different this tim
|
| + "AF", "0AF", "0.0", "13.", "13,000", "13.000", "13/5", |
| + // Generic overflow test. |
| + "9999999999999999999999999999999999999999999999999999999999999999", "Inf", |
| + "NaN", "null", "dog", |
| + }; |
| + |
| + T kFailResult = T(3); |
| + |
| + for (const auto& input : kInvalidTests) { |
| + T result = kFailResult; |
| + ASSERT_FALSE(ParseNonNegativeInteger(input, &result)) |
| + << "Succeeded to parse: " << input; |
| + EXPECT_EQ(kFailResult, result) << "Modified output for failed parsing"; |
| + } |
| + |
| + // Test that NUL in the input causes a rejection. |
| + T result = kFailResult; |
| + ASSERT_FALSE(ParseNonNegativeInteger(base::StringPiece("123\0", 4), &result)); |
| + EXPECT_EQ(kFailResult, result); |
| } |
| -TEST(ParseNumberTest, IntInvalidInputs) { |
| - const char* kTests[] = { |
| - "", |
| - "-23", |
| - "+42", |
| - " 123", |
| - "123 ", |
| - "123\n", |
| - "0xFF", |
| - "0x11", |
| - "x11", |
| - "F11", |
| - "AF", |
| - "0AF", |
| - "0.0", |
| - "13.", |
| - "13,000", |
| - "13.000", |
| - "13/5", |
| +// Common tests to run for each of the overloaded versions of |
| +// ParseSignedInteger(). |
| +template <typename T> |
| +void TestParseSignedInteger() { |
| + // Test valid inputs |
| + const struct { |
| + const char* input; |
| + T output; |
| + } kValidTests[] = { |
| + {"0", 0}, {"-0", 0}, {"00000", 0}, |
|
eroman
2016/03/24 01:24:19
The difference in these inputs is -0 and other neg
|
| + {"003", 3}, {"003", 3}, {"1234566", 1234566}, |
| + {"987", 987}, {"010", 10}, {"-632", -632}, |
| + }; |
| + |
| + for (const auto& test : kValidTests) { |
| + T result; |
| + ASSERT_TRUE(ParseSignedInteger(test.input, &result)) << "Failed to parse: " |
| + << test.input; |
| + EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; |
| + } |
| + |
| + // Test invalid inputs. |
| + const char* kInvalidTests[] = { |
| + "", "+42", " 123", "123 ", "123\n", "0xFF", "0x11", "x11", "F11", "AF", |
| + "0AF", "0.0", "13.", "13,000", "13.000", "13/5", |
| + // Generic overflow test (none of types used can hold this) |
| "9999999999999999999999999999999999999999999999999999999999999999", |
| - "Inf", |
| - "NaN", |
| - "null", |
| - "dog", |
| + // Generic under test (none of types used can hold this) |
| + "-9999999999999999999999999999999999999999999999999999999999999999", |
| + "Inf", "NaN", "null", "dog", |
| }; |
| - for (const auto& input : kTests) { |
| - int result = 0xDEAD; |
| - ASSERT_FALSE(ParseNonNegativeDecimalInt(input, &result)) |
| - << "Succeeded to parse: " << input; |
| - EXPECT_EQ(0xDEAD, result) << "Modified output for failed parsing"; |
| + T kFailResult = T(3); |
| + |
| + for (const auto& input : kInvalidTests) { |
| + T result = kFailResult; |
| + ASSERT_FALSE(ParseSignedInteger(input, &result)) << "Succeeded to parse: " |
| + << input; |
| + EXPECT_EQ(kFailResult, result) << "Modified output for failed parsing"; |
| } |
| + |
| + // Test that NUL in the input causes a rejection. |
| + T result = kFailResult; |
| + ASSERT_FALSE(ParseSignedInteger(base::StringPiece("123\0", 4), &result)); |
| + EXPECT_EQ(kFailResult, result); |
| +} |
| + |
| +TEST(ParseNumberTest, ParseNonNegativeIntegerInt32) { |
| + TestParseNonNegativeInteger<int32_t>(); |
| +} |
| + |
| +TEST(ParseNumberTest, ParseNonNegativeIntegerInt64) { |
| + TestParseNonNegativeInteger<int64_t>(); |
| +} |
| + |
| +TEST(ParseNumberTest, ParseNonNegativeIntegerUint32) { |
| + TestParseNonNegativeInteger<uint32_t>(); |
| +} |
| + |
| +TEST(ParseNumberTest, ParseNonNegativeIntegerUint64) { |
| + TestParseNonNegativeInteger<uint64_t>(); |
| +} |
| + |
| +TEST(ParseNumberTest, ParseNonNegativeIntegerSizeT) { |
| + // This will effectively be a duplicate of either the uint64_t or uint32_t |
| + // test, depending on bitedness of build. Yet it is worth testing nonetheless. |
| + TestParseNonNegativeInteger<size_t>(); |
| +} |
| + |
| +TEST(ParseNumberTest, ParseSignedIntegerInt32) { |
| + TestParseSignedInteger<int32_t>(); |
| } |
| -TEST(ParseNumberTest, IntInvalidInputsContainsNul) { |
| - int result = 0xDEAD; |
| - ASSERT_FALSE( |
| - ParseNonNegativeDecimalInt(base::StringPiece("123\0", 4), &result)); |
| - EXPECT_EQ(0xDEAD, result); |
| +TEST(ParseNumberTest, ParseSignedIntegerInt64) { |
| + TestParseSignedInteger<int64_t>(); |
| } |
| } // namespace |