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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..47199dfb0bcf32f4a27dcafef24a9f027d82e467 |
| --- /dev/null |
| +++ b/net/base/parse_number_unittest.cc |
| @@ -0,0 +1,67 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/parse_number.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| +namespace { |
| + |
| +TEST(ParseNumberTest, IntValidInputs) { |
| + struct { |
| + const char* input; |
| + int output; |
| + } tests[] = { |
|
mmenke
2016/03/23 19:32:28
+const
tests -> kTests
|
| + {"0", 0}, {"00000", 0}, {"003", 3}, {"003", 3}, {"1234566", 1234566}, |
|
mmenke
2016/03/23 19:32:28
You have no test that we can handle digits in decr
eroman
2016/03/23 19:50:30
Done
|
| + }; |
| + |
| + for (const auto& test : tests) { |
| + int result; |
| + ASSERT_TRUE(ParseNonNegativeDecimalInt(test.input, &result)) |
| + << "Failed to parse: " << test.input; |
| + EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; |
| + } |
| +} |
| + |
| +TEST(ParseNumberTest, IntInvalidInputs) { |
| + const char* tests[] = { |
| + "", |
| + "-23", |
| + "+42", |
| + " 123", |
| + "123 ", |
| + "123\n", |
| + "0xFF", |
|
mmenke
2016/03/23 19:32:28
0x11? Just checking hex value with non-hex chars.
eroman
2016/03/23 19:50:30
Good idea. Done.
|
| + "AF", |
| + "0AF", |
| + "0.0", |
| + "13.", |
| + "13,000", |
| + "13.000", |
| + "13/5", |
| + "9999999999999999999999999999999999999999999999999999999999999999", |
| + "Inf", |
| + "NaN", |
| + "null", |
| + "dog", |
| + }; |
| + |
| + for (const auto& input : tests) { |
| + int result = 0xDEAD; |
| + ASSERT_FALSE(ParseNonNegativeDecimalInt(input, &result)) |
| + << "Succeeded to parse: " << input; |
| + EXPECT_EQ(0xDEAD, result) << "Modified output for failed parsing"; |
| + } |
| +} |
| + |
| +TEST(ParseNumberTest, IntInvalidInputsContainsNul) { |
| + int result = 0xDEAD; |
| + ASSERT_FALSE( |
| + ParseNonNegativeDecimalInt(base::StringPiece("123\0", 4), &result)); |
| + EXPECT_EQ(0xDEAD, result); |
| +} |
| + |
| +} // namespace |
| +} // namespace net |