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