]> Cypherpunks repositories - gostls13.git/commitdiff
testing/quick: support generation of array types in Value
authorChris Kastorff <encryptio@gmail.com>
Wed, 4 Feb 2015 12:43:00 +0000 (04:43 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 4 Feb 2015 16:17:07 +0000 (16:17 +0000)
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>
src/testing/quick/quick.go
src/testing/quick/quick_test.go

index 909c65f788b78f5b197b57bfc1b952f9abf4b09d..edcaaa091aba27f19513a2744f037f9dd2d74c03 100644 (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)
index e925ba67507120d50301067b330fdc4c5fb4016b..ca340fe76d6d98dfad071450e6bc5e7af5e90d5b 100644 (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