OSDN Git Service

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