size_test.go 7.1 KB

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