blob: 1c3915cfc127433f96d77de58ce8514d7ded70d5 [file] [log] [blame]
Damien Neile6f060f2019-04-23 17:11:02 -07001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package proto_test
6
7import (
8 "testing"
9
10 "github.com/golang/protobuf/v2/internal/encoding/pack"
11 "github.com/golang/protobuf/v2/internal/scalar"
12 testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
13 test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
14 "github.com/golang/protobuf/v2/proto"
15)
16
17func TestEqual(t *testing.T) {
18 for _, test := range inequalities {
19 if !proto.Equal(test.a, test.a) {
20 t.Errorf("Equal(a, a) = false, want true\na = %T %v", test.a, marshalText(test.a))
21 }
22 if proto.Equal(test.a, test.b) {
23 t.Errorf("Equal(a, b) = true, want false\na = %T %v\nb = %T %v", test.a, marshalText(test.a), test.b, marshalText(test.b))
24 }
25 }
26}
27
28var inequalities = []struct{ a, b proto.Message }{
29 // Scalar values.
30 {
31 &testpb.TestAllTypes{OptionalInt32: scalar.Int32(1)},
32 &testpb.TestAllTypes{OptionalInt32: scalar.Int32(2)},
33 },
34 {
35 &testpb.TestAllTypes{OptionalInt64: scalar.Int64(1)},
36 &testpb.TestAllTypes{OptionalInt64: scalar.Int64(2)},
37 },
38 {
39 &testpb.TestAllTypes{OptionalUint32: scalar.Uint32(1)},
40 &testpb.TestAllTypes{OptionalUint32: scalar.Uint32(2)},
41 },
42 {
43 &testpb.TestAllTypes{OptionalUint64: scalar.Uint64(1)},
44 &testpb.TestAllTypes{OptionalUint64: scalar.Uint64(2)},
45 },
46 {
47 &testpb.TestAllTypes{OptionalSint32: scalar.Int32(1)},
48 &testpb.TestAllTypes{OptionalSint32: scalar.Int32(2)},
49 },
50 {
51 &testpb.TestAllTypes{OptionalSint64: scalar.Int64(1)},
52 &testpb.TestAllTypes{OptionalSint64: scalar.Int64(2)},
53 },
54 {
55 &testpb.TestAllTypes{OptionalFixed32: scalar.Uint32(1)},
56 &testpb.TestAllTypes{OptionalFixed32: scalar.Uint32(2)},
57 },
58 {
59 &testpb.TestAllTypes{OptionalFixed64: scalar.Uint64(1)},
60 &testpb.TestAllTypes{OptionalFixed64: scalar.Uint64(2)},
61 },
62 {
63 &testpb.TestAllTypes{OptionalSfixed32: scalar.Int32(1)},
64 &testpb.TestAllTypes{OptionalSfixed32: scalar.Int32(2)},
65 },
66 {
67 &testpb.TestAllTypes{OptionalSfixed64: scalar.Int64(1)},
68 &testpb.TestAllTypes{OptionalSfixed64: scalar.Int64(2)},
69 },
70 {
71 &testpb.TestAllTypes{OptionalFloat: scalar.Float32(1)},
72 &testpb.TestAllTypes{OptionalFloat: scalar.Float32(2)},
73 },
74 {
75 &testpb.TestAllTypes{OptionalDouble: scalar.Float64(1)},
76 &testpb.TestAllTypes{OptionalDouble: scalar.Float64(2)},
77 },
78 {
79 &testpb.TestAllTypes{OptionalBool: scalar.Bool(true)},
80 &testpb.TestAllTypes{OptionalBool: scalar.Bool(false)},
81 },
82 {
83 &testpb.TestAllTypes{OptionalString: scalar.String("a")},
84 &testpb.TestAllTypes{OptionalString: scalar.String("b")},
85 },
86 {
87 &testpb.TestAllTypes{OptionalBytes: []byte("a")},
88 &testpb.TestAllTypes{OptionalBytes: []byte("b")},
89 },
90 {
91 &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_FOO.Enum()},
92 &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum()},
93 },
94 // Proto2 presence.
95 {
96 &testpb.TestAllTypes{},
97 &testpb.TestAllTypes{OptionalInt32: scalar.Int32(0)},
98 },
99 {
100 &testpb.TestAllTypes{},
101 &testpb.TestAllTypes{OptionalInt64: scalar.Int64(0)},
102 },
103 {
104 &testpb.TestAllTypes{},
105 &testpb.TestAllTypes{OptionalUint32: scalar.Uint32(0)},
106 },
107 {
108 &testpb.TestAllTypes{},
109 &testpb.TestAllTypes{OptionalUint64: scalar.Uint64(0)},
110 },
111 {
112 &testpb.TestAllTypes{},
113 &testpb.TestAllTypes{OptionalSint32: scalar.Int32(0)},
114 },
115 {
116 &testpb.TestAllTypes{},
117 &testpb.TestAllTypes{OptionalSint64: scalar.Int64(0)},
118 },
119 {
120 &testpb.TestAllTypes{},
121 &testpb.TestAllTypes{OptionalFixed32: scalar.Uint32(0)},
122 },
123 {
124 &testpb.TestAllTypes{},
125 &testpb.TestAllTypes{OptionalFixed64: scalar.Uint64(0)},
126 },
127 {
128 &testpb.TestAllTypes{},
129 &testpb.TestAllTypes{OptionalSfixed32: scalar.Int32(0)},
130 },
131 {
132 &testpb.TestAllTypes{},
133 &testpb.TestAllTypes{OptionalSfixed64: scalar.Int64(0)},
134 },
135 {
136 &testpb.TestAllTypes{},
137 &testpb.TestAllTypes{OptionalFloat: scalar.Float32(0)},
138 },
139 {
140 &testpb.TestAllTypes{},
141 &testpb.TestAllTypes{OptionalDouble: scalar.Float64(0)},
142 },
143 {
144 &testpb.TestAllTypes{},
145 &testpb.TestAllTypes{OptionalBool: scalar.Bool(false)},
146 },
147 {
148 &testpb.TestAllTypes{},
149 &testpb.TestAllTypes{OptionalString: scalar.String("")},
150 },
151 {
152 &testpb.TestAllTypes{},
153 &testpb.TestAllTypes{OptionalBytes: []byte{}},
154 },
155 {
156 &testpb.TestAllTypes{},
157 &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_FOO.Enum()},
158 },
159 // Groups.
160 {
161 &testpb.TestAllTypes{Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
162 A: scalar.Int32(1),
163 }},
164 &testpb.TestAllTypes{Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
165 A: scalar.Int32(2),
166 }},
167 },
168 {
169 &testpb.TestAllTypes{},
170 &testpb.TestAllTypes{Optionalgroup: &testpb.TestAllTypes_OptionalGroup{}},
171 },
172 // Messages.
173 {
174 &testpb.TestAllTypes{OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
175 A: scalar.Int32(1),
176 }},
177 &testpb.TestAllTypes{OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
178 A: scalar.Int32(2),
179 }},
180 },
181 {
182 &testpb.TestAllTypes{},
183 &testpb.TestAllTypes{OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{}},
184 },
185 {
186 &test3pb.TestAllTypes{},
187 &test3pb.TestAllTypes{OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{}},
188 },
189 // Lists.
190 {
191 &testpb.TestAllTypes{RepeatedInt32: []int32{1}},
192 &testpb.TestAllTypes{RepeatedInt32: []int32{1, 2}},
193 },
194 {
195 &testpb.TestAllTypes{RepeatedInt32: []int32{1, 2}},
196 &testpb.TestAllTypes{RepeatedInt32: []int32{1, 3}},
197 },
198 {
199 &testpb.TestAllTypes{RepeatedInt64: []int64{1, 2}},
200 &testpb.TestAllTypes{RepeatedInt64: []int64{1, 3}},
201 },
202 {
203 &testpb.TestAllTypes{RepeatedUint32: []uint32{1, 2}},
204 &testpb.TestAllTypes{RepeatedUint32: []uint32{1, 3}},
205 },
206 {
207 &testpb.TestAllTypes{RepeatedUint64: []uint64{1, 2}},
208 &testpb.TestAllTypes{RepeatedUint64: []uint64{1, 3}},
209 },
210 {
211 &testpb.TestAllTypes{RepeatedSint32: []int32{1, 2}},
212 &testpb.TestAllTypes{RepeatedSint32: []int32{1, 3}},
213 },
214 {
215 &testpb.TestAllTypes{RepeatedSint64: []int64{1, 2}},
216 &testpb.TestAllTypes{RepeatedSint64: []int64{1, 3}},
217 },
218 {
219 &testpb.TestAllTypes{RepeatedFixed32: []uint32{1, 2}},
220 &testpb.TestAllTypes{RepeatedFixed32: []uint32{1, 3}},
221 },
222 {
223 &testpb.TestAllTypes{RepeatedFixed64: []uint64{1, 2}},
224 &testpb.TestAllTypes{RepeatedFixed64: []uint64{1, 3}},
225 },
226 {
227 &testpb.TestAllTypes{RepeatedSfixed32: []int32{1, 2}},
228 &testpb.TestAllTypes{RepeatedSfixed32: []int32{1, 3}},
229 },
230 {
231 &testpb.TestAllTypes{RepeatedSfixed64: []int64{1, 2}},
232 &testpb.TestAllTypes{RepeatedSfixed64: []int64{1, 3}},
233 },
234 {
235 &testpb.TestAllTypes{RepeatedFloat: []float32{1, 2}},
236 &testpb.TestAllTypes{RepeatedFloat: []float32{1, 3}},
237 },
238 {
239 &testpb.TestAllTypes{RepeatedDouble: []float64{1, 2}},
240 &testpb.TestAllTypes{RepeatedDouble: []float64{1, 3}},
241 },
242 {
243 &testpb.TestAllTypes{RepeatedBool: []bool{true, false}},
244 &testpb.TestAllTypes{RepeatedBool: []bool{true, true}},
245 },
246 {
247 &testpb.TestAllTypes{RepeatedString: []string{"a", "b"}},
248 &testpb.TestAllTypes{RepeatedString: []string{"a", "c"}},
249 },
250 {
251 &testpb.TestAllTypes{RepeatedBytes: [][]byte{[]byte("a"), []byte("b")}},
252 &testpb.TestAllTypes{RepeatedBytes: [][]byte{[]byte("a"), []byte("c")}},
253 },
254 {
255 &testpb.TestAllTypes{RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO}},
256 &testpb.TestAllTypes{RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_BAR}},
257 },
258 {
259 &testpb.TestAllTypes{Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
260 {A: scalar.Int32(1)},
261 {A: scalar.Int32(2)},
262 }},
263 &testpb.TestAllTypes{Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
264 {A: scalar.Int32(1)},
265 {A: scalar.Int32(3)},
266 }},
267 },
268 {
269 &testpb.TestAllTypes{RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
270 {A: scalar.Int32(1)},
271 {A: scalar.Int32(2)},
272 }},
273 &testpb.TestAllTypes{RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
274 {A: scalar.Int32(1)},
275 {A: scalar.Int32(3)},
276 }},
277 },
278 // Maps: various configurations.
279 {
280 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
281 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{3: 4}},
282 },
283 {
284 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
285 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
286 },
287 {
288 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
289 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
290 },
291 // Maps: various types.
292 {
293 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
294 &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 5}},
295 },
296 {
297 &testpb.TestAllTypes{MapInt64Int64: map[int64]int64{1: 2, 3: 4}},
298 &testpb.TestAllTypes{MapInt64Int64: map[int64]int64{1: 2, 3: 5}},
299 },
300 {
301 &testpb.TestAllTypes{MapUint32Uint32: map[uint32]uint32{1: 2, 3: 4}},
302 &testpb.TestAllTypes{MapUint32Uint32: map[uint32]uint32{1: 2, 3: 5}},
303 },
304 {
305 &testpb.TestAllTypes{MapUint64Uint64: map[uint64]uint64{1: 2, 3: 4}},
306 &testpb.TestAllTypes{MapUint64Uint64: map[uint64]uint64{1: 2, 3: 5}},
307 },
308 {
309 &testpb.TestAllTypes{MapSint32Sint32: map[int32]int32{1: 2, 3: 4}},
310 &testpb.TestAllTypes{MapSint32Sint32: map[int32]int32{1: 2, 3: 5}},
311 },
312 {
313 &testpb.TestAllTypes{MapSint64Sint64: map[int64]int64{1: 2, 3: 4}},
314 &testpb.TestAllTypes{MapSint64Sint64: map[int64]int64{1: 2, 3: 5}},
315 },
316 {
317 &testpb.TestAllTypes{MapFixed32Fixed32: map[uint32]uint32{1: 2, 3: 4}},
318 &testpb.TestAllTypes{MapFixed32Fixed32: map[uint32]uint32{1: 2, 3: 5}},
319 },
320 {
321 &testpb.TestAllTypes{MapFixed64Fixed64: map[uint64]uint64{1: 2, 3: 4}},
322 &testpb.TestAllTypes{MapFixed64Fixed64: map[uint64]uint64{1: 2, 3: 5}},
323 },
324 {
325 &testpb.TestAllTypes{MapSfixed32Sfixed32: map[int32]int32{1: 2, 3: 4}},
326 &testpb.TestAllTypes{MapSfixed32Sfixed32: map[int32]int32{1: 2, 3: 5}},
327 },
328 {
329 &testpb.TestAllTypes{MapSfixed64Sfixed64: map[int64]int64{1: 2, 3: 4}},
330 &testpb.TestAllTypes{MapSfixed64Sfixed64: map[int64]int64{1: 2, 3: 5}},
331 },
332 {
333 &testpb.TestAllTypes{MapInt32Float: map[int32]float32{1: 2, 3: 4}},
334 &testpb.TestAllTypes{MapInt32Float: map[int32]float32{1: 2, 3: 5}},
335 },
336 {
337 &testpb.TestAllTypes{MapInt32Double: map[int32]float64{1: 2, 3: 4}},
338 &testpb.TestAllTypes{MapInt32Double: map[int32]float64{1: 2, 3: 5}},
339 },
340 {
341 &testpb.TestAllTypes{MapBoolBool: map[bool]bool{true: false, false: true}},
342 &testpb.TestAllTypes{MapBoolBool: map[bool]bool{true: false, false: false}},
343 },
344 {
345 &testpb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "d"}},
346 &testpb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "e"}},
347 },
348 {
349 &testpb.TestAllTypes{MapStringBytes: map[string][]byte{"a": []byte("b"), "c": []byte("d")}},
350 &testpb.TestAllTypes{MapStringBytes: map[string][]byte{"a": []byte("b"), "c": []byte("e")}},
351 },
352 {
353 &testpb.TestAllTypes{MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
354 "a": {A: scalar.Int32(1)},
355 "b": {A: scalar.Int32(2)},
356 }},
357 &testpb.TestAllTypes{MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
358 "a": {A: scalar.Int32(1)},
359 "b": {A: scalar.Int32(3)},
360 }},
361 },
362 {
363 &testpb.TestAllTypes{MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
364 "a": testpb.TestAllTypes_FOO,
365 "b": testpb.TestAllTypes_BAR,
366 }},
367 &testpb.TestAllTypes{MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
368 "a": testpb.TestAllTypes_FOO,
369 "b": testpb.TestAllTypes_BAZ,
370 }},
371 },
372 // Unknown fields.
373 {
374 build(&testpb.TestAllTypes{}, unknown(100000, pack.Message{
375 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
376 }.Marshal())),
377 build(&testpb.TestAllTypes{}, unknown(100000, pack.Message{
378 pack.Tag{100000, pack.VarintType}, pack.Varint(2),
379 }.Marshal())),
380 },
381 {
382 build(&testpb.TestAllTypes{}, unknown(100000, pack.Message{
383 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
384 }.Marshal())),
385 &testpb.TestAllTypes{},
386 },
387 {
388 &testpb.TestAllTypes{},
389 build(&testpb.TestAllTypes{}, unknown(100000, pack.Message{
390 pack.Tag{100000, pack.VarintType}, pack.Varint(1),
391 }.Marshal())),
392 },
393 // Extensions.
394 {
395 build(&testpb.TestAllExtensions{},
396 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1)),
397 ),
398 build(&testpb.TestAllExtensions{},
399 extend(testpb.E_OptionalInt32Extension, scalar.Int32(2)),
400 ),
401 },
402 {
403 build(&testpb.TestAllExtensions{},
404 registerExtension(testpb.E_OptionalInt32Extension),
405 ),
406 build(&testpb.TestAllExtensions{},
407 extend(testpb.E_OptionalInt32Extension, scalar.Int32(2)),
408 ),
409 },
410 {
411 build(&testpb.TestAllExtensions{},
412 extend(testpb.E_OptionalInt32Extension, scalar.Int32(1)),
413 ),
414 build(&testpb.TestAllExtensions{},
415 registerExtension(testpb.E_OptionalInt32Extension),
416 ),
417 },
418 {
419 &testpb.TestAllExtensions{},
420 build(&testpb.TestAllExtensions{},
421 extend(testpb.E_OptionalInt32Extension, scalar.Int32(2)),
422 ),
423 },
424 {
425 &testpb.TestAllExtensions{},
426 build(&testpb.TestAllExtensions{},
427 registerExtension(testpb.E_OptionalInt32Extension),
428 ),
429 },
430 // Proto2 default values are not considered by Equal, so the following are still unequal.
431 {
432 &testpb.TestAllTypes{DefaultInt32: scalar.Int32(81)},
433 &testpb.TestAllTypes{},
434 },
435 {
436 &testpb.TestAllTypes{},
437 &testpb.TestAllTypes{DefaultInt32: scalar.Int32(81)},
438 },
439 {
440 &testpb.TestAllTypes{},
441 &testpb.TestAllTypes{DefaultInt64: scalar.Int64(82)},
442 },
443 {
444 &testpb.TestAllTypes{},
445 &testpb.TestAllTypes{DefaultUint32: scalar.Uint32(83)},
446 },
447 {
448 &testpb.TestAllTypes{},
449 &testpb.TestAllTypes{DefaultUint64: scalar.Uint64(84)},
450 },
451 {
452 &testpb.TestAllTypes{},
453 &testpb.TestAllTypes{DefaultSint32: scalar.Int32(-85)},
454 },
455 {
456 &testpb.TestAllTypes{},
457 &testpb.TestAllTypes{DefaultSint64: scalar.Int64(86)},
458 },
459 {
460 &testpb.TestAllTypes{},
461 &testpb.TestAllTypes{DefaultFixed32: scalar.Uint32(87)},
462 },
463 {
464 &testpb.TestAllTypes{},
465 &testpb.TestAllTypes{DefaultFixed64: scalar.Uint64(88)},
466 },
467 {
468 &testpb.TestAllTypes{},
469 &testpb.TestAllTypes{DefaultSfixed32: scalar.Int32(89)},
470 },
471 {
472 &testpb.TestAllTypes{},
473 &testpb.TestAllTypes{DefaultSfixed64: scalar.Int64(-90)},
474 },
475 {
476 &testpb.TestAllTypes{},
477 &testpb.TestAllTypes{DefaultFloat: scalar.Float32(91.5)},
478 },
479 {
480 &testpb.TestAllTypes{},
481 &testpb.TestAllTypes{DefaultDouble: scalar.Float64(92e3)},
482 },
483 {
484 &testpb.TestAllTypes{},
485 &testpb.TestAllTypes{DefaultBool: scalar.Bool(true)},
486 },
487 {
488 &testpb.TestAllTypes{},
489 &testpb.TestAllTypes{DefaultString: scalar.String("hello")},
490 },
491 {
492 &testpb.TestAllTypes{},
493 &testpb.TestAllTypes{DefaultBytes: []byte("world")},
494 },
495 {
496 &testpb.TestAllTypes{},
497 &testpb.TestAllTypes{DefaultNestedEnum: testpb.TestAllTypes_BAR.Enum()},
498 },
499 // Extension ddefault values are not considered by Equal, so the following are still unequal.
500 {
501 build(&testpb.TestAllExtensions{},
502 registerExtension(testpb.E_DefaultInt32Extension),
503 ),
504 build(&testpb.TestAllExtensions{},
505 extend(testpb.E_DefaultInt32Extension, scalar.Int32(81)),
506 ),
507 },
508 {
509 build(&testpb.TestAllExtensions{},
510 extend(testpb.E_DefaultInt32Extension, scalar.Int32(81)),
511 ),
512 build(&testpb.TestAllExtensions{},
513 registerExtension(testpb.E_DefaultInt32Extension),
514 ),
515 },
516}