Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Unified Diff: net/base/parse_number_unittest.cc

Issue 1829873002: Add base/parse_number.h to generalize number parsing done in //net. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more tests Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/parse_number.cc ('k') | net/base/sdch_manager.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..79cac9f43ba211b26aa8d58d1c943154b387f911
--- /dev/null
+++ b/net/base/parse_number_unittest.cc
@@ -0,0 +1,71 @@
+// 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) {
+ const struct {
+ const char* input;
+ int output;
+ } kTests[] = {
+ {"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))
+ << "Failed to parse: " << test.input;
+ EXPECT_EQ(result, test.output) << "Failed to parse: " << test.input;
+ }
+}
+
+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",
+ "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";
+ }
+}
+
+TEST(ParseNumberTest, IntInvalidInputsContainsNul) {
+ int result = 0xDEAD;
+ ASSERT_FALSE(
+ ParseNonNegativeDecimalInt(base::StringPiece("123\0", 4), &result));
+ EXPECT_EQ(0xDEAD, result);
+}
+
+} // namespace
+} // namespace net
« no previous file with comments | « net/base/parse_number.cc ('k') | net/base/sdch_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698