1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:50:05 -07:00

Revert "testing/quick: generate more map and slice states"

This reverts commit 0ccabe2e0b.

Change-Id: Ib1c230fb6801c0ee26f4a352b0c1130fa240a76a
Reviewed-on: https://go-review.googlesource.com/22860
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Russ Cox 2016-05-06 15:28:19 +00:00
parent 131231b8db
commit 670a5cda20
2 changed files with 35 additions and 29 deletions

View File

@ -14,7 +14,7 @@ import (
"strings"
)
var defaultMaxCount = flag.Int("quickchecks", 100, "The default number of iterations for each check")
var defaultMaxCount *int = flag.Int("quickchecks", 100, "The default number of iterations for each check")
// A Generator can generate random values of its own type.
type Generator interface {
@ -98,22 +98,18 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value,
case reflect.Uintptr:
v.SetUint(uint64(randInt64(rand)))
case reflect.Map:
if generateNilValue(rand) {
v.Set(reflect.Zero(concrete)) // Generate nil map.
} else {
numElems := rand.Intn(size)
v.Set(reflect.MakeMap(concrete))
for i := 0; i < numElems; i++ {
key, ok1 := sizedValue(concrete.Key(), rand, size)
value, ok2 := sizedValue(concrete.Elem(), rand, size)
if !ok1 || !ok2 {
return reflect.Value{}, false
}
v.SetMapIndex(key, value)
numElems := rand.Intn(size)
v.Set(reflect.MakeMap(concrete))
for i := 0; i < numElems; i++ {
key, ok1 := sizedValue(concrete.Key(), rand, size)
value, ok2 := sizedValue(concrete.Elem(), rand, size)
if !ok1 || !ok2 {
return reflect.Value{}, false
}
v.SetMapIndex(key, value)
}
case reflect.Ptr:
if generateNilValue(rand) {
if rand.Intn(size) == 0 {
v.Set(reflect.Zero(concrete)) // Generate nil pointer.
} else {
elem, ok := sizedValue(concrete.Elem(), rand, size)
@ -124,20 +120,15 @@ func sizedValue(t reflect.Type, rand *rand.Rand, size int) (value reflect.Value,
v.Elem().Set(elem)
}
case reflect.Slice:
if generateNilValue(rand) {
v.Set(reflect.Zero(concrete)) // Generate nil slice.
} else {
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)
if !ok {
return reflect.Value{}, false
}
v.Index(i).Set(elem)
numElems := rand.Intn(size)
sizeLeft := size - numElems
v.Set(reflect.MakeSlice(concrete, numElems, numElems))
for i := 0; i < numElems; i++ {
elem, ok := sizedValue(concrete.Elem(), rand, sizeLeft)
if !ok {
return reflect.Value{}, false
}
v.Index(i).Set(elem)
}
case reflect.Array:
for i := 0; i < v.Len(); i++ {
@ -385,5 +376,3 @@ func toString(interfaces []interface{}) string {
}
return strings.Join(s, ", ")
}
func generateNilValue(r *rand.Rand) bool { return r.Intn(20) == 0 }

View File

@ -290,3 +290,20 @@ func TestMutuallyRecursive(t *testing.T) {
f := func(a A) bool { return true }
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)
}
}