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

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

Issue 2979393002: [string_util] fix bug in ReplaceSubstringsAfterOffset() (Closed)
Patch Set: Unittest fixes. Created 3 years, 4 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
« base/strings/string_util.cc ('K') | « base/strings/string_util.cc ('k') | 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 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 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 {100LL*1024*1024*1024, "100 GB"}, 578 {100LL*1024*1024*1024, "100 GB"},
579 }; 579 };
580 580
581 for (size_t i = 0; i < arraysize(cases); ++i) { 581 for (size_t i = 0; i < arraysize(cases); ++i) {
582 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), 582 EXPECT_EQ(ASCIIToUTF16(cases[i].expected),
583 FormatBytesUnlocalized(cases[i].bytes)); 583 FormatBytesUnlocalized(cases[i].bytes));
584 } 584 }
585 } 585 }
586 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { 586 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) {
587 static const struct { 587 static const struct {
588 const char* str; 588 StringPiece str;
589 string16::size_type start_offset; 589 size_t start_offset;
590 const char* find_this; 590 StringPiece find_this;
591 const char* replace_with; 591 StringPiece replace_with;
592 const char* expected; 592 StringPiece expected;
593 } cases[] = { 593 } cases[] = {
594 {"aaa", 0, "a", "b", "bbb"}, 594 {"aaa", 0, "a", "b", "bbb"},
595 {"abb", 0, "ab", "a", "ab"}, 595 {"aaa", 0, "aa", "b", "ba"},
596 {"Removing some substrings inging", 0, "ing", "", "Remov some substrs "}, 596 {"aaa", 0, "aa", "bbb", "bbba"},
597 {"Not found", 0, "x", "0", "Not found"}, 597 {"aaaaa", 0, "aa", "b", "bba"},
598 {"Not found again", 5, "x", "0", "Not found again"}, 598 {"ababaaababa", 0, "aba", "", "baaba"},
599 {" Making it much longer ", 0, " ", "Four score and seven years ago", 599 {"ababaaababa", 0, "aba", "_", "_baa_ba"},
600 "Four score and seven years agoMakingFour score and seven years agoit" 600 {"ababaaababa", 0, "aba", "__", "__baa__ba"},
601 "Four score and seven years agomuchFour score and seven years agolonger" 601 {"ababaaababa", 0, "aba", "___", "___baa___ba"},
602 "Four score and seven years ago"}, 602 {"ababaaababa", 0, "aba", "____", "____baa____ba"},
603 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"}, 603 {"ababaaababa", 0, "aba", "_____", "_____baa_____ba"},
604 {"Replace me only me once", 9, "me ", "", "Replace me only once"}, 604 {"abb", 0, "ab", "a", "ab"},
605 {"abababab", 2, "ab", "c", "abccc"}, 605 {"Removing some substrings inging", 0, "ing", "", "Remov some substrs "},
606 {"Not found", 0, "x", "0", "Not found"},
607 {"Not found again", 5, "x", "0", "Not found again"},
608 {" Making it much longer ", 0, " ", "Four score and seven years ago",
609 "Four score and seven years agoMakingFour score and seven years agoit"
610 "Four score and seven years agomuchFour score and seven years agolonger"
611 "Four score and seven years ago"},
612 {" Making it much much much much shorter ", 0,
613 "Making it much much much much shorter", "", " "},
614 {"so much much much much much very much much much shorter", 0, "much ",
615 "", "so very shorter"},
616 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"},
617 {"Replace me only me once", 9, "me ", "", "Replace me only once"},
618 {"abababab", 2, "ab", "c", "abccc"},
606 }; 619 };
607 620
608 for (size_t i = 0; i < arraysize(cases); i++) { 621 for (const auto& scenario : cases) {
609 string16 str = ASCIIToUTF16(cases[i].str); 622 string16 str = ASCIIToUTF16(scenario.str);
610 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset, 623 ReplaceSubstringsAfterOffset(&str, scenario.start_offset,
611 ASCIIToUTF16(cases[i].find_this), 624 ASCIIToUTF16(scenario.find_this),
612 ASCIIToUTF16(cases[i].replace_with)); 625 ASCIIToUTF16(scenario.replace_with));
613 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); 626 EXPECT_EQ(ASCIIToUTF16(scenario.expected), str);
627 }
628
629 for (const auto& scenario : cases) {
630 // Insufficient capacity case; must realloc the string if it grows.
Peter Kasting 2017/07/28 08:29:24 Nit: Maybe want to hoist this (and the similar com
ncarter (slow) 2017/07/28 21:46:34 Done.
631 std::string str = scenario.str.as_string();
632 str.shrink_to_fit(); // This is nonbinding, but it's the best we've got.
633 ReplaceSubstringsAfterOffset(&str, scenario.start_offset,
634 scenario.find_this, scenario.replace_with);
635 EXPECT_EQ(scenario.expected, str);
636 }
637
638 for (const auto& scenario : cases) {
639 // Ample capacity case; should be possible to grow in-place.
640 std::string str = scenario.str.as_string();
641 str.reserve(std::max(scenario.str.length(), scenario.expected.length()) *
642 2);
643
644 ReplaceSubstringsAfterOffset(&str, scenario.start_offset,
645 scenario.find_this, scenario.replace_with);
646 EXPECT_EQ(scenario.expected, str);
614 } 647 }
615 } 648 }
616 649
617 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { 650 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) {
618 static const struct { 651 static const struct {
619 const char* str; 652 const char* str;
620 string16::size_type start_offset; 653 string16::size_type start_offset;
621 const char* find_this; 654 const char* find_this;
622 const char* replace_with; 655 const char* replace_with;
623 const char* expected; 656 const char* expected;
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
1231 const std::string live = kLive; 1264 const std::string live = kLive;
1232 std::string dead = live; 1265 std::string dead = live;
1233 strncpy(WriteInto(&dead, 5), kDead, 4); 1266 strncpy(WriteInto(&dead, 5), kDead, 4);
1234 EXPECT_EQ(kDead, dead); 1267 EXPECT_EQ(kDead, dead);
1235 EXPECT_EQ(4u, dead.size()); 1268 EXPECT_EQ(4u, dead.size());
1236 EXPECT_EQ(kLive, live); 1269 EXPECT_EQ(kLive, live);
1237 EXPECT_EQ(4u, live.size()); 1270 EXPECT_EQ(4u, live.size());
1238 } 1271 }
1239 1272
1240 } // namespace base 1273 } // namespace base
OLDNEW
« base/strings/string_util.cc ('K') | « base/strings/string_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698