mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2024-12-29 12:17:48 +00:00
4b7aff630a
The terminology Vector does not occur in protobuf documentation at all, so we should rename the Go use of the term to something more recognizable. As such, all instances that match the regexp "[Vv]ect(or)?" were replaced. The C++ documentation uses the term "Repeated", which is a reasonable name. However, the term became overloaded in 2014, when maps were added as a feature and implementated under the hood as repeated fields. This is confusing as it means "repeated" could either refer to repeated fields proper (i.e., explicitly marked with the "repeated" label in the proto file) or map fields. In the case of the C++ reflective API, this is not a problem since repeated fields proper and map fields are interacted with through the same RepeatedField type. In Go, we do not use a single type to handle both types of repeated fields: 1) We are coming up with the Go protobuf reflection API for the first time and so do not need to piggy-back on the repeated fields API to remain backwards compatible since no former usages of Go protobuf reflection exists. 2) Map fields are commonly represented in Go as the Go map type, which do not preserve ordering information. As such it is fundamentally impossible to present an unordered map as a consistently ordered list. Thus, Go needs two different interfaces for lists and maps. Given the above situation, "Repeated" is not a great term to use since it refers to two different things (when we only want one of the meanings). To distinguish between the two, we'll use the terms "List" and "Map" instead. There is some precedence for the term "List" in the protobuf codebase (e.g., "getRepeatedInt32List"). Change-Id: Iddcdb6b78e1e60c14fa4ca213c15f45e214b967b Reviewed-on: https://go-review.googlesource.com/c/149657 Reviewed-by: Damien Neil <dneil@google.com>
148 lines
4.4 KiB
Go
148 lines
4.4 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package protoreflect
|
|
|
|
import (
|
|
"bytes"
|
|
"math"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestValue(t *testing.T) {
|
|
fakeMessage := new(struct{ Message })
|
|
fakeList := new(struct{ List })
|
|
fakeMap := new(struct{ Map })
|
|
|
|
tests := []struct {
|
|
in Value
|
|
want interface{}
|
|
}{
|
|
{in: Value{}},
|
|
{in: ValueOf(nil)},
|
|
{in: ValueOf(true), want: true},
|
|
{in: ValueOf(int32(math.MaxInt32)), want: int32(math.MaxInt32)},
|
|
{in: ValueOf(int64(math.MaxInt64)), want: int64(math.MaxInt64)},
|
|
{in: ValueOf(uint32(math.MaxUint32)), want: uint32(math.MaxUint32)},
|
|
{in: ValueOf(uint64(math.MaxUint64)), want: uint64(math.MaxUint64)},
|
|
{in: ValueOf(float32(math.MaxFloat32)), want: float32(math.MaxFloat32)},
|
|
{in: ValueOf(float64(math.MaxFloat64)), want: float64(math.MaxFloat64)},
|
|
{in: ValueOf(string("hello")), want: string("hello")},
|
|
{in: ValueOf([]byte("hello")), want: []byte("hello")},
|
|
{in: ValueOf(fakeMessage), want: fakeMessage},
|
|
{in: ValueOf(fakeList), want: fakeList},
|
|
{in: ValueOf(fakeMap), want: fakeMap},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := tt.in.Interface()
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("Value(%v).Interface() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
|
|
if got := tt.in.IsValid(); got != (tt.want != nil) {
|
|
t.Errorf("Value(%v).IsValid() = %v, want %v", tt.in, got, tt.want != nil)
|
|
}
|
|
switch want := tt.want.(type) {
|
|
case int32:
|
|
if got := tt.in.Int(); got != int64(want) {
|
|
t.Errorf("Value(%v).Int() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case int64:
|
|
if got := tt.in.Int(); got != int64(want) {
|
|
t.Errorf("Value(%v).Int() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case uint32:
|
|
if got := tt.in.Uint(); got != uint64(want) {
|
|
t.Errorf("Value(%v).Uint() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case uint64:
|
|
if got := tt.in.Uint(); got != uint64(want) {
|
|
t.Errorf("Value(%v).Uint() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case float32:
|
|
if got := tt.in.Float(); got != float64(want) {
|
|
t.Errorf("Value(%v).Float() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case float64:
|
|
if got := tt.in.Float(); got != float64(want) {
|
|
t.Errorf("Value(%v).Float() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case string:
|
|
if got := tt.in.String(); got != string(want) {
|
|
t.Errorf("Value(%v).String() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case []byte:
|
|
if got := tt.in.Bytes(); !bytes.Equal(got, want) {
|
|
t.Errorf("Value(%v).Bytes() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case EnumNumber:
|
|
if got := tt.in.Enum(); got != want {
|
|
t.Errorf("Value(%v).Enum() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case Message:
|
|
if got := tt.in.Message(); got != want {
|
|
t.Errorf("Value(%v).Message() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case List:
|
|
if got := tt.in.List(); got != want {
|
|
t.Errorf("Value(%v).List() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
case Map:
|
|
if got := tt.in.Map(); got != want {
|
|
t.Errorf("Value(%v).Map() = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkValue(b *testing.B) {
|
|
const testdata = "The quick brown fox jumped over the lazy dog."
|
|
var sink1 string
|
|
var sink2 Value
|
|
var sink3 interface{}
|
|
|
|
// Baseline measures the time to store a string into a native variable.
|
|
b.Run("Baseline", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
sink1 = testdata[:len(testdata)%(i+1)]
|
|
}
|
|
})
|
|
|
|
// Inline measures the time to store a string into a Value,
|
|
// assuming that the compiler could inline the ValueOf function call.
|
|
b.Run("Inline", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
sink2 = valueOfString(testdata[:len(testdata)%(i+1)])
|
|
}
|
|
})
|
|
|
|
// Value measures the time to store a string into a Value using the general
|
|
// ValueOf function call. This should be identical to Inline.
|
|
//
|
|
// NOTE: As of Go1.11, this is not as efficient as Inline due to the lack
|
|
// of some compiler optimizations:
|
|
// https://golang.org/issue/22310
|
|
// https://golang.org/issue/25189
|
|
b.Run("Value", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
sink2 = ValueOf(string(testdata[:len(testdata)%(i+1)]))
|
|
}
|
|
})
|
|
|
|
// Interface measures the time to store a string into an interface.
|
|
b.Run("Interface", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
sink3 = string(testdata[:len(testdata)%(i+1)])
|
|
}
|
|
})
|
|
|
|
_, _, _ = sink1, sink2, sink3
|
|
}
|