Commit 0ccabe2e authored by Matt T. Proud's avatar Matt T. Proud Committed by Brad Fitzpatrick

testing/quick: generate more map and slice states

This change adds support in testing/quick to generate maps and slices
in additional states:

  (1.) nil maps

  (2.) nil slices

  (3.) empty slice occupancy: `len(s) == 0 && s != nil`

  (4.) partial slice occupancy: `len(s) < cap(s) && s != nil`

  (5.) full slice occupancy: `len(s) == cap(s) && s != nil`

Prior to this, only #5 was ever generated, thereby not sufficiently
exercising all of the fuzzable code path outcomes.

This change depends on https://go-review.googlesource.com/#/c/17499/.

Change-Id: I9343c475cefbd72ffc5237281826465c25872206
Reviewed-on: https://go-review.googlesource.com/16470Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 763e8fcc
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
"strings" "strings"
) )
var defaultMaxCount *int = flag.Int("quickchecks", 100, "The default number of iterations for each check") var defaultMaxCount = flag.Int("quickchecks", 100, "The default number of iterations for each check")
// A Generator can generate random values of its own type. // A Generator can generate random values of its own type.
type Generator interface { type Generator interface {
...@@ -98,6 +98,9 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value, ...@@ -98,6 +98,9 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value,
case reflect.Uintptr: case reflect.Uintptr:
v.SetUint(uint64(randInt64(rand))) v.SetUint(uint64(randInt64(rand)))
case reflect.Map: case reflect.Map:
if generateNilValue(rand) {
v.Set(reflect.Zero(concrete)) // Generate nil map.
} else {
numElems := rand.Intn(size) numElems := rand.Intn(size)
v.Set(reflect.MakeMap(concrete)) v.Set(reflect.MakeMap(concrete))
for i := 0; i < numElems; i++ { for i := 0; i < numElems; i++ {
...@@ -108,8 +111,9 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value, ...@@ -108,8 +111,9 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value,
} }
v.SetMapIndex(key, value) v.SetMapIndex(key, value)
} }
}
case reflect.Ptr: case reflect.Ptr:
if rand.Intn(size) == 0 { if generateNilValue(rand) {
v.Set(reflect.Zero(concrete)) // Generate nil pointer. v.Set(reflect.Zero(concrete)) // Generate nil pointer.
} else { } else {
elem, ok := sizedValue(concrete.Elem(), rand, size) elem, ok := sizedValue(concrete.Elem(), rand, size)
...@@ -120,16 +124,21 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value, ...@@ -120,16 +124,21 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value,
v.Elem().Set(elem) v.Elem().Set(elem)
} }
case reflect.Slice: case reflect.Slice:
numElems := rand.Intn(size) if generateNilValue(rand) {
sizeLeft := size - numElems v.Set(reflect.Zero(concrete)) // Generate nil slice.
v.Set(reflect.MakeSlice(concrete, numElems, numElems)) } else {
for i := 0; i < numElems; i++ { slCap := rand.Intn(size)
slLen := rand.Intn(slCap + 1)
sizeLeft := size - slCap
v.Set(reflect.MakeSlice(concrete, slLen, slCap))
for i := 0; i < slLen; i++ {
elem, ok := sizedValue(concrete.Elem(), rand, sizeLeft) elem, ok := sizedValue(concrete.Elem(), rand, sizeLeft)
if !ok { if !ok {
return reflect.Value{}, false return reflect.Value{}, false
} }
v.Index(i).Set(elem) v.Index(i).Set(elem)
} }
}
case reflect.Array: case reflect.Array:
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
elem, ok := sizedValue(concrete.Elem(), rand, size) elem, ok := sizedValue(concrete.Elem(), rand, size)
...@@ -384,3 +393,5 @@ func toString(interfaces []interface{}) string { ...@@ -384,3 +393,5 @@ func toString(interfaces []interface{}) string {
} }
return strings.Join(s, ", ") return strings.Join(s, ", ")
} }
func generateNilValue(r *rand.Rand) bool { return r.Intn(20) == 0 }
...@@ -290,20 +290,3 @@ func TestMutuallyRecursive(t *testing.T) { ...@@ -290,20 +290,3 @@ func TestMutuallyRecursive(t *testing.T) {
f := func(a A) bool { return true } f := func(a A) bool { return true }
Check(f, nil) Check(f, nil)
} }
// Some serialization formats (e.g. encoding/pem) cannot distinguish
// between a nil and an empty map or slice, so avoid generating the
// zero value for these.
func TestNonZeroSliceAndMap(t *testing.T) {
type Q struct {
M map[int]int
S []int
}
f := func(q Q) bool {
return q.M != nil && q.S != nil
}
err := Check(f, nil)
if err != nil {
t.Fatal(err)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment