github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/testutils/mutate.go (about) 1 package testutils 2 3 import ( 4 "github.com/gnolang/gno/tm2/pkg/random" 5 ) 6 7 // Contract: !bytes.Equal(input, output) && len(input) >= len(output) 8 // TODO: keep output size the same; search all usage first. 9 func MutateByteSlice(bytez []byte) []byte { 10 // If bytez is empty, panic 11 if len(bytez) == 0 { 12 panic("Cannot mutate an empty bytez") 13 } 14 15 // Copy bytez 16 mBytez := make([]byte, len(bytez)) 17 copy(mBytez, bytez) 18 bytez = mBytez 19 20 // Try a random mutation 21 switch random.RandInt() % 2 { 22 case 0: // Mutate a single byte 23 bytez[random.RandInt()%len(bytez)] += byte(random.RandInt()%255 + 1) 24 case 1: // Remove an arbitrary byte 25 pos := random.RandInt() % len(bytez) 26 bytez = append(bytez[:pos], bytez[pos+1:]...) 27 } 28 return bytez 29 }