equal_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2011 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. . "github.com/golang/protobuf/proto"
  8. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  9. pb "github.com/golang/protobuf/proto/test_proto"
  10. )
  11. // Four identical base messages.
  12. // The init function adds extensions to some of them.
  13. var messageWithoutExtension = &pb.MyMessage{Count: Int32(7)}
  14. var messageWithExtension1a = &pb.MyMessage{Count: Int32(7)}
  15. var messageWithExtension1b = &pb.MyMessage{Count: Int32(7)}
  16. var messageWithExtension2 = &pb.MyMessage{Count: Int32(7)}
  17. var messageWithExtension3a = &pb.MyMessage{Count: Int32(7)}
  18. var messageWithExtension3b = &pb.MyMessage{Count: Int32(7)}
  19. var messageWithExtension3c = &pb.MyMessage{Count: Int32(7)}
  20. // Two messages with non-message extensions.
  21. var messageWithInt32Extension1 = &pb.MyMessage{Count: Int32(8)}
  22. var messageWithInt32Extension2 = &pb.MyMessage{Count: Int32(8)}
  23. func init() {
  24. ext1 := &pb.Ext{Data: String("Kirk")}
  25. ext2 := &pb.Ext{Data: String("Picard")}
  26. // messageWithExtension1a has ext1, but never marshals it.
  27. if err := SetExtension(messageWithExtension1a, pb.E_Ext_More, ext1); err != nil {
  28. panic("SetExtension on 1a failed: " + err.Error())
  29. }
  30. // messageWithExtension1b is the unmarshaled form of messageWithExtension1a.
  31. if err := SetExtension(messageWithExtension1b, pb.E_Ext_More, ext1); err != nil {
  32. panic("SetExtension on 1b failed: " + err.Error())
  33. }
  34. buf, err := Marshal(messageWithExtension1b)
  35. if err != nil {
  36. panic("Marshal of 1b failed: " + err.Error())
  37. }
  38. messageWithExtension1b.Reset()
  39. if err := Unmarshal(buf, messageWithExtension1b); err != nil {
  40. panic("Unmarshal of 1b failed: " + err.Error())
  41. }
  42. // messageWithExtension2 has ext2.
  43. if err := SetExtension(messageWithExtension2, pb.E_Ext_More, ext2); err != nil {
  44. panic("SetExtension on 2 failed: " + err.Error())
  45. }
  46. if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(23)); err != nil {
  47. panic("SetExtension on Int32-1 failed: " + err.Error())
  48. }
  49. if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(24)); err != nil {
  50. panic("SetExtension on Int32-2 failed: " + err.Error())
  51. }
  52. // messageWithExtension3{a,b,c} has unregistered extension.
  53. if RegisteredExtensions(messageWithExtension3a)[200] != nil {
  54. panic("expect extension 200 unregistered")
  55. }
  56. bytes := []byte{
  57. 0xc0, 0x0c, 0x01, // id=200, wiretype=0 (varint), data=1
  58. }
  59. bytes2 := []byte{
  60. 0xc0, 0x0c, 0x02, // id=200, wiretype=0 (varint), data=2
  61. }
  62. SetRawExtension(messageWithExtension3a, 200, bytes)
  63. SetRawExtension(messageWithExtension3b, 200, bytes)
  64. SetRawExtension(messageWithExtension3c, 200, bytes2)
  65. }
  66. var EqualTests = []struct {
  67. desc string
  68. a, b Message
  69. exp bool
  70. }{
  71. {"different types", &pb.GoEnum{}, &pb.GoTestField{}, false},
  72. {"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true},
  73. {"nil vs nil", nil, nil, true},
  74. {"typed nil vs typed nil", (*pb.GoEnum)(nil), (*pb.GoEnum)(nil), true},
  75. {"typed nil vs empty", (*pb.GoEnum)(nil), &pb.GoEnum{}, false},
  76. {"different typed nil", (*pb.GoEnum)(nil), (*pb.GoTestField)(nil), false},
  77. {"one set field, one unset field", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{}, false},
  78. {"one set field zero, one unset field", &pb.GoTest{Param: Int32(0)}, &pb.GoTest{}, false},
  79. {"different set fields", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("bar")}, false},
  80. {"equal set", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("foo")}, true},
  81. {"repeated, one set", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{}, false},
  82. {"repeated, different length", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{F_Int32Repeated: []int32{2}}, false},
  83. {"repeated, different value", &pb.GoTest{F_Int32Repeated: []int32{2}}, &pb.GoTest{F_Int32Repeated: []int32{3}}, false},
  84. {"repeated, equal", &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, true},
  85. {"repeated, nil equal nil", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: nil}, true},
  86. {"repeated, nil equal empty", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: []int32{}}, true},
  87. {"repeated, empty equal nil", &pb.GoTest{F_Int32Repeated: []int32{}}, &pb.GoTest{F_Int32Repeated: nil}, true},
  88. {
  89. "nested, different",
  90. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("foo")}},
  91. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("bar")}},
  92. false,
  93. },
  94. {
  95. "nested, equal",
  96. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
  97. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
  98. true,
  99. },
  100. {"bytes", &pb.OtherMessage{Value: []byte("foo")}, &pb.OtherMessage{Value: []byte("foo")}, true},
  101. {"bytes, empty", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: []byte{}}, true},
  102. {"bytes, empty vs nil", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: nil}, false},
  103. {
  104. "repeated bytes",
  105. &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
  106. &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
  107. true,
  108. },
  109. // In proto3, []byte{} and []byte(nil) are equal.
  110. {"proto3 bytes, empty vs nil", &proto3pb.Message{Data: []byte{}}, &proto3pb.Message{Data: nil}, true},
  111. {"extension vs. no extension", messageWithoutExtension, messageWithExtension1a, false},
  112. {"extension vs. same extension", messageWithExtension1a, messageWithExtension1b, true},
  113. {"extension vs. different extension", messageWithExtension1a, messageWithExtension2, false},
  114. {"int32 extension vs. itself", messageWithInt32Extension1, messageWithInt32Extension1, true},
  115. {"int32 extension vs. a different int32", messageWithInt32Extension1, messageWithInt32Extension2, false},
  116. {"unregistered extension same", messageWithExtension3a, messageWithExtension3b, true},
  117. {"unregistered extension different", messageWithExtension3a, messageWithExtension3c, false},
  118. {
  119. "message with group",
  120. &pb.MyMessage{
  121. Count: Int32(1),
  122. Somegroup: &pb.MyMessage_SomeGroup{
  123. GroupField: Int32(5),
  124. },
  125. },
  126. &pb.MyMessage{
  127. Count: Int32(1),
  128. Somegroup: &pb.MyMessage_SomeGroup{
  129. GroupField: Int32(5),
  130. },
  131. },
  132. true,
  133. },
  134. {
  135. "map same",
  136. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  137. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  138. true,
  139. },
  140. {
  141. "map different entry",
  142. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  143. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob"}},
  144. false,
  145. },
  146. {
  147. "map different key only",
  148. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  149. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Ken"}},
  150. false,
  151. },
  152. {
  153. "map different value only",
  154. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  155. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob"}},
  156. false,
  157. },
  158. {
  159. "zero-length maps same",
  160. &pb.MessageWithMap{NameMapping: map[int32]string{}},
  161. &pb.MessageWithMap{NameMapping: nil},
  162. true,
  163. },
  164. {
  165. "orders in map don't matter",
  166. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken", 2: "Rob"}},
  167. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob", 1: "Ken"}},
  168. true,
  169. },
  170. {
  171. "oneof same",
  172. &pb.Communique{Union: &pb.Communique_Number{41}},
  173. &pb.Communique{Union: &pb.Communique_Number{41}},
  174. true,
  175. },
  176. {
  177. "oneof one nil",
  178. &pb.Communique{Union: &pb.Communique_Number{41}},
  179. &pb.Communique{},
  180. false,
  181. },
  182. {
  183. "oneof different",
  184. &pb.Communique{Union: &pb.Communique_Number{41}},
  185. &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}},
  186. false,
  187. },
  188. }
  189. func TestEqual(t *testing.T) {
  190. for _, tc := range EqualTests {
  191. if res := Equal(tc.a, tc.b); res != tc.exp {
  192. t.Errorf("%v: Equal(%v, %v) = %v, want %v", tc.desc, tc.a, tc.b, res, tc.exp)
  193. }
  194. }
  195. }