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

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: remove std::string -> char * -> std::string roundtrip 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/stringprintf.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 std::string size_t_max_string = StringPrintf("%" PRIuS, size_t_max);
343
344 static const struct {
345 std::string input;
346 size_t output;
347 bool success;
348 } cases[] = {
349 {"0", 0, true},
350 {"42", 42, true},
351 {"-2147483648", 0, false},
352 {"2147483647", INT_MAX, true},
353 {"-2147483649", 0, false},
354 {"-99999999999", 0, false},
355 {"2147483648", 2147483648U, true},
356 #if SIZE_MAX > 4294967295U
357 {"99999999999", 99999999999U, true},
358 #endif
359 {"-9223372036854775808", 0, false},
360 {"09", 9, true},
361 {"-09", 0, false},
362 {"", 0, false},
363 {" 42", 42, false},
364 {"42 ", 42, false},
365 {"0x42", 0, false},
366 {"\t\n\v\f\r 42", 42, false},
367 {"blah42", 0, false},
368 {"42blah", 42, false},
369 {"blah42blah", 0, false},
370 {"-273.15", 0, false},
371 {"+98.6", 98, false},
372 {"--123", 0, false},
373 {"++123", 0, false},
374 {"-+123", 0, false},
375 {"+-123", 0, false},
376 {"-", 0, false},
377 {"-9223372036854775809", 0, false},
378 {"-99999999999999999999", 0, false},
379 {"999999999999999999999999", size_t_max, false},
380 {size_t_max_string, size_t_max, true},
381 };
382
383 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
384 size_t output = 0;
385 EXPECT_EQ(cases[i].success, StringToSizeT(cases[i].input, &output));
386 EXPECT_EQ(cases[i].output, output);
387
388 string16 utf16_input = UTF8ToUTF16(cases[i].input);
389 output = 0;
390 EXPECT_EQ(cases[i].success, StringToSizeT(utf16_input, &output));
391 EXPECT_EQ(cases[i].output, output);
392 }
393
394 // One additional test to verify that conversion of numbers in strings with
395 // embedded NUL characters. The NUL and extra data after it should be
396 // interpreted as junk after the number.
397 const char input[] = "6\06";
398 std::string input_string(input, arraysize(input) - 1);
399 size_t output;
400 EXPECT_FALSE(StringToSizeT(input_string, &output));
401 EXPECT_EQ(6U, output);
402
403 string16 utf16_input = UTF8ToUTF16(input_string);
404 output = 0;
405 EXPECT_FALSE(StringToSizeT(utf16_input, &output));
406 EXPECT_EQ(6U, output);
407 }
408
204 TEST(StringNumberConversionsTest, HexStringToInt) { 409 TEST(StringNumberConversionsTest, HexStringToInt) {
205 static const struct { 410 static const struct {
206 std::string input; 411 std::string input;
207 int64 output; 412 int64 output;
208 bool success; 413 bool success;
209 } cases[] = { 414 } cases[] = {
210 {"0", 0, true}, 415 {"0", 0, true},
211 {"42", 66, true}, 416 {"42", 66, true},
212 {"-42", -66, true}, 417 {"-42", -66, true},
213 {"+42", 66, true}, 418 {"+42", 66, true},
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 704
500 TEST(StringNumberConversionsTest, HexEncode) { 705 TEST(StringNumberConversionsTest, HexEncode) {
501 std::string hex(HexEncode(NULL, 0)); 706 std::string hex(HexEncode(NULL, 0));
502 EXPECT_EQ(hex.length(), 0U); 707 EXPECT_EQ(hex.length(), 0U);
503 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; 708 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
504 hex = HexEncode(bytes, sizeof(bytes)); 709 hex = HexEncode(bytes, sizeof(bytes));
505 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); 710 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
506 } 711 }
507 712
508 } // namespace base 713 } // 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