cmd/stdiscosrv: New discovery server (fixes #4618)

This is a new revision of the discovery server. Relevant changes and
non-changes:

- Protocol towards clients is unchanged.

- Recommended large scale design is still to be deployed nehind nginx (I
  tested, and it's still a lot faster at terminating TLS).

- Database backend is leveldb again, only. It scales enough, is easy to
  setup, and we don't need any backend to take care of.

- Server supports replication. This is a simple TCP channel - protect it
  with a firewall when deploying over the internet. (We deploy this within
  the same datacenter, and with firewall.) Any incoming client announces
  are sent over the replication channel(s) to other peer discosrvs.
  Incoming replication changes are applied to the database as if they came
  from clients, but without the TLS/certificate overhead.

- Metrics are exposed using the prometheus library, when enabled.

- The database values and replication protocol is protobuf, because JSON
  was quite CPU intensive when I tried that and benchmarked it.

- The "Retry-After" value for failed lookups gets slowly increased from
  a default of 120 seconds, by 5 seconds for each failed lookup,
  independently by each discosrv. This lowers the query load over time for
  clients that are never seen. The Retry-After maxes out at 3600 after a
  couple of weeks of this increase. The number of failed lookups is
  stored in the database, now and then (avoiding making each lookup a
  database put).

All in all this means clients can be pointed towards a cluster using
just multiple A / AAAA records to gain both load sharing and redundancy
(if one is down, clients will talk to the remaining ones).

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4648
This commit is contained in:
Jakob Borg
2018-01-14 08:52:31 +00:00
parent 341b9691a7
commit 916ec63af6
864 changed files with 216825 additions and 64540 deletions

View File

@@ -0,0 +1,35 @@
# Protocol Buffers for Go with Gadgets
#
# Copyright (c) 2013, The GoGo Authors. All rights reserved.
# http://github.com/gogo/protobuf
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
regenerate:
go install github.com/gogo/protobuf/protoc-gen-combo
go install github.com/gogo/protobuf/protoc-gen-gogo
cp header.proto mapsproto2.proto
cat ../theproto3/maps.proto >> mapsproto2.proto
find combos -type d -not -name combos -exec cp mapsproto2_test.go.in {}/mapsproto2_test.go \;
protoc-gen-combo --version="3.0.0" --gogo_out=. --proto_path=../../../../../:../../protobuf/:. mapsproto2.proto

View File

@@ -0,0 +1,124 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2014 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
package proto2.maps;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.face_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
message FloatingPoint {
optional double f = 1;
}
message CustomMap {
map<string, bytes> Nullable128s = 1 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128"];
map<string, bytes> Uint128s = 2 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable)=false];
map<string, bytes> NullableIds = 3 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid"];
map<string, bytes> Ids = 4 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid", (gogoproto.nullable)=false];
}
enum MapEnum {
MA = 0;
MB = 1;
MC = 2;
}
message AllMaps {
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}
message AllMapsOrdered {
option (gogoproto.stable_marshaler) = true;
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}

View File

@@ -0,0 +1,104 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2015, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package proto2_maps
import (
"testing"
"github.com/gogo/protobuf/proto"
)
func TestNilMaps(t *testing.T) {
m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToMsgMap["a"]; !ok {
t.Error("element not in map")
} else if v != nil {
t.Errorf("element should be nil, but its %v", v)
}
}
func TestNilMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["a"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}
func TestEmptyMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["b"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}

View File

