equal_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/testdata"
  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. // Two messages with non-message extensions.
  45. var messageWithInt32Extension1 = &pb.MyMessage{Count: Int32(8)}
  46. var messageWithInt32Extension2 = &pb.MyMessage{Count: Int32(8)}
  47. func init() {
  48. ext1 := &pb.Ext{Data: String("Kirk")}
  49. ext2 := &pb.Ext{Data: String("Picard")}
  50. // messageWithExtension1a has ext1, but never marshals it.
  51. if err := SetExtension(messageWithExtension1a, pb.E_Ext_More, ext1); err != nil {
  52. panic("SetExtension on 1a failed: " + err.Error())
  53. }
  54. // messageWithExtension1b is the unmarshaled form of messageWithExtension1a.
  55. if err := SetExtension(messageWithExtension1b, pb.E_Ext_More, ext1); err != nil {
  56. panic("SetExtension on 1b failed: " + err.Error())
  57. }
  58. buf, err := Marshal(messageWithExtension1b)
  59. if err != nil {
  60. panic("Marshal of 1b failed: " + err.Error())
  61. }
  62. messageWithExtension1b.Reset()
  63. if err := Unmarshal(buf, messageWithExtension1b); err != nil {
  64. panic("Unmarshal of 1b failed: " + err.Error())
  65. }
  66. // messageWithExtension2 has ext2.
  67. if err := SetExtension(messageWithExtension2, pb.E_Ext_More, ext2); err != nil {
  68. panic("SetExtension on 2 failed: " + err.Error())
  69. }
  70. if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(23)); err != nil {
  71. panic("SetExtension on Int32-1 failed: " + err.Error())
  72. }
  73. if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(24)); err != nil {
  74. panic("SetExtension on Int32-2 failed: " + err.Error())
  75. }
  76. }
  77. var EqualTests = []struct {
  78. desc string
  79. a, b Message
  80. exp bool
  81. }{
  82. {"different types", &pb.GoEnum{}, &pb.GoTestField{}, false},
  83. {"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true},
  84. {"nil vs nil", nil, nil, true},
  85. {"typed nil vs typed nil", (*pb.GoEnum)(nil), (*pb.GoEnum)(nil), true},
  86. {"typed nil vs empty", (*pb.GoEnum)(nil), &pb.GoEnum{}, false},
  87. {"different typed nil", (*pb.GoEnum)(nil), (*pb.GoTestField)(nil), false},
  88. {"one set field, one unset field", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{}, false},
  89. {"one set field zero, one unset field", &pb.GoTest{Param: Int32(0)}, &pb.GoTest{}, false},
  90. {"different set fields", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("bar")}, false},
  91. {"equal set", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("foo")}, true},
  92. {"repeated, one set", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{}, false},
  93. {"repeated, different length", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{F_Int32Repeated: []int32{2}}, false},
  94. {"repeated, different value", &pb.GoTest{F_Int32Repeated: []int32{2}}, &pb.GoTest{F_Int32Repeated: []int32{3}}, false},
  95. {"repeated, equal", &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, true},
  96. {"repeated, nil equal nil", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: nil}, true},
  97. {"repeated, nil equal empty", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: []int32{}}, true},
  98. {"repeated, empty equal nil", &pb.GoTest{F_Int32Repeated: []int32{}}, &pb.GoTest{F_Int32Repeated: nil}, true},
  99. {
  100. "nested, different",
  101. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("foo")}},
  102. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("bar")}},
  103. false,
  104. },
  105. {
  106. "nested, equal",
  107. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
  108. &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
  109. true,
  110. },
  111. {"bytes", &pb.OtherMessage{Value: []byte("foo")}, &pb.OtherMessage{Value: []byte("foo")}, true},
  112. {"bytes, empty", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: []byte{}}, true},
  113. {"bytes, empty vs nil", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: nil}, false},
  114. {
  115. "repeated bytes",
  116. &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
  117. &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
  118. true,
  119. },
  120. // In proto3, []byte{} and []byte(nil) are equal.
  121. {"proto3 bytes, empty vs nil", &proto3pb.Message{Data: []byte{}}, &proto3pb.Message{Data: nil}, true},
  122. {"extension vs. no extension", messageWithoutExtension, messageWithExtension1a, false},
  123. {"extension vs. same extension", messageWithExtension1a, messageWithExtension1b, true},
  124. {"extension vs. different extension", messageWithExtension1a, messageWithExtension2, false},
  125. {"int32 extension vs. itself", messageWithInt32Extension1, messageWithInt32Extension1, true},
  126. {"int32 extension vs. a different int32", messageWithInt32Extension1, messageWithInt32Extension2, false},
  127. {
  128. "message with group",
  129. &pb.MyMessage{
  130. Count: Int32(1),
  131. Somegroup: &pb.MyMessage_SomeGroup{
  132. GroupField: Int32(5),
  133. },
  134. },
  135. &pb.MyMessage{
  136. Count: Int32(1),
  137. Somegroup: &pb.MyMessage_SomeGroup{
  138. GroupField: Int32(5),
  139. },
  140. },
  141. true,
  142. },
  143. {
  144. "map same",
  145. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  146. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  147. true,
  148. },
  149. {
  150. "map different entry",
  151. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  152. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob"}},
  153. false,
  154. },
  155. {
  156. "map different key only",
  157. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  158. &pb.MessageWithMap{NameMapping: map[int32]string{2: "Ken"}},
  159. false,
  160. },
  161. {
  162. "map different value only",
  163. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
  164. &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob"}},
  165. false,
  166. },
  167. {
  168. "oneof same",
  169. &pb.Communique{Union: &pb.Communique_Number{41}},
  170. &pb.Communique{Union: &pb.Communique_Number{41}},
  171. true,
  172. },
  173. {
  174. "oneof one nil",
  175. &pb.Communique{Union: &pb.Communique_Number{41}},
  176. &pb.Communique{},
  177. false,
  178. },
  179. {
  180. "oneof different",
  181. &pb.Communique{Union: &pb.Communique_Number{41}},
  182. &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}},
  183. false,
  184. },
  185. }
  186. func TestEqual(t *testing.T) {
  187. for _, tc := range EqualTests {
  188. if res := Equal(tc.a, tc.b); res != tc.exp {
  189. t.Errorf("%v: Equal(%v, %v) = %v, want %v", tc.desc, tc.a, tc.b, res, tc.exp)
  190. }
  191. }
  192. }