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

Side by Side Diff: base/strings/string_number_conversions_unittest.cc

Issue 15521004: add more string -> unsigned number conversion unit tests (attempt 2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use base/format_macros.h for a cross-platform size_t printf specifier Created 7 years, 6 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <errno.h> 5 #include <errno.h>
6 #include <math.h> 6 #include <stdint.h>
7 #include <stdio.h>
7 8
9 #include <cmath>
8 #include <limits> 10 #include <limits>
9 11
12 #include "base/format_macros.h"
10 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
13 17
14 namespace base { 18 namespace base {
15 19
16 namespace { 20 namespace {
17 21
18 template <typename INT> 22 template <typename INT>
19 struct IntToStringTest { 23 struct IntToStringTest {
20 INT num; 24 INT num;
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 output = 0; 133 output = 0;
130 EXPECT_FALSE(StringToInt(utf16_input, &output)); 134 EXPECT_FALSE(StringToInt(utf16_input, &output));
131 EXPECT_EQ(6, output); 135 EXPECT_EQ(6, output);
132 136
133 output = 0; 137 output = 0;
134 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; 138 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0};
135 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output)); 139 EXPECT_FALSE(StringToInt(string16(negative_wide_input), &output));
136 EXPECT_EQ(0, output); 140 EXPECT_EQ(0, output);
137 } 141 }
138 142
143 TEST(StringNumberConversionsTest, StringToUint) {
144 static const struct {
145 std::string input;
146 unsigned output;
147 bool success;
148 } cases[] = {
149 {"0", 0, true},
150 {"42", 42, true},
151 {"42\x99", 42, false},
152 {"\x99" "42\x99", 0, false},
153 {"-2147483648", 0, false},
154 {"2147483647", INT_MAX, true},
155 {"", 0, false},
156 {" 42", 42, false},
157 {"42 ", 42, false},
158 {"\t\n\v\f\r 42", 42, false},
159 {"blah42", 0, false},
160 {"42blah", 42, false},
161 {"blah42blah", 0, false},
162 {"-273.15", 0, false},
163 {"+98.6", 98, false},
164 {"--123", 0, false},
165 {"++123", 0, false},
166 {"-+123", 0, false},
167 {"+-123", 0, false},
168 {"-", 0, false},
169 {"-2147483649", 0, false},
170 {"-99999999999", 0, false},
171 {"4294967295", UINT_MAX, true},
172 {"4294967296", UINT_MAX, false},
173 {"99999999999", UINT_MAX, false},
174 };
175
176 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
177 unsigned output = 0;
178 EXPECT_EQ(cases[i].success, StringToUint(cases[i].input, &output));
179 EXPECT_EQ(cases[i].output, output);
180
181 string16 utf16_input = UTF8ToUTF16(cases[i].input);
182 output = 0;
183 EXPECT_EQ(cases[i].success, StringToUint(utf16_input, &output));
184 EXPECT_EQ(cases[i].output, output);
185 }
186
187 // One additional test to verify that conversion of numbers in strings with
188 // embedded NUL characters. The NUL and extra data after it should be
189 // interpreted as junk after the number.
190 const char input[] = "6\06";
191 std::string input_string(input, arraysize(input) - 1);
192 unsigned output;
193 EXPECT_FALSE(StringToUint(input_string, &output));
194 EXPECT_EQ(6U, output);
195
196 string16 utf16_input = UTF8ToUTF16(input_string);
197 output = 0;
198 EXPECT_FALSE(StringToUint(utf16_input, &output));
199 EXPECT_EQ(6U, output);
200
201 output = 0;
202 const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0};
203 EXPECT_FALSE(StringToUint(string16(negative_wide_input), &output));
204 EXPECT_EQ(0U, output);
205 }
206
139 TEST(StringNumberConversionsTest, StringToInt64) { 207 TEST(StringNumberConversionsTest, StringToInt64) {
140 static const struct { 208 static const struct {
141 std::string input; 209 std::string input;
142 int64 output; 210 int64 output;
143 bool success; 211 bool success;
144 } cases[] = { 212 } cases[] = {
145 {"0", 0, true}, 213 {"0", 0, true},
146 {"42", 42, true}, 214 {"42", 42, true},
147 {"-2147483648", INT_MIN, true}, 215 {"-2147483648", INT_MIN, true},
148 {"2147483647", INT_MAX, true}, 216 {"2147483647", INT_MAX, true},
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 int64 output; 262 int64 output;
195 EXPECT_FALSE(StringToInt64(input_string, &output)); 263 EXPECT_FALSE(StringToInt64(input_string, &output));
196 EXPECT_EQ(6, output); 264 EXPECT_EQ(6, output);
197 265
198 string16 utf16_input = UTF8ToUTF16(input_string); 266 string16 utf16_input = UTF8ToUTF16(input_string);
199 output = 0; 267 output = 0;
200 EXPECT_FALSE(StringToInt64(utf16_input, &output)); 268 EXPECT_FALSE(StringToInt64(utf16_input, &output));
201 EXPECT_EQ(6, output); 269 EXPECT_EQ(6, output);
202 } 270 }
203 271
272 TEST(StringNumberConversionsTest, StringToUint64) {
273 static const struct {
274 std::string input;
275 uint64 output;
276 bool success;
277 } cases[] = {
278 {"0", 0, true},
279 {"42", 42, true},
280 {"-2147483648", 0, false},
281 {"2147483647", INT_MAX, true},
282 {"-2147483649", 0, false},
283 {"-99999999999", 0, false},
284 {"2147483648", GG_UINT64_C(2147483648), true},
285 {"99999999999", GG_UINT64_C(99999999999), true},
286 {"9223372036854775807", kint64max, true},
287 {"-9223372036854775808", 0, false},
288 {"09", 9, true},
289 {"-09", 0, false},
290 {"", 0, false},
291 {" 42", 42, false},
292 {"42 ", 42, false},
293 {"0x42", 0, false},
294 {"\t\n\v\f\r 42", 42, false},
295 {"blah42", 0, false},
296 {"42blah", 42, false},
297 {"blah42blah", 0, false},
298 {"-273.15", 0, false},
299 {"+98.6", 98, false},
300 {"--123", 0, false},
301 {"++123", 0, false},
302 {"-+123", 0, false},
303 {"+-123", 0, false},
304 {"-", 0, false},
305 {"-9223372036854775809", 0, false},
306 {"-99999999999999999999", 0, false},
307 {"9223372036854775808", GG_UINT64_C(9223372036854775808), true},
308 {"99999999999999999999", kuint64max, false},
309 {"18446744073709551615", kuint64max, true},
310 {"18446744073709551616", kuint64max, false},
311 };
312
313 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
314 uint64 output = 0;
315 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output));
316 EXPECT_EQ(cases[i].output, output);
317
318 string16 utf16_input = UTF8ToUTF16(cases[i].input);
319 output = 0;
320 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output));
321 EXPECT_EQ(cases[i].output, output);
322 }
323
324 // One additional test to verify that conversion of numbers in strings with
325 // embedded NUL characters. The NUL and extra data after it should be
326 // interpreted as junk after the number.
327 const char input[] = "6\06";
328 std::string input_string(input, arraysize(input) - 1);
329 uint64 output;
330 EXPECT_FALSE(StringToUint64(input_string, &output));
331 EXPECT_EQ(6U, output);
332
333 string16 utf16_input = UTF8ToUTF16(input_string);
334 output = 0;
335 EXPECT_FALSE(StringToUint64(utf16_input, &output));
336 EXPECT_EQ(6U, output);
337 }
338
339 TEST(StringNumberConversionsTest, StringToSizeT) {
340
341 size_t size_t_max = std::numeric_limits<size_t>::max();
342 size_t digits = static_cast<size_t>(ceil(std::log10(
343 static_cast<double>(size_t_max))));
344 const unsigned int cstr_len = digits + 1;
345 char *size_t_max_cstr = new char[cstr_len];
346 base::snprintf(size_t_max_cstr, cstr_len, "%" PRIuS, size_t_max);
jar (doing other things) 2013/06/12 18:24:27 There is a method for sprint'ing into a std::strin
Mostyn Bramley-Moore 2013/06/12 20:29:16 Done. This also allows us to drop the string size
347
348 static const struct {
349 std::string input;
350 size_t output;
351 bool success;
352 } cases[] = {
353 {"0", 0, true},
354 {"42", 42, true},
355 {"-2147483648", 0, false},
356 {"2147483647", INT_MAX, true},
357 {"-2147483649", 0, false},
358 {"-99999999999", 0, false},
359 {"2147483648", 2147483648U, true},
360 #if SIZE_MAX > 4294967295U
361 {"99999999999", 99999999999U, true},
362 #endif
363 {"-9223372036854775808", 0, false},
364 {"09", 9, true},
365 {"-09", 0, false},
366 {"", 0, false},
367 {" 42", 42, false},
368 {"42 ", 42, false},
369 {"0x42", 0, false},
370 {"\t\n\v\f\r 42", 42, false},
371 {"blah42", 0, false},
372 {"42blah", 42, false},
373 {"blah42blah", 0, false},
374 {"-273.15", 0, false},
375 {"+98.6", 98, false},
376 {"--123", 0, false},
377 {"++123", 0, false},
378 {"-+123", 0, false},
379 {"+-123", 0, false},
380 {"-", 0, false},
381 {"-9223372036854775809", 0, false},
382 {"-99999999999999999999", 0, false},
383 {"999999999999999999999999", size_t_max, false},
384 {size_t_max_cstr, size_t_max, true},
385 };
386
387 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
388 size_t output = 0;
389 EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output));
390 EXPECT_EQ(cases[i].output, output);
391
392 string16 utf16_input = UTF8ToUTF16(cases[i].input);
393 output = 0;
394 EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output));
395 EXPECT_EQ(cases[i].output, output);
396 }
397
398 delete[] size_t_max_cstr;
399
400 // One additional test to verify that conversion of numbers in strings with
401 // embedded NUL characters. The NUL and extra data after it should be
402 // interpreted as junk after the number.
403 const char input[] = "6\06";
404 std::string input_string(input, arraysize(input) - 1);
405 size_t output;
406 EXPECT_FALSE(StringToSizeT(input_string, &output));
407 EXPECT_EQ(6U, output);
408
409 string16 utf16_input = UTF8ToUTF16(input_string);
410 output = 0;
411 EXPECT_FALSE(StringToSizeT(utf16_input, &output));
412 EXPECT_EQ(6U, output);
413 }
414
204 TEST(StringNumberConversionsTest, HexStringToInt) { 415 TEST(StringNumberConversionsTest, HexStringToInt) {
205 static const struct { 416 static const struct {
206 std::string input; 417 std::string input;
207 int64 output; 418 int64 output;
208 bool success; 419 bool success;
209 } cases[] = { 420 } cases[] = {
210 {"0", 0, true}, 421 {"0", 0, true},
211 {"42", 66, true}, 422 {"42", 66, true},
212 {"-42", -66, true}, 423 {"-42", -66, true},
213 {"+42", 66, true}, 424 {"+42", 66, true},
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 710
500 TEST(StringNumberConversionsTest, HexEncode) { 711 TEST(StringNumberConversionsTest, HexEncode) {
501 std::string hex(HexEncode(NULL, 0)); 712 std::string hex(HexEncode(NULL, 0));
502 EXPECT_EQ(hex.length(), 0U); 713 EXPECT_EQ(hex.length(), 0U);
503 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; 714 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
504 hex = HexEncode(bytes, sizeof(bytes)); 715 hex = HexEncode(bytes, sizeof(bytes));
505 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); 716 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
506 } 717 }
507 718
508 } // namespace base 719 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698