From 186618dfef5b3c8681fbbc27c82034a031850bfd Mon Sep 17 00:00:00 2001 From: David Neto Date: Fri, 15 Oct 2021 19:24:10 +0000 Subject: [PATCH] add test for tint::utils::ReplaceAll The new test proves that the algorithm needs to advance 'pos' past the replacement string. Change-Id: Ia8fdf6b2c08d6af09e8e631c1d8661752edcb7ce Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/66660 Auto-Submit: David Neto Kokoro: Kokoro Reviewed-by: Ben Clayton Commit-Queue: Ben Clayton --- src/utils/string.h | 2 +- src/utils/string_test.cc | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/string.h b/src/utils/string.h index 4465243eef..2e93f51371 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -23,7 +23,7 @@ namespace utils { /// @param str the string to apply replacements to /// @param substr the string to search for /// @param replacement the replacement string to use instead of `substr` -/// @returns `str` with all occurrances of `substr` replaced with `replacement` +/// @returns `str` with all occurrences of `substr` replaced with `replacement` inline std::string ReplaceAll(std::string str, const std::string& substr, const std::string& replacement) { diff --git a/src/utils/string_test.cc b/src/utils/string_test.cc index ab1e14b817..5b1847082d 100644 --- a/src/utils/string_test.cc +++ b/src/utils/string_test.cc @@ -27,6 +27,10 @@ TEST(StringTest, ReplaceAll) { ASSERT_EQ("xyxybbcc", ReplaceAll("aabbcc", "a", "xy")); ASSERT_EQ("aaxyxycc", ReplaceAll("aabbcc", "b", "xy")); ASSERT_EQ("aabbxyxy", ReplaceAll("aabbcc", "c", "xy")); + // Replacement string includes the searched-for string. + // This proves that the algorithm needs to advance 'pos' + // past the replacement. + ASSERT_EQ("aabxybbxybcc", ReplaceAll("aabbcc", "b", "bxyb")); } } // namespace