diff options
Diffstat (limited to 'src/testing')
| -rw-r--r-- | src/testing/quick/quick.go | 14 | ||||
| -rw-r--r-- | src/testing/quick/quick_test.go | 17 |
2 files changed, 26 insertions, 5 deletions
diff --git a/src/testing/quick/quick.go b/src/testing/quick/quick.go index 35b7b636b4..0e36810eb6 100644 --- a/src/testing/quick/quick.go +++ b/src/testing/quick/quick.go @@ -102,12 +102,16 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) { v.SetMapIndex(key, value) } case reflect.Ptr: - elem, ok := Value(concrete.Elem(), rand) - if !ok { - return reflect.Value{}, false + if rand.Intn(complexSize) == 0 { + v.Set(reflect.Zero(concrete)) // Generate nil pointer. + } else { + elem, ok := Value(concrete.Elem(), rand) + if !ok { + return reflect.Value{}, false + } + v.Set(reflect.New(concrete.Elem())) + v.Elem().Set(elem) } - v.Set(reflect.New(concrete.Elem())) - v.Elem().Set(elem) case reflect.Slice: numElems := rand.Intn(complexSize) v.Set(reflect.MakeSlice(concrete, numElems, numElems)) diff --git a/src/testing/quick/quick_test.go b/src/testing/quick/quick_test.go index 1b973027d5..c79f30ea1d 100644 --- a/src/testing/quick/quick_test.go +++ b/src/testing/quick/quick_test.go @@ -83,6 +83,9 @@ type TestMapAlias map[int]int func fMapAlias(a TestMapAlias) TestMapAlias { return a } func fPtr(a *int) *int { + if a == nil { + return nil + } b := *a return &b } @@ -255,3 +258,17 @@ func TestFailure(t *testing.T) { t.Errorf("#3 Error was not a SetupError: %s", err) } } + +// The following test didn't terminate because nil pointers were not +// generated. +// Issue 8818. +func TestNilPointers(t *testing.T) { + type Recursive struct { + Next *Recursive + } + + f := func(rec Recursive) bool { + return true + } + Check(f, nil) +} |
