Пример #1
0
func BenchmarkEncodeNested(b *testing.B) {
	popr := rand.New(rand.NewSource(616))
	total := 0
	pops := make([]reflectparser.ReflectParser, 10000)
	for i := 0; i < 10000; i++ {
		msg := NewPopulatedNested(popr, false)
		p := reflectparser.NewReflectParser()
		p.Init(reflect.ValueOf(msg))
		pops[i] = p
	}
	buf := make([]byte, 1<<20)
	enc, err := NewEncoder((&Nested{}).Description(), "prototests", "Nested")
	if err != nil {
		panic(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		data, err := enc.Encode(buf, pops[i%10000])
		if err != nil {
			panic(err)
		}
		total += len(data)
	}
	b.SetBytes(int64(total / b.N))
}
Пример #2
0
func testInject(t *testing.T, m *mem.Mem) bool {
	parser := reflectparser.NewReflectParser()
	parser.Init(reflect.ValueOf(&Number{Num: 456}))
	res, err := m.Validate(parser)
	if err != nil {
		t.Fatal(err)
	}
	return res
}
Пример #3
0
func testPartial(t *testing.T, input interface{}, expected interface{}) {
	p := reflectparser.NewReflectParser().Init(reflect.ValueOf(input))
	output := reflect.New(reflect.TypeOf(expected).Elem()).Interface()
	if err := Encode(p, output); err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(expected, output) {
		t.Fatalf("expected equal, but got %#v", output)
	}
}
Пример #4
0
func testMarshal(t *testing.T, input interface{}) {
	t.Logf("testing marshaling: %#v", input)
	p := reflectparser.NewReflectParser()
	p.Init(reflect.ValueOf(input))
	outputData, err := Encode(p)
	if err != nil {
		t.Fatal(err)
	}
	output := reflect.New(reflect.TypeOf(input).Elem()).Interface()
	if err := json.Unmarshal(outputData, output); err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(input, output) {
		t.Fatalf("expected equal, but got %s", string(outputData))
	}
}
Пример #5
0
func TestNested(t *testing.T) {
	msg := prototests.NewPopulatedNested(r, true)
	p := reflectparser.NewReflectParser()
	p.Init(reflect.ValueOf(msg))
	enc, err := NewEncoder(msg.Description(), "prototests", "Nested")
	if err != nil {
		t.Fatal(err)
	}
	buf, err := enc.Encode(make([]byte, 1024), p)
	if err != nil {
		t.Fatal(err)
	}
	msg2 := &prototests.Nested{}
	if err := proto.Unmarshal(buf, msg2); err != nil {
		t.Fatal(err)
	}
	if !msg.Equal(msg2) {
		t.Fatalf("%#v not equal %#v", msg, msg2)
	}
}