size_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "testing"
  35. proto3pb "./proto3_proto"
  36. pb "./testdata"
  37. . "github.com/golang/protobuf/proto"
  38. )
  39. var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
  40. // messageWithExtension2 is in equal_test.go.
  41. var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
  42. func init() {
  43. if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil {
  44. log.Panicf("SetExtension: %v", err)
  45. }
  46. if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil {
  47. log.Panicf("SetExtension: %v", err)
  48. }
  49. // Force messageWithExtension3 to have the extension encoded.
  50. Marshal(messageWithExtension3)
  51. }
  52. var SizeTests = []struct {
  53. desc string
  54. pb Message
  55. }{
  56. {"empty", &pb.OtherMessage{}},
  57. // Basic types.
  58. {"bool", &pb.Defaults{F_Bool: Bool(true)}},
  59. {"int32", &pb.Defaults{F_Int32: Int32(12)}},
  60. {"negative int32", &pb.Defaults{F_Int32: Int32(-1)}},
  61. {"small int64", &pb.Defaults{F_Int64: Int64(1)}},
  62. {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}},
  63. {"negative int64", &pb.Defaults{F_Int64: Int64(-1)}},
  64. {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}},
  65. {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}},
  66. {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}},
  67. {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}},
  68. {"float", &pb.Defaults{F_Float: Float32(12.6)}},
  69. {"double", &pb.Defaults{F_Double: Float64(13.9)}},
  70. {"string", &pb.Defaults{F_String: String("niles")}},
  71. {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}},
  72. {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}},
  73. {"sint32", &pb.Defaults{F_Sint32: Int32(65)}},
  74. {"sint64", &pb.Defaults{F_Sint64: Int64(67)}},
  75. {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}},
  76. // Repeated.
  77. {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}},
  78. {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}},
  79. {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
  80. {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
  81. {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
  82. {"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{
  83. // Need enough large numbers to verify that the header is counting the number of bytes
  84. // for the field, not the number of elements.
  85. 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
  86. 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
  87. }}},
  88. {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
  89. {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
  90. // Nested.
  91. {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
  92. {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
  93. // Other things.
  94. {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
  95. {"extension (unencoded)", messageWithExtension1},
  96. {"extension (encoded)", messageWithExtension3},
  97. // proto3 message
  98. {"proto3 empty", &proto3pb.Message{}},
  99. {"proto3 bool", &proto3pb.Message{TrueScotsman: true}},
  100. {"proto3 int64", &proto3pb.Message{ResultCount: 1}},
  101. {"proto3 uint32", &proto3pb.Message{HeightInCm: 123}},
  102. {"proto3 float", &proto3pb.Message{Score: 12.6}},
  103. {"proto3 string", &proto3pb.Message{Name: "Snezana"}},
  104. {"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}},
  105. {"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}},
  106. {"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  107. {"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
  108. {"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: &pb.FloatingPoint{F: Float64(2.0)}}}},
  109. {"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
  110. }
  111. func TestSize(t *testing.T) {
  112. for _, tc := range SizeTests {
  113. size := Size(tc.pb)
  114. b, err := Marshal(tc.pb)
  115. if err != nil {
  116. t.Errorf("%v: Marshal failed: %v", tc.desc, err)
  117. continue
  118. }
  119. if size != len(b) {
  120. t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
  121. t.Logf("%v: bytes: %#v", tc.desc, b)
  122. }
  123. }
  124. }