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

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

Issue 1548993002: Switch to standard integer types in base/strings/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 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 | « base/strings/string_number_conversions.cc ('k') | base/strings/string_piece.h » ('j') | 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 "base/strings/string_number_conversions.h" 5 #include "base/strings/string_number_conversions.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <stddef.h>
8 #include <stdint.h> 9 #include <stdint.h>
9 #include <stdio.h> 10 #include <stdio.h>
10 11
11 #include <cmath> 12 #include <cmath>
12 #include <limits> 13 #include <limits>
13 14
14 #include "base/format_macros.h" 15 #include "base/format_macros.h"
16 #include "base/macros.h"
15 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 namespace base { 21 namespace base {
20 22
21 namespace { 23 namespace {
22 24
23 template <typename INT> 25 template <typename INT>
24 struct IntToStringTest { 26 struct IntToStringTest {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 const IntToStringTest<int64_t>* test = &int64_tests[i]; 60 const IntToStringTest<int64_t>* test = &int64_tests[i];
59 EXPECT_EQ(Int64ToString(test->num), test->sexpected); 61 EXPECT_EQ(Int64ToString(test->num), test->sexpected);
60 EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected)); 62 EXPECT_EQ(Int64ToString16(test->num), UTF8ToUTF16(test->sexpected));
61 EXPECT_EQ(Uint64ToString(test->num), test->uexpected); 63 EXPECT_EQ(Uint64ToString(test->num), test->uexpected);
62 EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected)); 64 EXPECT_EQ(Uint64ToString16(test->num), UTF8ToUTF16(test->uexpected));
63 } 65 }
64 } 66 }
65 67
66 TEST(StringNumberConversionsTest, Uint64ToString) { 68 TEST(StringNumberConversionsTest, Uint64ToString) {
67 static const struct { 69 static const struct {
68 uint64 input; 70 uint64_t input;
69 std::string output; 71 std::string output;
70 } cases[] = { 72 } cases[] = {
71 {0, "0"}, 73 {0, "0"},
72 {42, "42"}, 74 {42, "42"},
73 {INT_MAX, "2147483647"}, 75 {INT_MAX, "2147483647"},
74 {std::numeric_limits<uint64_t>::max(), "18446744073709551615"}, 76 {std::numeric_limits<uint64_t>::max(), "18446744073709551615"},
75 }; 77 };
76 78
77 for (size_t i = 0; i < arraysize(cases); ++i) 79 for (size_t i = 0; i < arraysize(cases); ++i)
78 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input)); 80 EXPECT_EQ(cases[i].output, Uint64ToString(cases[i].input));
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 291
290 string16 utf16_input = UTF8ToUTF16(input_string); 292 string16 utf16_input = UTF8ToUTF16(input_string);
291 output = 0; 293 output = 0;
292 EXPECT_FALSE(StringToInt64(utf16_input, &output)); 294 EXPECT_FALSE(StringToInt64(utf16_input, &output));
293 EXPECT_EQ(6, output); 295 EXPECT_EQ(6, output);
294 } 296 }
295 297
296 TEST(StringNumberConversionsTest, StringToUint64) { 298 TEST(StringNumberConversionsTest, StringToUint64) {
297 static const struct { 299 static const struct {
298 std::string input; 300 std::string input;
299 uint64 output; 301 uint64_t output;
300 bool success; 302 bool success;
301 } cases[] = { 303 } cases[] = {
302 {"0", 0, true}, 304 {"0", 0, true},
303 {"42", 42, true}, 305 {"42", 42, true},
304 {"-2147483648", 0, false}, 306 {"-2147483648", 0, false},
305 {"2147483647", INT_MAX, true}, 307 {"2147483647", INT_MAX, true},
306 {"-2147483649", 0, false}, 308 {"-2147483649", 0, false},
307 {"-99999999999", 0, false}, 309 {"-99999999999", 0, false},
308 {"2147483648", UINT64_C(2147483648), true}, 310 {"2147483648", UINT64_C(2147483648), true},
309 {"99999999999", UINT64_C(99999999999), true}, 311 {"99999999999", UINT64_C(99999999999), true},
(...skipping 18 matching lines...) Expand all
328 {"-", 0, false}, 330 {"-", 0, false},
329 {"-9223372036854775809", 0, false}, 331 {"-9223372036854775809", 0, false},
330 {"-99999999999999999999", 0, false}, 332 {"-99999999999999999999", 0, false},
331 {"9223372036854775808", UINT64_C(9223372036854775808), true}, 333 {"9223372036854775808", UINT64_C(9223372036854775808), true},
332 {"99999999999999999999", std::numeric_limits<uint64_t>::max(), false}, 334 {"99999999999999999999", std::numeric_limits<uint64_t>::max(), false},
333 {"18446744073709551615", std::numeric_limits<uint64_t>::max(), true}, 335 {"18446744073709551615", std::numeric_limits<uint64_t>::max(), true},
334 {"18446744073709551616", std::numeric_limits<uint64_t>::max(), false}, 336 {"18446744073709551616", std::numeric_limits<uint64_t>::max(), false},
335 }; 337 };
336 338
337 for (size_t i = 0; i < arraysize(cases); ++i) { 339 for (size_t i = 0; i < arraysize(cases); ++i) {
338 uint64 output = 0; 340 uint64_t output = 0;
339 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output)); 341 EXPECT_EQ(cases[i].success, StringToUint64(cases[i].input, &output));
340 EXPECT_EQ(cases[i].output, output); 342 EXPECT_EQ(cases[i].output, output);
341 343
342 string16 utf16_input = UTF8ToUTF16(cases[i].input); 344 string16 utf16_input = UTF8ToUTF16(cases[i].input);
343 output = 0; 345 output = 0;
344 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output)); 346 EXPECT_EQ(cases[i].success, StringToUint64(utf16_input, &output));
345 EXPECT_EQ(cases[i].output, output); 347 EXPECT_EQ(cases[i].output, output);
346 } 348 }
347 349
348 // One additional test to verify that conversion of numbers in strings with 350 // One additional test to verify that conversion of numbers in strings with
349 // embedded NUL characters. The NUL and extra data after it should be 351 // embedded NUL characters. The NUL and extra data after it should be
350 // interpreted as junk after the number. 352 // interpreted as junk after the number.
351 const char input[] = "6\06"; 353 const char input[] = "6\06";
352 std::string input_string(input, arraysize(input) - 1); 354 std::string input_string(input, arraysize(input) - 1);
353 uint64 output; 355 uint64_t output;
354 EXPECT_FALSE(StringToUint64(input_string, &output)); 356 EXPECT_FALSE(StringToUint64(input_string, &output));
355 EXPECT_EQ(6U, output); 357 EXPECT_EQ(6U, output);
356 358
357 string16 utf16_input = UTF8ToUTF16(input_string); 359 string16 utf16_input = UTF8ToUTF16(input_string);
358 output = 0; 360 output = 0;
359 EXPECT_FALSE(StringToUint64(utf16_input, &output)); 361 EXPECT_FALSE(StringToUint64(utf16_input, &output));
360 EXPECT_EQ(6U, output); 362 EXPECT_EQ(6U, output);
361 } 363 }
362 364
363 TEST(StringNumberConversionsTest, StringToSizeT) { 365 TEST(StringNumberConversionsTest, StringToSizeT) {
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 const char input[] = "0xc0ffee\0" "9"; 603 const char input[] = "0xc0ffee\0" "9";
602 std::string input_string(input, arraysize(input) - 1); 604 std::string input_string(input, arraysize(input) - 1);
603 int64_t output; 605 int64_t output;
604 EXPECT_FALSE(HexStringToInt64(input_string, &output)); 606 EXPECT_FALSE(HexStringToInt64(input_string, &output));
605 EXPECT_EQ(0xc0ffee, output); 607 EXPECT_EQ(0xc0ffee, output);
606 } 608 }
607 609
608 TEST(StringNumberConversionsTest, HexStringToUInt64) { 610 TEST(StringNumberConversionsTest, HexStringToUInt64) {
609 static const struct { 611 static const struct {
610 std::string input; 612 std::string input;
611 uint64 output; 613 uint64_t output;
612 bool success; 614 bool success;
613 } cases[] = { 615 } cases[] = {
614 {"0", 0, true}, 616 {"0", 0, true},
615 {"42", 66, true}, 617 {"42", 66, true},
616 {"-42", 0, false}, 618 {"-42", 0, false},
617 {"+42", 66, true}, 619 {"+42", 66, true},
618 {"40acd88557b", INT64_C(4444444448123), true}, 620 {"40acd88557b", INT64_C(4444444448123), true},
619 {"7fffffff", INT_MAX, true}, 621 {"7fffffff", INT_MAX, true},
620 {"-80000000", 0, false}, 622 {"-80000000", 0, false},
621 {"ffffffff", 0xffffffff, true}, 623 {"ffffffff", 0xffffffff, true},
(...skipping 25 matching lines...) Expand all
647 {"45:", 0x45, false}, 649 {"45:", 0x45, false},
648 {"efgh", 0xef, false}, 650 {"efgh", 0xef, false},
649 {"0xefgh", 0xef, false}, 651 {"0xefgh", 0xef, false},
650 {"hgfe", 0, false}, 652 {"hgfe", 0, false},
651 {"-", 0, false}, 653 {"-", 0, false},
652 {"", 0, false}, 654 {"", 0, false},
653 {"0x", 0, false}, 655 {"0x", 0, false},
654 }; 656 };
655 657
656 for (size_t i = 0; i < arraysize(cases); ++i) { 658 for (size_t i = 0; i < arraysize(cases); ++i) {
657 uint64 output = 0; 659 uint64_t output = 0;
658 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output)); 660 EXPECT_EQ(cases[i].success, HexStringToUInt64(cases[i].input, &output));
659 EXPECT_EQ(cases[i].output, output); 661 EXPECT_EQ(cases[i].output, output);
660 } 662 }
661 // One additional test to verify that conversion of numbers in strings with 663 // One additional test to verify that conversion of numbers in strings with
662 // embedded NUL characters. The NUL and extra data after it should be 664 // embedded NUL characters. The NUL and extra data after it should be
663 // interpreted as junk after the number. 665 // interpreted as junk after the number.
664 const char input[] = "0xc0ffee\0" "9"; 666 const char input[] = "0xc0ffee\0" "9";
665 std::string input_string(input, arraysize(input) - 1); 667 std::string input_string(input, arraysize(input) - 1);
666 uint64 output; 668 uint64_t output;
667 EXPECT_FALSE(HexStringToUInt64(input_string, &output)); 669 EXPECT_FALSE(HexStringToUInt64(input_string, &output));
668 EXPECT_EQ(0xc0ffeeU, output); 670 EXPECT_EQ(0xc0ffeeU, output);
669 } 671 }
670 672
671 TEST(StringNumberConversionsTest, HexStringToBytes) { 673 TEST(StringNumberConversionsTest, HexStringToBytes) {
672 static const struct { 674 static const struct {
673 const std::string input; 675 const std::string input;
674 const char* output; 676 const char* output;
675 size_t output_len; 677 size_t output_len;
676 bool success; 678 bool success;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 797
796 TEST(StringNumberConversionsTest, HexEncode) { 798 TEST(StringNumberConversionsTest, HexEncode) {
797 std::string hex(HexEncode(NULL, 0)); 799 std::string hex(HexEncode(NULL, 0));
798 EXPECT_EQ(hex.length(), 0U); 800 EXPECT_EQ(hex.length(), 0U);
799 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81}; 801 unsigned char bytes[] = {0x01, 0xff, 0x02, 0xfe, 0x03, 0x80, 0x81};
800 hex = HexEncode(bytes, sizeof(bytes)); 802 hex = HexEncode(bytes, sizeof(bytes));
801 EXPECT_EQ(hex.compare("01FF02FE038081"), 0); 803 EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
802 } 804 }
803 805
804 } // namespace base 806 } // namespace base
OLDNEW
« no previous file with comments | « base/strings/string_number_conversions.cc ('k') | base/strings/string_piece.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698