OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / gogo / protobuf / proto / map_test.go
1 package proto_test
2
3 import (
4         "fmt"
5         "reflect"
6         "testing"
7
8         "github.com/gogo/protobuf/proto"
9         ppb "github.com/gogo/protobuf/proto/proto3_proto"
10 )
11
12 func TestMap(t *testing.T) {
13         var b []byte
14         fmt.Sscanf("a2010c0a044b657931120456616c31a201130a044b657932120556616c3261120456616c32a201240a044b6579330d05000000120556616c33621a0556616c3361120456616c331505000000a20100a201260a044b657934130a07536f6d6555524c1209536f6d655469746c651a08536e69707065743114", "%x", &b)
15
16         var m ppb.Message
17         if err := proto.Unmarshal(b, &m); err != nil {
18                 t.Fatalf("proto.Unmarshal error: %v", err)
19         }
20
21         got := m.StringMap
22         want := map[string]string{
23                 "":     "",
24                 "Key1": "Val1",
25                 "Key2": "Val2",
26                 "Key3": "Val3",
27                 "Key4": "",
28         }
29
30         if !reflect.DeepEqual(got, want) {
31                 t.Errorf("maps differ:\ngot  %#v\nwant %#v", got, want)
32         }
33 }
34
35 func marshalled() []byte {
36         m := &ppb.IntMaps{}
37         for i := 0; i < 1000; i++ {
38                 m.Maps = append(m.Maps, &ppb.IntMap{
39                         Rtt: map[int32]int32{1: 2},
40                 })
41         }
42         b, err := proto.Marshal(m)
43         if err != nil {
44                 panic(fmt.Sprintf("Can't marshal %+v: %v", m, err))
45         }
46         return b
47 }
48
49 func BenchmarkConcurrentMapUnmarshal(b *testing.B) {
50         in := marshalled()
51         b.RunParallel(func(pb *testing.PB) {
52                 for pb.Next() {
53                         var out ppb.IntMaps
54                         if err := proto.Unmarshal(in, &out); err != nil {
55                                 b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
56                         }
57                 }
58         })
59 }
60
61 func BenchmarkSequentialMapUnmarshal(b *testing.B) {
62         in := marshalled()
63         b.ResetTimer()
64         for i := 0; i < b.N; i++ {
65                 var out ppb.IntMaps
66                 if err := proto.Unmarshal(in, &out); err != nil {
67                         b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
68                 }
69         }
70 }