size_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 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. "log"
  34. "strings"
  35. "testing"
  36. . "github.com/golang/protobuf/proto"
  37. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  38. pb "github.com/golang/protobuf/proto/testdata"
  39. )
  40. var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
  41. // messageWithExtension2 is in equal_test.go.
  42. var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
  43. func init() {
  44. if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil {
  45. log.Panicf("SetExtension: %v", err)
  46. }
  47. if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil {
  48. log.Panicf("SetExtension: %v", err)
  49. }
  50. // Force messageWithExtension3 to have the extension encoded.
  51. Marshal(messageWithExtension3)
  52. }
  53. var SizeTests = []struct {
  54. desc string
  55. pb Message
  56. }{
  57. {"empty", &pb.OtherMessage{}},
  58. // Basic types.
  59. {"bool", &pb.Defaults{F_Bool: Bool(true)}},
  60. {"int32", &pb.Defaults{F_Int32: Int32(12)}},
  61. {"negative int32", &pb.Defaults{F_Int32: Int32(-1)}},
  62. {"small int64", &pb.Defaults{F_Int64: Int64(1)}},
  63. {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}},
  64. {"negative int64", &pb.Defaults{F_Int64: Int64(-1)}},
  65. {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}},
  66. {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}},
  67. {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}},
  68. {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}},
  69. {"float", &pb.Defaults{F_Float: Float32(12.6)}},
  70. {"double", &pb.Defaults{F_Double: Float64(13.9)}},
  71. {"string", &pb.Defaults{F_String: String("niles")}},
  72. {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}},
  73. {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}},
  74. {"sint32", &pb.Defaults{F_Sint32: Int32(65)}},
  75. {"sint64", &pb.Defaults{F_Sint64: Int64(67)}},
  76. {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}},
  77. // Repeated.
  78. {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}},
  79. {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}},
  80. {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
  81. {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
  82. {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
  83. {"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{
  84. // Need enough large numbers to verify that the header is counting the number of bytes
  85. // for the field, not the number of elements.
  86. 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
  87. 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
  88. }}},
  89. {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
  90. {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
  91. // Nested.
  92. {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
  93. {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
  94. // Other things.
  95. {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
  96. {"extension (unencoded)", messageWithExtension1},
  97. {"extension (encoded)", messageWithExtension3},
  98. // proto3 message
  99. {"proto3 empty", &proto3pb.Message{}},
  100. {"proto3 bool", &proto3pb.Message{TrueScotsman: true}},
  101. {"proto3 int64", &proto3pb.Message{ResultCount: 1}},
  102. {"proto3 uint32", &proto3pb.Message{HeightInCm: 123}},
  103. {"proto3 float", &proto3pb.Message{Score: 12.6}},
  104. {"proto3 string", &proto3pb.Message{Name: "Snezana"}},
  105. {"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}},
  106. {"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}},
  107. {"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  108. {"proto3 map field with empty bytes", &proto3pb.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}},
  109. {"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
  110. {"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: &pb.FloatingPoint{F: Float64(2.0)}}}},
  111. {"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
  112. {"map field with empty bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}},
  113. {"map field with big entry", &pb.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}},
  114. {"map field with big key and val", &pb.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}},
  115. {"map field with big numeric key", &pb.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}},
  116. {"oneof not set", &pb.Oneof{}},
  117. {"oneof bool", &pb.Oneof{Union: &pb.Oneof_F_Bool{true}}},
  118. {"oneof zero int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{0}}},
  119. {"oneof big int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{1 << 20}}},
  120. {"oneof int64", &pb.Oneof{Union: &pb.Oneof_F_Int64{42}}},
  121. {"oneof fixed32", &pb.Oneof{Union: &pb.Oneof_F_Fixed32{43}}},
  122. {"oneof fixed64", &pb.Oneof{Union: &pb.Oneof_F_Fixed64{44}}},
  123. {"oneof uint32", &pb.Oneof{Union: &pb.Oneof_F_Uint32{45}}},
  124. {"oneof uint64", &pb.Oneof{Union: &pb.Oneof_F_Uint64{46}}},
  125. {"oneof float", &pb.Oneof{Union: &pb.Oneof_F_Float{47.1}}},
  126. {"oneof double", &pb.Oneof{Union: &pb.Oneof_F_Double{48.9}}},
  127. {"oneof string", &pb.Oneof{Union: &pb.Oneof_F_String{"Rhythmic Fman"}}},
  128. {"oneof bytes", &pb.Oneof{Union: &pb.Oneof_F_Bytes{[]byte("let go")}}},
  129. {"oneof sint32", &pb.Oneof{Union: &pb.Oneof_F_Sint32{50}}},
  130. {"oneof sint64", &pb.Oneof{Union: &pb.Oneof_F_Sint64{51}}},
  131. {"oneof enum", &pb.Oneof{Union: &pb.Oneof_F_Enum{pb.MyMessage_BLUE}}},
  132. {"message for oneof", &pb.GoTestField{Label: String("k"), Type: String("v")}},
  133. {"oneof message", &pb.Oneof{Union: &pb.Oneof_F_Message{&pb.GoTestField{Label: String("k"), Type: String("v")}}}},
  134. {"oneof group", &pb.Oneof{Union: &pb.Oneof_FGroup{&pb.Oneof_F_Group{X: Int32(52)}}}},
  135. {"oneof largest tag", &pb.Oneof{Union: &pb.Oneof_F_Largest_Tag{1}}},
  136. {"multiple oneofs", &pb.Oneof{Union: &pb.Oneof_F_Int32{1}, Tormato: &pb.Oneof_Value{2}}},
  137. }
  138. func TestSize(t *testing.T) {
  139. for _, tc := range SizeTests {
  140. size := Size(tc.pb)
  141. b, err := Marshal(tc.pb)
  142. if err != nil {
  143. t.Errorf("%v: Marshal failed: %v", tc.desc, err)
  144. continue
  145. }
  146. if size != len(b) {
  147. t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
  148. t.Logf("%v: bytes: %#v", tc.desc, b)
  149. }
  150. }
  151. }