blob: 31aaf54910e84623e5e3a624b2f6df6aa4f9b4dc [file] [log] [blame]
David Symondsfc16c2c2012-11-07 23:41:17 +11001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2012 Google Inc. 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
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)}
43var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
44
45func init() {
46 if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil {
47 log.Panicf("SetExtension: %v", err)
48 }
49 if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil {
50 log.Panicf("SetExtension: %v", err)
51 }
52
53 // Force messageWithExtension3 to have the extension encoded.
54 Marshal(messageWithExtension3)
55}
56
57var SizeTests = []struct {
58 desc string
59 pb Message
60}{
61 {"empty", &pb.OtherMessage{}},
62 // Basic types.
63 {"bool", &pb.Defaults{F_Bool: Bool(true)}},
64 {"int32", &pb.Defaults{F_Int32: Int32(12)}},
65 {"small int64", &pb.Defaults{F_Int64: Int64(1)}},
66 {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}},
67 {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}},
68 {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}},
69 {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}},
70 {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}},
71 {"float", &pb.Defaults{F_Float: Float32(12.6)}},
72 {"double", &pb.Defaults{F_Double: Float64(13.9)}},
73 {"string", &pb.Defaults{F_String: String("niles")}},
74 {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}},
75 {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}},
76 {"sint32", &pb.Defaults{F_Sint32: Int32(65)}},
77 {"sint64", &pb.Defaults{F_Sint64: Int64(67)}},
78 {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}},
79 // Repeated.
80 {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}},
81 {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}},
82 {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
83 {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729}}},
84 {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
85 {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
86 // Nested.
87 {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
88 // Other things.
89 {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
90 {"extension (unencoded)", messageWithExtension1},
91 {"extension (encoded)", messageWithExtension3},
92}
93
94func TestSize(t *testing.T) {
95 for _, tc := range SizeTests {
96 size := Size(tc.pb)
97 b, err := Marshal(tc.pb)
98 if err != nil {
99 t.Errorf("%v: Marshal failed: %v", tc.desc, err)
100 continue
101 }
102 if size != len(b) {
103 t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
104 t.Logf("%v: bytes: %#v", tc.desc, b)
105 }
106 }
107}