update gofuzz

This commit is contained in:
Dan Winship 2016-12-01 09:07:12 -05:00
parent 44f00e1019
commit 43e1f6ae1f
3 changed files with 15 additions and 8 deletions

2
Godeps/Godeps.json generated
View File

@ -1348,7 +1348,7 @@
}, },
{ {
"ImportPath": "github.com/google/gofuzz", "ImportPath": "github.com/google/gofuzz",
"Rev": "bbcb9da2d746f8bdbd6a936686a0a6067ada0ec5" "Rev": "44d81051d367757e1c7c6a5a86423ece9afcf63c"
}, },
{ {
"ImportPath": "github.com/gorilla/context", "ImportPath": "github.com/gorilla/context",

View File

@ -14,21 +14,21 @@ This is useful for testing:
Import with ```import "github.com/google/gofuzz"``` Import with ```import "github.com/google/gofuzz"```
You can use it on single variables: You can use it on single variables:
``` ```go
f := fuzz.New() f := fuzz.New()
var myInt int var myInt int
f.Fuzz(&myInt) // myInt gets a random value. f.Fuzz(&myInt) // myInt gets a random value.
``` ```
You can use it on maps: You can use it on maps:
``` ```go
f := fuzz.New().NilChance(0).NumElements(1, 1) f := fuzz.New().NilChance(0).NumElements(1, 1)
var myMap map[ComplexKeyType]string var myMap map[ComplexKeyType]string
f.Fuzz(&myMap) // myMap will have exactly one element. f.Fuzz(&myMap) // myMap will have exactly one element.
``` ```
Customize the chance of getting a nil pointer: Customize the chance of getting a nil pointer:
``` ```go
f := fuzz.New().NilChance(.5) f := fuzz.New().NilChance(.5)
var fancyStruct struct { var fancyStruct struct {
A, B, C, D *string A, B, C, D *string
@ -37,7 +37,7 @@ f.Fuzz(&fancyStruct) // About half the pointers should be set.
``` ```
You can even customize the randomization completely if needed: You can even customize the randomization completely if needed:
``` ```go
type MyEnum string type MyEnum string
const ( const (
A MyEnum = "A" A MyEnum = "A"

View File

@ -129,7 +129,7 @@ func (f *Fuzzer) genElementCount() int {
if f.minElements == f.maxElements { if f.minElements == f.maxElements {
return f.minElements return f.minElements
} }
return f.minElements + f.r.Intn(f.maxElements-f.minElements) return f.minElements + f.r.Intn(f.maxElements-f.minElements+1)
} }
func (f *Fuzzer) genShouldFill() bool { func (f *Fuzzer) genShouldFill() bool {
@ -229,12 +229,19 @@ func (f *Fuzzer) doFuzz(v reflect.Value, flags uint64) {
return return
} }
v.Set(reflect.Zero(v.Type())) v.Set(reflect.Zero(v.Type()))
case reflect.Array:
if f.genShouldFill() {
n := v.Len()
for i := 0; i < n; i++ {
f.doFuzz(v.Index(i), 0)
}
return
}
v.Set(reflect.Zero(v.Type()))
case reflect.Struct: case reflect.Struct:
for i := 0; i < v.NumField(); i++ { for i := 0; i < v.NumField(); i++ {
f.doFuzz(v.Field(i), 0) f.doFuzz(v.Field(i), 0)
} }
case reflect.Array:
fallthrough
case reflect.Chan: case reflect.Chan:
fallthrough fallthrough
case reflect.Func: case reflect.Func: