| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/parse_number.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace net { |
| 10 namespace { |
| 11 |
| 12 TEST(ParseNumberTest, IntValidInputs) { |
| 13 const struct { |
| 14 const char* input; |
| 15 int output; |
| 16 } kTests[] = { |
| 17 {"0", 0}, {"00000", 0}, {"003", 3}, {"003", 3}, {"1234566", 1234566}, |
| 18 {"987", 987}, {"010", 10}, |
| 19 }; |
| 20 |
| 21 for (const auto& test : kTests) { |
| 22 int result; |
| 23 ASSERT_TRUE(ParseNonNegativeDecimalInt(test.input, &result)) |
| 24 << "Failed to parse: " << test.input; |
| 25 EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input; |
| 26 } |
| 27 } |
| 28 |
| 29 TEST(ParseNumberTest, IntInvalidInputs) { |
| 30 const char* kTests[] = { |
| 31 "", |
| 32 "-23", |
| 33 "+42", |
| 34 " 123", |
| 35 "123 ", |
| 36 "123\n", |
| 37 "0xFF", |
| 38 "0x11", |
| 39 "x11", |
| 40 "F11", |
| 41 "AF", |
| 42 "0AF", |
| 43 "0.0", |
| 44 "13.", |
| 45 "13,000", |
| 46 "13.000", |
| 47 "13/5", |
| 48 "9999999999999999999999999999999999999999999999999999999999999999", |
| 49 "Inf", |
| 50 "NaN", |
| 51 "null", |
| 52 "dog", |
| 53 }; |
| 54 |
| 55 for (const auto& input : kTests) { |
| 56 int result = 0xDEAD; |
| 57 ASSERT_FALSE(ParseNonNegativeDecimalInt(input, &result)) |
| 58 << "Succeeded to parse: " << input; |
| 59 EXPECT_EQ(0xDEAD, result) << "Modified output for failed parsing"; |
| 60 } |
| 61 } |
| 62 |
| 63 TEST(ParseNumberTest, IntInvalidInputsContainsNul) { |
| 64 int result = 0xDEAD; |
| 65 ASSERT_FALSE( |
| 66 ParseNonNegativeDecimalInt(base::StringPiece("123\0", 4), &result)); |
| 67 EXPECT_EQ(0xDEAD, result); |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 } // namespace net |
| OLD | NEW |