1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:44:41 -07:00

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 <josharian@gmail.com>
This commit is contained in:
Chris Kastorff 2015-02-04 04:43:00 -08:00 committed by Josh Bleecher Snyder
parent 22f337656d
commit 3e9ed273a2
2 changed files with 16 additions and 0 deletions

View File

@ -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)

View File

@ -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