github.com/gogo/protobuf@v1.3.2/test/mapsproto2/combos/both/mapsproto2_test.go (about) 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 proto2_maps 30 31 import ( 32 "testing" 33 34 "github.com/gogo/protobuf/proto" 35 ) 36 37 func TestNilMaps(t *testing.T) { 38 m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}} 39 data, err := proto.Marshal(m) 40 if err != nil { 41 t.Fatal(err) 42 } 43 size := m.Size() 44 protoSize := proto.Size(m) 45 marshaledSize := len(data) 46 if size != protoSize || marshaledSize != protoSize { 47 t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize) 48 } 49 m2 := &AllMaps{} 50 if err := proto.Unmarshal(data, m2); err != nil { 51 t.Fatal(err) 52 } 53 if v, ok := m2.StringToMsgMap["a"]; !ok { 54 t.Error("element not in map") 55 } else if v != nil { 56 t.Errorf("element should be nil, but its %v", v) 57 } 58 } 59 60 func TestNilMapsBytes(t *testing.T) { 61 m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}} 62 data, err := proto.Marshal(m) 63 if err != nil { 64 t.Fatal(err) 65 } 66 size := m.Size() 67 protoSize := proto.Size(m) 68 marshaledSize := len(data) 69 if size != protoSize || marshaledSize != protoSize { 70 t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize) 71 } 72 m2 := &AllMaps{} 73 if err := proto.Unmarshal(data, m2); err != nil { 74 t.Fatal(err) 75 } 76 if v, ok := m2.StringToBytesMap["a"]; !ok { 77 t.Error("element not in map") 78 } else if len(v) != 0 { 79 t.Errorf("element should be empty, but its %v", v) 80 } 81 } 82 83 func TestEmptyMapsBytes(t *testing.T) { 84 m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}} 85 data, err := proto.Marshal(m) 86 if err != nil { 87 t.Fatal(err) 88 } 89 size := m.Size() 90 protoSize := proto.Size(m) 91 marshaledSize := len(data) 92 if size != protoSize || marshaledSize != protoSize { 93 t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize) 94 } 95 m2 := &AllMaps{} 96 if err := proto.Unmarshal(data, m2); err != nil { 97 t.Fatal(err) 98 } 99 if v, ok := m2.StringToBytesMap["b"]; !ok { 100 t.Error("element not in map") 101 } else if len(v) != 0 { 102 t.Errorf("element should be empty, but its %v", v) 103 } 104 }