github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/iavl/common/mutate.go (about)

     1  package common
     2  
     3  // Contract: !bytes.Equal(input, output) && len(input) >= len(output)
     4  func MutateByteSlice(bytez []byte) []byte {
     5  	// If bytez is empty, panic
     6  	if len(bytez) == 0 {
     7  		panic("Cannot mutate an empty bytez")
     8  	}
     9  
    10  	// Copy bytez
    11  	mBytez := make([]byte, len(bytez))
    12  	copy(mBytez, bytez)
    13  	bytez = mBytez
    14  
    15  	// Try a random mutation
    16  	switch RandInt() % 2 {
    17  	case 0: // Mutate a single byte
    18  		bytez[RandInt()%len(bytez)] += byte(RandInt()%255 + 1)
    19  	case 1: // Remove an arbitrary byte
    20  		pos := RandInt() % len(bytez)
    21  		bytez = append(bytez[:pos], bytez[pos+1:]...)
    22  	}
    23  	return bytez
    24  }