...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package theproto3
30
31 import (
32 "reflect"
33 "testing"
34
35 "github.com/gogo/protobuf/proto"
36 )
37
38 func TestNilMaps(t *testing.T) {
39 m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}}
40 data, err := proto.Marshal(m)
41 if err != nil {
42 t.Fatal(err)
43 }
44 size := m.Size()
45 protoSize := proto.Size(m)
46 marshaledSize := len(data)
47 if size != protoSize || marshaledSize != protoSize {
48 t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
49 }
50 m2 := &AllMaps{}
51 if err := proto.Unmarshal(data, m2); err != nil {
52 t.Fatal(err)
53 }
54 if v, ok := m2.StringToMsgMap["a"]; !ok {
55 t.Error("element not in map")
56 } else if v != nil {
57 t.Errorf("element should be nil, but its %v", v)
58 }
59 }
60
61 func TestNilMapsBytes(t *testing.T) {
62 m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}}
63 data, err := proto.Marshal(m)
64 if err != nil {
65 t.Fatal(err)
66 }
67 size := m.Size()
68 protoSize := proto.Size(m)
69 marshaledSize := len(data)
70 if size != protoSize || marshaledSize != protoSize {
71 t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
72 }
73 m2 := &AllMaps{}
74 if err := proto.Unmarshal(data, m2); err != nil {
75 t.Fatal(err)
76 }
77 if v, ok := m2.StringToBytesMap["a"]; !ok {
78 t.Error("element not in map")
79 } else if len(v) != 0 {
80 t.Errorf("element should be empty, but its %v", v)
81 }
82 }
83
84 func TestEmptyMapsBytes(t *testing.T) {
85 m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}}
86 data, err := proto.Marshal(m)
87 if err != nil {
88 t.Fatal(err)
89 }
90 size := m.Size()
91 protoSize := proto.Size(m)
92 marshaledSize := len(data)
93 if size != protoSize || marshaledSize != protoSize {
94 t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
95 }
96 m2 := &AllMaps{}
97 if err := proto.Unmarshal(data, m2); err != nil {
98 t.Fatal(err)
99 }
100 if v, ok := m2.StringToBytesMap["b"]; !ok {
101 t.Error("element not in map")
102 } else if len(v) != 0 {
103 t.Errorf("element should be empty, but its %v", v)
104 }
105 }
106
107 func TestCustomTypeSize(t *testing.T) {
108 m := &Uint128Pair{}
109 m.Size()
110 }
111
112 func TestCustomTypeMarshalUnmarshal(t *testing.T) {
113 m1 := &Uint128Pair{}
114 if b, err := proto.Marshal(m1); err != nil {
115 t.Fatal(err)
116 } else {
117 m2 := &Uint128Pair{}
118 if err := proto.Unmarshal(b, m2); err != nil {
119 t.Fatal(err)
120 }
121 if !m1.Equal(m2) {
122 t.Errorf("expected %+v, got %+v", m1, m2)
123 }
124 }
125 }
126
127 func TestNotPackedToPacked(t *testing.T) {
128 input := []uint64{1, 10e9}
129 notpacked := &NotPacked{Key: input}
130 if data, err := proto.Marshal(notpacked); err != nil {
131 t.Fatal(err)
132 } else {
133 packed := &Message{}
134 if err := proto.Unmarshal(data, packed); err != nil {
135 t.Fatal(err)
136 }
137 output := packed.Key
138 if !reflect.DeepEqual(input, output) {
139 t.Fatalf("expected %#v, got %#v", input, output)
140 }
141 }
142 }
143
144 func TestPackedToNotPacked(t *testing.T) {
145 input := []uint64{1, 10e9}
146 packed := &Message{Key: input}
147 if data, err := proto.Marshal(packed); err != nil {
148 t.Fatal(err)
149 } else {
150 notpacked := &NotPacked{}
151 if err := proto.Unmarshal(data, notpacked); err != nil {
152 t.Fatal(err)
153 }
154 output := notpacked.Key
155 if !reflect.DeepEqual(input, output) {
156 t.Fatalf("expected %#v, got %#v", input, output)
157 }
158 }
159 }
160
View as plain text