github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/protobuf/proto/size_test.go (about)

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