aboutsummaryrefslogtreecommitdiff
path: root/src/testing/quick
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2016-05-06 15:28:19 +0000
committerRuss Cox <rsc@golang.org>2016-05-06 18:25:28 +0000
commit670a5cda2048af8d83958af0f4b2fda8f7b4ea72 (patch)
tree981ba3b18d155520135d23d99b007232b7220551 /src/testing/quick
parent131231b8db26b38c9c2fdc52fb788241f5c2de51 (diff)
downloadgo-670a5cda2048af8d83958af0f4b2fda8f7b4ea72.tar.xz
Revert "testing/quick: generate more map and slice states"
This reverts commit 0ccabe2e0b42a2602e0f37ce28d5368aa811f530. 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>
Diffstat (limited to 'src/testing/quick')
-rw-r--r--src/testing/quick/quick.go47
-rw-r--r--src/testing/quick/quick_test.go17
2 files changed, 35 insertions, 29 deletions
diff --git a/src/testing/quick/quick.go b/src/testing/quick/quick.go
index 4bc8e3fc2e..798d41aa7d 100644
--- a/src/testing/quick/quick.go
+++ b/src/testing/quick/quick.go
@@ -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 }
diff --git a/src/testing/quick/quick_test.go b/src/testing/quick/quick_test.go
index 018ece2a52..fe443592f8 100644
--- a/src/testing/quick/quick_test.go
+++ b/src/testing/quick/quick_test.go
@@ -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)
+ }
+}