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

Unified Diff: net/base/parse_number_unittest.cc

Issue 1828103002: Extend net/base/parse_number.h for parsing of negative numbers, and determining if there was overflo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy_num
Patch Set: 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
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
« 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