From 3e9ed273a244da5d96472656af911a6d2714a9e8 Mon Sep 17 00:00:00 2001 From: Chris Kastorff Date: Wed, 4 Feb 2015 04:43:00 -0800 Subject: [PATCH] testing/quick: support generation of array types in Value Generating array types like [4]int would fail even though the int type is generatable. Allow generating values of array types when the inner type is generatable. Change-Id: I7d71b3c18edb3737e2fec1ddf5e36c9dc8401971 Reviewed-on: https://go-review.googlesource.com/3865 Reviewed-by: Josh Bleecher Snyder --- src/testing/quick/quick.go | 8 ++++++++ src/testing/quick/quick_test.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/testing/quick/quick.go b/src/testing/quick/quick.go index 909c65f788..edcaaa091a 100644 --- a/src/testing/quick/quick.go +++ b/src/testing/quick/quick.go @@ -118,6 +118,14 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) { } v.Index(i).Set(elem) } + case reflect.Array: + for i := 0; i < v.Len(); i++ { + elem, ok := Value(concrete.Elem(), rand) + if !ok { + return reflect.Value{}, false + } + v.Index(i).Set(elem) + } case reflect.String: numChars := rand.Intn(complexSize) codePoints := make([]rune, numChars) diff --git a/src/testing/quick/quick_test.go b/src/testing/quick/quick_test.go index e925ba6750..ca340fe76d 100644 --- a/src/testing/quick/quick_test.go +++ b/src/testing/quick/quick_test.go @@ -144,6 +144,12 @@ type TestIntptrAlias *int func fIntptrAlias(a TestIntptrAlias) TestIntptrAlias { return a } +func fArray(a [4]byte) [4]byte { return a } + +type TestArrayAlias [4]byte + +func fArrayAlias(a TestArrayAlias) TestArrayAlias { return a } + func reportError(property string, err error, t *testing.T) { if err != nil { t.Errorf("%s: %s", property, err) @@ -195,6 +201,8 @@ func TestCheckEqual(t *testing.T) { reportError("fUintptrAlias", CheckEqual(fUintptrAlias, fUintptrAlias, nil), t) reportError("fIntptr", CheckEqual(fIntptr, fIntptr, nil), t) reportError("fIntptrAlias", CheckEqual(fIntptrAlias, fIntptrAlias, nil), t) + reportError("fArray", CheckEqual(fArray, fArray, nil), t) + reportError("fArrayAlais", CheckEqual(fArrayAlias, fArrayAlias, nil), t) } // This tests that ArbitraryValue is working by checking that all the arbitrary -- 2.48.1