OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_util.h" | 5 #include "base/strings/string_util.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 TEST(StringUtilTest, LowerCaseEqualsASCII) { | 532 TEST(StringUtilTest, LowerCaseEqualsASCII) { |
533 static const struct { | 533 static const struct { |
534 const char* src_a; | 534 const char* src_a; |
535 const char* dst; | 535 const char* dst; |
536 } lowercase_cases[] = { | 536 } lowercase_cases[] = { |
537 { "FoO", "foo" }, | 537 { "FoO", "foo" }, |
538 { "foo", "foo" }, | 538 { "foo", "foo" }, |
539 { "FOO", "foo" }, | 539 { "FOO", "foo" }, |
540 }; | 540 }; |
541 | 541 |
542 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(lowercase_cases); ++i) { | 542 for (size_t i = 0; i < arraysize(lowercase_cases); ++i) { |
543 EXPECT_TRUE(LowerCaseEqualsASCII(ASCIIToUTF16(lowercase_cases[i].src_a), | 543 EXPECT_TRUE(LowerCaseEqualsASCII(ASCIIToUTF16(lowercase_cases[i].src_a), |
544 lowercase_cases[i].dst)); | 544 lowercase_cases[i].dst)); |
545 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_a, | 545 EXPECT_TRUE(LowerCaseEqualsASCII(lowercase_cases[i].src_a, |
546 lowercase_cases[i].dst)); | 546 lowercase_cases[i].dst)); |
547 } | 547 } |
548 } | 548 } |
549 | 549 |
550 TEST(StringUtilTest, FormatBytesUnlocalized) { | 550 TEST(StringUtilTest, FormatBytesUnlocalized) { |
551 static const struct { | 551 static const struct { |
552 int64 bytes; | 552 int64 bytes; |
(...skipping 16 matching lines...) Expand all Loading... |
569 {~(1LL<<63), "8192 PB"}, | 569 {~(1LL<<63), "8192 PB"}, |
570 | 570 |
571 {99*1024 + 103, "99.1 kB"}, | 571 {99*1024 + 103, "99.1 kB"}, |
572 {1024*1024 + 103, "1.0 MB"}, | 572 {1024*1024 + 103, "1.0 MB"}, |
573 {1024*1024 + 205 * 1024, "1.2 MB"}, | 573 {1024*1024 + 205 * 1024, "1.2 MB"}, |
574 {1024*1024*1024 + (927 * 1024*1024), "1.9 GB"}, | 574 {1024*1024*1024 + (927 * 1024*1024), "1.9 GB"}, |
575 {10LL*1024*1024*1024, "10.0 GB"}, | 575 {10LL*1024*1024*1024, "10.0 GB"}, |
576 {100LL*1024*1024*1024, "100 GB"}, | 576 {100LL*1024*1024*1024, "100 GB"}, |
577 }; | 577 }; |
578 | 578 |
579 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 579 for (size_t i = 0; i < arraysize(cases); ++i) { |
580 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), | 580 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), |
581 FormatBytesUnlocalized(cases[i].bytes)); | 581 FormatBytesUnlocalized(cases[i].bytes)); |
582 } | 582 } |
583 } | 583 } |
584 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { | 584 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { |
585 static const struct { | 585 static const struct { |
586 const char* str; | 586 const char* str; |
587 string16::size_type start_offset; | 587 string16::size_type start_offset; |
588 const char* find_this; | 588 const char* find_this; |
589 const char* replace_with; | 589 const char* replace_with; |
590 const char* expected; | 590 const char* expected; |
591 } cases[] = { | 591 } cases[] = { |
592 {"aaa", 0, "a", "b", "bbb"}, | 592 {"aaa", 0, "a", "b", "bbb"}, |
593 {"abb", 0, "ab", "a", "ab"}, | 593 {"abb", 0, "ab", "a", "ab"}, |
594 {"Removing some substrings inging", 0, "ing", "", "Remov some substrs "}, | 594 {"Removing some substrings inging", 0, "ing", "", "Remov some substrs "}, |
595 {"Not found", 0, "x", "0", "Not found"}, | 595 {"Not found", 0, "x", "0", "Not found"}, |
596 {"Not found again", 5, "x", "0", "Not found again"}, | 596 {"Not found again", 5, "x", "0", "Not found again"}, |
597 {" Making it much longer ", 0, " ", "Four score and seven years ago", | 597 {" Making it much longer ", 0, " ", "Four score and seven years ago", |
598 "Four score and seven years agoMakingFour score and seven years agoit" | 598 "Four score and seven years agoMakingFour score and seven years agoit" |
599 "Four score and seven years agomuchFour score and seven years agolonger" | 599 "Four score and seven years agomuchFour score and seven years agolonger" |
600 "Four score and seven years ago"}, | 600 "Four score and seven years ago"}, |
601 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"}, | 601 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"}, |
602 {"Replace me only me once", 9, "me ", "", "Replace me only once"}, | 602 {"Replace me only me once", 9, "me ", "", "Replace me only once"}, |
603 {"abababab", 2, "ab", "c", "abccc"}, | 603 {"abababab", 2, "ab", "c", "abccc"}, |
604 }; | 604 }; |
605 | 605 |
606 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | 606 for (size_t i = 0; i < arraysize(cases); i++) { |
607 string16 str = ASCIIToUTF16(cases[i].str); | 607 string16 str = ASCIIToUTF16(cases[i].str); |
608 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset, | 608 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset, |
609 ASCIIToUTF16(cases[i].find_this), | 609 ASCIIToUTF16(cases[i].find_this), |
610 ASCIIToUTF16(cases[i].replace_with)); | 610 ASCIIToUTF16(cases[i].replace_with)); |
611 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); | 611 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); |
612 } | 612 } |
613 } | 613 } |
614 | 614 |
615 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { | 615 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { |
616 static const struct { | 616 static const struct { |
617 const char* str; | 617 const char* str; |
618 string16::size_type start_offset; | 618 string16::size_type start_offset; |
619 const char* find_this; | 619 const char* find_this; |
620 const char* replace_with; | 620 const char* replace_with; |
621 const char* expected; | 621 const char* expected; |
622 } cases[] = { | 622 } cases[] = { |
623 {"aaa", 0, "a", "b", "baa"}, | 623 {"aaa", 0, "a", "b", "baa"}, |
624 {"abb", 0, "ab", "a", "ab"}, | 624 {"abb", 0, "ab", "a", "ab"}, |
625 {"Removing some substrings inging", 0, "ing", "", | 625 {"Removing some substrings inging", 0, "ing", "", |
626 "Remov some substrings inging"}, | 626 "Remov some substrings inging"}, |
627 {"Not found", 0, "x", "0", "Not found"}, | 627 {"Not found", 0, "x", "0", "Not found"}, |
628 {"Not found again", 5, "x", "0", "Not found again"}, | 628 {"Not found again", 5, "x", "0", "Not found again"}, |
629 {" Making it much longer ", 0, " ", "Four score and seven years ago", | 629 {" Making it much longer ", 0, " ", "Four score and seven years ago", |
630 "Four score and seven years agoMaking it much longer "}, | 630 "Four score and seven years agoMaking it much longer "}, |
631 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"}, | 631 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"}, |
632 {"Replace me only me once", 4, "me ", "", "Replace only me once"}, | 632 {"Replace me only me once", 4, "me ", "", "Replace only me once"}, |
633 {"abababab", 2, "ab", "c", "abcabab"}, | 633 {"abababab", 2, "ab", "c", "abcabab"}, |
634 }; | 634 }; |
635 | 635 |
636 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | 636 for (size_t i = 0; i < arraysize(cases); i++) { |
637 string16 str = ASCIIToUTF16(cases[i].str); | 637 string16 str = ASCIIToUTF16(cases[i].str); |
638 ReplaceFirstSubstringAfterOffset(&str, cases[i].start_offset, | 638 ReplaceFirstSubstringAfterOffset(&str, cases[i].start_offset, |
639 ASCIIToUTF16(cases[i].find_this), | 639 ASCIIToUTF16(cases[i].find_this), |
640 ASCIIToUTF16(cases[i].replace_with)); | 640 ASCIIToUTF16(cases[i].replace_with)); |
641 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); | 641 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); |
642 } | 642 } |
643 } | 643 } |
644 | 644 |
645 TEST(StringUtilTest, HexDigitToInt) { | 645 TEST(StringUtilTest, HexDigitToInt) { |
646 EXPECT_EQ(0, HexDigitToInt('0')); | 646 EXPECT_EQ(0, HexDigitToInt('0')); |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 { L"%f", true }, | 1141 { L"%f", true }, |
1142 { L"%f %F", false }, | 1142 { L"%f %F", false }, |
1143 { L"%d %D", false }, | 1143 { L"%d %D", false }, |
1144 { L"%o %O", false }, | 1144 { L"%o %O", false }, |
1145 { L"%u %U", false }, | 1145 { L"%u %U", false }, |
1146 { L"%f %d %o %u", true }, | 1146 { L"%f %d %o %u", true }, |
1147 { L"%-8d (%02.1f%)", true }, | 1147 { L"%-8d (%02.1f%)", true }, |
1148 { L"% 10s", false }, | 1148 { L"% 10s", false }, |
1149 { L"% 10ls", true } | 1149 { L"% 10ls", true } |
1150 }; | 1150 }; |
1151 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) | 1151 for (size_t i = 0; i < arraysize(cases); ++i) |
1152 EXPECT_EQ(cases[i].portable, base::IsWprintfFormatPortable(cases[i].input)); | 1152 EXPECT_EQ(cases[i].portable, base::IsWprintfFormatPortable(cases[i].input)); |
1153 } | 1153 } |
1154 | 1154 |
1155 TEST(StringUtilTest, RemoveChars) { | 1155 TEST(StringUtilTest, RemoveChars) { |
1156 const char* kRemoveChars = "-/+*"; | 1156 const char* kRemoveChars = "-/+*"; |
1157 std::string input = "A-+bc/d!*"; | 1157 std::string input = "A-+bc/d!*"; |
1158 EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input)); | 1158 EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input)); |
1159 EXPECT_EQ("Abcd!", input); | 1159 EXPECT_EQ("Abcd!", input); |
1160 | 1160 |
1161 // No characters match kRemoveChars. | 1161 // No characters match kRemoveChars. |
(...skipping 22 matching lines...) Expand all Loading... |
1184 { "test", "e", "!?", "t!?st", true }, | 1184 { "test", "e", "!?", "t!?st", true }, |
1185 { "test", "ez", "!", "t!st", true }, | 1185 { "test", "ez", "!", "t!st", true }, |
1186 { "test", "zed", "!?", "t!?st", true }, | 1186 { "test", "zed", "!?", "t!?st", true }, |
1187 { "test", "t", "!?", "!?es!?", true }, | 1187 { "test", "t", "!?", "!?es!?", true }, |
1188 { "test", "et", "!>", "!>!>s!>", true }, | 1188 { "test", "et", "!>", "!>!>s!>", true }, |
1189 { "test", "zest", "!", "!!!!", true }, | 1189 { "test", "zest", "!", "!!!!", true }, |
1190 { "test", "szt", "!", "!e!!", true }, | 1190 { "test", "szt", "!", "!e!!", true }, |
1191 { "test", "t", "test", "testestest", true }, | 1191 { "test", "t", "test", "testestest", true }, |
1192 }; | 1192 }; |
1193 | 1193 |
1194 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 1194 for (size_t i = 0; i < arraysize(cases); ++i) { |
1195 std::string output; | 1195 std::string output; |
1196 bool result = ReplaceChars(cases[i].input, | 1196 bool result = ReplaceChars(cases[i].input, |
1197 cases[i].replace_chars, | 1197 cases[i].replace_chars, |
1198 cases[i].replace_with, | 1198 cases[i].replace_with, |
1199 &output); | 1199 &output); |
1200 EXPECT_EQ(cases[i].result, result); | 1200 EXPECT_EQ(cases[i].result, result); |
1201 EXPECT_EQ(cases[i].output, output); | 1201 EXPECT_EQ(cases[i].output, output); |
1202 } | 1202 } |
1203 } | 1203 } |
1204 | 1204 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 const std::string live = kLive; | 1259 const std::string live = kLive; |
1260 std::string dead = live; | 1260 std::string dead = live; |
1261 strncpy(WriteInto(&dead, 5), kDead, 4); | 1261 strncpy(WriteInto(&dead, 5), kDead, 4); |
1262 EXPECT_EQ(kDead, dead); | 1262 EXPECT_EQ(kDead, dead); |
1263 EXPECT_EQ(4u, dead.size()); | 1263 EXPECT_EQ(4u, dead.size()); |
1264 EXPECT_EQ(kLive, live); | 1264 EXPECT_EQ(kLive, live); |
1265 EXPECT_EQ(4u, live.size()); | 1265 EXPECT_EQ(4u, live.size()); |
1266 } | 1266 } |
1267 | 1267 |
1268 } // namespace base | 1268 } // namespace base |
OLD | NEW |