| 1 |
// Protocol Buffers for Go with Gadgets |
| 2 |
// |
| 3 |
// Copyright (c) 2015, The GoGo Authors. All rights reserved. |
| 4 |
// http://github.com/gogo/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 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
package theproto3 |
| 30 |
|
| 31 |
import ( |
| 32 |
"reflect" |
| 33 |
"testing" |
| 34 |
|
| 35 |
"github.com/gogo/protobuf/proto" |
| 36 |
) |
| 37 |
|
| 38 |
func TestNilMaps(t *testing.T) { |
| 39 |
m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}} |
| 40 |
data, err := proto.Marshal(m) |
| 41 |
if err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
size := m.Size() |
| 45 |
protoSize := proto.Size(m) |
| 46 |
marshaledSize := len(data) |
| 47 |
if size != protoSize || marshaledSize != protoSize { |
| 48 |
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize) |
| 49 |
} |
| 50 |
m2 := &AllMaps{} |
| 51 |
if err := proto.Unmarshal(data, m2); err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
if v, ok := m2.StringToMsgMap["a"]; !ok { |
| 55 |
t.Error("element not in map") |
| 56 |
} else if v != nil { |
| 57 |
t.Errorf("element should be nil, but its %v", v) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestNilMapsBytes(t *testing.T) { |
| 62 |
m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}} |
| 63 |
data, err := proto.Marshal(m) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
size := m.Size() |
| 68 |
protoSize := proto.Size(m) |
| 69 |
marshaledSize := len(data) |
| 70 |
if size != protoSize || marshaledSize != protoSize { |
| 71 |
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize) |
| 72 |
} |
| 73 |
m2 := &AllMaps{} |
| 74 |
if err := proto.Unmarshal(data, m2); err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
if v, ok := m2.StringToBytesMap["a"]; !ok { |
| 78 |
t.Error("element not in map") |
| 79 |
} else if len(v) != 0 { |
| 80 |
t.Errorf("element should be empty, but its %v", v) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestEmptyMapsBytes(t *testing.T) { |
| 85 |
m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}} |
| 86 |
data, err := proto.Marshal(m) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
size := m.Size() |
| 91 |
protoSize := proto.Size(m) |
| 92 |
marshaledSize := len(data) |
| 93 |
if size != protoSize || marshaledSize != protoSize { |
| 94 |
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize) |
| 95 |
} |
| 96 |
m2 := &AllMaps{} |
| 97 |
if err := proto.Unmarshal(data, m2); err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
if v, ok := m2.StringToBytesMap["b"]; !ok { |
| 101 |
t.Error("element not in map") |
| 102 |
} else if len(v) != 0 { |
| 103 |
t.Errorf("element should be empty, but its %v", v) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestCustomTypeSize(t *testing.T) { |
| 108 |
m := &Uint128Pair{} |
| 109 |
m.Size() // Should not panic. |
| 110 |
} |
| 111 |
|
| 112 |
func TestCustomTypeMarshalUnmarshal(t *testing.T) { |
| 113 |
m1 := &Uint128Pair{} |
| 114 |
if b, err := proto.Marshal(m1); err != nil { |
| 115 |
t.Fatal(err) |
| 116 |
} else { |
| 117 |
m2 := &Uint128Pair{} |
| 118 |
if err := proto.Unmarshal(b, m2); err != nil { |
| 119 |
t.Fatal(err) |
| 120 |
} |
| 121 |
if !m1.Equal(m2) { |
| 122 |
t.Errorf("expected %+v, got %+v", m1, m2) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestNotPackedToPacked(t *testing.T) { |
| 128 |
input := []uint64{1, 10e9} |
| 129 |
notpacked := &NotPacked{Key: input} |
| 130 |
if data, err := proto.Marshal(notpacked); err != nil { |
| 131 |
t.Fatal(err) |
| 132 |
} else { |
| 133 |
packed := &Message{} |
| 134 |
if err := proto.Unmarshal(data, packed); err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
output := packed.Key |
| 138 |
if !reflect.DeepEqual(input, output) { |
| 139 |
t.Fatalf("expected %#v, got %#v", input, output) |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func TestPackedToNotPacked(t *testing.T) { |
| 145 |
input := []uint64{1, 10e9} |
| 146 |
packed := &Message{Key: input} |
| 147 |
if data, err := proto.Marshal(packed); err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} else { |
| 150 |
notpacked := &NotPacked{} |
| 151 |
if err := proto.Unmarshal(data, notpacked); err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
output := notpacked.Key |
| 155 |
if !reflect.DeepEqual(input, output) { |
| 156 |
t.Fatalf("expected %#v, got %#v", input, output) |
| 157 |
} |
| 158 |
} |
| 159 |
} |