equal_test.go 14 KB

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