diff options
| author | Jorropo <jorropo.pgm@gmail.com> | 2025-03-14 21:29:08 +0100 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-03-15 13:13:56 -0700 |
| commit | e6ffe764cf1f35e96a116ca144733a5fba02578e (patch) | |
| tree | 8e3712b9b39568bbf60f7b117755bb2f6a8cd5cf /src/strings/strings_test.go | |
| parent | e0edd3e15519eb10a885d99422878596acd97899 (diff) | |
| download | go-e6ffe764cf1f35e96a116ca144733a5fba02578e.tar.xz | |
strings: add FuzzReplace test
While reviewing CL 657935 I've notied there a
couple tricky reslices that depends on multiple
things being correct.
Might as well fuzz it.
Change-Id: Id78921bcb252e73a8a06e6deb4c920445a87d525
Reviewed-on: https://go-review.googlesource.com/c/go/+/658075
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/strings/strings_test.go')
| -rw-r--r-- | src/strings/strings_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go index 8067380664..aa3458c5c9 100644 --- a/src/strings/strings_test.go +++ b/src/strings/strings_test.go @@ -1485,6 +1485,52 @@ func TestReplace(t *testing.T) { } } +func FuzzReplace(f *testing.F) { + for _, tt := range ReplaceTests { + f.Add(tt.in, tt.old, tt.new, tt.n) + } + f.Fuzz(func(t *testing.T, in, old, new string, n int) { + differentImpl := func(in, old, new string, n int) string { + var out Builder + if n < 0 { + n = math.MaxInt + } + for i := 0; i < len(in); { + if n == 0 { + out.WriteString(in[i:]) + break + } + if HasPrefix(in[i:], old) { + out.WriteString(new) + i += len(old) + n-- + if len(old) != 0 { + continue + } + if i == len(in) { + break + } + } + if len(old) == 0 { + _, length := utf8.DecodeRuneInString(in[i:]) + out.WriteString(in[i : i+length]) + i += length + } else { + out.WriteByte(in[i]) + i++ + } + } + if len(old) == 0 && n != 0 { + out.WriteString(new) + } + return out.String() + } + if simple, replace := differentImpl(in, old, new, n), Replace(in, old, new, n); simple != replace { + t.Errorf("The two implementations do not match %q != %q for Replace(%q, %q, %q, %d)", simple, replace, in, old, new, n) + } + }) +} + var TitleTests = []struct { in, out string }{ |
