equal_test.go 14 KB

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