// randFloat64 generates a random float taking the full range of a float64. func randFloat64(rand *rand.Rand) float64 { f := rand.Float64() if rand.Int()&1 == 1 { f = -f } return f }
// randFloat32 generates a random float taking the full range of a float32. func randFloat32(rand *rand.Rand) float32 { f := rand.Float64() * math.MaxFloat32 if rand.Int()&1 == 1 { f = -f } return float32(f) }
// Value returns an arbitrary value of the given type. // If the type implements the Generator interface, that will be used. // Note: in order to create arbitrary values for structs, all the members must be public. func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) { if m, ok := reflect.Zero(t).Interface().(Generator); ok { return m.Generate(rand, complexSize), true } switch concrete := t; concrete.Kind() { case reflect.Bool: return reflect.ValueOf(rand.Int()&1 == 0), true case reflect.Float32: return reflect.ValueOf(randFloat32(rand)), true case reflect.Float64: return reflect.ValueOf(randFloat64(rand)), true case reflect.Complex64: return reflect.ValueOf(complex(randFloat32(rand), randFloat32(rand))), true case reflect.Complex128: return reflect.ValueOf(complex(randFloat64(rand), randFloat64(rand))), true case reflect.Int16: return reflect.ValueOf(int16(randInt64(rand))), true case reflect.Int32: return reflect.ValueOf(int32(randInt64(rand))), true case reflect.Int64: return reflect.ValueOf(randInt64(rand)), true case reflect.Int8: return reflect.ValueOf(int8(randInt64(rand))), true case reflect.Int: return reflect.ValueOf(int(randInt64(rand))), true case reflect.Uint16: return reflect.ValueOf(uint16(randInt64(rand))), true case reflect.Uint32: return reflect.ValueOf(uint32(randInt64(rand))), true case reflect.Uint64: return reflect.ValueOf(uint64(randInt64(rand))), true case reflect.Uint8: return reflect.ValueOf(uint8(randInt64(rand))), true case reflect.Uint: return reflect.ValueOf(uint(randInt64(rand))), true case reflect.Uintptr: return reflect.ValueOf(uintptr(randInt64(rand))), true case reflect.Map: numElems := rand.Intn(complexSize) m := reflect.MakeMap(concrete) for i := 0; i < numElems; i++ { key, ok1 := Value(concrete.Key(), rand) value, ok2 := Value(concrete.Elem(), rand) if !ok1 || !ok2 { return reflect.Value{}, false } m.SetMapIndex(key, value) } return m, true case reflect.Ptr: v, ok := Value(concrete.Elem(), rand) if !ok { return reflect.Value{}, false } p := reflect.New(concrete.Elem()) p.Elem().Set(v) return p, true case reflect.Slice: numElems := rand.Intn(complexSize) s := reflect.MakeSlice(concrete, numElems, numElems) for i := 0; i < numElems; i++ { v, ok := Value(concrete.Elem(), rand) if !ok { return reflect.Value{}, false } s.Index(i).Set(v) } return s, true case reflect.String: numChars := rand.Intn(complexSize) codePoints := make([]int, numChars) for i := 0; i < numChars; i++ { codePoints[i] = rand.Intn(0x10ffff) } return reflect.ValueOf(string(codePoints)), true case reflect.Struct: s := reflect.New(t).Elem() for i := 0; i < s.NumField(); i++ { v, ok := Value(concrete.Field(i).Type, rand) if !ok { return reflect.Value{}, false } s.Field(i).Set(v) } return s, true default: return reflect.Value{}, false } return }
// Value returns an arbitrary value of the given type. // If the type implements the Generator interface, that will be used. // Note: in order to create arbitrary values for structs, all the members must be public. func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) { if m, ok := reflect.MakeZero(t).Interface().(Generator); ok { return m.Generate(rand, complexSize), true } switch concrete := t.(type) { case *reflect.BoolType: return reflect.NewValue(rand.Int()&1 == 0), true case *reflect.Float32Type: return reflect.NewValue(randFloat32(rand)), true case *reflect.Float64Type: return reflect.NewValue(randFloat64(rand)), true case *reflect.FloatType: if t.Size() == 4 { return reflect.NewValue(float(randFloat32(rand))), true } else { return reflect.NewValue(float(randFloat64(rand))), true } case *reflect.Int16Type: return reflect.NewValue(int16(randInt64(rand))), true case *reflect.Int32Type: return reflect.NewValue(int32(randInt64(rand))), true case *reflect.Int64Type: return reflect.NewValue(randInt64(rand)), true case *reflect.Int8Type: return reflect.NewValue(int8(randInt64(rand))), true case *reflect.IntType: return reflect.NewValue(int(randInt64(rand))), true case *reflect.MapType: numElems := rand.Intn(complexSize) m := reflect.MakeMap(concrete) for i := 0; i < numElems; i++ { key, ok1 := Value(concrete.Key(), rand) value, ok2 := Value(concrete.Elem(), rand) if !ok1 || !ok2 { return nil, false } m.SetElem(key, value) } return m, true case *reflect.PtrType: v, ok := Value(concrete.Elem(), rand) if !ok { return nil, false } p := reflect.MakeZero(concrete) p.(*reflect.PtrValue).PointTo(v) return p, true case *reflect.SliceType: numElems := rand.Intn(complexSize) s := reflect.MakeSlice(concrete, numElems, numElems) for i := 0; i < numElems; i++ { v, ok := Value(concrete.Elem(), rand) if !ok { return nil, false } s.Elem(i).SetValue(v) } return s, true case *reflect.StringType: numChars := rand.Intn(complexSize) codePoints := make([]int, numChars) for i := 0; i < numChars; i++ { codePoints[i] = rand.Intn(0x10ffff) } return reflect.NewValue(string(codePoints)), true case *reflect.StructType: s := reflect.MakeZero(t).(*reflect.StructValue) for i := 0; i < s.NumField(); i++ { v, ok := Value(concrete.Field(i).Type, rand) if !ok { return nil, false } s.Field(i).SetValue(v) } return s, true case *reflect.Uint16Type: return reflect.NewValue(uint16(randInt64(rand))), true case *reflect.Uint32Type: return reflect.NewValue(uint32(randInt64(rand))), true case *reflect.Uint64Type: return reflect.NewValue(uint64(randInt64(rand))), true case *reflect.Uint8Type: return reflect.NewValue(uint8(randInt64(rand))), true case *reflect.UintType: return reflect.NewValue(uint(randInt64(rand))), true case *reflect.UintptrType: return reflect.NewValue(uintptr(randInt64(rand))), true default: return nil, false } return }
func uint8rand(r *rand.Rand) uint8 { return uint8(r.Int() % 255) }