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

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

Issue 2979393002: [string_util] fix bug in ReplaceSubstringsAfterOffset() (Closed)
Patch Set: Reduce scope of change to just correctness fix. 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 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 const char* str;
589 string16::size_type start_offset; 589 string16::size_type start_offset;
590 const char* find_this; 590 const char* find_this;
591 const char* replace_with; 591 const char* replace_with;
592 const char* expected; 592 const char* 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 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"},
613 {"Replace me only me once", 9, "me ", "", "Replace me only once"},
614 {"abababab", 2, "ab", "c", "abccc"},
606 }; 615 };
607 616
608 for (size_t i = 0; i < arraysize(cases); i++) { 617 for (size_t i = 0; i < arraysize(cases); i++) {
609 string16 str = ASCIIToUTF16(cases[i].str); 618 string16 str = ASCIIToUTF16(cases[i].str);
610 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset, 619 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset,
611 ASCIIToUTF16(cases[i].find_this), 620 ASCIIToUTF16(cases[i].find_this),
612 ASCIIToUTF16(cases[i].replace_with)); 621 ASCIIToUTF16(cases[i].replace_with));
613 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); 622 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str);
614 } 623 }
624
625 for (size_t i = 0; i < arraysize(cases); i++) {
Peter Kasting 2017/07/27 05:25:14 Nit: Use range-based for (2 places)
ncarter (slow) 2017/07/28 02:34:25 Done.
626 // Insufficient capacity case; will realloc the string.
627 std::string str = cases[i].str;
628 str.shrink_to_fit();
Peter Kasting 2017/07/27 05:25:14 Note that shrink_to_fit() is a non-binding request
ncarter (slow) 2017/07/28 02:34:25 Done.
629 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset,
630 cases[i].find_this, cases[i].replace_with);
631 EXPECT_EQ(cases[i].expected, str);
632 }
633
634 for (size_t i = 0; i < arraysize(cases); i++) {
635 // Ample capacity case; will try to in place
Peter Kasting 2017/07/27 05:25:14 Nit: Will try to ____ in place?
ncarter (slow) 2017/07/28 02:34:25 Done.
636 std::string str = cases[i].str;
637 str.reserve(strlen(cases[i].expected) * 2);
638 const void* original_buffer = str.data();
Peter Kasting 2017/07/27 05:25:14 Nit: Why not char*? That said, I'm uncomfortable
ncarter (slow) 2017/07/28 02:34:25 Removed.
639 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset,
640 cases[i].find_this, cases[i].replace_with);
641 EXPECT_EQ(cases[i].expected, str);
642 EXPECT_EQ(original_buffer, str.data());
643 }
615 } 644 }
616 645
617 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { 646 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) {
618 static const struct { 647 static const struct {
619 const char* str; 648 const char* str;
620 string16::size_type start_offset; 649 string16::size_type start_offset;
621 const char* find_this; 650 const char* find_this;
622 const char* replace_with; 651 const char* replace_with;
623 const char* expected; 652 const char* expected;
624 } cases[] = { 653 } cases[] = {
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after
1231 const std::string live = kLive; 1260 const std::string live = kLive;
1232 std::string dead = live; 1261 std::string dead = live;
1233 strncpy(WriteInto(&dead, 5), kDead, 4); 1262 strncpy(WriteInto(&dead, 5), kDead, 4);
1234 EXPECT_EQ(kDead, dead); 1263 EXPECT_EQ(kDead, dead);
1235 EXPECT_EQ(4u, dead.size()); 1264 EXPECT_EQ(4u, dead.size());
1236 EXPECT_EQ(kLive, live); 1265 EXPECT_EQ(kLive, live);
1237 EXPECT_EQ(4u, live.size()); 1266 EXPECT_EQ(4u, live.size());
1238 } 1267 }
1239 1268
1240 } // namespace base 1269 } // 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