本文整理汇总了Golang中testing/quick.Value函数的典型用法代码示例。如果您正苦于以下问题:Golang Value函数的具体用法?Golang Value怎么用?Golang Value使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Value函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testWritesRandom
func testWritesRandom(t *testing.T, writer *cdb.Writer) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
records := make([][][]byte, 0, 1000)
seenKeys := make(map[string]bool)
stringType := reflect.TypeOf("")
// Make sure we don't end up with duplicate keys, since that makes testing
// hard.
for len(records) < cap(records) {
key, _ := quick.Value(stringType, random)
if !seenKeys[key.String()] {
value, _ := quick.Value(stringType, random)
keyBytes := []byte(key.String())
valueBytes := []byte(value.String())
records = append(records, [][]byte{keyBytes, valueBytes})
seenKeys[key.String()] = true
}
}
for _, record := range records {
err := writer.Put(record[0], record[1])
require.NoError(t, err)
}
db, err := writer.Freeze()
require.NoError(t, err)
for _, record := range records {
msg := "while fetching " + string(record[0])
val, err := db.Get(record[0])
require.Nil(t, err)
assert.Equal(t, string(record[1]), string(val), msg)
}
}
示例2: benchmarkPut
func benchmarkPut(b *testing.B, writer *cdb.Writer) {
random := rand.New(rand.NewSource(time.Now().UnixNano()))
stringType := reflect.TypeOf("")
b.ResetTimer()
for i := 0; i < b.N; i++ {
key, _ := quick.Value(stringType, random)
value, _ := quick.Value(stringType, random)
keyBytes := []byte(key.String())
valueBytes := []byte(value.String())
writer.Put(keyBytes, valueBytes)
}
}
示例3: TestMarshalUnmarshal
func TestMarshalUnmarshal(t *testing.T) {
rand := rand.New(rand.NewSource(0))
iface := &msgAllTypes{}
ty := reflect.ValueOf(iface).Type()
n := 100
if testing.Short() {
n = 5
}
for j := 0; j < n; j++ {
v, ok := quick.Value(ty, rand)
if !ok {
t.Errorf("failed to create value")
break
}
m1 := v.Elem().Interface()
m2 := iface
marshaled := Marshal(m1)
if err := Unmarshal(marshaled, m2); err != nil {
t.Errorf("Unmarshal %#v: %s", m1, err)
break
}
if !reflect.DeepEqual(v.Interface(), m2) {
t.Errorf("got: %#v\nwant:%#v\n%x", m2, m1, marshaled)
break
}
}
}
示例4: main
func main() {
val, ok := quick.Value(reflect.TypeOf(1), rand.New(rand.NewSource(time.Now().Unix())))
if ok {
fmt.Println(val.Int())
}
}
示例5: Value
// Value emits a Value for the requested session.
func (f *Fuzz) Value(r *rand.Rand, n int) (v reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
if err, ok = r.(error); ok {
} else {
err = fmt.Errorf("fuzz: %v", r)
}
}
}()
v = reflect.New(f.typ).Elem()
for name, field := range f.fields {
gen, ok := f.bindings[name]
switch {
case ok:
elem, err := gen.Generate(r, n)
if err != nil {
return v, err
}
v.FieldByName(name).Set(elem)
case f.zeroValueFallthrough:
continue
default:
elem, ok := quick.Value(field.Type, r)
if !ok {
return v, errIllegal
}
v.FieldByName(name).Set(elem)
}
}
return v, err
}
示例6: generateString
// generates valid utf-8-encoded strings
func generateString(rand *rand.Rand) (string, bool) {
typ := reflect.TypeOf([]rune{})
if val, ok := quick.Value(typ, rand); ok {
return string(val.Interface().([]rune)), true
}
return "", false
}
示例7: gen
// gen wraps quick.Value so it's easier to use.
// it generates a random value of the given value's type.
func gen(typ interface{}, rand *rand.Rand) interface{} {
v, ok := quick.Value(reflect.TypeOf(typ), rand)
if !ok {
panic(fmt.Sprintf("couldn't generate random value of type %T", typ))
}
return v.Interface()
}
示例8: Generate
func (RemoveSliceTestInput) Generate(rand *rand.Rand, size int) reflect.Value {
ret := RemoveSliceTestInput{}
// Keep searching for a non-zero length input. Technically this could run forever, but
// realistically it won't. Thus I don't care too much.
for len(ret.Input) == 0 {
val, ok := quick.Value(reflect.TypeOf(testIntSlice{}), rand)
if ok != true {
panic("Failed to generate input slice elements!!!!!")
}
ret.Input = val.Interface().(testIntSlice)
}
removeElementSize := rand.Intn(len(ret.Input))
ret.ToRemove = make(testIntSlice, removeElementSize)
for index := range ret.ToRemove {
ret.ToRemove[index] = ret.Input[rand.Intn(len(ret.Input))]
}
// Random numbers may generate dups. Just remove them brute force style.
ret.Input.RemoveDuplicates()
ret.ToRemove.RemoveDuplicates()
sort.Sort(ret.Input)
sort.Sort(ret.ToRemove)
return reflect.ValueOf(ret)
}
示例9: TestMarshalUnmarshal
func TestMarshalUnmarshal(t *testing.T) {
rand := rand.New(rand.NewSource(0))
for i, iface := range messageTypes {
ty := reflect.ValueOf(iface).Type()
n := 100
if testing.Short() {
n = 5
}
for j := 0; j < n; j++ {
v, ok := quick.Value(ty, rand)
if !ok {
t.Errorf("#%d: failed to create value", i)
break
}
m1 := v.Elem().Interface()
m2 := iface
marshaled := marshal(msgIgnore, m1)
if err := unmarshal(m2, marshaled, msgIgnore); err != nil {
t.Errorf("#%d failed to unmarshal %#v: %s", i, m1, err)
break
}
if !reflect.DeepEqual(v.Interface(), m2) {
t.Errorf("#%d\ngot: %#v\nwant:%#v\n%x", i, m2, m1, marshaled)
break
}
}
}
}
示例10: TestClone
func TestClone(t *testing.T) {
var c1 Config
v := reflect.ValueOf(&c1).Elem()
rnd := rand.New(rand.NewSource(time.Now().Unix()))
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
f := v.Field(i)
if !f.CanSet() {
// unexported field; not cloned.
continue
}
// testing/quick can't handle functions or interfaces.
fn := typ.Field(i).Name
switch fn {
case "Rand":
f.Set(reflect.ValueOf(io.Reader(os.Stdin)))
continue
case "Time", "GetCertificate", "GetConfigForClient", "VerifyPeerCertificate", "GetClientCertificate":
// DeepEqual can't compare functions.
continue
case "Certificates":
f.Set(reflect.ValueOf([]Certificate{
{Certificate: [][]byte{{'b'}}},
}))
continue
case "NameToCertificate":
f.Set(reflect.ValueOf(map[string]*Certificate{"a": nil}))
continue
case "RootCAs", "ClientCAs":
f.Set(reflect.ValueOf(x509.NewCertPool()))
continue
case "ClientSessionCache":
f.Set(reflect.ValueOf(NewLRUClientSessionCache(10)))
continue
case "KeyLogWriter":
f.Set(reflect.ValueOf(io.Writer(os.Stdout)))
continue
}
q, ok := quick.Value(f.Type(), rnd)
if !ok {
t.Fatalf("quick.Value failed on field %s", fn)
}
f.Set(q)
}
c2 := c1.Clone()
// DeepEqual also compares unexported fields, thus c2 needs to have run
// serverInit in order to be DeepEqual to c1. Cloning it and discarding
// the result is sufficient.
c2.Clone()
if !reflect.DeepEqual(&c1, c2) {
t.Errorf("clone failed to copy a field")
}
}
示例11: quickValue
func quickValue(x interface{}, r *rand.Rand) interface{} {
t := reflect.TypeOf(x)
value, ok := quick.Value(t, r)
if !ok {
panic("Failed to create a quick value: " + t.Name())
}
return value.Interface()
}
示例12: newSid
func newSid() string {
var temp [3]byte
t := reflect.TypeOf(temp)
r := rand.New(rand.NewSource(int64(rand.Int())))
v, _ := quick.Value(t, r)
a := v.Interface().([3]byte)
return hex.EncodeToString(a[:3])
}
示例13: TestFoo
func TestFoo(t *testing.T) {
var x *myint
val, ok := quick.Value(reflect.TypeOf(x), rnd)
if !ok {
t.Log("Error creating value")
}
t.Log(val)
foo(nil)
}
示例14: TestBitReader_Quick
// Ensure bit reader can read random bits written to a stream.
func TestBitReader_Quick(t *testing.T) {
if err := quick.Check(func(values []uint64, nbits []uint) bool {
// Limit nbits to 64.
for i := 0; i < len(values) && i < len(nbits); i++ {
nbits[i] = (nbits[i] % 64) + 1
values[i] = values[i] & (math.MaxUint64 >> (64 - nbits[i]))
}
// Write bits to a buffer.
var buf bytes.Buffer
w := bitstream.NewWriter(&buf)
for i := 0; i < len(values) && i < len(nbits); i++ {
w.WriteBits(values[i], int(nbits[i]))
}
w.Flush(bitstream.Zero)
// Read bits from the buffer.
r := tsm1.NewBitReader(buf.Bytes())
for i := 0; i < len(values) && i < len(nbits); i++ {
v, err := r.ReadBits(nbits[i])
if err != nil {
t.Errorf("unexpected error(%d): %s", i, err)
return false
} else if v != values[i] {
t.Errorf("value mismatch(%d): got=%d, exp=%d (nbits=%d)", i, v, values[i], nbits[i])
return false
}
}
return true
}, &quick.Config{
Values: func(a []reflect.Value, rand *rand.Rand) {
a[0], _ = quick.Value(reflect.TypeOf([]uint64{}), rand)
a[1], _ = quick.Value(reflect.TypeOf([]uint{}), rand)
},
}); err != nil {
t.Fatal(err)
}
}
示例15: generate
// generate generates a random value into the given pointer.
//
// var i int
// generate(&i, rand)
//
// If the type implements the quick.Generator interface, that is used.
func generate(v interface{}, r *rand.Rand) {
t := reflect.TypeOf(v)
if t.Kind() != reflect.Ptr {
panic(fmt.Sprintf("%v is not a pointer type", t))
}
out, ok := quick.Value(t.Elem(), r)
if !ok {
panic(fmt.Sprintf("could not generate a value for %v", t))
}
reflect.ValueOf(v).Elem().Set(out)
}