github.com/lingyao2333/mo-zero@v1.4.1/core/stringx/replacer_fuzz_test.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 package stringx 5 6 import ( 7 "fmt" 8 "math/rand" 9 "strings" 10 "testing" 11 ) 12 13 func FuzzReplacerReplace(f *testing.F) { 14 keywords := make(map[string]string) 15 for i := 0; i < 20; i++ { 16 keywords[Randn(rand.Intn(10)+5)] = Randn(rand.Intn(5) + 1) 17 } 18 rep := NewReplacer(keywords) 19 printableKeywords := func() string { 20 var buf strings.Builder 21 for k, v := range keywords { 22 fmt.Fprintf(&buf, "%q: %q,\n", k, v) 23 } 24 return buf.String() 25 } 26 27 f.Add(50) 28 f.Fuzz(func(t *testing.T, n int) { 29 text := Randn(rand.Intn(n%50+50) + 1) 30 defer func() { 31 if r := recover(); r != nil { 32 t.Errorf("mapping: %s\ntext: %s", printableKeywords(), text) 33 } 34 }() 35 val := rep.Replace(text) 36 keys := rep.(*replacer).node.find([]rune(val)) 37 if len(keys) > 0 { 38 t.Errorf("mapping: %s\ntext: %s\nresult: %s\nmatch: %v", 39 printableKeywords(), text, val, keys) 40 } 41 }) 42 }