コード例 #1
0
ファイル: writer_test.go プロジェクト: colinmarc/cdb
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
0
ファイル: writer_test.go プロジェクト: colinmarc/cdb
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
0
ファイル: messages_test.go プロジェクト: Blystad/deis
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
0
func main() {
	val, ok := quick.Value(reflect.TypeOf(1), rand.New(rand.NewSource(time.Now().Unix())))

	if ok {
		fmt.Println(val.Int())
	}
}
コード例 #5
0
ファイル: fuzz.go プロジェクト: caddyshack/fuzz
// 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
0
ファイル: utils_test.go プロジェクト: kballard/go-osx-plist
// 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
0
// 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
0
ファイル: slice_ops_test.go プロジェクト: edwardt/kilium
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
0
ファイル: messages_test.go プロジェクト: Jyggafey/drone
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
0
ファイル: tls_test.go プロジェクト: Harvey-OS/go
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
0
ファイル: pinghub_test.go プロジェクト: Automattic/pinghub
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
0
ファイル: ddb_test.go プロジェクト: sir-wiggles/bcfs
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
0
ファイル: main_test.go プロジェクト: breml/mygoplayground
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
0
// 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
0
ファイル: envelope_test.go プロジェクト: yarpc/yarpc-go
// 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)
}
コード例 #16
0
ファイル: tls_test.go プロジェクト: Xiahl1990/go
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":
			// DeepEqual can't compare functions.
			continue
		case "Certificates":
			f.Set(reflect.ValueOf([]Certificate{
				{Certificate: [][]byte{[]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
		}

		q, ok := quick.Value(f.Type(), rnd)
		if !ok {
			t.Fatalf("quick.Value failed on field %s", fn)
		}
		f.Set(q)
	}

	c2 := c1.clone()

	if !reflect.DeepEqual(&c1, c2) {
		t.Errorf("clone failed to copy a field")
	}
}
コード例 #17
0
ファイル: writer_test.go プロジェクト: postfix/cdb
func BenchmarkPut(b *testing.B) {
	f, err := ioutil.TempFile("", "test-cdb")
	require.NoError(b, err)
	defer func() {
		f.Close()
		os.Remove(f.Name())
	}()

	writer, err := cdb.NewWriter(f)
	require.NoError(b, err)

	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)
	}
}
func report(typ reflect.Type) {
	defer func() {
		if err := recover(); err != nil {
			// IIRC, arrays would panic in 1.4.1 and previous.
			fmt.Println("\tCOULD NOT GENERATE")
			return
		}
	}()
	val, ok := quick.Value(typ, rnd)
	if !ok {
		fmt.Println("\tCOULD NOT GENERATE")
		return
	}
	fmt.Printf("\tGENERATED: %#v\n", val.Interface())
}
コード例 #19
0
ファイル: quick.go プロジェクト: dominikh/go-quickcheck
func (fsm *FSM) Run(v interface{}) []Result {
	// FIXME Right now, Run will only return once it has encountered a
	// failure
	//
	// FIXME Run only makes one (infinitely long) attempt at
	// triggering a failure.
	var out []Result

	rv := reflect.New(reflect.TypeOf(v))
	fsm.init(rv)

	for {
		if len(fsm.transitions[fsm.state]) == 0 {
			log.Println("dead end")
			return out
		}

		idx := fsm.rng.Intn(len(fsm.transitions[fsm.state]))
		trans := fsm.transitions[fsm.state][idx]
		idx = fsm.rng.Intn(len(trans.methods))
		// TODO check that the name is valid

		step := Step{
			State:    fsm.state,
			NewState: trans.to,
			Method:   trans.methods[idx],
		}

		call := rv.MethodByName(step.Method + "Call")
		for i, n := 0, reflect.TypeOf(call.Interface()).NumIn(); i < n; i++ {
			// TODO optimize reflection
			v, ok := quick.Value(reflect.TypeOf(call.Interface()).In(i), fsm.rng)
			if !ok {
				panic("cannot generate value") // XXX
			}
			step.Args = append(step.Args, v.Interface())
		}

		res := fsm.step(step, rv)
		if res.PreFail {
			continue
		}
		out = append(out, res)
		if res.PostFail {
			return out
		}
	}
}
コード例 #20
0
ファイル: base64_test.go プロジェクト: cention-sany/go.enmime
func TestBase64PadCombinerRandom(t *testing.T) {
	f := func(as []string, split string) bool {
		aenc := make([]string, len(as))
		for i, s := range as {
			aenc[i] = base64.StdEncoding.EncodeToString([]byte(s))
		}
		whole := strings.Join(aenc, split)
		br := bytes.NewReader([]byte(whole))
		c := NewBase64Combiner(br)
		buf := new(bytes.Buffer)
		_, err := buf.ReadFrom(c)
		if err != nil {
			t.Error("error in ReadFrom combiner")
			return false
		}
		whole = strings.Join(as, "")
		result := reflect.DeepEqual([]byte(whole), buf.Bytes())
		if !result {
			t.Error("\nwhole->", []byte(whole))
			t.Error("\nbytes->", buf.Bytes())
		}
		return result
	}
	c := quick.Config{
		Rand: rand.New(rand.NewSource(time.Now().Unix())),
		Values: func(v []reflect.Value, r *rand.Rand) {
			typ := reflect.TypeOf(randomSplitter{})
			as := make([]string, r.Intn(9))   // small integer value
			bs := make([]byte, r.Intn(23456)) // big integer value
			for j, _ := range as {
				for i, _ := range bs {
					bs[i] = byte(r.Int() % 0x100)
				}
				as[j] = string(bs)
			}
			v[0] = reflect.ValueOf(as)
			va, ok := quick.Value(typ, r)
			if ok {
				v[1] = reflect.ValueOf(string(va.Bytes()))
			} else {
				v[1] = reflect.ValueOf("")
			}
		},
	}
	if err := quick.Check(f, &c); err != nil {
		t.Error("failed on combiner black box test", err)
	}
}
コード例 #21
0
func GenerateParsedFeed(rand *rand.Rand) (out ParsedFeedData) {
	out = ParsedFeedData{}
	if titleOut, ok := quick.Value(reflect.TypeOf(out.Title), rand); ok {
		out.Title = titleOut.Interface().(string)
	} else {
		panic("Couldn't make a title!")
	}

	out.Items = make([]ParsedFeedItem, MaximumFeedItems+20)
	for i, _ := range out.Items {
		out.Items[i].GenericKey = makeHash(key_uniquer + strconv.Itoa(rand.Int()))
		out.Items[i].Title = strconv.Itoa(i)
	}

	return
}
コード例 #22
0
ファイル: quick_test.go プロジェクト: thriftrw/thriftrw-go
func TestQuickRoundTrip(t *testing.T) {
	tests := []reflect.Type{
		reflect.TypeOf(tc.PrimitiveContainers{}),
		reflect.TypeOf(tc.PrimitiveContainersRequired{}),
		reflect.TypeOf(tc.EnumContainers{}),
		reflect.TypeOf(ts.PrimitiveRequiredStruct{}),
		reflect.TypeOf(ts.PrimitiveOptionalStruct{}),
		reflect.TypeOf(ts.Point{}),
		reflect.TypeOf(ts.Size{}),
		// TODO Uncomment once we validate required fields
		// reflect.TypeOf(testdata.Frame{}),
		// reflect.TypeOf(testdata.Edge{}),
		// reflect.TypeOf(testdata.Graph{}),
		reflect.TypeOf(ts.ContactInfo{}),
		reflect.TypeOf(ts.User{}),
		reflect.TypeOf(tc.ContainersOfContainers{}),
	}

	rand := rand.New(rand.NewSource(time.Now().UnixNano()))
	attempts := 1000
	for _, tt := range tests {
		i := 0
		for i < attempts {
			structValue, ok := quick.Value(tt, rand)
			if !ok {
				t.Fatalf("failed to generate a value for %v", tt)
			}

			result := structValue.Addr().MethodByName("ToWire").Call(nil)
			if result[1].Interface() != nil {
				continue // invalid value generated
			}

			i++ // increment i only if we found a valid sample

			wireValue := result[0]
			parsedValue := reflect.New(tt)
			result = parsedValue.MethodByName("FromWire").
				Call([]reflect.Value{wireValue})
			if result[0].Interface() != nil {
				t.Fatal("failed to parse", tt, "from", wireValue.Interface())
			}

			assert.Equal(t, structValue.Addr().Interface(), parsedValue.Interface())
		}
	}
}
コード例 #23
0
func TestMarshalUnmarshal(t *testing.T) {
	rand := rand.New(rand.NewSource(0))

	for i, iface := range tests {
		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.Interface().(testMessage)
			marshaled := m1.marshal()
			m2 := iface.(testMessage)
			if !m2.unmarshal(marshaled) {
				t.Errorf("#%d failed to unmarshal %#v %x", i, m1, marshaled)
				break
			}
			m2.marshal() // to fill any marshal cache in the message

			if !m1.equal(m2) {
				t.Errorf("#%d got:%#v want:%#v %x", i, m2, m1, marshaled)
				break
			}

			if i >= 3 {
				// The first three message types (ClientHello,
				// ServerHello and Finished) are allowed to
				// have parsable prefixes because the extension
				// data is optional and the length of the
				// Finished varies across versions.
				for j := 0; j < len(marshaled); j++ {
					if m2.unmarshal(marshaled[0:j]) {
						t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1)
						break
					}
				}
			}
		}
	}
}
コード例 #24
0
ファイル: integration_test.go プロジェクト: ZhuHangpeng/mig
func (c Publishing) Generate(r *rand.Rand, _ int) reflect.Value {
	var ok bool
	var t reflect.Value

	p := Publishing{}
	//p.DeliveryMode = uint8(r.Intn(3))
	//p.Priority = uint8(r.Intn(8))

	if r.Intn(2) > 0 {
		p.ContentType = "application/octet-stream"
	}

	if r.Intn(2) > 0 {
		p.ContentEncoding = "gzip"
	}

	if r.Intn(2) > 0 {
		p.CorrelationId = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.ReplyTo = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.MessageId = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.Type = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.AppId = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.Timestamp = time.Unix(r.Int63(), r.Int63())
	}

	if t, ok = quick.Value(reflect.TypeOf(p.Body), r); ok {
		p.Body = t.Bytes()
	}

	return reflect.ValueOf(p)
}
コード例 #25
0
ファイル: metric_test.go プロジェクト: yanghongkjxy/mtail
func TestMetricJSONRoundTrip(t *testing.T) {
	rand := rand.New(rand.NewSource(0))
	f := func(name, prog string, kind Kind, keys []string, val, ti, tns int64) bool {
		m := NewMetric(name, prog, kind, keys...)
		var labels []string
		for _ = range keys {
			if l, ok := quick.Value(reflect.TypeOf(name), rand); ok {
				labels = append(labels, l.String())
			} else {
				t.Errorf("failed to create value for labels")
				break
			}
		}
		d, _ := m.GetDatum(labels...)
		d.Set(val, timeGenerator(rand))

		j, e := json.Marshal(m)
		if e != nil {
			t.Errorf("json.Marshal failed: %s\n", e)
			return false
		}

		r := &Metric{}
		e = json.Unmarshal(j, &r)
		if e != nil {
			t.Errorf("json.Unmarshal failed: %s\n", e)
			return false
		}

		// pretty.Compare uses the opposite order to xUnit for comparisons.
		diff := pretty.Compare(r, m)
		if len(diff) > 0 {
			t.Errorf("Round trip wasn't stable:\n%s", diff)
			return false
		}
		return true
	}
	q := quick.Config{MaxCount: 100000}
	if testing.Short() {
		q.MaxCount = 1000
	}
	if err := quick.Check(f, nil); err != nil {
		t.Error(err)
	}
}
コード例 #26
0
ファイル: bz1_test.go プロジェクト: nooproblem/influxdb
func GeneratePoints(rand *rand.Rand, size, seriesN int, timestampFn func(int) time.Time) Points {
	// Generate series with a random number of points in each.
	m := make(Points)
	for i := 0; i < seriesN; i++ {
		key := strconv.Itoa(i)

		// Generate points for the series.
		for j, pointN := 0, rand.Intn(size); j < pointN; j++ {
			timestamp := timestampFn(j)
			data, ok := quick.Value(reflect.TypeOf([]byte(nil)), rand)
			if !ok {
				panic("cannot generate data")
			}
			m[key] = append(m[key], bz1.MarshalEntry(timestamp.UnixNano(), data.Interface().([]byte)))
		}
	}
	return m
}
コード例 #27
0
ファイル: cursor_test.go プロジェクト: KoeSystems/influxdb
// Generate returns a randomly generated cursor. Implements quick.Generator.
func (c Cursor) Generate(rand *rand.Rand, size int) reflect.Value {
	c.index = 0

	c.items = make([]CursorItem, rand.Intn(size))
	for i := range c.items {
		value, _ := quick.Value(reflect.TypeOf([]byte(nil)), rand)

		c.items[i] = CursorItem{
			Key:   u64tob(uint64(rand.Intn(size))),
			Value: value.Interface().([]byte),
		}
	}

	// Sort items by key.
	sort.Sort(CursorItems(c.items))

	return reflect.ValueOf(c)
}
コード例 #28
0
ファイル: bz1_test.go プロジェクト: nickscript0/telegraf
func (Points) Generate(rand *rand.Rand, size int) reflect.Value {
	// Generate series with a random number of points in each.
	m := make(map[string][][]byte)
	for i, seriesN := 0, rand.Intn(size); i < seriesN; i++ {
		key := strconv.Itoa(rand.Intn(20))

		// Generate points for the series.
		for j, pointN := 0, rand.Intn(size); j < pointN; j++ {
			timestamp := time.Unix(0, 0).Add(time.Duration(rand.Intn(100)))
			data, ok := quick.Value(reflect.TypeOf([]byte(nil)), rand)
			if !ok {
				panic("cannot generate data")
			}
			m[key] = append(m[key], bz1.MarshalEntry(timestamp.UnixNano(), data.Interface().([]byte)))
		}
	}

	return reflect.ValueOf(Points(m))
}
コード例 #29
0
// values returns
func values(args []reflect.Value, f reflect.Type, rand *rand.Rand) {
	for j := 0; j < len(args); j++ {
		var ok bool
		switch rndValueType(rand) {
		case xMin:
			args[j], ok = minValue(f.In(j), rand)
		case xMax:
			args[j], ok = maxValue(f.In(j), rand)
		case xZero:
			args[j], ok = zeroValue(f.In(j), rand)
		default:
			args[j], ok = quick.Value(f.In(j), rand)
		}
		if !ok {
			log.Printf("Error creating random value for type %#v\n", f.In(j))
		}
	}
	return
}
コード例 #30
0
func TestMarshalUnmarshal(t *testing.T) {
	rand := rand.New(rand.NewSource(0))
	for i, iface := range tests {
		ty := reflect.NewValue(iface).Type()

		for j := 0; j < 100; j++ {
			v, ok := quick.Value(ty, rand)
			if !ok {
				t.Errorf("#%d: failed to create value", i)
				break
			}

			m1 := v.Interface().(testMessage)
			marshaled := m1.marshal()
			m2 := iface.(testMessage)
			if !m2.unmarshal(marshaled) {
				t.Errorf("#%d failed to unmarshal %#v %x", i, m1, marshaled)
				break
			}
			m2.marshal() // to fill any marshal cache in the message

			if !reflect.DeepEqual(m1, m2) {
				t.Errorf("#%d got:%#v want:%#v %x", i, m2, m1, marshaled)
				break
			}

			if i >= 2 {
				// The first two message types (ClientHello and
				// ServerHello) are allowed to have parsable
				// prefixes because the extension data is
				// optional.
				for j := 0; j < len(marshaled); j++ {
					if m2.unmarshal(marshaled[0:j]) {
						t.Errorf("#%d unmarshaled a prefix of length %d of %#v", i, j, m1)
						break
					}
				}
			}
		}
	}
}