@@ -0,0 +1,989 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: combos/both/mapsproto2.proto
/*
Package proto2_maps is a generated protocol buffer package.
It is generated from these files:
combos/both/mapsproto2.proto
It has these top-level messages:
FloatingPoint
CustomMap
AllMaps
AllMapsOrdered
*/
package proto2_maps
import testing "testing"
import rand "math/rand"
import time "time"
import proto "github.com/gogo/protobuf/proto"
import jsonpb "github.com/gogo/protobuf/jsonpb"
import fmt "fmt"
import parser "go/parser"
import math "math"
import _ "github.com/gogo/protobuf/gogoproto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
func TestFloatingPointProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestFloatingPointMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkFloatingPointProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkFloatingPointProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedFloatingPoint(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &FloatingPoint{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestCustomMapMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkCustomMapProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkCustomMapProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedCustomMap(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &CustomMap{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestAllMapsMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkAllMapsProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMaps(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMaps{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestAllMapsOrderedMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkAllMapsOrderedProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsOrderedProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMapsOrdered(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMapsOrdered{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestCustomMapJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsOrderedJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestFloatingPointProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestFloatingPointProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.CompactTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.CompactTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestMapsproto2Description(t *testing.T) {
Mapsproto2Description()
}
func TestFloatingPointVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestCustomMapVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsOrderedVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestFloatingPointFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestCustomMapFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsOrderedFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestFloatingPointGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestCustomMapGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsOrderedGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestFloatingPointSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkFloatingPointSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkCustomMapSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsOrderedSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestCustomMapStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsOrderedStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
//These tests are generated by github.com/gogo/protobuf/plugin/testgen

View File

@@ -0,0 +1,124 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2014 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
package proto2.maps;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.face_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
message FloatingPoint {
optional double f = 1;
}
message CustomMap {
map<string, bytes> Nullable128s = 1 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128"];
map<string, bytes> Uint128s = 2 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable)=false];
map<string, bytes> NullableIds = 3 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid"];
map<string, bytes> Ids = 4 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid", (gogoproto.nullable)=false];
}
enum MapEnum {
MA = 0;
MB = 1;
MC = 2;
}
message AllMaps {
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}
message AllMapsOrdered {
option (gogoproto.stable_marshaler) = true;
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}

View File

@@ -0,0 +1,104 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2015, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package proto2_maps
import (
"testing"
"github.com/gogo/protobuf/proto"
)
func TestNilMaps(t *testing.T) {
m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToMsgMap["a"]; !ok {
t.Error("element not in map")
} else if v != nil {
t.Errorf("element should be nil, but its %v", v)
}
}
func TestNilMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["a"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}
func TestEmptyMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["b"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}

View File

@@ -0,0 +1,989 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: combos/marshaler/mapsproto2.proto
/*
Package proto2_maps is a generated protocol buffer package.
It is generated from these files:
combos/marshaler/mapsproto2.proto
It has these top-level messages:
FloatingPoint
CustomMap
AllMaps
AllMapsOrdered
*/
package proto2_maps
import testing "testing"
import rand "math/rand"
import time "time"
import proto "github.com/gogo/protobuf/proto"
import jsonpb "github.com/gogo/protobuf/jsonpb"
import fmt "fmt"
import parser "go/parser"
import math "math"
import _ "github.com/gogo/protobuf/gogoproto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
func TestFloatingPointProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestFloatingPointMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkFloatingPointProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkFloatingPointProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedFloatingPoint(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &FloatingPoint{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestCustomMapMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkCustomMapProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkCustomMapProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedCustomMap(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &CustomMap{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestAllMapsMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkAllMapsProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMaps(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMaps{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func TestAllMapsOrderedMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
_, err := p.MarshalTo(dAtA)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func BenchmarkAllMapsOrderedProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsOrderedProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMapsOrdered(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMapsOrdered{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestCustomMapJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsOrderedJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestFloatingPointProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestFloatingPointProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.CompactTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.CompactTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestMapsproto2Description(t *testing.T) {
Mapsproto2Description()
}
func TestFloatingPointVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestCustomMapVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsOrderedVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestFloatingPointFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestCustomMapFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsOrderedFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestFloatingPointGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestCustomMapGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsOrderedGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestFloatingPointSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkFloatingPointSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkCustomMapSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsOrderedSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestCustomMapStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsOrderedStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
//These tests are generated by github.com/gogo/protobuf/plugin/testgen

View File

@@ -0,0 +1,124 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2014 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
package proto2.maps;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.face_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
message FloatingPoint {
optional double f = 1;
}
message CustomMap {
map<string, bytes> Nullable128s = 1 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128"];
map<string, bytes> Uint128s = 2 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable)=false];
map<string, bytes> NullableIds = 3 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid"];
map<string, bytes> Ids = 4 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid", (gogoproto.nullable)=false];
}
enum MapEnum {
MA = 0;
MB = 1;
MC = 2;
}
message AllMaps {
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}
message AllMapsOrdered {
option (gogoproto.stable_marshaler) = true;
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}

View File

@@ -0,0 +1,104 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2015, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package proto2_maps
import (
"testing"
"github.com/gogo/protobuf/proto"
)
func TestNilMaps(t *testing.T) {
m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToMsgMap["a"]; !ok {
t.Error("element not in map")
} else if v != nil {
t.Errorf("element should be nil, but its %v", v)
}
}
func TestNilMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["a"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}
func TestEmptyMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["b"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}

View File

@@ -0,0 +1,877 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: combos/neither/mapsproto2.proto
/*
Package proto2_maps is a generated protocol buffer package.
It is generated from these files:
combos/neither/mapsproto2.proto
It has these top-level messages:
FloatingPoint
CustomMap
AllMaps
AllMapsOrdered
*/
package proto2_maps
import testing "testing"
import rand "math/rand"
import time "time"
import proto "github.com/gogo/protobuf/proto"
import jsonpb "github.com/gogo/protobuf/jsonpb"
import fmt "fmt"
import parser "go/parser"
import math "math"
import _ "github.com/gogo/protobuf/gogoproto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
func TestFloatingPointProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkFloatingPointProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkFloatingPointProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedFloatingPoint(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &FloatingPoint{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkCustomMapProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkCustomMapProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedCustomMap(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &CustomMap{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkAllMapsProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMaps(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMaps{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkAllMapsOrderedProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsOrderedProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMapsOrdered(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMapsOrdered{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestCustomMapJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsOrderedJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestFloatingPointProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestFloatingPointProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.CompactTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.CompactTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestMapsproto2Description(t *testing.T) {
Mapsproto2Description()
}
func TestFloatingPointVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestCustomMapVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsOrderedVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestFloatingPointFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestCustomMapFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsOrderedFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestFloatingPointGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestCustomMapGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsOrderedGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestFloatingPointSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkFloatingPointSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkCustomMapSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsOrderedSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestCustomMapStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsOrderedStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
//These tests are generated by github.com/gogo/protobuf/plugin/testgen

View File

@@ -0,0 +1,124 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2014 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
package proto2.maps;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.face_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.marshaler_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
message FloatingPoint {
optional double f = 1;
}
message CustomMap {
map<string, bytes> Nullable128s = 1 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128"];
map<string, bytes> Uint128s = 2 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable)=false];
map<string, bytes> NullableIds = 3 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid"];
map<string, bytes> Ids = 4 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid", (gogoproto.nullable)=false];
}
enum MapEnum {
MA = 0;
MB = 1;
MC = 2;
}
message AllMaps {
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}
message AllMapsOrdered {
option (gogoproto.stable_marshaler) = true;
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}

View File

@@ -0,0 +1,104 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2015, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package proto2_maps
import (
"testing"
"github.com/gogo/protobuf/proto"
)
func TestNilMaps(t *testing.T) {
m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToMsgMap["a"]; !ok {
t.Error("element not in map")
} else if v != nil {
t.Errorf("element should be nil, but its %v", v)
}
}
func TestNilMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["a"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}
func TestEmptyMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"b": {}}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["b"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}

View File

@@ -0,0 +1,877 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: combos/unmarshaler/mapsproto2.proto
/*
Package proto2_maps is a generated protocol buffer package.
It is generated from these files:
combos/unmarshaler/mapsproto2.proto
It has these top-level messages:
FloatingPoint
CustomMap
AllMaps
AllMapsOrdered
*/
package proto2_maps
import testing "testing"
import rand "math/rand"
import time "time"
import proto "github.com/gogo/protobuf/proto"
import jsonpb "github.com/gogo/protobuf/jsonpb"
import fmt "fmt"
import parser "go/parser"
import math "math"
import _ "github.com/gogo/protobuf/gogoproto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
func TestFloatingPointProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkFloatingPointProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkFloatingPointProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedFloatingPoint(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &FloatingPoint{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkCustomMapProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkCustomMapProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedCustomMap(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &CustomMap{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkAllMapsProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMaps(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMaps{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
littlefuzz := make([]byte, len(dAtA))
copy(littlefuzz, dAtA)
for i := range dAtA {
dAtA[i] = byte(popr.Intn(256))
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
if len(littlefuzz) > 0 {
fuzzamount := 100
for i := 0; i < fuzzamount; i++ {
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256))
littlefuzz = append(littlefuzz, byte(popr.Intn(256)))
}
// shouldn't panic
_ = proto.Unmarshal(littlefuzz, msg)
}
}
func BenchmarkAllMapsOrderedProtoMarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 10000)
for i := 0; i < 10000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
dAtA, err := proto.Marshal(pops[i%10000])
if err != nil {
panic(err)
}
total += len(dAtA)
}
b.SetBytes(int64(total / b.N))
}
func BenchmarkAllMapsOrderedProtoUnmarshal(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
datas := make([][]byte, 10000)
for i := 0; i < 10000; i++ {
dAtA, err := proto.Marshal(NewPopulatedAllMapsOrdered(popr, false))
if err != nil {
panic(err)
}
datas[i] = dAtA
}
msg := &AllMapsOrdered{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += len(datas[i%10000])
if err := proto.Unmarshal(datas[i%10000], msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &FloatingPoint{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestCustomMapJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &CustomMap{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMaps{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestAllMapsOrderedJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
marshaler := jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &AllMapsOrdered{}
err = jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestFloatingPointProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestFloatingPointProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
dAtA := proto.CompactTextString(p)
msg := &FloatingPoint{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestCustomMapProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
dAtA := proto.CompactTextString(p)
msg := &CustomMap{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMaps{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.MarshalTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestAllMapsOrderedProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
dAtA := proto.CompactTextString(p)
msg := &AllMapsOrdered{}
if err := proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err)
}
if !p.Equal(msg) {
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p)
}
}
func TestMapsproto2Description(t *testing.T) {
Mapsproto2Description()
}
func TestFloatingPointVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &FloatingPoint{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestCustomMapVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &CustomMap{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMaps{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestAllMapsOrderedVerboseEqual(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
dAtA, err := proto.Marshal(p)
if err != nil {
panic(err)
}
msg := &AllMapsOrdered{}
if err := proto.Unmarshal(dAtA, msg); err != nil {
panic(err)
}
if err := p.VerboseEqual(msg); err != nil {
t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
}
}
func TestFloatingPointFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestCustomMapFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestAllMapsOrderedFace(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, true)
msg := p.TestProto()
if !p.Equal(msg) {
t.Fatalf("%#v !Face Equal %#v", msg, p)
}
}
func TestFloatingPointGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestCustomMapGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestAllMapsOrderedGoString(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.GoString()
s2 := fmt.Sprintf("%#v", p)
if s1 != s2 {
t.Fatalf("GoString want %v got %v", s1, s2)
}
_, err := parser.ParseExpr(s1)
if err != nil {
t.Fatal(err)
}
}
func TestFloatingPointSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedFloatingPoint(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkFloatingPointSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*FloatingPoint, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedFloatingPoint(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestCustomMapSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedCustomMap(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkCustomMapSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*CustomMap, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedCustomMap(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMaps(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMaps, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMaps(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestAllMapsOrderedSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := rand.New(rand.NewSource(seed))
p := NewPopulatedAllMapsOrdered(popr, true)
size2 := proto.Size(p)
dAtA, err := proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
size := p.Size()
if len(dAtA) != size {
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA))
}
if size2 != size {
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)
}
size3 := proto.Size(p)
if size3 != size {
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)
}
}
func BenchmarkAllMapsOrderedSize(b *testing.B) {
popr := rand.New(rand.NewSource(616))
total := 0
pops := make([]*AllMapsOrdered, 1000)
for i := 0; i < 1000; i++ {
pops[i] = NewPopulatedAllMapsOrdered(popr, false)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
total += pops[i%1000].Size()
}
b.SetBytes(int64(total / b.N))
}
func TestFloatingPointStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedFloatingPoint(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestCustomMapStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedCustomMap(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMaps(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
func TestAllMapsOrderedStringer(t *testing.T) {
popr := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPopulatedAllMapsOrdered(popr, false)
s1 := p.String()
s2 := fmt.Sprintf("%v", p)
if s1 != s2 {
t.Fatalf("String want %v got %v", s1, s2)
}
}
//These tests are generated by github.com/gogo/protobuf/plugin/testgen

View File

@@ -0,0 +1,76 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2014 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
package proto2.maps;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.face_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
message FloatingPoint {
optional double f = 1;
}
message CustomMap {
map<string, bytes> Nullable128s = 1 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128"];
map<string, bytes> Uint128s = 2 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable)=false];
map<string, bytes> NullableIds = 3 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid"];
map<string, bytes> Ids = 4 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid", (gogoproto.nullable)=false];
}

View File

@@ -0,0 +1,124 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2014 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
package proto2.maps;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.stringer_all) = true;
option (gogoproto.gostring_all) = true;
option (gogoproto.face_all) = true;
option (gogoproto.description_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.enum_stringer_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
message FloatingPoint {
optional double f = 1;
}
message CustomMap {
map<string, bytes> Nullable128s = 1 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128"];
map<string, bytes> Uint128s = 2 [(gogoproto.customtype)="github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable)=false];
map<string, bytes> NullableIds = 3 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid"];
map<string, bytes> Ids = 4 [(gogoproto.customtype)="github.com/gogo/protobuf/test.Uuid", (gogoproto.nullable)=false];
}
enum MapEnum {
MA = 0;
MB = 1;
MC = 2;
}
message AllMaps {
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}
message AllMapsOrdered {
option (gogoproto.stable_marshaler) = true;
map<string, double> StringToDoubleMap = 1;
map<string, float> StringToFloatMap = 2;
map<int32, int32> Int32Map = 3;
map<int64, int64> Int64Map = 4;
map<uint32, uint32> Uint32Map = 5;
map<uint64, uint64> Uint64Map = 6;
map<sint32, sint32> Sint32Map = 7;
map<sint64, sint64> Sint64Map = 8;
map<fixed32, fixed32> Fixed32Map = 9;
map<sfixed32, sfixed32> Sfixed32Map = 10;
map<fixed64, fixed64> Fixed64Map = 11;
map<sfixed64, sfixed64> Sfixed64Map = 12;
map<bool, bool> BoolMap = 13;
map<string, string> StringMap = 14;
map<string, bytes> StringToBytesMap = 15;
map<string, MapEnum> StringToEnumMap = 16;
map<string, FloatingPoint> StringToMsgMap = 17;
}

View File

@@ -0,0 +1,104 @@
// Protocol Buffers for Go with Gadgets
//
// Copyright (c) 2015, The GoGo Authors. All rights reserved.
// http://github.com/gogo/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package proto2_maps
import (
"testing"
"github.com/gogo/protobuf/proto"
)
func TestNilMaps(t *testing.T) {
m := &AllMaps{StringToMsgMap: map[string]*FloatingPoint{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToMsgMap["a"]; !ok {
t.Error("element not in map")
} else if v != nil {
t.Errorf("element should be nil, but its %v", v)
}
}
func TestNilMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"a": nil}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["a"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}
func TestEmptyMapsBytes(t *testing.T) {
m := &AllMaps{StringToBytesMap: map[string][]byte{"b": []byte{}}}
data, err := proto.Marshal(m)
if err != nil {
t.Fatal(err)
}
size := m.Size()
protoSize := proto.Size(m)
marshaledSize := len(data)
if size != protoSize || marshaledSize != protoSize {
t.Errorf("size %d != protoSize %d != marshaledSize %d", size, protoSize, marshaledSize)
}
m2 := &AllMaps{}
if err := proto.Unmarshal(data, m2); err != nil {
t.Fatal(err)
}
if v, ok := m2.StringToBytesMap["b"]; !ok {
t.Error("element not in map")
} else if len(v) != 0 {
t.Errorf("element should be empty, but its %v", v)
}
}