summaryrefslogtreecommitdiffstats
path: root/base/string_number_conversions_unittest.cc
diff options
context:
space:
mode:
authorerikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-21 14:05:38 +0000
committererikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-21 14:05:38 +0000
commitcfcf2da53e0439a8e7529392fd19762d0a39e43a (patch)
treee667dd8ef096afff7f6dc6bbe3c81d4e5cc007c0 /base/string_number_conversions_unittest.cc
parent0d6dfa67b2dc3b6560412420dde2a47bb8ee32b6 (diff)
downloadchromium_src-cfcf2da53e0439a8e7529392fd19762d0a39e43a.zip
chromium_src-cfcf2da53e0439a8e7529392fd19762d0a39e43a.tar.gz
chromium_src-cfcf2da53e0439a8e7529392fd19762d0a39e43a.tar.bz2
Add StringToInt and HexStringToInt for iterator ranges.
BUG=52601 TEST=base_unittests / StringNumberConversionsTest.*StringToInt Review URL: http://codereview.chromium.org/3866001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63357 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_number_conversions_unittest.cc')
-rw-r--r--base/string_number_conversions_unittest.cc106
1 files changed, 102 insertions, 4 deletions
diff --git a/base/string_number_conversions_unittest.cc b/base/string_number_conversions_unittest.cc
index 9ae7bdb..55487df 100644
--- a/base/string_number_conversions_unittest.cc
+++ b/base/string_number_conversions_unittest.cc
@@ -2,8 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <math.h>
+
#include <limits>
-#include <math.h> // For HUGE_VAL.
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
@@ -102,13 +103,34 @@ TEST(StringNumberConversionsTest, StringToInt) {
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int output;
+ const char* ascii_chars = cases[i].input.c_str();
+ int output = 0;
EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output));
EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt(cases[i].input.begin(),
+ cases[i].input.end(),
+ &output));
+ EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt(
+ ascii_chars, ascii_chars + cases[i].input.length(), &output));
+ EXPECT_EQ(cases[i].output, output);
string16 utf16_input = UTF8ToUTF16(cases[i].input);
+ const char16* utf16_chars = utf16_input.c_str();
+ output = 0;
EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output));
EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt(utf16_input.begin(),
+ utf16_input.end(),
+ &output));
+ EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt(
+ utf16_chars, utf16_chars + utf16_input.length(), &output));
+ EXPECT_EQ(cases[i].output, output);
}
// One additional test to verify that conversion of numbers in strings with
@@ -119,10 +141,26 @@ TEST(StringNumberConversionsTest, StringToInt) {
int output;
EXPECT_FALSE(StringToInt(input_string, &output));
EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt(input_string.begin(), input_string.end(), &output));
+ EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt(input, input + arraysize(input), &output));
+ EXPECT_EQ(6, output);
string16 utf16_input = UTF8ToUTF16(input_string);
+ const char16* utf16_chars = utf16_input.c_str();
+ output = 0;
EXPECT_FALSE(StringToInt(utf16_input, &output));
EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt(utf16_input.begin(), utf16_input.end(), &output));
+ EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt(utf16_chars,
+ utf16_chars + utf16_input.length(),
+ &output));
+ EXPECT_EQ(6, output);
}
TEST(StringNumberConversionsTest, StringToInt64) {
@@ -146,6 +184,7 @@ TEST(StringNumberConversionsTest, StringToInt64) {
{"", 0, false},
{" 42", 42, false},
{"42 ", 42, false},
+ {"0x42", 0, false},
{"\t\n\v\f\r 42", 42, false},
{"blah42", 0, false},
{"42blah", 42, false},
@@ -164,13 +203,34 @@ TEST(StringNumberConversionsTest, StringToInt64) {
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int64 output;
+ const char* ascii_chars = cases[i].input.c_str();
+ int64 output = 0;
EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output));
EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input.begin(),
+ cases[i].input.end(),
+ &output));
+ EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt64(
+ ascii_chars, ascii_chars + cases[i].input.length(), &output));
+ EXPECT_EQ(cases[i].output, output);
string16 utf16_input = UTF8ToUTF16(cases[i].input);
+ const char16* utf16_chars = utf16_input.c_str();
+ output = 0;
EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output));
EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt64(utf16_input.begin(),
+ utf16_input.end(),
+ &output));
+ EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, StringToInt64(
+ utf16_chars, utf16_chars + utf16_input.length(), &output));
+ EXPECT_EQ(cases[i].output, output);
}
// One additional test to verify that conversion of numbers in strings with
@@ -181,10 +241,28 @@ TEST(StringNumberConversionsTest, StringToInt64) {
int64 output;
EXPECT_FALSE(StringToInt64(input_string, &output));
EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt64(input_string.begin(),
+ input_string.end(),
+ &output));
+ EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt64(input, input + arraysize(input), &output));
+ EXPECT_EQ(6, output);
string16 utf16_input = UTF8ToUTF16(input_string);
+ const char16* utf16_chars = utf16_input.c_str();
+ output = 0;
EXPECT_FALSE(StringToInt64(utf16_input, &output));
EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt64(utf16_input.begin(), utf16_input.end(), &output));
+ EXPECT_EQ(6, output);
+ output = 0;
+ EXPECT_FALSE(StringToInt64(utf16_chars,
+ utf16_chars + utf16_input.length(),
+ &output));
+ EXPECT_EQ(6, output);
}
TEST(StringNumberConversionsTest, HexStringToInt) {
@@ -214,6 +292,7 @@ TEST(StringNumberConversionsTest, HexStringToInt) {
{"\t\n\v\f\r 0x45", 0x45, false},
{" 45", 0x45, false},
{"45 ", 0x45, false},
+ {"45:", 0x45, false},
{"efgh", 0xef, false},
{"0xefgh", 0xef, false},
{"hgfe", 0, false},
@@ -223,9 +302,19 @@ TEST(StringNumberConversionsTest, HexStringToInt) {
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- int output;
+ const char* ascii_chars = cases[i].input.c_str();
+ int output = 0;
EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output));
EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input.begin(),
+ cases[i].input.end(),
+ &output));
+ EXPECT_EQ(cases[i].output, output);
+ output = 0;
+ EXPECT_EQ(cases[i].success, HexStringToInt(
+ ascii_chars, ascii_chars + cases[i].input.length(), &output));
+ EXPECT_EQ(cases[i].output, output);
}
// One additional test to verify that conversion of numbers in strings with
// embedded NUL characters. The NUL and extra data after it should be
@@ -235,6 +324,14 @@ TEST(StringNumberConversionsTest, HexStringToInt) {
int output;
EXPECT_FALSE(HexStringToInt(input_string, &output));
EXPECT_EQ(0xc0ffee, output);
+ output = 0;
+ EXPECT_FALSE(HexStringToInt(input_string.begin(),
+ input_string.end(),
+ &output));
+ EXPECT_EQ(0xc0ffee, output);
+ output = 0;
+ EXPECT_FALSE(HexStringToInt(input, input + arraysize(input), &output));
+ EXPECT_EQ(0xc0ffee, output);
}
TEST(StringNumberConversionsTest, HexStringToBytes) {
@@ -295,6 +392,7 @@ TEST(StringNumberConversionsTest, StringToDouble) {
{"9e99999999999999999999", HUGE_VAL, false},
{"-9e99999999999999999999", -HUGE_VAL, false},
{"1e-2", 0.01, true},
+ {"42 ", 42.0, false},
{" 1e-2", 0.01, false},
{"1e-2 ", 0.01, false},
{"-1E-7", -0.0000001, true},