1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// 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
}
|