I doubt/suspect that your Reverse(orig)
function returns an error but you ignore it. If your function returns an error like:
func Reverse(s string) (string, error) {
if !utf8.ValidString(s) {
return s, errors.New("input is not valid UTF-8")
}
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r), nil
}
Then your test should look like:
func FuzzReverse(f *testing.F) {
testcases := []string{"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
rev := Reverse(orig)
if err1 != nil {
t.Skip()
}
doubleRev, err2 := Reverse(rev)
if err2 != nil {
t.Skip()
}
if orig != doubleRev {
t.Errorf("Before: %q, after: %q", orig, doubleRev)
}
if utf8.ValidString(orig) && !utf8.ValidString(rev) {
t.Errorf("Reverse produced invalid UTF-8 string %q", rev)
}
})
}
Also make sure your main package's folder is writable and you may need to clean the cache if the issue still persists:
Running 'go clean -fuzzcache' removes all cached fuzzing values. This may make fuzzing less effective, temporarily.
I advise to go through the doc step by step avoiding jumping/skipping some steps or just copying the code without reading the instructions & explanations as it's useful for avoiding such unknown errors. LMK more details and/or share your full code including the function itself that you're testing (the fuzz target).
PS: You may find the following commands useful:
go help testflag
go help testflag
You may additionally find this link useful!