equal_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2011 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto_test
  32. import (
  33. "testing"
  34. . "github.com/golang/protobuf/proto"
  35. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  36. pb "github.com/golang/protobuf/proto/test_proto"
  37. )
  38. // Four identical base messages.
  39. // The init function adds extensions to some of them.
  40. var messageWithoutExtension = &pb.MyMessage{Count: Int32(7)}
  41. var messageWithExtension1a = &pb.MyMessage{Count: Int32(7)}
  42. var messageWithExtension1b = &pb.MyMessage{Count: Int32(7)}
  43. var messageWithExtension2 = &pb.MyMessage{Count: Int32(7)}
  44. var messageWithExtension3a = &pb.MyMessage{Count: Int32(7)}
  45. var messageWithExtension3b = &pb.MyMessage{Count: Int32(7)}
  46. var messageWithExtension3c = &pb.MyMessage{Count: Int32(7)}
  47. // Two messages with non-message extensions.
  48. var messageWithInt32Extension1 = &pb.MyMessage{Count: Int32(8)}
  49. var messageWithInt32Extension2 = &pb.MyMessage{Count: Int32(8)}
  50. func init() {
  51. ext1 := &pb.Ext{Data: String("Kirk")}
  52. ext2 := &pb.Ext{Data: String("Picard")}
  53. // messageWithExtension1a has ext1, but never marshals it.
  54. if err := SetExtension(messageWithExtension1a, pb.E_Ext_More, ext1); err != nil {
  55. panic("SetExtension on 1a failed: " + err.Error())
  56. }
  57. // messageWithExtension1b is the unmarshaled form of messageWithExtension1a.
  58. if err := SetExtension(messageWithExtension1b, pb.E_Ext_More, ext1); err != nil {
  59. panic("SetExtension on 1b failed: " + err.Error())
  60. }
  61. buf, err := Marshal(messageWithExtension1b)
  62. if err != nil {
  63. panic("Marshal of 1b failed: " + err.Error())
  64. }
  65. messageWithExtension1b.Reset()
  66. if err := Unmarshal(buf, messageWithExtension1b); err != nil {
  67. panic("Unmarshal of 1b failed: " + err.Error())
  68. }
  69. // messageWithExtension2 has ext2.
  70. if err := SetExtension(messageWithExtension2, pb.E_Ext_More, ext2); err != nil {
  71. panic("SetExtension on 2 failed: " + err.Error())
  72. }
  73. if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(23)); err != nil {
  74. panic("SetExtension on Int32-1 failed: " + err.Error())
  75. }
  76. if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(24)); err != nil {
  77. panic("SetExtension on Int32-2 failed: " + err.Error())
  78. }
  79. // messageWithExtension3{a,b,c} has unregistered extension.
  80. if RegisteredExtensions(messageWithExtension3a)[200] != nil {
  81. panic("expect extension 200 unregistered")
  82. }
  83. bytes := []byte{
  84. 0xc0, 0x0c, 0x01, // id=200, wiretype=0 (varint), data=1
  85. }
  86. bytes2 := []byte{
  87. 0xc0, 0x0c, 0x02, // id=200, wiretype=0 (varint), data=2
  88. }
  89. SetRawExtension(messageWithExtension3a, 200, bytes)
  90. SetRawExtension(messageWithExtension3b, 200, bytes)
  91. SetRawExtension(messageWithExtension3c, 200, bytes2)
  92. }
  93. var EqualTests = []struct {
  94. desc string
  95. a, b Message
  96. exp bool
  97. }{
  98. {"different types", &pb.GoEnum{}, &pb.GoTestField{}, false},
  99. {"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true},
  100. {"nil vs nil", nil, nil, true},
  101. {"typed nil vs typed nil", (*pb.GoEnum)(nil), (*pb.GoEnum)(nil), true},
  102. {"typed nil vs empty", (*pb.GoEnum)(nil), &pb.GoEnum{}, false},
  103. {"different typed nil", (*pb.GoEnum)(nil), (*pb.GoTestField)(nil), false},
  104. {"one set field, one unset field", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{}, false},
  105. {"one set field zero, one unset field", &pb.GoTest{Param: Int32(0)}, &pb.GoTest{}, false},
  106. {"different set fields", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("bar")}, false},
  107. {"equal set", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("foo")}, true},
  108. {"repeated, one set", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{}, false},
  109. {"repeated, different length", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{F_Int32Repeated: []int32{2}}, false},
  110. {"repeated, different value", &pb.GoTest{F_Int32Repeated: []int32{2}}, &pb.GoTest{F_Int32Repeated: []int32{3}}, false},
  111. {"repeated, equal", &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, true},
  112. {"repeated, nil equal nil", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: nil}, true},
  113. {"repeated, nil equal empty", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: []int32{}}, true},
  114. {"repeated, empty equal nil", &pb.GoTest{F_Int32Repeated: []int32{}}, &pb.GoTest{F_Int32Repeated: nil}, true},
  115. {
  116. "nested, different",
  117. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("foo")}},
  118. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("bar")}},
  119. false,
  120. },
  121. {
  122. "nested, equal",
  123. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
  124. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
  125. true,
  126. },
  127. {"bytes", &pb.OtherMessage{Value: []byte("foo")}, &pb.OtherMessage{Value: []byte("foo")}, true},
  128. {"bytes, empty", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: []byte{}}, true},
  129. {"bytes, empty vs nil", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: nil}, false},
  130. {
  131. "repeated bytes",
  132. &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
  133. &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
  134. true,
  135. },
  136. // In proto3, []byte{} and []byte(nil) are equal.
  137. {"proto3 bytes, empty vs nil", &proto3pb.Message{Data: []byte{}}, &proto3pb.Message{Data: nil}, true},
  138. {"extension vs. no extension", messageWithoutExtension, messageWithExtension1a, false},
  139. {"extension vs. same extension", messageWithExtension1a, messageWithExtension1b, true},
  140. {"extension vs. different extension", messageWithExtension1a, messageWithExtension2, false},
  141. {"int32 extension vs. itself", messageWithInt32Extension1, messageWithInt32Extension1, true},
  142. {"int32 extension vs. a different int32", messageWithInt32Extension1, messageWithInt32Extension2, false},
  143. {"unregistered extension same", messageWithExtension3a, messageWithExtension3b, true},
  144. {"unregistered extension different", messageWithExtension3a, messageWithExtension3c, false},
  145. {
  146. "message with group",
  147. &pb.MyMessage{
  148. Count: Int32(1),
  149. Somegroup: &pb.MyMessage_SomeGroup{
  150. GroupField: Int32(5),
  151. },
  152. },
  153. &pb.MyMessage{
  154. Count: Int32(1),
  155. Somegroup: &pb.MyMessage_SomeGroup{
  156. GroupField: Int32(5),
  157. },
  158. },
  159. true,
  160. },
  161. {
  162. "map same",
  163. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  164. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  165. true,
  166. },
  167. {
  168. "map different entry",
  169. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  170. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob"}},
  171. false,
  172. },
  173. {
  174. "map different key only",
  175. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  176. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Ken"}},
  177. false,
  178. },
  179. {
  180. "map different value only",
  181. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  182. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob"}},
  183. false,
  184. },
  185. {
  186. "zero-length maps same",
  187. &pb.MessageWithMap{NameMapping: map[int32]string{}},
  188. &pb.MessageWithMap{NameMapping: nil},
  189. true,
  190. },
  191. {
  192. "orders in map don't matter",
  193. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken", 2: "Rob"}},
  194. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob", 1: "Ken"}},
  195. true,
  196. },
  197. {
  198. "oneof same",
  199. &pb.Communique{Union: &pb.Communique_Number{41}},
  200. &pb.Communique{Union: &pb.Communique_Number{41}},
  201. true,
  202. },
  203. {
  204. "oneof one nil",
  205. &pb.Communique{Union: &pb.Communique_Number{41}},
  206. &pb.Communique{},
  207. false,
  208. },
  209. {
  210. "oneof different",
  211. &pb.Communique{Union: &pb.Communique_Number{41}},
  212. &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}},
  213. false,
  214. },
  215. }
  216. func TestEqual(t *testing.T) {
  217. for _, tc := range EqualTests {
  218. if res := Equal(tc.a, tc.b); res != tc.exp {
  219. t.Errorf("%v: Equal(%v, %v) = %v, want %v", tc.desc, tc.a, tc.b, res, tc.exp)
  220. }
  221. }
  222. }