blob: babd52b9ea6e583815c3fe19a98cc0d6e0559e76 [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 (
Joe Tsai6bd33b62019-07-15 13:08:00 -07008 "math"
Damien Neile6f060f2019-04-23 17:11:02 -07009 "testing"
10
Joe Tsai74b14602020-01-06 15:44:09 -080011 "google.golang.org/protobuf/encoding/prototext"
Mitko Asenov380c3392022-06-09 11:53:27 +020012 "google.golang.org/protobuf/internal/pragma"
Joe Tsaif2c4ddc2019-09-19 21:28:52 -070013 "google.golang.org/protobuf/proto"
Joe Tsaicfd80492020-02-14 18:13:14 -080014 "google.golang.org/protobuf/testing/protopack"
Joe Tsaif2c4ddc2019-09-19 21:28:52 -070015
Damien Neile89e6242019-05-13 23:55:40 -070016 testpb "google.golang.org/protobuf/internal/testprotos/test"
17 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Lasse Folger19445772024-02-20 16:52:39 +010018 testeditionspb "google.golang.org/protobuf/internal/testprotos/testeditions"
Damien Neile6f060f2019-04-23 17:11:02 -070019)
20
21func TestEqual(t *testing.T) {
Mitko Asenov380c3392022-06-09 11:53:27 +020022 identicalPtrPb := &testpb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "d"}}
23
24 type incomparableMessage struct {
25 *testpb.TestAllTypes
26 pragma.DoNotCompare
27 }
28
Joe Tsaif2c4ddc2019-09-19 21:28:52 -070029 tests := []struct {
30 x, y proto.Message
31 eq bool
32 }{
33 {
34 x: nil,
35 y: nil,
36 eq: true,
37 }, {
38 x: (*testpb.TestAllTypes)(nil),
39 y: nil,
40 eq: false,
41 }, {
42 x: (*testpb.TestAllTypes)(nil),
Joe Tsai96a44732020-01-03 19:52:28 -080043 y: (*testpb.TestAllTypes)(nil),
44 eq: true,
45 }, {
46 x: new(testpb.TestAllTypes),
47 y: (*testpb.TestAllTypes)(nil),
48 eq: false,
49 }, {
50 x: new(testpb.TestAllTypes),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -070051 y: new(testpb.TestAllTypes),
52 eq: true,
Tzu-Chiao Yehf62736d2020-09-06 08:54:02 +080053 }, {
Damien Neil2ad3f242020-01-06 15:12:19 -080054 x: (*testpb.TestAllTypes)(nil),
55 y: (*testpb.TestAllExtensions)(nil),
56 eq: false,
Tzu-Chiao Yehf62736d2020-09-06 08:54:02 +080057 }, {
Damien Neil2ad3f242020-01-06 15:12:19 -080058 x: (*testpb.TestAllTypes)(nil),
59 y: new(testpb.TestAllExtensions),
60 eq: false,
Tzu-Chiao Yehf62736d2020-09-06 08:54:02 +080061 }, {
Damien Neil2ad3f242020-01-06 15:12:19 -080062 x: new(testpb.TestAllTypes),
63 y: new(testpb.TestAllExtensions),
64 eq: false,
65 },
Joe Tsaif2c4ddc2019-09-19 21:28:52 -070066
Mitko Asenov380c3392022-06-09 11:53:27 +020067 // Identical input pointers
68 {
69 x: identicalPtrPb,
70 y: identicalPtrPb,
71 eq: true,
72 },
73
74 // Incomparable types. The top-level types are not actually directly
75 // compared (which would panic), but rather the comparison happens on the
76 // objects returned by ProtoReflect(). These tests are here just to ensure
77 // that any short-circuit checks do not accidentally try to compare
78 // incomparable top-level types.
79 {
80 x: incomparableMessage{TestAllTypes: identicalPtrPb},
81 y: incomparableMessage{TestAllTypes: identicalPtrPb},
82 eq: true,
83 },
84 {
85 x: identicalPtrPb,
86 y: incomparableMessage{TestAllTypes: identicalPtrPb},
87 eq: true,
88 },
89 {
90 x: identicalPtrPb,
91 y: &incomparableMessage{TestAllTypes: identicalPtrPb},
92 eq: true,
93 },
94
Joe Tsaif2c4ddc2019-09-19 21:28:52 -070095 // Proto2 scalars.
96 {
97 x: &testpb.TestAllTypes{OptionalInt32: proto.Int32(1)},
98 y: &testpb.TestAllTypes{OptionalInt32: proto.Int32(2)},
99 }, {
100 x: &testpb.TestAllTypes{OptionalInt64: proto.Int64(1)},
101 y: &testpb.TestAllTypes{OptionalInt64: proto.Int64(2)},
102 }, {
103 x: &testpb.TestAllTypes{OptionalUint32: proto.Uint32(1)},
104 y: &testpb.TestAllTypes{OptionalUint32: proto.Uint32(2)},
105 }, {
106 x: &testpb.TestAllTypes{OptionalUint64: proto.Uint64(1)},
107 y: &testpb.TestAllTypes{OptionalUint64: proto.Uint64(2)},
108 }, {
109 x: &testpb.TestAllTypes{OptionalSint32: proto.Int32(1)},
110 y: &testpb.TestAllTypes{OptionalSint32: proto.Int32(2)},
111 }, {
112 x: &testpb.TestAllTypes{OptionalSint64: proto.Int64(1)},
113 y: &testpb.TestAllTypes{OptionalSint64: proto.Int64(2)},
114 }, {
115 x: &testpb.TestAllTypes{OptionalFixed32: proto.Uint32(1)},
116 y: &testpb.TestAllTypes{OptionalFixed32: proto.Uint32(2)},
117 }, {
118 x: &testpb.TestAllTypes{OptionalFixed64: proto.Uint64(1)},
119 y: &testpb.TestAllTypes{OptionalFixed64: proto.Uint64(2)},
120 }, {
121 x: &testpb.TestAllTypes{OptionalSfixed32: proto.Int32(1)},
122 y: &testpb.TestAllTypes{OptionalSfixed32: proto.Int32(2)},
123 }, {
124 x: &testpb.TestAllTypes{OptionalSfixed64: proto.Int64(1)},
125 y: &testpb.TestAllTypes{OptionalSfixed64: proto.Int64(2)},
126 }, {
127 x: &testpb.TestAllTypes{OptionalFloat: proto.Float32(1)},
128 y: &testpb.TestAllTypes{OptionalFloat: proto.Float32(2)},
129 }, {
130 x: &testpb.TestAllTypes{OptionalDouble: proto.Float64(1)},
131 y: &testpb.TestAllTypes{OptionalDouble: proto.Float64(2)},
132 }, {
133 x: &testpb.TestAllTypes{OptionalFloat: proto.Float32(float32(math.NaN()))},
134 y: &testpb.TestAllTypes{OptionalFloat: proto.Float32(0)},
135 }, {
136 x: &testpb.TestAllTypes{OptionalDouble: proto.Float64(float64(math.NaN()))},
137 y: &testpb.TestAllTypes{OptionalDouble: proto.Float64(0)},
138 }, {
139 x: &testpb.TestAllTypes{OptionalBool: proto.Bool(true)},
140 y: &testpb.TestAllTypes{OptionalBool: proto.Bool(false)},
141 }, {
142 x: &testpb.TestAllTypes{OptionalString: proto.String("a")},
143 y: &testpb.TestAllTypes{OptionalString: proto.String("b")},
144 }, {
145 x: &testpb.TestAllTypes{OptionalBytes: []byte("a")},
146 y: &testpb.TestAllTypes{OptionalBytes: []byte("b")},
147 }, {
148 x: &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_FOO.Enum()},
149 y: &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum()},
Tzu-Chiao Yehf62736d2020-09-06 08:54:02 +0800150 }, {
151 x: &testpb.TestAllTypes{OptionalInt32: proto.Int32(2)},
152 y: &testpb.TestAllTypes{OptionalInt32: proto.Int32(2)},
153 eq: true,
154 }, {
155 x: &testpb.TestAllTypes{OptionalInt64: proto.Int64(2)},
156 y: &testpb.TestAllTypes{OptionalInt64: proto.Int64(2)},
157 eq: true,
158 }, {
159 x: &testpb.TestAllTypes{OptionalUint32: proto.Uint32(2)},
160 y: &testpb.TestAllTypes{OptionalUint32: proto.Uint32(2)},
161 eq: true,
162 }, {
163 x: &testpb.TestAllTypes{OptionalUint64: proto.Uint64(2)},
164 y: &testpb.TestAllTypes{OptionalUint64: proto.Uint64(2)},
165 eq: true,
166 }, {
167 x: &testpb.TestAllTypes{OptionalSint32: proto.Int32(2)},
168 y: &testpb.TestAllTypes{OptionalSint32: proto.Int32(2)},
169 eq: true,
170 }, {
171 x: &testpb.TestAllTypes{OptionalSint64: proto.Int64(2)},
172 y: &testpb.TestAllTypes{OptionalSint64: proto.Int64(2)},
173 eq: true,
174 }, {
175 x: &testpb.TestAllTypes{OptionalFixed32: proto.Uint32(2)},
176 y: &testpb.TestAllTypes{OptionalFixed32: proto.Uint32(2)},
177 eq: true,
178 }, {
179 x: &testpb.TestAllTypes{OptionalFixed64: proto.Uint64(2)},
180 y: &testpb.TestAllTypes{OptionalFixed64: proto.Uint64(2)},
181 eq: true,
182 }, {
183 x: &testpb.TestAllTypes{OptionalSfixed32: proto.Int32(2)},
184 y: &testpb.TestAllTypes{OptionalSfixed32: proto.Int32(2)},
185 eq: true,
186 }, {
187 x: &testpb.TestAllTypes{OptionalSfixed64: proto.Int64(2)},
188 y: &testpb.TestAllTypes{OptionalSfixed64: proto.Int64(2)},
189 eq: true,
190 }, {
191 x: &testpb.TestAllTypes{OptionalFloat: proto.Float32(2)},
192 y: &testpb.TestAllTypes{OptionalFloat: proto.Float32(2)},
193 eq: true,
194 }, {
195 x: &testpb.TestAllTypes{OptionalDouble: proto.Float64(2)},
196 y: &testpb.TestAllTypes{OptionalDouble: proto.Float64(2)},
197 eq: true,
198 }, {
199 x: &testpb.TestAllTypes{OptionalFloat: proto.Float32(float32(math.NaN()))},
200 y: &testpb.TestAllTypes{OptionalFloat: proto.Float32(float32(math.NaN()))},
201 eq: true,
202 }, {
203 x: &testpb.TestAllTypes{OptionalDouble: proto.Float64(float64(math.NaN()))},
204 y: &testpb.TestAllTypes{OptionalDouble: proto.Float64(float64(math.NaN()))},
205 eq: true,
206 }, {
207 x: &testpb.TestAllTypes{OptionalBool: proto.Bool(true)},
208 y: &testpb.TestAllTypes{OptionalBool: proto.Bool(true)},
209 eq: true,
210 }, {
211 x: &testpb.TestAllTypes{OptionalString: proto.String("abc")},
212 y: &testpb.TestAllTypes{OptionalString: proto.String("abc")},
213 eq: true,
214 }, {
215 x: &testpb.TestAllTypes{OptionalBytes: []byte("abc")},
216 y: &testpb.TestAllTypes{OptionalBytes: []byte("abc")},
217 eq: true,
218 }, {
219 x: &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_FOO.Enum()},
220 y: &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_FOO.Enum()},
221 eq: true,
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700222 },
223
Lasse Folger19445772024-02-20 16:52:39 +0100224 // Editions scalars.
225 {
226 x: &testeditionspb.TestAllTypes{OptionalInt32: proto.Int32(1)},
227 y: &testeditionspb.TestAllTypes{OptionalInt32: proto.Int32(2)},
228 }, {
229 x: &testeditionspb.TestAllTypes{OptionalInt64: proto.Int64(1)},
230 y: &testeditionspb.TestAllTypes{OptionalInt64: proto.Int64(2)},
231 }, {
232 x: &testeditionspb.TestAllTypes{OptionalUint32: proto.Uint32(1)},
233 y: &testeditionspb.TestAllTypes{OptionalUint32: proto.Uint32(2)},
234 }, {
235 x: &testeditionspb.TestAllTypes{OptionalUint64: proto.Uint64(1)},
236 y: &testeditionspb.TestAllTypes{OptionalUint64: proto.Uint64(2)},
237 }, {
238 x: &testeditionspb.TestAllTypes{OptionalSint32: proto.Int32(1)},
239 y: &testeditionspb.TestAllTypes{OptionalSint32: proto.Int32(2)},
240 }, {
241 x: &testeditionspb.TestAllTypes{OptionalSint64: proto.Int64(1)},
242 y: &testeditionspb.TestAllTypes{OptionalSint64: proto.Int64(2)},
243 }, {
244 x: &testeditionspb.TestAllTypes{OptionalFixed32: proto.Uint32(1)},
245 y: &testeditionspb.TestAllTypes{OptionalFixed32: proto.Uint32(2)},
246 }, {
247 x: &testeditionspb.TestAllTypes{OptionalFixed64: proto.Uint64(1)},
248 y: &testeditionspb.TestAllTypes{OptionalFixed64: proto.Uint64(2)},
249 }, {
250 x: &testeditionspb.TestAllTypes{OptionalSfixed32: proto.Int32(1)},
251 y: &testeditionspb.TestAllTypes{OptionalSfixed32: proto.Int32(2)},
252 }, {
253 x: &testeditionspb.TestAllTypes{OptionalSfixed64: proto.Int64(1)},
254 y: &testeditionspb.TestAllTypes{OptionalSfixed64: proto.Int64(2)},
255 }, {
256 x: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(1)},
257 y: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(2)},
258 }, {
259 x: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(1)},
260 y: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(2)},
261 }, {
262 x: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(float32(math.NaN()))},
263 y: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(0)},
264 }, {
265 x: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(float64(math.NaN()))},
266 y: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(0)},
267 }, {
268 x: &testeditionspb.TestAllTypes{OptionalBool: proto.Bool(true)},
269 y: &testeditionspb.TestAllTypes{OptionalBool: proto.Bool(false)},
270 }, {
271 x: &testeditionspb.TestAllTypes{OptionalString: proto.String("a")},
272 y: &testeditionspb.TestAllTypes{OptionalString: proto.String("b")},
273 }, {
274 x: &testeditionspb.TestAllTypes{OptionalBytes: []byte("a")},
275 y: &testeditionspb.TestAllTypes{OptionalBytes: []byte("b")},
276 }, {
277 x: &testeditionspb.TestAllTypes{OptionalNestedEnum: testeditionspb.TestAllTypes_FOO.Enum()},
278 y: &testeditionspb.TestAllTypes{OptionalNestedEnum: testeditionspb.TestAllTypes_BAR.Enum()},
279 }, {
280 x: &testeditionspb.TestAllTypes{OptionalInt32: proto.Int32(2)},
281 y: &testeditionspb.TestAllTypes{OptionalInt32: proto.Int32(2)},
282 eq: true,
283 }, {
284 x: &testeditionspb.TestAllTypes{OptionalInt64: proto.Int64(2)},
285 y: &testeditionspb.TestAllTypes{OptionalInt64: proto.Int64(2)},
286 eq: true,
287 }, {
288 x: &testeditionspb.TestAllTypes{OptionalUint32: proto.Uint32(2)},
289 y: &testeditionspb.TestAllTypes{OptionalUint32: proto.Uint32(2)},
290 eq: true,
291 }, {
292 x: &testeditionspb.TestAllTypes{OptionalUint64: proto.Uint64(2)},
293 y: &testeditionspb.TestAllTypes{OptionalUint64: proto.Uint64(2)},
294 eq: true,
295 }, {
296 x: &testeditionspb.TestAllTypes{OptionalSint32: proto.Int32(2)},
297 y: &testeditionspb.TestAllTypes{OptionalSint32: proto.Int32(2)},
298 eq: true,
299 }, {
300 x: &testeditionspb.TestAllTypes{OptionalSint64: proto.Int64(2)},
301 y: &testeditionspb.TestAllTypes{OptionalSint64: proto.Int64(2)},
302 eq: true,
303 }, {
304 x: &testeditionspb.TestAllTypes{OptionalFixed32: proto.Uint32(2)},
305 y: &testeditionspb.TestAllTypes{OptionalFixed32: proto.Uint32(2)},
306 eq: true,
307 }, {
308 x: &testeditionspb.TestAllTypes{OptionalFixed64: proto.Uint64(2)},
309 y: &testeditionspb.TestAllTypes{OptionalFixed64: proto.Uint64(2)},
310 eq: true,
311 }, {
312 x: &testeditionspb.TestAllTypes{OptionalSfixed32: proto.Int32(2)},
313 y: &testeditionspb.TestAllTypes{OptionalSfixed32: proto.Int32(2)},
314 eq: true,
315 }, {
316 x: &testeditionspb.TestAllTypes{OptionalSfixed64: proto.Int64(2)},
317 y: &testeditionspb.TestAllTypes{OptionalSfixed64: proto.Int64(2)},
318 eq: true,
319 }, {
320 x: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(2)},
321 y: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(2)},
322 eq: true,
323 }, {
324 x: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(2)},
325 y: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(2)},
326 eq: true,
327 }, {
328 x: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(float32(math.NaN()))},
329 y: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(float32(math.NaN()))},
330 eq: true,
331 }, {
332 x: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(float64(math.NaN()))},
333 y: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(float64(math.NaN()))},
334 eq: true,
335 }, {
336 x: &testeditionspb.TestAllTypes{OptionalBool: proto.Bool(true)},
337 y: &testeditionspb.TestAllTypes{OptionalBool: proto.Bool(true)},
338 eq: true,
339 }, {
340 x: &testeditionspb.TestAllTypes{OptionalString: proto.String("abc")},
341 y: &testeditionspb.TestAllTypes{OptionalString: proto.String("abc")},
342 eq: true,
343 }, {
344 x: &testeditionspb.TestAllTypes{OptionalBytes: []byte("abc")},
345 y: &testeditionspb.TestAllTypes{OptionalBytes: []byte("abc")},
346 eq: true,
347 }, {
348 x: &testeditionspb.TestAllTypes{OptionalNestedEnum: testeditionspb.TestAllTypes_FOO.Enum()},
349 y: &testeditionspb.TestAllTypes{OptionalNestedEnum: testeditionspb.TestAllTypes_FOO.Enum()},
350 eq: true,
351 },
352
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700353 // Proto2 presence.
354 {
355 x: &testpb.TestAllTypes{},
356 y: &testpb.TestAllTypes{OptionalInt32: proto.Int32(0)},
357 }, {
358 x: &testpb.TestAllTypes{},
359 y: &testpb.TestAllTypes{OptionalInt64: proto.Int64(0)},
360 }, {
361 x: &testpb.TestAllTypes{},
362 y: &testpb.TestAllTypes{OptionalUint32: proto.Uint32(0)},
363 }, {
364 x: &testpb.TestAllTypes{},
365 y: &testpb.TestAllTypes{OptionalUint64: proto.Uint64(0)},
366 }, {
367 x: &testpb.TestAllTypes{},
368 y: &testpb.TestAllTypes{OptionalSint32: proto.Int32(0)},
369 }, {
370 x: &testpb.TestAllTypes{},
371 y: &testpb.TestAllTypes{OptionalSint64: proto.Int64(0)},
372 }, {
373 x: &testpb.TestAllTypes{},
374 y: &testpb.TestAllTypes{OptionalFixed32: proto.Uint32(0)},
375 }, {
376 x: &testpb.TestAllTypes{},
377 y: &testpb.TestAllTypes{OptionalFixed64: proto.Uint64(0)},
378 }, {
379 x: &testpb.TestAllTypes{},
380 y: &testpb.TestAllTypes{OptionalSfixed32: proto.Int32(0)},
381 }, {
382 x: &testpb.TestAllTypes{},
383 y: &testpb.TestAllTypes{OptionalSfixed64: proto.Int64(0)},
384 }, {
385 x: &testpb.TestAllTypes{},
386 y: &testpb.TestAllTypes{OptionalFloat: proto.Float32(0)},
387 }, {
388 x: &testpb.TestAllTypes{},
389 y: &testpb.TestAllTypes{OptionalDouble: proto.Float64(0)},
390 }, {
391 x: &testpb.TestAllTypes{},
392 y: &testpb.TestAllTypes{OptionalBool: proto.Bool(false)},
393 }, {
394 x: &testpb.TestAllTypes{},
395 y: &testpb.TestAllTypes{OptionalString: proto.String("")},
396 }, {
397 x: &testpb.TestAllTypes{},
398 y: &testpb.TestAllTypes{OptionalBytes: []byte{}},
399 }, {
400 x: &testpb.TestAllTypes{},
401 y: &testpb.TestAllTypes{OptionalNestedEnum: testpb.TestAllTypes_FOO.Enum()},
402 },
403
Lasse Folger19445772024-02-20 16:52:39 +0100404 // Editions presence.
405 {
406 x: &testeditionspb.TestAllTypes{},
407 y: &testeditionspb.TestAllTypes{OptionalInt32: proto.Int32(0)},
408 }, {
409 x: &testeditionspb.TestAllTypes{},
410 y: &testeditionspb.TestAllTypes{OptionalInt64: proto.Int64(0)},
411 }, {
412 x: &testeditionspb.TestAllTypes{},
413 y: &testeditionspb.TestAllTypes{OptionalUint32: proto.Uint32(0)},
414 }, {
415 x: &testeditionspb.TestAllTypes{},
416 y: &testeditionspb.TestAllTypes{OptionalUint64: proto.Uint64(0)},
417 }, {
418 x: &testeditionspb.TestAllTypes{},
419 y: &testeditionspb.TestAllTypes{OptionalSint32: proto.Int32(0)},
420 }, {
421 x: &testeditionspb.TestAllTypes{},
422 y: &testeditionspb.TestAllTypes{OptionalSint64: proto.Int64(0)},
423 }, {
424 x: &testeditionspb.TestAllTypes{},
425 y: &testeditionspb.TestAllTypes{OptionalFixed32: proto.Uint32(0)},
426 }, {
427 x: &testeditionspb.TestAllTypes{},
428 y: &testeditionspb.TestAllTypes{OptionalFixed64: proto.Uint64(0)},
429 }, {
430 x: &testeditionspb.TestAllTypes{},
431 y: &testeditionspb.TestAllTypes{OptionalSfixed32: proto.Int32(0)},
432 }, {
433 x: &testeditionspb.TestAllTypes{},
434 y: &testeditionspb.TestAllTypes{OptionalSfixed64: proto.Int64(0)},
435 }, {
436 x: &testeditionspb.TestAllTypes{},
437 y: &testeditionspb.TestAllTypes{OptionalFloat: proto.Float32(0)},
438 }, {
439 x: &testeditionspb.TestAllTypes{},
440 y: &testeditionspb.TestAllTypes{OptionalDouble: proto.Float64(0)},
441 }, {
442 x: &testeditionspb.TestAllTypes{},
443 y: &testeditionspb.TestAllTypes{OptionalBool: proto.Bool(false)},
444 }, {
445 x: &testeditionspb.TestAllTypes{},
446 y: &testeditionspb.TestAllTypes{OptionalString: proto.String("")},
447 }, {
448 x: &testeditionspb.TestAllTypes{},
449 y: &testeditionspb.TestAllTypes{OptionalBytes: []byte{}},
450 }, {
451 x: &testeditionspb.TestAllTypes{},
452 y: &testeditionspb.TestAllTypes{OptionalNestedEnum: testeditionspb.TestAllTypes_FOO.Enum()},
453 },
454
Joe Tsai387873d2020-04-28 14:44:38 -0700455 // Proto3 presence.
456 {
457 x: &test3pb.TestAllTypes{},
458 y: &test3pb.TestAllTypes{OptionalInt32: proto.Int32(0)},
459 }, {
460 x: &test3pb.TestAllTypes{},
461 y: &test3pb.TestAllTypes{OptionalInt64: proto.Int64(0)},
462 }, {
463 x: &test3pb.TestAllTypes{},
464 y: &test3pb.TestAllTypes{OptionalUint32: proto.Uint32(0)},
465 }, {
466 x: &test3pb.TestAllTypes{},
467 y: &test3pb.TestAllTypes{OptionalUint64: proto.Uint64(0)},
468 }, {
469 x: &test3pb.TestAllTypes{},
470 y: &test3pb.TestAllTypes{OptionalSint32: proto.Int32(0)},
471 }, {
472 x: &test3pb.TestAllTypes{},
473 y: &test3pb.TestAllTypes{OptionalSint64: proto.Int64(0)},
474 }, {
475 x: &test3pb.TestAllTypes{},
476 y: &test3pb.TestAllTypes{OptionalFixed32: proto.Uint32(0)},
477 }, {
478 x: &test3pb.TestAllTypes{},
479 y: &test3pb.TestAllTypes{OptionalFixed64: proto.Uint64(0)},
480 }, {
481 x: &test3pb.TestAllTypes{},
482 y: &test3pb.TestAllTypes{OptionalSfixed32: proto.Int32(0)},
483 }, {
484 x: &test3pb.TestAllTypes{},
485 y: &test3pb.TestAllTypes{OptionalSfixed64: proto.Int64(0)},
486 }, {
487 x: &test3pb.TestAllTypes{},
488 y: &test3pb.TestAllTypes{OptionalFloat: proto.Float32(0)},
489 }, {
490 x: &test3pb.TestAllTypes{},
491 y: &test3pb.TestAllTypes{OptionalDouble: proto.Float64(0)},
492 }, {
493 x: &test3pb.TestAllTypes{},
494 y: &test3pb.TestAllTypes{OptionalBool: proto.Bool(false)},
495 }, {
496 x: &test3pb.TestAllTypes{},
497 y: &test3pb.TestAllTypes{OptionalString: proto.String("")},
498 }, {
499 x: &test3pb.TestAllTypes{},
500 y: &test3pb.TestAllTypes{OptionalBytes: []byte{}},
501 }, {
502 x: &test3pb.TestAllTypes{},
503 y: &test3pb.TestAllTypes{OptionalNestedEnum: test3pb.TestAllTypes_FOO.Enum()},
504 },
505
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700506 // Proto2 default values are not considered by Equal, so the following are still unequal.
507 {
508 x: &testpb.TestAllTypes{DefaultInt32: proto.Int32(81)},
509 y: &testpb.TestAllTypes{},
510 }, {
511 x: &testpb.TestAllTypes{},
512 y: &testpb.TestAllTypes{DefaultInt32: proto.Int32(81)},
513 }, {
514 x: &testpb.TestAllTypes{},
515 y: &testpb.TestAllTypes{DefaultInt64: proto.Int64(82)},
516 }, {
517 x: &testpb.TestAllTypes{},
518 y: &testpb.TestAllTypes{DefaultUint32: proto.Uint32(83)},
519 }, {
520 x: &testpb.TestAllTypes{},
521 y: &testpb.TestAllTypes{DefaultUint64: proto.Uint64(84)},
522 }, {
523 x: &testpb.TestAllTypes{},
524 y: &testpb.TestAllTypes{DefaultSint32: proto.Int32(-85)},
525 }, {
526 x: &testpb.TestAllTypes{},
527 y: &testpb.TestAllTypes{DefaultSint64: proto.Int64(86)},
528 }, {
529 x: &testpb.TestAllTypes{},
530 y: &testpb.TestAllTypes{DefaultFixed32: proto.Uint32(87)},
531 }, {
532 x: &testpb.TestAllTypes{},
533 y: &testpb.TestAllTypes{DefaultFixed64: proto.Uint64(88)},
534 }, {
535 x: &testpb.TestAllTypes{},
536 y: &testpb.TestAllTypes{DefaultSfixed32: proto.Int32(89)},
537 }, {
538 x: &testpb.TestAllTypes{},
539 y: &testpb.TestAllTypes{DefaultSfixed64: proto.Int64(-90)},
540 }, {
541 x: &testpb.TestAllTypes{},
542 y: &testpb.TestAllTypes{DefaultFloat: proto.Float32(91.5)},
543 }, {
544 x: &testpb.TestAllTypes{},
545 y: &testpb.TestAllTypes{DefaultDouble: proto.Float64(92e3)},
546 }, {
547 x: &testpb.TestAllTypes{},
548 y: &testpb.TestAllTypes{DefaultBool: proto.Bool(true)},
549 }, {
550 x: &testpb.TestAllTypes{},
551 y: &testpb.TestAllTypes{DefaultString: proto.String("hello")},
552 }, {
553 x: &testpb.TestAllTypes{},
554 y: &testpb.TestAllTypes{DefaultBytes: []byte("world")},
555 }, {
556 x: &testpb.TestAllTypes{},
557 y: &testpb.TestAllTypes{DefaultNestedEnum: testpb.TestAllTypes_BAR.Enum()},
558 },
559
Lasse Folger19445772024-02-20 16:52:39 +0100560 // Edition default values are not considered by Equal, so the following are still unequal.
561 {
562 x: &testeditionspb.TestAllTypes{DefaultInt32: proto.Int32(81)},
563 y: &testeditionspb.TestAllTypes{},
564 }, {
565 x: &testeditionspb.TestAllTypes{},
566 y: &testeditionspb.TestAllTypes{DefaultInt32: proto.Int32(81)},
567 }, {
568 x: &testeditionspb.TestAllTypes{},
569 y: &testeditionspb.TestAllTypes{DefaultInt64: proto.Int64(82)},
570 }, {
571 x: &testeditionspb.TestAllTypes{},
572 y: &testeditionspb.TestAllTypes{DefaultUint32: proto.Uint32(83)},
573 }, {
574 x: &testeditionspb.TestAllTypes{},
575 y: &testeditionspb.TestAllTypes{DefaultUint64: proto.Uint64(84)},
576 }, {
577 x: &testeditionspb.TestAllTypes{},
578 y: &testeditionspb.TestAllTypes{DefaultSint32: proto.Int32(-85)},
579 }, {
580 x: &testeditionspb.TestAllTypes{},
581 y: &testeditionspb.TestAllTypes{DefaultSint64: proto.Int64(86)},
582 }, {
583 x: &testeditionspb.TestAllTypes{},
584 y: &testeditionspb.TestAllTypes{DefaultFixed32: proto.Uint32(87)},
585 }, {
586 x: &testeditionspb.TestAllTypes{},
587 y: &testeditionspb.TestAllTypes{DefaultFixed64: proto.Uint64(88)},
588 }, {
589 x: &testeditionspb.TestAllTypes{},
590 y: &testeditionspb.TestAllTypes{DefaultSfixed32: proto.Int32(89)},
591 }, {
592 x: &testeditionspb.TestAllTypes{},
593 y: &testeditionspb.TestAllTypes{DefaultSfixed64: proto.Int64(-90)},
594 }, {
595 x: &testeditionspb.TestAllTypes{},
596 y: &testeditionspb.TestAllTypes{DefaultFloat: proto.Float32(91.5)},
597 }, {
598 x: &testeditionspb.TestAllTypes{},
599 y: &testeditionspb.TestAllTypes{DefaultDouble: proto.Float64(92e3)},
600 }, {
601 x: &testeditionspb.TestAllTypes{},
602 y: &testeditionspb.TestAllTypes{DefaultBool: proto.Bool(true)},
603 }, {
604 x: &testeditionspb.TestAllTypes{},
605 y: &testeditionspb.TestAllTypes{DefaultString: proto.String("hello")},
606 }, {
607 x: &testeditionspb.TestAllTypes{},
608 y: &testeditionspb.TestAllTypes{DefaultBytes: []byte("world")},
609 }, {
610 x: &testeditionspb.TestAllTypes{},
611 y: &testeditionspb.TestAllTypes{DefaultNestedEnum: testeditionspb.TestAllTypes_BAR.Enum()},
612 },
613
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700614 // Groups.
615 {
616 x: &testpb.TestAllTypes{Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
617 A: proto.Int32(1),
618 }},
619 y: &testpb.TestAllTypes{Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
620 A: proto.Int32(2),
621 }},
622 }, {
623 x: &testpb.TestAllTypes{},
624 y: &testpb.TestAllTypes{Optionalgroup: &testpb.TestAllTypes_OptionalGroup{}},
Lasse Folger19445772024-02-20 16:52:39 +0100625 }, {
626 x: &testeditionspb.TestAllTypes{Optionalgroup: &testeditionspb.TestAllTypes_OptionalGroup{
627 A: proto.Int32(1),
628 }},
629 y: &testeditionspb.TestAllTypes{Optionalgroup: &testeditionspb.TestAllTypes_OptionalGroup{
630 A: proto.Int32(2),
631 }},
632 }, {
633 x: &testeditionspb.TestAllTypes{},
634 y: &testeditionspb.TestAllTypes{Optionalgroup: &testeditionspb.TestAllTypes_OptionalGroup{}},
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700635 },
636
637 // Messages.
638 {
639 x: &testpb.TestAllTypes{OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
640 A: proto.Int32(1),
641 }},
642 y: &testpb.TestAllTypes{OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
643 A: proto.Int32(2),
644 }},
645 }, {
646 x: &testpb.TestAllTypes{},
647 y: &testpb.TestAllTypes{OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{}},
648 }, {
Lasse Folger19445772024-02-20 16:52:39 +0100649 x: &testeditionspb.TestAllTypes{},
650 y: &testeditionspb.TestAllTypes{OptionalNestedMessage: &testeditionspb.TestAllTypes_NestedMessage{}},
651 }, {
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700652 x: &test3pb.TestAllTypes{},
653 y: &test3pb.TestAllTypes{OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{}},
654 },
655
656 // Lists.
657 {
658 x: &testpb.TestAllTypes{RepeatedInt32: []int32{1}},
659 y: &testpb.TestAllTypes{RepeatedInt32: []int32{1, 2}},
660 }, {
661 x: &testpb.TestAllTypes{RepeatedInt32: []int32{1, 2}},
662 y: &testpb.TestAllTypes{RepeatedInt32: []int32{1, 3}},
663 }, {
664 x: &testpb.TestAllTypes{RepeatedInt64: []int64{1, 2}},
665 y: &testpb.TestAllTypes{RepeatedInt64: []int64{1, 3}},
666 }, {
667 x: &testpb.TestAllTypes{RepeatedUint32: []uint32{1, 2}},
668 y: &testpb.TestAllTypes{RepeatedUint32: []uint32{1, 3}},
669 }, {
670 x: &testpb.TestAllTypes{RepeatedUint64: []uint64{1, 2}},
671 y: &testpb.TestAllTypes{RepeatedUint64: []uint64{1, 3}},
672 }, {
673 x: &testpb.TestAllTypes{RepeatedSint32: []int32{1, 2}},
674 y: &testpb.TestAllTypes{RepeatedSint32: []int32{1, 3}},
675 }, {
676 x: &testpb.TestAllTypes{RepeatedSint64: []int64{1, 2}},
677 y: &testpb.TestAllTypes{RepeatedSint64: []int64{1, 3}},
678 }, {
679 x: &testpb.TestAllTypes{RepeatedFixed32: []uint32{1, 2}},
680 y: &testpb.TestAllTypes{RepeatedFixed32: []uint32{1, 3}},
681 }, {
682 x: &testpb.TestAllTypes{RepeatedFixed64: []uint64{1, 2}},
683 y: &testpb.TestAllTypes{RepeatedFixed64: []uint64{1, 3}},
684 }, {
685 x: &testpb.TestAllTypes{RepeatedSfixed32: []int32{1, 2}},
686 y: &testpb.TestAllTypes{RepeatedSfixed32: []int32{1, 3}},
687 }, {
688 x: &testpb.TestAllTypes{RepeatedSfixed64: []int64{1, 2}},
689 y: &testpb.TestAllTypes{RepeatedSfixed64: []int64{1, 3}},
690 }, {
691 x: &testpb.TestAllTypes{RepeatedFloat: []float32{1, 2}},
692 y: &testpb.TestAllTypes{RepeatedFloat: []float32{1, 3}},
693 }, {
694 x: &testpb.TestAllTypes{RepeatedDouble: []float64{1, 2}},
695 y: &testpb.TestAllTypes{RepeatedDouble: []float64{1, 3}},
696 }, {
697 x: &testpb.TestAllTypes{RepeatedBool: []bool{true, false}},
698 y: &testpb.TestAllTypes{RepeatedBool: []bool{true, true}},
699 }, {
700 x: &testpb.TestAllTypes{RepeatedString: []string{"a", "b"}},
701 y: &testpb.TestAllTypes{RepeatedString: []string{"a", "c"}},
702 }, {
703 x: &testpb.TestAllTypes{RepeatedBytes: [][]byte{[]byte("a"), []byte("b")}},
704 y: &testpb.TestAllTypes{RepeatedBytes: [][]byte{[]byte("a"), []byte("c")}},
705 }, {
706 x: &testpb.TestAllTypes{RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO}},
707 y: &testpb.TestAllTypes{RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_BAR}},
708 }, {
709 x: &testpb.TestAllTypes{Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
710 {A: proto.Int32(1)},
711 {A: proto.Int32(2)},
712 }},
713 y: &testpb.TestAllTypes{Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
714 {A: proto.Int32(1)},
715 {A: proto.Int32(3)},
716 }},
717 }, {
718 x: &testpb.TestAllTypes{RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
719 {A: proto.Int32(1)},
720 {A: proto.Int32(2)},
721 }},
722 y: &testpb.TestAllTypes{RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
723 {A: proto.Int32(1)},
724 {A: proto.Int32(3)},
725 }},
726 },
727
Lasse Folger19445772024-02-20 16:52:39 +0100728 // Editions Lists.
729 {
730 x: &testeditionspb.TestAllTypes{RepeatedInt32: []int32{1}},
731 y: &testeditionspb.TestAllTypes{RepeatedInt32: []int32{1, 2}},
732 }, {
733 x: &testeditionspb.TestAllTypes{RepeatedInt32: []int32{1, 2}},
734 y: &testeditionspb.TestAllTypes{RepeatedInt32: []int32{1, 3}},
735 }, {
736 x: &testeditionspb.TestAllTypes{RepeatedInt64: []int64{1, 2}},
737 y: &testeditionspb.TestAllTypes{RepeatedInt64: []int64{1, 3}},
738 }, {
739 x: &testeditionspb.TestAllTypes{RepeatedUint32: []uint32{1, 2}},
740 y: &testeditionspb.TestAllTypes{RepeatedUint32: []uint32{1, 3}},
741 }, {
742 x: &testeditionspb.TestAllTypes{RepeatedUint64: []uint64{1, 2}},
743 y: &testeditionspb.TestAllTypes{RepeatedUint64: []uint64{1, 3}},
744 }, {
745 x: &testeditionspb.TestAllTypes{RepeatedSint32: []int32{1, 2}},
746 y: &testeditionspb.TestAllTypes{RepeatedSint32: []int32{1, 3}},
747 }, {
748 x: &testeditionspb.TestAllTypes{RepeatedSint64: []int64{1, 2}},
749 y: &testeditionspb.TestAllTypes{RepeatedSint64: []int64{1, 3}},
750 }, {
751 x: &testeditionspb.TestAllTypes{RepeatedFixed32: []uint32{1, 2}},
752 y: &testeditionspb.TestAllTypes{RepeatedFixed32: []uint32{1, 3}},
753 }, {
754 x: &testeditionspb.TestAllTypes{RepeatedFixed64: []uint64{1, 2}},
755 y: &testeditionspb.TestAllTypes{RepeatedFixed64: []uint64{1, 3}},
756 }, {
757 x: &testeditionspb.TestAllTypes{RepeatedSfixed32: []int32{1, 2}},
758 y: &testeditionspb.TestAllTypes{RepeatedSfixed32: []int32{1, 3}},
759 }, {
760 x: &testeditionspb.TestAllTypes{RepeatedSfixed64: []int64{1, 2}},
761 y: &testeditionspb.TestAllTypes{RepeatedSfixed64: []int64{1, 3}},
762 }, {
763 x: &testeditionspb.TestAllTypes{RepeatedFloat: []float32{1, 2}},
764 y: &testeditionspb.TestAllTypes{RepeatedFloat: []float32{1, 3}},
765 }, {
766 x: &testeditionspb.TestAllTypes{RepeatedDouble: []float64{1, 2}},
767 y: &testeditionspb.TestAllTypes{RepeatedDouble: []float64{1, 3}},
768 }, {
769 x: &testeditionspb.TestAllTypes{RepeatedBool: []bool{true, false}},
770 y: &testeditionspb.TestAllTypes{RepeatedBool: []bool{true, true}},
771 }, {
772 x: &testeditionspb.TestAllTypes{RepeatedString: []string{"a", "b"}},
773 y: &testeditionspb.TestAllTypes{RepeatedString: []string{"a", "c"}},
774 }, {
775 x: &testeditionspb.TestAllTypes{RepeatedBytes: [][]byte{[]byte("a"), []byte("b")}},
776 y: &testeditionspb.TestAllTypes{RepeatedBytes: [][]byte{[]byte("a"), []byte("c")}},
777 }, {
778 x: &testeditionspb.TestAllTypes{RepeatedNestedEnum: []testeditionspb.TestAllTypes_NestedEnum{testeditionspb.TestAllTypes_FOO}},
779 y: &testeditionspb.TestAllTypes{RepeatedNestedEnum: []testeditionspb.TestAllTypes_NestedEnum{testeditionspb.TestAllTypes_BAR}},
780 }, {
781 x: &testeditionspb.TestAllTypes{Repeatedgroup: []*testeditionspb.TestAllTypes_RepeatedGroup{
782 {A: proto.Int32(1)},
783 {A: proto.Int32(2)},
784 }},
785 y: &testeditionspb.TestAllTypes{Repeatedgroup: []*testeditionspb.TestAllTypes_RepeatedGroup{
786 {A: proto.Int32(1)},
787 {A: proto.Int32(3)},
788 }},
789 }, {
790 x: &testeditionspb.TestAllTypes{RepeatedNestedMessage: []*testeditionspb.TestAllTypes_NestedMessage{
791 {A: proto.Int32(1)},
792 {A: proto.Int32(2)},
793 }},
794 y: &testeditionspb.TestAllTypes{RepeatedNestedMessage: []*testeditionspb.TestAllTypes_NestedMessage{
795 {A: proto.Int32(1)},
796 {A: proto.Int32(3)},
797 }},
798 },
799
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700800 // Maps: various configurations.
801 {
802 x: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
803 y: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{3: 4}},
804 }, {
805 x: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
806 y: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
807 }, {
808 x: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
809 y: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
Lasse Folger19445772024-02-20 16:52:39 +0100810 }, {
811 x: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
812 y: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{3: 4}},
813 }, {
814 x: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
815 y: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
816 }, {
817 x: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
818 y: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2}},
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700819 },
820
821 // Maps: various types.
822 {
823 x: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
824 y: &testpb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 5}},
825 }, {
826 x: &testpb.TestAllTypes{MapInt64Int64: map[int64]int64{1: 2, 3: 4}},
827 y: &testpb.TestAllTypes{MapInt64Int64: map[int64]int64{1: 2, 3: 5}},
828 }, {
829 x: &testpb.TestAllTypes{MapUint32Uint32: map[uint32]uint32{1: 2, 3: 4}},
830 y: &testpb.TestAllTypes{MapUint32Uint32: map[uint32]uint32{1: 2, 3: 5}},
831 }, {
832 x: &testpb.TestAllTypes{MapUint64Uint64: map[uint64]uint64{1: 2, 3: 4}},
833 y: &testpb.TestAllTypes{MapUint64Uint64: map[uint64]uint64{1: 2, 3: 5}},
834 }, {
835 x: &testpb.TestAllTypes{MapSint32Sint32: map[int32]int32{1: 2, 3: 4}},
836 y: &testpb.TestAllTypes{MapSint32Sint32: map[int32]int32{1: 2, 3: 5}},
837 }, {
838 x: &testpb.TestAllTypes{MapSint64Sint64: map[int64]int64{1: 2, 3: 4}},
839 y: &testpb.TestAllTypes{MapSint64Sint64: map[int64]int64{1: 2, 3: 5}},
840 }, {
841 x: &testpb.TestAllTypes{MapFixed32Fixed32: map[uint32]uint32{1: 2, 3: 4}},
842 y: &testpb.TestAllTypes{MapFixed32Fixed32: map[uint32]uint32{1: 2, 3: 5}},
843 }, {
844 x: &testpb.TestAllTypes{MapFixed64Fixed64: map[uint64]uint64{1: 2, 3: 4}},
845 y: &testpb.TestAllTypes{MapFixed64Fixed64: map[uint64]uint64{1: 2, 3: 5}},
846 }, {
847 x: &testpb.TestAllTypes{MapSfixed32Sfixed32: map[int32]int32{1: 2, 3: 4}},
848 y: &testpb.TestAllTypes{MapSfixed32Sfixed32: map[int32]int32{1: 2, 3: 5}},
849 }, {
850 x: &testpb.TestAllTypes{MapSfixed64Sfixed64: map[int64]int64{1: 2, 3: 4}},
851 y: &testpb.TestAllTypes{MapSfixed64Sfixed64: map[int64]int64{1: 2, 3: 5}},
852 }, {
853 x: &testpb.TestAllTypes{MapInt32Float: map[int32]float32{1: 2, 3: 4}},
854 y: &testpb.TestAllTypes{MapInt32Float: map[int32]float32{1: 2, 3: 5}},
855 }, {
856 x: &testpb.TestAllTypes{MapInt32Double: map[int32]float64{1: 2, 3: 4}},
857 y: &testpb.TestAllTypes{MapInt32Double: map[int32]float64{1: 2, 3: 5}},
858 }, {
859 x: &testpb.TestAllTypes{MapBoolBool: map[bool]bool{true: false, false: true}},
860 y: &testpb.TestAllTypes{MapBoolBool: map[bool]bool{true: false, false: false}},
861 }, {
862 x: &testpb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "d"}},
863 y: &testpb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "e"}},
864 }, {
865 x: &testpb.TestAllTypes{MapStringBytes: map[string][]byte{"a": []byte("b"), "c": []byte("d")}},
866 y: &testpb.TestAllTypes{MapStringBytes: map[string][]byte{"a": []byte("b"), "c": []byte("e")}},
867 }, {
868 x: &testpb.TestAllTypes{MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
869 "a": {A: proto.Int32(1)},
870 "b": {A: proto.Int32(2)},
871 }},
872 y: &testpb.TestAllTypes{MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
873 "a": {A: proto.Int32(1)},
874 "b": {A: proto.Int32(3)},
875 }},
876 }, {
877 x: &testpb.TestAllTypes{MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
878 "a": testpb.TestAllTypes_FOO,
879 "b": testpb.TestAllTypes_BAR,
880 }},
881 y: &testpb.TestAllTypes{MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
882 "a": testpb.TestAllTypes_FOO,
883 "b": testpb.TestAllTypes_BAZ,
884 }},
Lasse Folger19445772024-02-20 16:52:39 +0100885 }, {
886 x: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 4}},
887 y: &testeditionspb.TestAllTypes{MapInt32Int32: map[int32]int32{1: 2, 3: 5}},
888 }, {
889 x: &testeditionspb.TestAllTypes{MapInt64Int64: map[int64]int64{1: 2, 3: 4}},
890 y: &testeditionspb.TestAllTypes{MapInt64Int64: map[int64]int64{1: 2, 3: 5}},
891 }, {
892 x: &testeditionspb.TestAllTypes{MapUint32Uint32: map[uint32]uint32{1: 2, 3: 4}},
893 y: &testeditionspb.TestAllTypes{MapUint32Uint32: map[uint32]uint32{1: 2, 3: 5}},
894 }, {
895 x: &testeditionspb.TestAllTypes{MapUint64Uint64: map[uint64]uint64{1: 2, 3: 4}},
896 y: &testeditionspb.TestAllTypes{MapUint64Uint64: map[uint64]uint64{1: 2, 3: 5}},
897 }, {
898 x: &testeditionspb.TestAllTypes{MapSint32Sint32: map[int32]int32{1: 2, 3: 4}},
899 y: &testeditionspb.TestAllTypes{MapSint32Sint32: map[int32]int32{1: 2, 3: 5}},
900 }, {
901 x: &testeditionspb.TestAllTypes{MapSint64Sint64: map[int64]int64{1: 2, 3: 4}},
902 y: &testeditionspb.TestAllTypes{MapSint64Sint64: map[int64]int64{1: 2, 3: 5}},
903 }, {
904 x: &testeditionspb.TestAllTypes{MapFixed32Fixed32: map[uint32]uint32{1: 2, 3: 4}},
905 y: &testeditionspb.TestAllTypes{MapFixed32Fixed32: map[uint32]uint32{1: 2, 3: 5}},
906 }, {
907 x: &testeditionspb.TestAllTypes{MapFixed64Fixed64: map[uint64]uint64{1: 2, 3: 4}},
908 y: &testeditionspb.TestAllTypes{MapFixed64Fixed64: map[uint64]uint64{1: 2, 3: 5}},
909 }, {
910 x: &testeditionspb.TestAllTypes{MapSfixed32Sfixed32: map[int32]int32{1: 2, 3: 4}},
911 y: &testeditionspb.TestAllTypes{MapSfixed32Sfixed32: map[int32]int32{1: 2, 3: 5}},
912 }, {
913 x: &testeditionspb.TestAllTypes{MapSfixed64Sfixed64: map[int64]int64{1: 2, 3: 4}},
914 y: &testeditionspb.TestAllTypes{MapSfixed64Sfixed64: map[int64]int64{1: 2, 3: 5}},
915 }, {
916 x: &testeditionspb.TestAllTypes{MapInt32Float: map[int32]float32{1: 2, 3: 4}},
917 y: &testeditionspb.TestAllTypes{MapInt32Float: map[int32]float32{1: 2, 3: 5}},
918 }, {
919 x: &testeditionspb.TestAllTypes{MapInt32Double: map[int32]float64{1: 2, 3: 4}},
920 y: &testeditionspb.TestAllTypes{MapInt32Double: map[int32]float64{1: 2, 3: 5}},
921 }, {
922 x: &testeditionspb.TestAllTypes{MapBoolBool: map[bool]bool{true: false, false: true}},
923 y: &testeditionspb.TestAllTypes{MapBoolBool: map[bool]bool{true: false, false: false}},
924 }, {
925 x: &testeditionspb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "d"}},
926 y: &testeditionspb.TestAllTypes{MapStringString: map[string]string{"a": "b", "c": "e"}},
927 }, {
928 x: &testeditionspb.TestAllTypes{MapStringBytes: map[string][]byte{"a": []byte("b"), "c": []byte("d")}},
929 y: &testeditionspb.TestAllTypes{MapStringBytes: map[string][]byte{"a": []byte("b"), "c": []byte("e")}},
930 }, {
931 x: &testeditionspb.TestAllTypes{MapStringNestedMessage: map[string]*testeditionspb.TestAllTypes_NestedMessage{
932 "a": {A: proto.Int32(1)},
933 "b": {A: proto.Int32(2)},
934 }},
935 y: &testeditionspb.TestAllTypes{MapStringNestedMessage: map[string]*testeditionspb.TestAllTypes_NestedMessage{
936 "a": {A: proto.Int32(1)},
937 "b": {A: proto.Int32(3)},
938 }},
939 }, {
940 x: &testeditionspb.TestAllTypes{MapStringNestedEnum: map[string]testeditionspb.TestAllTypes_NestedEnum{
941 "a": testeditionspb.TestAllTypes_FOO,
942 "b": testeditionspb.TestAllTypes_BAR,
943 }},
944 y: &testeditionspb.TestAllTypes{MapStringNestedEnum: map[string]testeditionspb.TestAllTypes_NestedEnum{
945 "a": testeditionspb.TestAllTypes_FOO,
946 "b": testeditionspb.TestAllTypes_BAZ,
947 }},
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700948 },
949
950 // Extensions.
951 {
952 x: build(&testpb.TestAllExtensions{},
Damien Neild025c952020-02-02 00:53:34 -0800953 extend(testpb.E_OptionalInt32, int32(1)),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700954 ),
955 y: build(&testpb.TestAllExtensions{},
Damien Neild025c952020-02-02 00:53:34 -0800956 extend(testpb.E_OptionalInt32, int32(2)),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700957 ),
958 }, {
959 x: &testpb.TestAllExtensions{},
960 y: build(&testpb.TestAllExtensions{},
Damien Neild025c952020-02-02 00:53:34 -0800961 extend(testpb.E_OptionalInt32, int32(2)),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700962 ),
963 },
964
965 // Unknown fields.
966 {
Joe Tsaicfd80492020-02-14 18:13:14 -0800967 x: build(&testpb.TestAllTypes{}, unknown(protopack.Message{
968 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700969 }.Marshal())),
Joe Tsaicfd80492020-02-14 18:13:14 -0800970 y: build(&testpb.TestAllTypes{}, unknown(protopack.Message{
971 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(2),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700972 }.Marshal())),
973 }, {
Joe Tsaicfd80492020-02-14 18:13:14 -0800974 x: build(&testpb.TestAllTypes{}, unknown(protopack.Message{
975 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700976 }.Marshal())),
977 y: &testpb.TestAllTypes{},
Lasse Folger19445772024-02-20 16:52:39 +0100978 }, {
979 x: build(&testeditionspb.TestAllTypes{}, unknown(protopack.Message{
980 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
981 }.Marshal())),
982 y: build(&testeditionspb.TestAllTypes{}, unknown(protopack.Message{
983 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(2),
984 }.Marshal())),
985 }, {
986 x: build(&testeditionspb.TestAllTypes{}, unknown(protopack.Message{
987 protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
988 }.Marshal())),
989 y: &testeditionspb.TestAllTypes{},
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700990 },
991 }
992
993 for _, tt := range tests {
994 if !tt.eq && !proto.Equal(tt.x, tt.x) {
Joe Tsai74b14602020-01-06 15:44:09 -0800995 t.Errorf("Equal(x, x) = false, want true\n==== x ====\n%v", prototext.Format(tt.x))
Damien Neile6f060f2019-04-23 17:11:02 -0700996 }
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700997 if !tt.eq && !proto.Equal(tt.y, tt.y) {
Joe Tsai74b14602020-01-06 15:44:09 -0800998 t.Errorf("Equal(y, y) = false, want true\n==== y ====\n%v", prototext.Format(tt.y))
Joe Tsaif2c4ddc2019-09-19 21:28:52 -0700999 }
1000 if eq := proto.Equal(tt.x, tt.y); eq != tt.eq {
Joe Tsai74b14602020-01-06 15:44:09 -08001001 t.Errorf("Equal(x, y) = %v, want %v\n==== x ====\n%v==== y ====\n%v", eq, tt.eq, prototext.Format(tt.x), prototext.Format(tt.y))
Damien Neile6f060f2019-04-23 17:11:02 -07001002 }
1003 }
1004}
Mitko Asenov380c3392022-06-09 11:53:27 +02001005
1006func BenchmarkEqualWithSmallEmpty(b *testing.B) {
1007 x := &testpb.ForeignMessage{}
1008 y := &testpb.ForeignMessage{}
1009
1010 b.ResetTimer()
1011 for i := 0; i < b.N; i++ {
1012 proto.Equal(x, y)
1013 }
1014}
1015
1016func BenchmarkEqualWithIdenticalPtrEmpty(b *testing.B) {
1017 x := &testpb.ForeignMessage{}
1018
1019 b.ResetTimer()
1020 for i := 0; i < b.N; i++ {
1021 proto.Equal(x, x)
1022 }
1023}
1024
1025func BenchmarkEqualWithLargeEmpty(b *testing.B) {
1026 x := &testpb.TestAllTypes{}
1027 y := &testpb.TestAllTypes{}
1028
1029 b.ResetTimer()
1030 for i := 0; i < b.N; i++ {
1031 proto.Equal(x, y)
1032 }
1033}
1034
1035func makeNested(depth int) *testpb.TestAllTypes {
1036 if depth <= 0 {
1037 return nil
1038 }
Damien Neil5f429f72022-07-20 10:18:12 -07001039 return &testpb.TestAllTypes{
1040 OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
Mitko Asenov380c3392022-06-09 11:53:27 +02001041 Corecursive: makeNested(depth - 1),
Damien Neil5f429f72022-07-20 10:18:12 -07001042 },
1043 }
Mitko Asenov380c3392022-06-09 11:53:27 +02001044}
1045
1046func BenchmarkEqualWithDeeplyNestedEqual(b *testing.B) {
1047 x := makeNested(20)
1048 y := makeNested(20)
1049
1050 b.ResetTimer()
1051 for i := 0; i < b.N; i++ {
1052 proto.Equal(x, y)
1053 }
1054}
1055
1056func BenchmarkEqualWithDeeplyNestedDifferent(b *testing.B) {
1057 x := makeNested(20)
1058 y := makeNested(21)
1059
1060 b.ResetTimer()
1061 for i := 0; i < b.N; i++ {
1062 proto.Equal(x, y)
1063 }
1064}
1065
1066func BenchmarkEqualWithDeeplyNestedIdenticalPtr(b *testing.B) {
1067 x := makeNested(20)
1068
1069 b.ResetTimer()
1070 for i := 0; i < b.N; i++ {
1071 proto.Equal(x, x)
1072 }
1073}