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

Side by Side Diff: base/string_number_conversions_unittest.cc

Issue 8921006: Standardize StringToInt{,64} interface. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Updated StringPiece constructor interface to use iterators. Created 9 years 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <math.h> 5 #include <math.h>
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 {"-+123", 0, false}, 98 {"-+123", 0, false},
99 {"+-123", 0, false}, 99 {"+-123", 0, false},
100 {"-", 0, false}, 100 {"-", 0, false},
101 {"-2147483649", INT_MIN, false}, 101 {"-2147483649", INT_MIN, false},
102 {"-99999999999", INT_MIN, false}, 102 {"-99999999999", INT_MIN, false},
103 {"2147483648", INT_MAX, false}, 103 {"2147483648", INT_MAX, false},
104 {"99999999999", INT_MAX, false}, 104 {"99999999999", INT_MAX, false},
105 }; 105 };
106 106
107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { 107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
108 const char* ascii_chars = cases[i].input.c_str();
109 int output = 0; 108 int output = 0;
110 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); 109 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output));
111 EXPECT_EQ(cases[i].output, output); 110 EXPECT_EQ(cases[i].output, output);
112 output = 0;
113 EXPECT_EQ(cases[i].success, StringToInt(cases[i].input.begin(),
114 cases[i].input.end(),
115 &output));
116 EXPECT_EQ(cases[i].output, output);
117 output = 0;
118 EXPECT_EQ(cases[i].success, StringToInt(
119 ascii_chars, ascii_chars + cases[i].input.length(), &output));
120 EXPECT_EQ(cases[i].output, output);
121 111
122 string16 utf16_input = UTF8ToUTF16(cases[i].input); 112 string16 utf16_input = UTF8ToUTF16(cases[i].input);
123 const char16* utf16_chars = utf16_input.c_str();
124 output = 0; 113 output = 0;
125 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); 114 EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output));
126 EXPECT_EQ(cases[i].output, output); 115 EXPECT_EQ(cases[i].output, output);
127 output = 0;
128 EXPECT_EQ(cases[i].success, StringToInt(utf16_input.begin(),
129 utf16_input.end(),
130 &output));
131 EXPECT_EQ(cases[i].output, output);
132 output = 0;
133 EXPECT_EQ(cases[i].success, StringToInt(
134 utf16_chars, utf16_chars + utf16_input.length(), &output));
135 EXPECT_EQ(cases[i].output, output);
136 } 116 }
137 117
138 // One additional test to verify that conversion of numbers in strings with 118 // One additional test to verify that conversion of numbers in strings with
139 // embedded NUL characters. The NUL and extra data after it should be 119 // embedded NUL characters. The NUL and extra data after it should be
140 // interpreted as junk after the number. 120 // interpreted as junk after the number.
141 const char input[] = "6\06"; 121 const char input[] = "6\06";
142 std::string input_string(input, arraysize(input) - 1); 122 std::string input_string(input, arraysize(input) - 1);
143 int output; 123 int output;
144 EXPECT_FALSE(StringToInt(input_string, &output)); 124 EXPECT_FALSE(StringToInt(input_string, &output));
145 EXPECT_EQ(6, output); 125 EXPECT_EQ(6, output);
146 output = 0;
147 EXPECT_FALSE(StringToInt(input_string.begin(), input_string.end(), &output));
148 EXPECT_EQ(6, output);
149 output = 0;
150 EXPECT_FALSE(StringToInt(input, input + arraysize(input), &output));
151 EXPECT_EQ(6, output);
152 126
153 string16 utf16_input = UTF8ToUTF16(input_string); 127 string16 utf16_input = UTF8ToUTF16(input_string);
154 const char16* utf16_chars = utf16_input.c_str();
155 output = 0; 128 output = 0;
156 EXPECT_FALSE(StringToInt(utf16_input, &output)); 129 EXPECT_FALSE(StringToInt(utf16_input, &output));
157 EXPECT_EQ(6, output); 130 EXPECT_EQ(6, output);
158 output = 0;
159 EXPECT_FALSE(StringToInt(utf16_input.begin(), utf16_input.end(), &output));
160 EXPECT_EQ(6, output);
161 output = 0;
162 EXPECT_FALSE(StringToInt(utf16_chars,
163 utf16_chars + utf16_input.length(),
164 &output));
165 EXPECT_EQ(6, output);
166 131
167 output = 0; 132 output = 0;
168 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; 133 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0};
169 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); 134 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output));
170 EXPECT_EQ(0, output); 135 EXPECT_EQ(0, output);
171 } 136 }
172 137
173 TEST(StringNumberConversionsTest, StringToInt64) { 138 TEST(StringNumberConversionsTest, StringToInt64) {
174 static const struct { 139 static const struct {
175 std::string input; 140 std::string input;
(...skipping 27 matching lines...) Expand all
203 {"-+123", 0, false}, 168 {"-+123", 0, false},
204 {"+-123", 0, false}, 169 {"+-123", 0, false},
205 {"-", 0, false}, 170 {"-", 0, false},
206 {"-9223372036854775809", kint64min, false}, 171 {"-9223372036854775809", kint64min, false},
207 {"-99999999999999999999", kint64min, false}, 172 {"-99999999999999999999", kint64min, false},
208 {"9223372036854775808", kint64max, false}, 173 {"9223372036854775808", kint64max, false},
209 {"99999999999999999999", kint64max, false}, 174 {"99999999999999999999", kint64max, false},
210 }; 175 };
211 176
212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { 177 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
213 const char* ascii_chars = cases[i].input.c_str();
214 int64 output = 0; 178 int64 output = 0;
215 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); 179 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output));
216 EXPECT_EQ(cases[i].output, output); 180 EXPECT_EQ(cases[i].output, output);
217 output = 0;
218 EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input.begin(),
219 cases[i].input.end(),
220 &output));
221 EXPECT_EQ(cases[i].output, output);
222 output = 0;
223 EXPECT_EQ(cases[i].success, StringToInt64(
224 ascii_chars, ascii_chars + cases[i].input.length(), &output));
225 EXPECT_EQ(cases[i].output, output);
226 181
227 string16 utf16_input = UTF8ToUTF16(cases[i].input); 182 string16 utf16_input = UTF8ToUTF16(cases[i].input);
228 const char16* utf16_chars = utf16_input.c_str();
229 output = 0; 183 output = 0;
230 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); 184 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output));
231 EXPECT_EQ(cases[i].output, output); 185 EXPECT_EQ(cases[i].output, output);
232 output = 0;
233 EXPECT_EQ(cases[i].success, StringToInt64(utf16_input.begin(),
234 utf16_input.end(),
235 &output));
236 EXPECT_EQ(cases[i].output, output);
237 output = 0;
238 EXPECT_EQ(cases[i].success, StringToInt64(
239 utf16_chars, utf16_chars + utf16_input.length(), &output));
240 EXPECT_EQ(cases[i].output, output);
241 } 186 }
242 187
243 // One additional test to verify that conversion of numbers in strings with 188 // One additional test to verify that conversion of numbers in strings with
244 // embedded NUL characters. The NUL and extra data after it should be 189 // embedded NUL characters. The NUL and extra data after it should be
245 // interpreted as junk after the number. 190 // interpreted as junk after the number.
246 const char input[] = "6\06"; 191 const char input[] = "6\06";
247 std::string input_string(input, arraysize(input) - 1); 192 std::string input_string(input, arraysize(input) - 1);
248 int64 output; 193 int64 output;
249 EXPECT_FALSE(StringToInt64(input_string, &output)); 194 EXPECT_FALSE(StringToInt64(input_string, &output));
250 EXPECT_EQ(6, output); 195 EXPECT_EQ(6, output);
251 output = 0;
252 EXPECT_FALSE(StringToInt64(input_string.begin(),
253 input_string.end(),
254 &output));
255 EXPECT_EQ(6, output);
256 output = 0;
257 EXPECT_FALSE(StringToInt64(input, input + arraysize(input), &output));
258 EXPECT_EQ(6, output);
259 196
260 string16 utf16_input = UTF8ToUTF16(input_string); 197 string16 utf16_input = UTF8ToUTF16(input_string);
261 const char16* utf16_chars = utf16_input.c_str();
262 output = 0; 198 output = 0;
263 EXPECT_FALSE(StringToInt64(utf16_input, &output)); 199 EXPECT_FALSE(StringToInt64(utf16_input, &output));
264 EXPECT_EQ(6, output); 200 EXPECT_EQ(6, output);
265 output = 0;
266 EXPECT_FALSE(StringToInt64(utf16_input.begin(), utf16_input.end(), &output));
267 EXPECT_EQ(6, output);
268 output = 0;
269 EXPECT_FALSE(StringToInt64(utf16_chars,
270 utf16_chars + utf16_input.length(),
271 &output));
272 EXPECT_EQ(6, output);
273 } 201 }
274 202
275 TEST(StringNumberConversionsTest, HexStringToInt) { 203 TEST(StringNumberConversionsTest, HexStringToInt) {
276 static const struct { 204 static const struct {
277 std::string input; 205 std::string input;
278 int output; 206 int output;
279 bool success; 207 bool success;
280 } cases[] = { 208 } cases[] = {
281 {"0", 0, true}, 209 {"0", 0, true},
282 {"42", 66, true}, 210 {"42", 66, true},
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 364
437 TEST(StringNumberConversionsTest, HexEncode) { 365 TEST(StringNumberConversionsTest, HexEncode) {
438 std::string hex(HexEncode(NULL, 0)); 366 std::string hex(HexEncode(NULL, 0));
439 EXPECT_EQ(hex.length(), 0U); 367 EXPECT_EQ(hex.length(), 0U);
440 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; 368 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
441 hex = HexEncode(bytes, sizeof(bytes)); 369 hex = HexEncode(bytes, sizeof(bytes));
442 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); 370 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
443 } 371 }
444 372
445 } // namespace base 373 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698