blob: 9b735b774f7099a125365fada24647dd70116b2f [file] [log] [blame]
David Symondsfc16c2c2012-11-07 23:41:17 +11001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2012 The Go Authors. All rights reserved.
David Symondsfc16c2c2012-11-07 23:41:17 +11004// 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
32package proto_test
33
34import (
35 "log"
36 "testing"
37
38 pb "./testdata"
39 . "code.google.com/p/goprotobuf/proto"
40)
41
42var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
David Symondsbe02a4a2012-12-06 15:20:41 +110043
44// messageWithExtension2 is in equal_test.go.
David Symondsfc16c2c2012-11-07 23:41:17 +110045var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
46
47func 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)
David Symonds20370902013-03-23 17:20:01 +110057
David Symondsfc16c2c2012-11-07 23:41:17 +110058}
59
60var 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 string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
David Symonds40877592013-06-08 09:17:00 +100089 {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
David Symondsfc16c2c2012-11-07 23:41:17 +110090 // Nested.
91 {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
David Symonds6677c3e2013-06-11 08:48:47 +100092 {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
David Symondsfc16c2c2012-11-07 23:41:17 +110093 // Other things.
94 {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
95 {"extension (unencoded)", messageWithExtension1},
96 {"extension (encoded)", messageWithExtension3},
97}
98
99func TestSize(t *testing.T) {
100 for _, tc := range SizeTests {
101 size := Size(tc.pb)
102 b, err := Marshal(tc.pb)
103 if err != nil {
104 t.Errorf("%v: Marshal failed: %v", tc.desc, err)
105 continue
106 }
107 if size != len(b) {
108 t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
109 t.Logf("%v: bytes: %#v", tc.desc, b)
110 }
111 }
112}