blob: 40ac6a95fa8db25b86c2e5e1a0736a892f8a300e [file] [log] [blame]
// Copyright 2009 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 reflect_test
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"go/token"
"internal/goarch"
"io"
"math"
"math/rand"
"os"
. "reflect"
"reflect/internal/example1"
"reflect/internal/example2"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"unsafe"
)
var sink interface{}
func TestBool(t *testing.T) {
v := ValueOf(true)
if v.Bool() != true {
t.Fatal("ValueOf(true).Bool() = false")
}
}
type integer int
type T struct {
a int
b float64
c string
d *int
}
type pair struct {
i interface{}
s string
}
func assert(t *testing.T, s, want string) {
if s != want {
t.Errorf("have %#q want %#q", s, want)
}
}
var typeTests = []pair{
{struct{ x int }{}, "int"},
{struct{ x int8 }{}, "int8"},
{struct{ x int16 }{}, "int16"},
{struct{ x int32 }{}, "int32"},
{struct{ x int64 }{}, "int64"},
{struct{ x uint }{}, "uint"},
{struct{ x uint8 }{}, "uint8"},
{struct{ x uint16 }{}, "uint16"},
{struct{ x uint32 }{}, "uint32"},
{struct{ x uint64 }{}, "uint64"},
{struct{ x float32 }{}, "float32"},
{struct{ x float64 }{}, "float64"},
{struct{ x int8 }{}, "int8"},
{struct{ x (**int8) }{}, "**int8"},
{struct{ x (**integer) }{}, "**reflect_test.integer"},
{struct{ x ([32]int32) }{}, "[32]int32"},
{struct{ x ([]int8) }{}, "[]int8"},
{struct{ x (map[string]int32) }{}, "map[string]int32"},
{struct{ x (chan<- string) }{}, "chan<- string"},
{struct{ x (chan<- chan string) }{}, "chan<- chan string"},
{struct{ x (chan<- <-chan string) }{}, "chan<- <-chan string"},
{struct{ x (<-chan <-chan string) }{}, "<-chan <-chan string"},
{struct{ x (chan (<-chan string)) }{}, "chan (<-chan string)"},
{struct {
x struct {
c chan *int32
d float32
}
}{},
"struct { c chan *int32; d float32 }",
},
{struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"},
{struct {
x struct {
c func(chan *integer, *int8)
}
}{},
"struct { c func(chan *reflect_test.integer, *int8) }",
},
{struct {
x struct {
a int8
b int32
}
}{},
"struct { a int8; b int32 }",
},
{struct {
x struct {
a int8
b int8
c int32
}
}{},
"struct { a int8; b int8; c int32 }",
},
{struct {
x struct {
a int8
b int8
c int8
d int32
}
}{},
"struct { a int8; b int8; c int8; d int32 }",
},
{struct {
x struct {
a int8
b int8
c int8
d int8
e int32
}
}{},
"struct { a int8; b int8; c int8; d int8; e int32 }",
},
{struct {
x struct {
a int8
b int8
c int8
d int8
e int8
f int32
}
}{},
"struct { a int8; b int8; c int8; d int8; e int8; f int32 }",
},
{struct {
x struct {
a int8 `reflect:"hi there"`
}
}{},
`struct { a int8 "reflect:\"hi there\"" }`,
},
{struct {
x struct {
a int8 `reflect:"hi \x00there\t\n\"\\"`
}
}{},
`struct { a int8 "reflect:\"hi \\x00there\\t\\n\\\"\\\\\"" }`,
},
{struct {
x struct {
f func(args ...int)
}
}{},
"struct { f func(...int) }",
},
{struct {
x (interface {
a(func(func(int) int) func(func(int)) int)
b()
})
}{},
"interface { reflect_test.a(func(func(int) int) func(func(int)) int); reflect_test.b() }",
},
{struct {
x struct {
int32
int64
}
}{},
"struct { int32; int64 }",
},
}
var valueTests = []pair{
{new(int), "132"},
{new(int8), "8"},
{new(int16), "16"},
{new(int32), "32"},
{new(int64), "64"},
{new(uint), "132"},
{new(uint8), "8"},
{new(uint16), "16"},
{new(uint32), "32"},
{new(uint64), "64"},
{new(float32), "256.25"},
{new(float64), "512.125"},
{new(complex64), "532.125+10i"},
{new(complex128), "564.25+1i"},
{new(string), "stringy cheese"},
{new(bool), "true"},
{new(*int8), "*int8(0)"},
{new(**int8), "**int8(0)"},
{new([5]int32), "[5]int32{0, 0, 0, 0, 0}"},
{new(**integer), "**reflect_test.integer(0)"},
{new(map[string]int32), "map[string]int32{<can't iterate on maps>}"},
{new(chan<- string), "chan<- string"},
{new(func(a int8, b int32)), "func(int8, int32)(0)"},
{new(struct {
c chan *int32
d float32
}),
"struct { c chan *int32; d float32 }{chan *int32, 0}",
},
{new(struct{ c func(chan *integer, *int8) }),
"struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
},
{new(struct {
a int8
b int32
}),
"struct { a int8; b int32 }{0, 0}",
},
{new(struct {
a int8
b int8
c int32
}),
"struct { a int8; b int8; c int32 }{0, 0, 0}",
},
}
func testType(t *testing.T, i int, typ Type, want string) {
s := typ.String()
if s != want {
t.Errorf("#%d: have %#q, want %#q", i, s, want)
}
}
func TestTypes(t *testing.T) {
for i, tt := range typeTests {
testType(t, i, ValueOf(tt.i).Field(0).Type(), tt.s)
}
}
func TestSet(t *testing.T) {
for i, tt := range valueTests {
v := ValueOf(tt.i)
v = v.Elem()
switch v.Kind() {
case Int:
v.SetInt(132)
case Int8:
v.SetInt(8)
case Int16:
v.SetInt(16)
case Int32:
v.SetInt(32)
case Int64:
v.SetInt(64)
case Uint:
v.SetUint(132)
case Uint8:
v.SetUint(8)
case Uint16:
v.SetUint(16)
case Uint32:
v.SetUint(32)
case Uint64:
v.SetUint(64)
case Float32:
v.SetFloat(256.25)
case Float64:
v.SetFloat(512.125)
case Complex64:
v.SetComplex(532.125 + 10i)
case Complex128:
v.SetComplex(564.25 + 1i)
case String:
v.SetString("stringy cheese")
case Bool:
v.SetBool(true)
}
s := valueToString(v)
if s != tt.s {
t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
}
}
}
func TestSetValue(t *testing.T) {
for i, tt := range valueTests {
v := ValueOf(tt.i).Elem()
switch v.Kind() {
case Int:
v.Set(ValueOf(int(132)))
case Int8:
v.Set(ValueOf(int8(8)))
case Int16:
v.Set(ValueOf(int16(16)))
case Int32:
v.Set(ValueOf(int32(32)))
case Int64:
v.Set(ValueOf(int64(64)))
case Uint:
v.Set(ValueOf(uint(132)))
case Uint8:
v.Set(ValueOf(uint8(8)))
case Uint16:
v.Set(ValueOf(uint16(16)))
case Uint32:
v.Set(ValueOf(uint32(32)))
case Uint64:
v.Set(ValueOf(uint64(64)))
case Float32:
v.Set(ValueOf(float32(256.25)))
case Float64:
v.Set(ValueOf(512.125))
case Complex64:
v.Set(ValueOf(complex64(532.125 + 10i)))
case Complex128:
v.Set(ValueOf(complex128(564.25 + 1i)))
case String:
v.Set(ValueOf("stringy cheese"))
case Bool:
v.Set(ValueOf(true))
}
s := valueToString(v)
if s != tt.s {
t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
}
}
}
func TestMapIterSet(t *testing.T) {
m := make(map[string]interface{}, len(valueTests))
for _, tt := range valueTests {
m[tt.s] = tt.i
}
v := ValueOf(m)
k := New(v.Type().Key()).Elem()
e := New(v.Type().Elem()).Elem()
iter := v.MapRange()
for iter.Next() {
iter.SetKey(k)
iter.SetValue(e)
want := m[k.String()]
got := e.Interface()
if got != want {
t.Errorf("%q: want (%T) %v, got (%T) %v", k.String(), want, want, got, got)
}
if setkey, key := valueToString(k), valueToString(iter.Key()); setkey != key {
t.Errorf("MapIter.Key() = %q, MapIter.SetKey() = %q", key, setkey)
}
if setval, val := valueToString(e), valueToString(iter.Value()); setval != val {
t.Errorf("MapIter.Value() = %q, MapIter.SetValue() = %q", val, setval)
}
}
got := int(testing.AllocsPerRun(10, func() {
iter := v.MapRange()
for iter.Next() {
iter.SetKey(k)
iter.SetValue(e)
}
}))
// Making a *MapIter and making an hiter both allocate.
// Those should be the only two allocations.
if got != 2 {
t.Errorf("wanted 2 allocs, got %d", got)
}
}
func TestCanSetField(t *testing.T) {
type embed struct{ x, X int }
type Embed struct{ x, X int }
type S1 struct {
embed
x, X int
}
type S2 struct {
*embed
x, X int
}
type S3 struct {
Embed
x, X int
}
type S4 struct {
*Embed
x, X int
}
type testCase struct {
// -1 means Addr().Elem() of current value
index []int
canSet bool
}
tests := []struct {
val Value
cases []testCase
}{{
val: ValueOf(&S1{}),
cases: []testCase{
{[]int{0}, false},
{[]int{0, -1}, false},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{1, -1}, false},
{[]int{2}, true},
{[]int{2, -1}, true},
},
}, {
val: ValueOf(&S2{embed: &embed{}}),
cases: []testCase{
{[]int{0}, false},
{[]int{0, -1}, false},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{2}, true},
},
}, {
val: ValueOf(&S3{}),
cases: []testCase{
{[]int{0}, true},
{[]int{0, -1}, true},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{2}, true},
},
}, {
val: ValueOf(&S4{Embed: &Embed{}}),
cases: []testCase{
{[]int{0}, true},
{[]int{0, -1}, true},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{2}, true},
},
}}
for _, tt := range tests {
t.Run(tt.val.Type().Name(), func(t *testing.T) {
for _, tc := range tt.cases {
f := tt.val
for _, i := range tc.index {
if f.Kind() == Ptr {
f = f.Elem()
}
if i == -1 {
f = f.Addr().Elem()
} else {
f = f.Field(i)
}
}
if got := f.CanSet(); got != tc.canSet {
t.Errorf("CanSet() = %v, want %v", got, tc.canSet)
}
}
})
}
}
var _i = 7
var valueToStringTests = []pair{
{123, "123"},
{123.5, "123.5"},
{byte(123), "123"},
{"abc", "abc"},
{T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"},
{new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)"},
{[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
{&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "*[10]int(&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
{&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "*[]int(&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})"},
}
func TestValueToString(t *testing.T) {
for i, test := range valueToStringTests {
s := valueToString(ValueOf(test.i))
if s != test.s {
t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
}
}
}
func TestArrayElemSet(t *testing.T) {
v := ValueOf(&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Elem()
v.Index(4).SetInt(123)
s := valueToString(v)
const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
if s != want {
t.Errorf("[10]int: have %#q want %#q", s, want)
}
v = ValueOf([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
v.Index(4).SetInt(123)
s = valueToString(v)
const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
if s != want1 {
t.Errorf("[]int: have %#q want %#q", s, want1)
}
}
func TestPtrPointTo(t *testing.T) {
var ip *int32
var i int32 = 1234
vip := ValueOf(&ip)
vi := ValueOf(&i).Elem()
vip.Elem().Set(vi.Addr())
if *ip != 1234 {
t.Errorf("got %d, want 1234", *ip)
}
ip = nil
vp := ValueOf(&ip).Elem()
vp.Set(Zero(vp.Type()))
if ip != nil {
t.Errorf("got non-nil (%p), want nil", ip)
}
}
func TestPtrSetNil(t *testing.T) {
var i int32 = 1234
ip := &i
vip := ValueOf(&ip)
vip.Elem().Set(Zero(vip.Elem().Type()))
if ip != nil {
t.Errorf("got non-nil (%d), want nil", *ip)
}
}
func TestMapSetNil(t *testing.T) {
m := make(map[string]int)
vm := ValueOf(&m)
vm.Elem().Set(Zero(vm.Elem().Type()))
if m != nil {
t.Errorf("got non-nil (%p), want nil", m)
}
}
func TestAll(t *testing.T) {
testType(t, 1, TypeOf((int8)(0)), "int8")
testType(t, 2, TypeOf((*int8)(nil)).Elem(), "int8")
typ := TypeOf((*struct {
c chan *int32
d float32
})(nil))
testType(t, 3, typ, "*struct { c chan *int32; d float32 }")
etyp := typ.Elem()
testType(t, 4, etyp, "struct { c chan *int32; d float32 }")
styp := etyp
f := styp.Field(0)
testType(t, 5, f.Type, "chan *int32")
f, present := styp.FieldByName("d")
if !present {
t.Errorf("FieldByName says present field is absent")
}
testType(t, 6, f.Type, "float32")
f, present = styp.FieldByName("absent")
if present {
t.Errorf("FieldByName says absent field is present")
}
typ = TypeOf([32]int32{})
testType(t, 7, typ, "[32]int32")
testType(t, 8, typ.Elem(), "int32")
typ = TypeOf((map[string]*int32)(nil))
testType(t, 9, typ, "map[string]*int32")
mtyp := typ
testType(t, 10, mtyp.Key(), "string")
testType(t, 11, mtyp.Elem(), "*int32")
typ = TypeOf((chan<- string)(nil))
testType(t, 12, typ, "chan<- string")
testType(t, 13, typ.Elem(), "string")
// make sure tag strings are not part of element type
typ = TypeOf(struct {
d []uint32 `reflect:"TAG"`
}{}).Field(0).Type
testType(t, 14, typ, "[]uint32")
}
func TestInterfaceGet(t *testing.T) {
var inter struct {
E interface{}
}
inter.E = 123.456
v1 := ValueOf(&inter)
v2 := v1.Elem().Field(0)
assert(t, v2.Type().String(), "interface {}")
i2 := v2.Interface()
v3 := ValueOf(i2)
assert(t, v3.Type().String(), "float64")
}
func TestInterfaceValue(t *testing.T) {
var inter struct {
E interface{}
}
inter.E = 123.456
v1 := ValueOf(&inter)
v2 := v1.Elem().Field(0)
assert(t, v2.Type().String(), "interface {}")
v3 := v2.Elem()
assert(t, v3.Type().String(), "float64")
i3 := v2.Interface()
if _, ok := i3.(float64); !ok {
t.Error("v2.Interface() did not return float64, got ", TypeOf(i3))
}
}
func TestFunctionValue(t *testing.T) {
var x interface{} = func() {}
v := ValueOf(x)
if fmt.Sprint(v.Interface()) != fmt.Sprint(x) {
t.Fatalf("TestFunction returned wrong pointer")
}
assert(t, v.Type().String(), "func()")
}
var appendTests = []struct {
orig, extra []int
}{
{make([]int, 2, 4), []int{22}},
{make([]int, 2, 4), []int{22, 33, 44}},
}
func sameInts(x, y []int) bool {
if len(x) != len(y) {
return false
}
for i, xx := range x {
if xx != y[i] {
return false
}
}
return true
}
func TestAppend(t *testing.T) {
for i, test := range appendTests {
origLen, extraLen := len(test.orig), len(test.extra)
want := append(test.orig, test.extra...)
// Convert extra from []int to []Value.
e0 := make([]Value, len(test.extra))
for j, e := range test.extra {
e0[j] = ValueOf(e)
}
// Convert extra from []int to *SliceValue.
e1 := ValueOf(test.extra)
// Test Append.
a0 := ValueOf(test.orig)
have0 := Append(a0, e0...).Interface().([]int)
if !sameInts(have0, want) {
t.Errorf("Append #%d: have %v, want %v (%p %p)", i, have0, want, test.orig, have0)
}
// Check that the orig and extra slices were not modified.
if len(test.orig) != origLen {
t.Errorf("Append #%d origLen: have %v, want %v", i, len(test.orig), origLen)
}
if len(test.extra) != extraLen {
t.Errorf("Append #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
}
// Test AppendSlice.
a1 := ValueOf(test.orig)
have1 := AppendSlice(a1, e1).Interface().([]int)
if !sameInts(have1, want) {
t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want)
}
// Check that the orig and extra slices were not modified.
if len(test.orig) != origLen {
t.Errorf("AppendSlice #%d origLen: have %v, want %v", i, len(test.orig), origLen)
}
if len(test.extra) != extraLen {
t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
}
}
}
func TestCopy(t *testing.T) {
a := []int{1, 2, 3, 4, 10, 9, 8, 7}
b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
for i := 0; i < len(b); i++ {
if b[i] != c[i] {
t.Fatalf("b != c before test")
}
}
a1 := a
b1 := b
aa := ValueOf(&a1).Elem()
ab := ValueOf(&b1).Elem()
for tocopy := 1; tocopy <= 7; tocopy++ {
aa.SetLen(tocopy)
Copy(ab, aa)
aa.SetLen(8)
for i := 0; i < tocopy; i++ {
if a[i] != b[i] {
t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
tocopy, i, a[i], i, b[i])
}
}
for i := tocopy; i < len(b); i++ {
if b[i] != c[i] {
if i < len(a) {
t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
tocopy, i, a[i], i, b[i], i, c[i])
} else {
t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
tocopy, i, b[i], i, c[i])
}
} else {
t.Logf("tocopy=%d elem %d is okay\n", tocopy, i)
}
}
}
}
func TestCopyString(t *testing.T) {
t.Run("Slice", func(t *testing.T) {
s := bytes.Repeat([]byte{'_'}, 8)
val := ValueOf(s)
n := Copy(val, ValueOf(""))
if expecting := []byte("________"); n != 0 || !bytes.Equal(s, expecting) {
t.Errorf("got n = %d, s = %s, expecting n = 0, s = %s", n, s, expecting)
}
n = Copy(val, ValueOf("hello"))
if expecting := []byte("hello___"); n != 5 || !bytes.Equal(s, expecting) {
t.Errorf("got n = %d, s = %s, expecting n = 5, s = %s", n, s, expecting)
}
n = Copy(val, ValueOf("helloworld"))
if expecting := []byte("hellowor"); n != 8 || !bytes.Equal(s, expecting) {
t.Errorf("got n = %d, s = %s, expecting n = 8, s = %s", n, s, expecting)
}
})
t.Run("Array", func(t *testing.T) {
s := [...]byte{'_', '_', '_', '_', '_', '_', '_', '_'}
val := ValueOf(&s).Elem()
n := Copy(val, ValueOf(""))
if expecting := []byte("________"); n != 0 || !bytes.Equal(s[:], expecting) {
t.Errorf("got n = %d, s = %s, expecting n = 0, s = %s", n, s[:], expecting)
}
n = Copy(val, ValueOf("hello"))
if expecting := []byte("hello___"); n != 5 || !bytes.Equal(s[:], expecting) {
t.Errorf("got n = %d, s = %s, expecting n = 5, s = %s", n, s[:], expecting)
}
n = Copy(val, ValueOf("helloworld"))
if expecting := []byte("hellowor"); n != 8 || !bytes.Equal(s[:], expecting) {
t.Errorf("got n = %d, s = %s, expecting n = 8, s = %s", n, s[:], expecting)
}
})
}
func TestCopyArray(t *testing.T) {
a := [8]int{1, 2, 3, 4, 10, 9, 8, 7}
b := [11]int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
c := b
aa := ValueOf(&a).Elem()
ab := ValueOf(&b).Elem()
Copy(ab, aa)
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
t.Errorf("(i) a[%d]=%d, b[%d]=%d", i, a[i], i, b[i])
}
}
for i := len(a); i < len(b); i++ {
if b[i] != c[i] {
t.Errorf("(ii) b[%d]=%d, c[%d]=%d", i, b[i], i, c[i])
} else {
t.Logf("elem %d is okay\n", i)
}
}
}
func TestBigUnnamedStruct(t *testing.T) {
b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
v := ValueOf(b)
b1 := v.Interface().(struct {
a, b, c, d int64
})
if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
t.Errorf("ValueOf(%v).Interface().(*Big) = %v", b, b1)
}
}
type big struct {
a, b, c, d, e int64
}
func TestBigStruct(t *testing.T) {
b := big{1, 2, 3, 4, 5}
v := ValueOf(b)
b1 := v.Interface().(big)
if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
t.Errorf("ValueOf(%v).Interface().(big) = %v", b, b1)
}
}
type Basic struct {
x int
y float32
}
type NotBasic Basic
type DeepEqualTest struct {
a, b interface{}
eq bool
}
// Simple functions for DeepEqual tests.
var (
fn1 func() // nil.
fn2 func() // nil.
fn3 = func() { fn1() } // Not nil.
)
type self struct{}
type Loop *Loop
type Loopy interface{}
var loop1, loop2 Loop
var loopy1, loopy2 Loopy
var cycleMap1, cycleMap2, cycleMap3 map[string]interface{}
type structWithSelfPtr struct {
p *structWithSelfPtr
s string
}
func init() {
loop1 = &loop2
loop2 = &loop1
loopy1 = &loopy2
loopy2 = &loopy1
cycleMap1 = map[string]interface{}{}
cycleMap1["cycle"] = cycleMap1
cycleMap2 = map[string]interface{}{}
cycleMap2["cycle"] = cycleMap2
cycleMap3 = map[string]interface{}{}
cycleMap3["different"] = cycleMap3
}
var deepEqualTests = []DeepEqualTest{
// Equalities
{nil, nil, true},
{1, 1, true},
{int32(1), int32(1), true},
{0.5, 0.5, true},
{float32(0.5), float32(0.5), true},
{"hello", "hello", true},
{make([]int, 10), make([]int, 10), true},
{&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true},
{Basic{1, 0.5}, Basic{1, 0.5}, true},
{error(nil), error(nil), true},
{map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true},
{fn1, fn2, true},
// Inequalities
{1, 2, false},
{int32(1), int32(2), false},
{0.5, 0.6, false},
{float32(0.5), float32(0.6), false},
{"hello", "hey", false},
{make([]int, 10), make([]int, 11), false},
{&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false},
{Basic{1, 0.5}, Basic{1, 0.6}, false},
{Basic{1, 0}, Basic{2, 0}, false},
{map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false},
{map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false},
{map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false},
{map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false},
{nil, 1, false},
{1, nil, false},
{fn1, fn3, false},
{fn3, fn3, false},
{[][]int{{1}}, [][]int{{2}}, false},
{math.NaN(), math.NaN(), false},
{&[1]float64{math.NaN()}, &[1]float64{math.NaN()}, false},
{&[1]float64{math.NaN()}, self{}, true},
{[]float64{math.NaN()}, []float64{math.NaN()}, false},
{[]float64{math.NaN()}, self{}, true},
{map[float64]float64{math.NaN(): 1}, map[float64]float64{1: 2}, false},
{map[float64]float64{math.NaN(): 1}, self{}, true},
{&structWithSelfPtr{p: &structWithSelfPtr{s: "a"}}, &structWithSelfPtr{p: &structWithSelfPtr{s: "b"}}, false},
// Nil vs empty: not the same.
{[]int{}, []int(nil), false},
{[]int{}, []int{}, true},
{[]int(nil), []int(nil), true},
{map[int]int{}, map[int]int(nil), false},
{map[int]int{}, map[int]int{}, true},
{map[int]int(nil), map[int]int(nil), true},
// Mismatched types
{1, 1.0, false},
{int32(1), int64(1), false},
{0.5, "hello", false},
{[]int{1, 2, 3}, [3]int{1, 2, 3}, false},
{&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false},
{Basic{1, 0.5}, NotBasic{1, 0.5}, false},
{map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, false},
// Possible loops.
{&loop1, &loop1, true},
{&loop1, &loop2, true},
{&loopy1, &loopy1, true},
{&loopy1, &loopy2, true},
{&cycleMap1, &cycleMap2, true},
{&cycleMap1, &cycleMap3, false},
}
func TestDeepEqual(t *testing.T) {
for _, test := range deepEqualTests {
if test.b == (self{}) {
test.b = test.a
}
if r := DeepEqual(test.a, test.b); r != test.eq {
t.Errorf("DeepEqual(%#v, %#v) = %v, want %v", test.a, test.b, r, test.eq)
}
}
}
func TestTypeOf(t *testing.T) {
// Special case for nil
if typ := TypeOf(nil); typ != nil {
t.Errorf("expected nil type for nil value; got %v", typ)
}
for _, test := range deepEqualTests {
v := ValueOf(test.a)
if !v.IsValid() {
continue
}
typ := TypeOf(test.a)
if typ != v.Type() {
t.Errorf("TypeOf(%v) = %v, but ValueOf(%v).Type() = %v", test.a, typ, test.a, v.Type())
}
}
}
type Recursive struct {
x int
r *Recursive
}
func TestDeepEqualRecursiveStruct(t *testing.T) {
a, b := new(Recursive), new(Recursive)
*a = Recursive{12, a}
*b = Recursive{12, b}
if !DeepEqual(a, b) {
t.Error("DeepEqual(recursive same) = false, want true")
}
}
type _Complex struct {
a int
b [3]*_Complex
c *string
d map[float64]float64
}
func TestDeepEqualComplexStruct(t *testing.T) {
m := make(map[float64]float64)
stra, strb := "hello", "hello"
a, b := new(_Complex), new(_Complex)
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
*b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
if !DeepEqual(a, b) {
t.Error("DeepEqual(complex same) = false, want true")
}
}
func TestDeepEqualComplexStructInequality(t *testing.T) {
m := make(map[float64]float64)
stra, strb := "hello", "helloo" // Difference is here
a, b := new(_Complex), new(_Complex)
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
*b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
if DeepEqual(a, b) {
t.Error("DeepEqual(complex different) = true, want false")
}
}
type UnexpT struct {
m map[int]int
}
func TestDeepEqualUnexportedMap(t *testing.T) {
// Check that DeepEqual can look at unexported fields.
x1 := UnexpT{map[int]int{1: 2}}
x2 := UnexpT{map[int]int{1: 2}}
if !DeepEqual(&x1, &x2) {
t.Error("DeepEqual(x1, x2) = false, want true")
}
y1 := UnexpT{map[int]int{2: 3}}
if DeepEqual(&x1, &y1) {
t.Error("DeepEqual(x1, y1) = true, want false")
}
}
func check2ndField(x interface{}, offs uintptr, t *testing.T) {
s := ValueOf(x)
f := s.Type().Field(1)
if f.Offset != offs {
t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
}
}
// Check that structure alignment & offsets viewed through reflect agree with those
// from the compiler itself.
func TestAlignment(t *testing.T) {
type T1inner struct {
a int
}
type T1 struct {
T1inner
f int
}
type T2inner struct {
a, b int
}
type T2 struct {
T2inner
f int
}
x := T1{T1inner{2}, 17}
check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t)
x1 := T2{T2inner{2, 3}, 17}
check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t)
}
func Nil(a interface{}, t *testing.T) {
n := ValueOf(a).Field(0)
if !n.IsNil() {
t.Errorf("%v should be nil", a)
}
}
func NotNil(a interface{}, t *testing.T) {
n := ValueOf(a).Field(0)
if n.IsNil() {
t.Errorf("value of type %v should not be nil", ValueOf(a).Type().String())
}
}
func TestIsNil(t *testing.T) {
// These implement IsNil.
// Wrap in extra struct to hide interface type.
doNil := []interface{}{
struct{ x *int }{},
struct{ x interface{} }{},
struct{ x map[string]int }{},
struct{ x func() bool }{},
struct{ x chan int }{},
struct{ x []string }{},
struct{ x unsafe.Pointer }{},
}
for _, ts := range doNil {
ty := TypeOf(ts).Field(0).Type
v := Zero(ty)
v.IsNil() // panics if not okay to call
}
// Check the implementations
var pi struct {
x *int
}
Nil(pi, t)
pi.x = new(int)
NotNil(pi, t)
var si struct {
x []int
}
Nil(si, t)
si.x = make([]int, 10)
NotNil(si, t)
var ci struct {
x chan int
}
Nil(ci, t)
ci.x = make(chan int)
NotNil(ci, t)
var mi struct {
x map[int]int
}
Nil(mi, t)
mi.x = make(map[int]int)
NotNil(mi, t)
var ii struct {
x interface{}
}
Nil(ii, t)
ii.x = 2
NotNil(ii, t)
var fi struct {
x func(t *testing.T)
}
Nil(fi, t)
fi.x = TestIsNil
NotNil(fi, t)
}
func TestIsZero(t *testing.T) {
for i, tt := range []struct {
x interface{}
want bool
}{
// Booleans
{true, false},
{false, true},
// Numeric types
{int(0), true},
{int(1), false},
{int8(0), true},
{int8(1), false},
{int16(0), true},
{int16(1), false},
{int32(0), true},
{int32(1), false},
{int64(0), true},
{int64(1), false},
{uint(0), true},
{uint(1), false},
{uint8(0), true},
{uint8(1), false},
{uint16(0), true},
{uint16(1), false},
{uint32(0), true},
{uint32(1), false},
{uint64(0), true},
{uint64(1), false},
{float32(0), true},
{float32(1.2), false},
{float64(0), true},
{float64(1.2), false},
{math.Copysign(0, -1), false},
{complex64(0), true},
{complex64(1.2), false},
{complex128(0), true},
{complex128(1.2), false},
{complex(math.Copysign(0, -1), 0), false},
{complex(0, math.Copysign(0, -1)), false},
{complex(math.Copysign(0, -1), math.Copysign(0, -1)), false},
{uintptr(0), true},
{uintptr(128), false},
// Array
{Zero(TypeOf([5]string{})).Interface(), true},
{[5]string{"", "", "", "", ""}, true},
{[5]string{}, true},
{[5]string{"", "", "", "a", ""}, false},
// Chan
{(chan string)(nil), true},
{make(chan string), false},
{time.After(1), false},
// Func
{(func())(nil), true},
{New, false},
// Interface
{New(TypeOf(new(error)).Elem()).Elem(), true},
{(io.Reader)(strings.NewReader("")), false},
// Map
{(map[string]string)(nil), true},
{map[string]string{}, false},
{make(map[string]string), false},
// Ptr
{(*func())(nil), true},
{(*int)(nil), true},
{new(int), false},
// Slice
{[]string{}, false},
{([]string)(nil), true},
{make([]string, 0), false},
// Strings
{"", true},
{"not-zero", false},
// Structs
{T{}, true},
{T{123, 456.75, "hello", &_i}, false},
// UnsafePointer
{(unsafe.Pointer)(nil), true},
{(unsafe.Pointer)(new(int)), false},
} {
var x Value
if v, ok := tt.x.(Value); ok {
x = v
} else {
x = ValueOf(tt.x)
}
b := x.IsZero()
if b != tt.want {
t.Errorf("%d: IsZero((%s)(%+v)) = %t, want %t", i, x.Kind(), tt.x, b, tt.want)
}
if !Zero(TypeOf(tt.x)).IsZero() {
t.Errorf("%d: IsZero(Zero(TypeOf((%s)(%+v)))) is false", i, x.Kind(), tt.x)
}
}
func() {
defer func() {
if r := recover(); r == nil {
t.Error("should panic for invalid value")
}
}()
(Value{}).IsZero()
}()
}
func TestInterfaceExtraction(t *testing.T) {
var s struct {
W io.Writer
}
s.W = os.Stdout
v := Indirect(ValueOf(&s)).Field(0).Interface()
if v != s.W.(interface{}) {
t.Error("Interface() on interface: ", v, s.W)
}
}
func TestNilPtrValueSub(t *testing.T) {
var pi *int
if pv := ValueOf(pi); pv.Elem().IsValid() {
t.Error("ValueOf((*int)(nil)).Elem().IsValid()")
}
}
func TestMap(t *testing.T) {
m := map[string]int{"a": 1, "b": 2}
mv := ValueOf(m)
if n := mv.Len(); n != len(m) {
t.Errorf("Len = %d, want %d", n, len(m))
}
keys := mv.MapKeys()
newmap := MakeMap(mv.Type())
for k, v := range m {
// Check that returned Keys match keys in range.
// These aren't required to be in the same order.
seen := false
for _, kv := range keys {
if kv.String() == k {
seen = true
break
}
}
if !seen {
t.Errorf("Missing key %q", k)
}
// Check that value lookup is correct.
vv := mv.MapIndex(ValueOf(k))
if vi := vv.Int(); vi != int64(v) {
t.Errorf("Key %q: have value %d, want %d", k, vi, v)
}
// Copy into new map.
newmap.SetMapIndex(ValueOf(k), ValueOf(v))
}
vv := mv.MapIndex(ValueOf("not-present"))
if vv.IsValid() {
t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
}
newm := newmap.Interface().(map[string]int)
if len(newm) != len(m) {
t.Errorf("length after copy: newm=%d, m=%d", len(newm), len(m))
}
for k, v := range newm {
mv, ok := m[k]
if mv != v {
t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok)
}
}
newmap.SetMapIndex(ValueOf("a"), Value{})
v, ok := newm["a"]
if ok {
t.Errorf("newm[\"a\"] = %d after delete", v)
}
mv = ValueOf(&m).Elem()
mv.Set(Zero(mv.Type()))
if m != nil {
t.Errorf("mv.Set(nil) failed")
}
}
func TestNilMap(t *testing.T) {
var m map[string]int
mv := ValueOf(m)
keys := mv.MapKeys()
if len(keys) != 0 {
t.Errorf(">0 keys for nil map: %v", keys)
}
// Check that value for missing key is zero.
x := mv.MapIndex(ValueOf("hello"))
if x.Kind() != Invalid {
t.Errorf("m.MapIndex(\"hello\") for nil map = %v, want Invalid Value", x)
}
// Check big value too.
var mbig map[string][10 << 20]byte
x = ValueOf(mbig).MapIndex(ValueOf("hello"))
if x.Kind() != Invalid {
t.Errorf("mbig.MapIndex(\"hello\") for nil map = %v, want Invalid Value", x)
}
// Test that deletes from a nil map succeed.
mv.SetMapIndex(ValueOf("hi"), Value{})
}
func TestChan(t *testing.T) {
for loop := 0; loop < 2; loop++ {
var c chan int
var cv Value
// check both ways to allocate channels
switch loop {
case 1:
c = make(chan int, 1)
cv = ValueOf(c)
case 0:
cv = MakeChan(TypeOf(c), 1)
c = cv.Interface().(chan int)
}
// Send
cv.Send(ValueOf(2))
if i := <-c; i != 2 {
t.Errorf("reflect Send 2, native recv %d", i)
}
// Recv
c <- 3
if i, ok := cv.Recv(); i.Int() != 3 || !ok {
t.Errorf("native send 3, reflect Recv %d, %t", i.Int(), ok)
}
// TryRecv fail
val, ok := cv.TryRecv()
if val.IsValid() || ok {
t.Errorf("TryRecv on empty chan: %s, %t", valueToString(val), ok)
}
// TryRecv success
c <- 4
val, ok = cv.TryRecv()
if !val.IsValid() {
t.Errorf("TryRecv on ready chan got nil")
} else if i := val.Int(); i != 4 || !ok {
t.Errorf("native send 4, TryRecv %d, %t", i, ok)
}
// TrySend fail
c <- 100
ok = cv.TrySend(ValueOf(5))
i := <-c
if ok {
t.Errorf("TrySend on full chan succeeded: value %d", i)
}
// TrySend success
ok = cv.TrySend(ValueOf(6))
if !ok {
t.Errorf("TrySend on empty chan failed")
select {
case x := <-c:
t.Errorf("TrySend failed but it did send %d", x)
default:
}
} else {
if i = <-c; i != 6 {
t.Errorf("TrySend 6, recv %d", i)
}
}
// Close
c <- 123
cv.Close()
if i, ok := cv.Recv(); i.Int() != 123 || !ok {
t.Errorf("send 123 then close; Recv %d, %t", i.Int(), ok)
}
if i, ok := cv.Recv(); i.Int() != 0 || ok {
t.Errorf("after close Recv %d, %t", i.Int(), ok)
}
}
// check creation of unbuffered channel
var c chan int
cv := MakeChan(TypeOf(c), 0)
c = cv.Interface().(chan int)
if cv.TrySend(ValueOf(7)) {
t.Errorf("TrySend on sync chan succeeded")
}
if v, ok := cv.TryRecv(); v.IsValid() || ok {
t.Errorf("TryRecv on sync chan succeeded: isvalid=%v ok=%v", v.IsValid(), ok)
}
// len/cap
cv = MakeChan(TypeOf(c), 10)
c = cv.Interface().(chan int)
for i := 0; i < 3; i++ {
c <- i
}
if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
}
}
// caseInfo describes a single case in a select test.
type caseInfo struct {
desc string
canSelect bool
recv Value
closed bool
helper func()
panic bool
}
var allselect = flag.Bool("allselect", false, "exhaustive select test")
func TestSelect(t *testing.T) {
selectWatch.once.Do(func() { go selectWatcher() })
var x exhaustive
nch := 0
newop := func(n int, cap int) (ch, val Value) {
nch++
if nch%101%2 == 1 {
c := make(chan int, cap)
ch = ValueOf(c)
val = ValueOf(n)
} else {
c := make(chan string, cap)
ch = ValueOf(c)
val = ValueOf(fmt.Sprint(n))
}
return
}
for n := 0; x.Next(); n++ {
if testing.Short() && n >= 1000 {
break
}
if n >= 100000 && !*allselect {
break
}
if n%100000 == 0 && testing.Verbose() {
println("TestSelect", n)
}
var cases []SelectCase
var info []caseInfo
// Ready send.
if x.Maybe() {
ch, val := newop(len(cases), 1)
cases = append(cases, SelectCase{
Dir: SelectSend,
Chan: ch,
Send: val,
})
info = append(info, caseInfo{desc: "ready send", canSelect: true})
}
// Ready recv.
if x.Maybe() {
ch, val := newop(len(cases), 1)
ch.Send(val)
cases = append(cases, SelectCase{
Dir: SelectRecv,
Chan: ch,
})
info = append(info, caseInfo{desc: "ready recv", canSelect: true, recv: val})
}
// Blocking send.
if x.Maybe() {
ch, val := newop(len(cases), 0)
cases = append(cases, SelectCase{
Dir: SelectSend,
Chan: ch,
Send: val,
})
// Let it execute?
if x.Maybe() {
f := func() { ch.Recv() }
info = append(info, caseInfo{desc: "blocking send", helper: f})
} else {
info = append(info, caseInfo{desc: "blocking send"})
}
}
// Blocking recv.
if x.Maybe() {
ch, val := newop(len(cases), 0)
cases = append(cases, SelectCase{
Dir: SelectRecv,
Chan: ch,
})
// Let it execute?
if x.Maybe() {
f := func() { ch.Send(val) }
info = append(info, caseInfo{desc: "blocking recv", recv: val, helper: f})
} else {
info = append(info, caseInfo{desc: "blocking recv"})
}
}
// Zero Chan send.
if x.Maybe() {
// Maybe include value to send.
var val Value
if x.Maybe() {
val = ValueOf(100)
}
cases = append(cases, SelectCase{
Dir: SelectSend,
Send: val,
})
info = append(info, caseInfo{desc: "zero Chan send"})
}
// Zero Chan receive.
if x.Maybe() {
cases = append(cases, SelectCase{
Dir: SelectRecv,
})
info = append(info, caseInfo{desc: "zero Chan recv"})
}
// nil Chan send.
if x.Maybe() {
cases = append(cases, SelectCase{
Dir: SelectSend,
Chan: ValueOf((chan int)(nil)),
Send: ValueOf(101),
})
info = append(info, caseInfo{desc: "nil Chan send"})
}
// nil Chan recv.
if x.Maybe() {
cases = append(cases, SelectCase{
Dir: SelectRecv,
Chan: ValueOf((chan int)(nil)),
})
info = append(info, caseInfo{desc: "nil Chan recv"})
}
// closed Chan send.
if x.Maybe() {
ch := make(chan int)
close(ch)
cases = append(cases, SelectCase{
Dir: SelectSend,
Chan: ValueOf(ch),
Send: ValueOf(101),
})
info = append(info, caseInfo{desc: "closed Chan send", canSelect: true, panic: true})
}
// closed Chan recv.
if x.Maybe() {
ch, val := newop(len(cases), 0)
ch.Close()
val = Zero(val.Type())
cases = append(cases, SelectCase{
Dir: SelectRecv,
Chan: ch,
})
info = append(info, caseInfo{desc: "closed Chan recv", canSelect: true, closed: true, recv: val})
}
var helper func() // goroutine to help the select complete
// Add default? Must be last case here, but will permute.
// Add the default if the select would otherwise
// block forever, and maybe add it anyway.
numCanSelect := 0
canProceed := false
canBlock := true
canPanic := false
helpers := []int{}
for i, c := range info {
if c.canSelect {
canProceed = true
canBlock = false
numCanSelect++
if c.panic {
canPanic = true
}
} else if c.helper != nil {
canProceed = true
helpers = append(helpers, i)
}
}
if !canProceed || x.Maybe() {
cases = append(cases, SelectCase{
Dir: SelectDefault,
})
info = append(info, caseInfo{desc: "default", canSelect: canBlock})
numCanSelect++
} else if canBlock {
// Select needs to communicate with another goroutine.
cas := &info[helpers[x.Choose(len(helpers))]]
helper = cas.helper
cas.canSelect = true
numCanSelect++
}
// Permute cases and case info.
// Doing too much here makes the exhaustive loop
// too exhausting, so just do two swaps.
for loop := 0; loop < 2; loop++ {
i := x.Choose(len(cases))
j := x.Choose(len(cases))
cases[i], cases[j] = cases[j], cases[i]
info[i], info[j] = info[j], info[i]
}
if helper != nil {
// We wait before kicking off a goroutine to satisfy a blocked select.
// The pause needs to be big enough to let the select block before
// we run the helper, but if we lose that race once in a while it's okay: the
// select will just proceed immediately. Not a big deal.
// For short tests we can grow [sic] the timeout a bit without fear of taking too long
pause := 10 * time.Microsecond
if testing.Short() {
pause = 100 * time.Microsecond
}
time.AfterFunc(pause, helper)
}
// Run select.
i, recv, recvOK, panicErr := runSelect(cases, info)
if panicErr != nil && !canPanic {
t.Fatalf("%s\npanicked unexpectedly: %v", fmtSelect(info), panicErr)
}
if panicErr == nil && canPanic && numCanSelect == 1 {
t.Fatalf("%s\nselected #%d incorrectly (should panic)", fmtSelect(info), i)
}
if panicErr != nil {
continue
}
cas := info[i]
if !cas.canSelect {
recvStr := ""
if recv.IsValid() {
recvStr = fmt.Sprintf(", received %v, %v", recv.Interface(), recvOK)
}
t.Fatalf("%s\nselected #%d incorrectly%s", fmtSelect(info), i, recvStr)
continue
}
if cas.panic {
t.Fatalf("%s\nselected #%d incorrectly (case should panic)", fmtSelect(info), i)
continue
}
if cases[i].Dir == SelectRecv {
if !recv.IsValid() {
t.Fatalf("%s\nselected #%d but got %v, %v, want %v, %v", fmtSelect(info), i, recv, recvOK, cas.recv.Interface(), !cas.closed)
}
if !cas.recv.IsValid() {
t.Fatalf("%s\nselected #%d but internal error: missing recv value", fmtSelect(info), i)
}
if recv.Interface() != cas.recv.Interface() || recvOK != !cas.closed {
if recv.Interface() == cas.recv.Interface() && recvOK == !cas.closed {
t.Fatalf("%s\nselected #%d, got %#v, %v, and DeepEqual is broken on %T", fmtSelect(info), i, recv.Interface(), recvOK, recv.Interface())
}
t.Fatalf("%s\nselected #%d but got %#v, %v, want %#v, %v", fmtSelect(info), i, recv.Interface(), recvOK, cas.recv.Interface(), !cas.closed)
}
} else {
if recv.IsValid() || recvOK {
t.Fatalf("%s\nselected #%d but got %v, %v, want %v, %v", fmtSelect(info), i, recv, recvOK, Value{}, false)
}
}
}
}
func TestSelectMaxCases(t *testing.T) {
var sCases []SelectCase
channel := make(chan int)
close(channel)
for i := 0; i < 65536; i++ {
sCases = append(sCases, SelectCase{
Dir: SelectRecv,
Chan: ValueOf(channel),
})
}
// Should not panic
_, _, _ = Select(sCases)
sCases = append(sCases, SelectCase{
Dir: SelectRecv,
Chan: ValueOf(channel),
})
defer func() {
if err := recover(); err != nil {
if err.(string) != "reflect.Select: too many cases (max 65536)" {
t.Fatalf("unexpected error from select call with greater than max supported cases")
}
} else {
t.Fatalf("expected select call to panic with greater than max supported cases")
}
}()
// Should panic
_, _, _ = Select(sCases)
}
func TestSelectNop(t *testing.T) {
// "select { default: }" should always return the default case.
chosen, _, _ := Select([]SelectCase{{Dir: SelectDefault}})
if chosen != 0 {
t.Fatalf("expected Select to return 0, but got %#v", chosen)
}
}
func BenchmarkSelect(b *testing.B) {
channel := make(chan int)
close(channel)
var cases []SelectCase
for i := 0; i < 8; i++ {
cases = append(cases, SelectCase{
Dir: SelectRecv,
Chan: ValueOf(channel),
})
}
for _, numCases := range []int{1, 4, 8} {
b.Run(strconv.Itoa(numCases), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _, _ = Select(cases[:numCases])
}
})
}
}
// selectWatch and the selectWatcher are a watchdog mechanism for running Select.
// If the selectWatcher notices that the select has been blocked for >1 second, it prints
// an error describing the select and panics the entire test binary.
var selectWatch struct {
sync.Mutex
once sync.Once
now time.Time
info []caseInfo
}
func selectWatcher() {
for {
time.Sleep(1 * time.Second)
selectWatch.Lock()
if selectWatch.info != nil && time.Since(selectWatch.now) > 10*time.Second {
fmt.Fprintf(os.Stderr, "TestSelect:\n%s blocked indefinitely\n", fmtSelect(selectWatch.info))
panic("select stuck")
}
selectWatch.Unlock()
}
}
// runSelect runs a single select test.
// It returns the values returned by Select but also returns
// a panic value if the Select panics.
func runSelect(cases []SelectCase, info []caseInfo) (chosen int, recv Value, recvOK bool, panicErr interface{}) {
defer func() {
panicErr = recover()
selectWatch.Lock()
selectWatch.info = nil
selectWatch.Unlock()
}()
selectWatch.Lock()
selectWatch.now = time.Now()
selectWatch.info = info
selectWatch.Unlock()
chosen, recv, recvOK = Select(cases)
return
}
// fmtSelect formats the information about a single select test.
func fmtSelect(info []caseInfo) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "\nselect {\n")
for i, cas := range info {
fmt.Fprintf(&buf, "%d: %s", i, cas.desc)
if cas.recv.IsValid() {
fmt.Fprintf(&buf, " val=%#v", cas.recv.Interface())
}
if cas.canSelect {
fmt.Fprintf(&buf, " canselect")
}
if cas.panic {
fmt.Fprintf(&buf, " panic")
}
fmt.Fprintf(&buf, "\n")
}
fmt.Fprintf(&buf, "}")
return buf.String()
}
type two [2]uintptr
// Difficult test for function call because of
// implicit padding between arguments.
func dummy(b byte, c int, d byte, e two, f byte, g float32, h byte) (i byte, j int, k byte, l two, m byte, n float32, o byte) {
return b, c, d, e, f, g, h
}
func TestFunc(t *testing.T) {
ret := ValueOf(dummy).Call([]Value{
ValueOf(byte(10)),
ValueOf(20),
ValueOf(byte(30)),
ValueOf(two{40, 50}),
ValueOf(byte(60)),
ValueOf(float32(70)),
ValueOf(byte(80)),
})
if len(ret) != 7 {
t.Fatalf("Call returned %d values, want 7", len(ret))
}
i := byte(ret[0].Uint())
j := int(ret[1].Int())
k := byte(ret[2].Uint())
l := ret[3].Interface().(two)
m := byte(ret[4].Uint())
n := float32(ret[5].Float())
o := byte(ret[6].Uint())
if i != 10 || j != 20 || k != 30 || l != (two{40, 50}) || m != 60 || n != 70 || o != 80 {
t.Errorf("Call returned %d, %d, %d, %v, %d, %g, %d; want 10, 20, 30, [40, 50], 60, 70, 80", i, j, k, l, m, n, o)
}
for i, v := range ret {
if v.CanAddr() {
t.Errorf("result %d is addressable", i)
}
}
}
func TestCallConvert(t *testing.T) {
v := ValueOf(new(io.ReadWriter)).Elem()
f := ValueOf(func(r io.Reader) io.Reader { return r })
out := f.Call([]Value{v})
if len(out) != 1 || out[0].Type() != TypeOf(new(io.Reader)).Elem() || !out[0].IsNil() {
t.Errorf("expected [nil], got %v", out)
}
}
type emptyStruct struct{}
type nonEmptyStruct struct {
member int
}
func returnEmpty() emptyStruct {
return emptyStruct{}
}
func takesEmpty(e emptyStruct) {
}
func returnNonEmpty(i int) nonEmptyStruct {
return nonEmptyStruct{member: i}
}
func takesNonEmpty(n nonEmptyStruct) int {
return n.member
}
func TestCallWithStruct(t *testing.T) {
r := ValueOf(returnEmpty).Call(nil)
if len(r) != 1 || r[0].Type() != TypeOf(emptyStruct{}) {
t.Errorf("returning empty struct returned %#v instead", r)
}
r = ValueOf(takesEmpty).Call([]Value{ValueOf(emptyStruct{})})
if len(r) != 0 {
t.Errorf("takesEmpty returned values: %#v", r)
}
r = ValueOf(returnNonEmpty).Call([]Value{ValueOf(42)})
if len(r) != 1 || r[0].Type() != TypeOf(nonEmptyStruct{}) || r[0].Field(0).Int() != 42 {
t.Errorf("returnNonEmpty returned %#v", r)
}
r = ValueOf(takesNonEmpty).Call([]Value{ValueOf(nonEmptyStruct{member: 42})})
if len(r) != 1 || r[0].Type() != TypeOf(1) || r[0].Int() != 42 {
t.Errorf("takesNonEmpty returned %#v", r)
}
}
func TestCallReturnsEmpty(t *testing.T) {
// Issue 21717: past-the-end pointer write in Call with
// nonzero-sized frame and zero-sized return value.
runtime.GC()
var finalized uint32
f := func() (emptyStruct, *[2]int64) {
i := new([2]int64) // big enough to not be tinyalloc'd, so finalizer always runs when i dies
runtime.SetFinalizer(i, func(*[2]int64) { atomic.StoreUint32(&finalized, 1) })
return emptyStruct{}, i
}
v := ValueOf(f).Call(nil)[0] // out[0] should not alias out[1]'s memory, so the finalizer should run.
timeout := time.After(5 * time.Second)
for atomic.LoadUint32(&finalized) == 0 {
select {
case <-timeout:
t.Fatal("finalizer did not run")
default:
}
runtime.Gosched()
runtime.GC()
}
runtime.KeepAlive(v)
}
func BenchmarkCall(b *testing.B) {
fv := ValueOf(func(a, b string) {})
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
args := []Value{ValueOf("a"), ValueOf("b")}
for pb.Next() {
fv.Call(args)
}
})
}
type myint int64
func (i *myint) inc() {
*i = *i + 1
}
func BenchmarkCallMethod(b *testing.B) {
b.ReportAllocs()
z := new(myint)
v := ValueOf(z.inc)
for i := 0; i < b.N; i++ {
v.Call(nil)
}
}
func BenchmarkCallArgCopy(b *testing.B) {
byteArray := func(n int) Value {
return Zero(ArrayOf(n, TypeOf(byte(0))))
}
sizes := [...]struct {
fv Value
arg Value
}{
{ValueOf(func(a [128]byte) {}), byteArray(128)},
{ValueOf(func(a [256]byte) {}), byteArray(256)},
{ValueOf(func(a [1024]byte) {}), byteArray(1024)},
{ValueOf(func(a [4096]byte) {}), byteArray(4096)},
{ValueOf(func(a [65536]byte) {}), byteArray(65536)},
}
for _, size := range sizes {
bench := func(b *testing.B) {
args := []Value{size.arg}
b.SetBytes(int64(size.arg.Len()))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
size.fv.Call(args)
}
})
}
name := fmt.Sprintf("size=%v", size.arg.Len())
b.Run(name, bench)
}
}
func TestMakeFunc(t *testing.T) {
f := dummy
fv := MakeFunc(TypeOf(f), func(in []Value) []Value { return in })
ValueOf(&f).Elem().Set(fv)
// Call g with small arguments so that there is
// something predictable (and different from the
// correct results) in those positions on the stack.
g := dummy
g(1, 2, 3, two{4, 5}, 6, 7, 8)
// Call constructed function f.
i, j, k, l, m, n, o := f(10, 20, 30, two{40, 50}, 60, 70, 80)
if i != 10 || j != 20 || k != 30 || l != (two{40, 50}) || m != 60 || n != 70 || o != 80 {
t.Errorf("Call returned %d, %d, %d, %v, %d, %g, %d; want 10, 20, 30, [40, 50], 60, 70, 80", i, j, k, l, m, n, o)
}
}
func TestMakeFuncInterface(t *testing.T) {
fn := func(i int) int { return i }
incr := func(in []Value) []Value {
return []Value{ValueOf(int(in[0].Int() + 1))}
}
fv := MakeFunc(TypeOf(fn), incr)
ValueOf(&fn).Elem().Set(fv)
if r := fn(2); r != 3 {
t.Errorf("Call returned %d, want 3", r)
}
if r := fv.Call([]Value{ValueOf(14)})[0].Int(); r != 15 {
t.Errorf("Call returned %d, want 15", r)
}
if r := fv.Interface().(func(int) int)(26); r != 27 {
t.Errorf("Call returned %d, want 27", r)
}
}
func TestMakeFuncVariadic(t *testing.T) {
// Test that variadic arguments are packed into a slice and passed as last arg
fn := func(_ int, is ...int) []int { return nil }
fv := MakeFunc(TypeOf(fn), func(in []Value) []Value { return in[1:2] })
ValueOf(&fn).Elem().Set(fv)
r := fn(1, 2, 3)
if r[0] != 2 || r[1] != 3 {
t.Errorf("Call returned [%v, %v]; want 2, 3", r[0], r[1])
}
r = fn(1, []int{2, 3}...)
if r[0] != 2 || r[1] != 3 {
t.Errorf("Call returned [%v, %v]; want 2, 3", r[0], r[1])
}
r = fv.Call([]Value{ValueOf(1), ValueOf(2), ValueOf(3)})[0].Interface().([]int)
if r[0] != 2 || r[1] != 3 {
t.Errorf("Call returned [%v, %v]; want 2, 3", r[0], r[1])
}
r = fv.CallSlice([]Value{ValueOf(1), ValueOf([]int{2, 3})})[0].Interface().([]int)
if r[0] != 2 || r[1] != 3 {
t.Errorf("Call returned [%v, %v]; want 2, 3", r[0], r[1])
}
f := fv.Interface().(func(int, ...int) []int)
r = f(1, 2, 3)
if r[0] != 2 || r[1] != 3 {
t.Errorf("Call returned [%v, %v]; want 2, 3", r[0], r[1])
}
r = f(1, []int{2, 3}...)
if r[0] != 2 || r[1] != 3 {
t.Errorf("Call returned [%v, %v]; want 2, 3", r[0], r[1])
}
}
// Dummy type that implements io.WriteCloser
type WC struct {
}
func (w *WC) Write(p []byte) (n int, err error) {
return 0, nil
}
func (w *WC) Close() error {
return nil
}
func TestMakeFuncValidReturnAssignments(t *testing.T) {
// reflect.Values returned from the wrapped function should be assignment-converted
// to the types returned by the result of MakeFunc.
// Concrete types should be promotable to interfaces they implement.
var f func() error
f = MakeFunc(TypeOf(f), func([]Value) []Value {
return []Value{ValueOf(io.EOF)}
}).Interface().(func() error)
f()
// Super-interfaces should be promotable to simpler interfaces.
var g func() io.Writer
g = MakeFunc(TypeOf(g), func([]Value) []Value {
var w io.WriteCloser = &WC{}
return []Value{ValueOf(&w).Elem()}
}).Interface().(func() io.Writer)
g()
// Channels should be promotable to directional channels.
var h func() <-chan int
h = MakeFunc(TypeOf(h), func([]Value) []Value {
return []Value{ValueOf(make(chan int))}
}).Interface().(func() <-chan int)
h()
// Unnamed types should be promotable to named types.
type T struct{ a, b, c int }
var i func() T
i = MakeFunc(TypeOf(i), func([]Value) []Value {
return []Value{ValueOf(struct{ a, b, c int }{a: 1, b: 2, c: 3})}
}).Interface().(func() T)
i()
}
func TestMakeFuncInvalidReturnAssignments(t *testing.T) {
// Type doesn't implement the required interface.
shouldPanic("", func() {
var f func() error
f = MakeFunc(TypeOf(f), func([]Value) []Value {
return []Value{ValueOf(int(7))}
}).Interface().(func() error)
f()
})
// Assigning to an interface with additional methods.
shouldPanic("", func() {
var f func() io.ReadWriteCloser
f = MakeFunc(TypeOf(f), func([]Value) []Value {
var w io.WriteCloser = &WC{}
return []Value{ValueOf(&w).Elem()}
}).Interface().(func() io.ReadWriteCloser)
f()
})
// Directional channels can't be assigned to bidirectional ones.
shouldPanic("", func() {
var f func() chan int
f = MakeFunc(TypeOf(f), func([]Value) []Value {
var c <-chan int = make(chan int)
return []Value{ValueOf(c)}
}).Interface().(func() chan int)
f()
})
// Two named types which are otherwise identical.
shouldPanic("", func() {
type T struct{ a, b, c int }
type U struct{ a, b, c int }
var f func() T
f = MakeFunc(TypeOf(f), func([]Value) []Value {
return []Value{ValueOf(U{a: 1, b: 2, c: 3})}
}).Interface().(func() T)
f()
})
}
type Point struct {
x, y int
}
// This will be index 0.
func (p Point) AnotherMethod(scale int) int {
return -1
}
// This will be index 1.
func (p Point) Dist(scale int) int {
//println("Point.Dist", p.x, p.y, scale)
return p.x*p.x*scale + p.y*p.y*scale
}
// This will be index 2.
func (p Point) GCMethod(k int) int {
runtime.GC()
return k + p.x
}
// This will be index 3.
func (p Point) NoArgs() {
// Exercise no-argument/no-result paths.
}
// This will be index 4.
func (p Point) TotalDist(points ...Point) int {
tot := 0
for _, q := range points {
dx := q.x - p.x
dy := q.y - p.y
tot += dx*dx + dy*dy // Should call Sqrt, but it's just a test.
}
return tot
}
// This will be index 5.
func (p *Point) Int64Method(x int64) int64 {
return x
}
// This will be index 6.
func (p *Point) Int32Method(x int32) int32 {
return x
}
func TestMethod(t *testing.T) {
// Non-curried method of type.
p := Point{3, 4}
i := TypeOf(p).Method(1).Func.Call([]Value{ValueOf(p), ValueOf(10)})[0].Int()
if i != 250 {
t.Errorf("Type Method returned %d; want 250", i)
}
m, ok := TypeOf(p).MethodByName("Dist")
if !ok {
t.Fatalf("method by name failed")
}
i = m.Func.Call([]Value{ValueOf(p), ValueOf(11)})[0].Int()
if i != 275 {
t.Errorf("Type MethodByName returned %d; want 275", i)
}
m, ok = TypeOf(p).MethodByName("NoArgs")
if !ok {
t.Fatalf("method by name failed")
}
n := len(m.Func.Call([]Value{ValueOf(p)}))
if n != 0 {
t.Errorf("NoArgs returned %d values; want 0", n)
}
i = TypeOf(&p).Method(1).Func.Call([]Value{ValueOf(&p), ValueOf(12)})[0].Int()
if i != 300 {
t.Errorf("Pointer Type Method returned %d; want 300", i)
}
m, ok = TypeOf(&p).MethodByName("Dist")
if !ok {
t.Fatalf("ptr method by name failed")
}
i = m.Func.Call([]Value{ValueOf(&p), ValueOf(13)})[0].Int()
if i != 325 {
t.Errorf("Pointer Type MethodByName returned %d; want 325", i)
}
m, ok = TypeOf(&p).MethodByName("NoArgs")
if !ok {
t.Fatalf("method by name failed")
}
n = len(m.Func.Call([]Value{ValueOf(&p)}))
if n != 0 {
t.Errorf("NoArgs returned %d values; want 0", n)
}
// Curried method of value.
tfunc := TypeOf((func(int) int)(nil))
v := ValueOf(p).Method(1)
if tt := v.Type(); tt != tfunc {
t.Errorf("Value Method Type is %s; want %s", tt, tfunc)
}
i = v.Call([]Value{ValueOf(14)})[0].Int()
if i != 350 {
t.Errorf("Value Method returned %d; want 350", i)
}
v = ValueOf(p).MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Value MethodByName Type is %s; want %s", tt, tfunc)
}
i = v.Call([]Value{ValueOf(15)})[0].Int()
if i != 375 {
t.Errorf("Value MethodByName returned %d; want 375", i)
}
v = ValueOf(p).MethodByName("NoArgs")
v.Call(nil)
// Curried method of pointer.
v = ValueOf(&p).Method(1)
if tt := v.Type(); tt != tfunc {
t.Errorf("Pointer Value Method Type is %s; want %s", tt, tfunc)
}
i = v.Call([]Value{ValueOf(16)})[0].Int()
if i != 400 {
t.Errorf("Pointer Value Method returned %d; want 400", i)
}
v = ValueOf(&p).MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Pointer Value MethodByName Type is %s; want %s", tt, tfunc)
}
i = v.Call([]Value{ValueOf(17)})[0].Int()
if i != 425 {
t.Errorf("Pointer Value MethodByName returned %d; want 425", i)
}
v = ValueOf(&p).MethodByName("NoArgs")
v.Call(nil)
// Curried method of interface value.
// Have to wrap interface value in a struct to get at it.
// Passing it to ValueOf directly would
// access the underlying Point, not the interface.
var x interface {
Dist(int) int
} = p
pv := ValueOf(&x).Elem()
v = pv.Method(0)
if tt := v.Type(); tt != tfunc {
t.Errorf("Interface Method Type is %s; want %s", tt, tfunc)
}
i = v.Call([]Value{ValueOf(18)})[0].Int()
if i != 450 {
t.Errorf("Interface Method returned %d; want 450", i)
}
v = pv.MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Interface MethodByName Type is %s; want %s", tt, tfunc)
}
i = v.Call([]Value{ValueOf(19)})[0].Int()
if i != 475 {
t.Errorf("Interface MethodByName returned %d; want 475", i)
}
}
func TestMethodValue(t *testing.T) {
p := Point{3, 4}
var i int64
// Curried method of value.
tfunc := TypeOf((func(int) int)(nil))
v := ValueOf(p).Method(1)
if tt := v.Type(); tt != tfunc {
t.Errorf("Value Method Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(10)})[0].Int()
if i != 250 {
t.Errorf("Value Method returned %d; want 250", i)
}
v = ValueOf(p).MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Value MethodByName Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(11)})[0].Int()
if i != 275 {
t.Errorf("Value MethodByName returned %d; want 275", i)
}
v = ValueOf(p).MethodByName("NoArgs")
ValueOf(v.Interface()).Call(nil)
v.Interface().(func())()
// Curried method of pointer.
v = ValueOf(&p).Method(1)
if tt := v.Type(); tt != tfunc {
t.Errorf("Pointer Value Method Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(12)})[0].Int()
if i != 300 {
t.Errorf("Pointer Value Method returned %d; want 300", i)
}
v = ValueOf(&p).MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Pointer Value MethodByName Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(13)})[0].Int()
if i != 325 {
t.Errorf("Pointer Value MethodByName returned %d; want 325", i)
}
v = ValueOf(&p).MethodByName("NoArgs")
ValueOf(v.Interface()).Call(nil)
v.Interface().(func())()
// Curried method of pointer to pointer.
pp := &p
v = ValueOf(&pp).Elem().Method(1)
if tt := v.Type(); tt != tfunc {
t.Errorf("Pointer Pointer Value Method Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(14)})[0].Int()
if i != 350 {
t.Errorf("Pointer Pointer Value Method returned %d; want 350", i)
}
v = ValueOf(&pp).Elem().MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Pointer Pointer Value MethodByName Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(15)})[0].Int()
if i != 375 {
t.Errorf("Pointer Pointer Value MethodByName returned %d; want 375", i)
}
// Curried method of interface value.
// Have to wrap interface value in a struct to get at it.
// Passing it to ValueOf directly would
// access the underlying Point, not the interface.
var s = struct {
X interface {
Dist(int) int
}
}{p}
pv := ValueOf(s).Field(0)
v = pv.Method(0)
if tt := v.Type(); tt != tfunc {
t.Errorf("Interface Method Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(16)})[0].Int()
if i != 400 {
t.Errorf("Interface Method returned %d; want 400", i)
}
v = pv.MethodByName("Dist")
if tt := v.Type(); tt != tfunc {
t.Errorf("Interface MethodByName Type is %s; want %s", tt, tfunc)
}
i = ValueOf(v.Interface()).Call([]Value{ValueOf(17)})[0].Int()
if i != 425 {
t.Errorf("Interface MethodByName returned %d; want 425", i)
}
// For issue #33628: method args are not stored at the right offset
// on amd64p32.
m64 := ValueOf(&p).MethodByName("Int64Method").Interface().(func(int64) int64)
if x := m64(123); x != 123 {
t.Errorf("Int64Method returned %d; want 123", x)
}
m32 := ValueOf(&p).MethodByName("Int32Method").Interface().(func(int32) int32)
if x := m32(456); x != 456 {
t.Errorf("Int32Method returned %d; want 456", x)
}
}
func TestVariadicMethodValue(t *testing.T) {
p := Point{3, 4}
points := []Point{{20, 21}, {22, 23}, {24, 25}}
want := int64(p.TotalDist(points[0], points[1], points[2]))
// Variadic method of type.
tfunc := TypeOf((func(Point, ...Point) int)(nil))
if tt := TypeOf(p).Method(4).Type; tt != tfunc {
t.Errorf("Variadic Method Type from TypeOf is %s; want %s", tt, tfunc)
}
// Curried method of value.
tfunc = TypeOf((func(...Point) int)(nil))
v := ValueOf(p).Method(4)
if tt := v.Type(); tt != tfunc {
t.Errorf("Variadic Method Type is %s; want %s", tt, tfunc)
}
i := ValueOf(v.Interface()).Call([]Value{ValueOf(points[0]), ValueOf(points[1]), ValueOf(points[2])})[0].Int()
if i != want {
t.Errorf("Variadic Method returned %d; want %d", i, want)
}
i = ValueOf(v.Interface()).CallSlice([]Value{ValueOf(points)})[0].Int()
if i != want {
t.Errorf("Variadic Method CallSlice returned %d; want %d", i, want)
}
f := v.Interface().(func(...Point) int)
i = int64(f(points[0], points[1], points[2]))
if i != want {
t.Errorf("Variadic Method Interface returned %d; want %d", i, want)
}
i = int64(f(points...))
if i != want {
t.Errorf("Variadic Method Interface Slice returned %d; want %d", i, want)
}
}
type DirectIfaceT struct {
p *int
}
func (d DirectIfaceT) M() int { return *d.p }
func TestDirectIfaceMethod(t *testing.T) {
x := 42
v := DirectIfaceT{&x}
typ := TypeOf(v)
m, ok := typ.MethodByName("M")
if !ok {
t.Fatalf("cannot find method M")
}
in := []Value{ValueOf(v)}
out := m.Func.Call(in)
if got := out[0].Int(); got != 42 {
t.Errorf("Call with value receiver got %d, want 42", got)
}
pv := &v
typ = TypeOf(pv)
m, ok = typ.MethodByName("M")
if !ok {
t.Fatalf("cannot find method M")
}
in = []Value{ValueOf(pv)}
out = m.Func.Call(in)
if got := out[0].Int(); got != 42 {
t.Errorf("Call with pointer receiver got %d, want 42", got)
}
}
// Reflect version of $GOROOT/test/method5.go
// Concrete types implementing M method.
// Smaller than a word, word-sized, larger than a word.
// Value and pointer receivers.
type Tinter interface {
M(int, byte) (byte, int)
}
type Tsmallv byte
func (v Tsmallv) M(x int, b byte) (byte, int) { return b, x + int(v) }
type Tsmallp byte
func (p *Tsmallp) M(x int, b byte) (byte, int) { return b, x + int(*p) }
type Twordv uintptr
func (v Twordv) M(x int, b byte) (byte, int) { return b, x + int(v) }
type Twordp uintptr
func (p *Twordp) M(x int, b byte) (byte, int) { return b, x + int(*p) }
type Tbigv [2]uintptr
func (v Tbigv) M(x int, b byte) (byte, int) { return b, x + int(v[0]) + int(v[1]) }
type Tbigp [2]uintptr
func (p *Tbigp) M(x int, b byte) (byte, int) { return b, x + int(p[0]) + int(p[1]) }
type tinter interface {
m(int, byte) (byte, int)
}
// Embedding via pointer.
type Tm1 struct {
Tm2
}
type Tm2 struct {
*Tm3
}
type Tm3 struct {
*Tm4
}
type Tm4 struct {
}
func (t4 Tm4) M(x int, b byte) (byte, int) { return b, x + 40 }
func TestMethod5(t *testing.T) {
CheckF := func(name string, f func(int, byte) (byte, int), inc int) {
b, x := f(1000, 99)
if b != 99 || x != 1000+inc {
t.Errorf("%s(1000, 99) = %v, %v, want 99, %v", name, b, x, 1000+inc)
}
}
CheckV := func(name string, i Value, inc int) {
bx := i.Method(0).Call([]Value{ValueOf(1000), ValueOf(byte(99))})
b := bx[0].Interface()
x := bx[1].Interface()
if b != byte(99) || x != 1000+inc {
t.Errorf("direct %s.M(1000, 99) = %v, %v, want 99, %v", name, b, x, 1000+inc)
}
CheckF(name+".M", i.Method(0).Interface().(func(int, byte) (byte, int)), inc)
}
var TinterType = TypeOf(new(Tinter)).Elem()
CheckI := func(name string, i interface{}, inc int) {
v := ValueOf(i)
CheckV(name, v, inc)
CheckV("(i="+name+")", v.Convert(TinterType), inc)
}
sv := Tsmallv(1)
CheckI("sv", sv, 1)
CheckI("&sv", &sv, 1)
sp := Tsmallp(2)
CheckI("&sp", &sp, 2)
wv := Twordv(3)
CheckI("wv", wv, 3)
CheckI("&wv", &wv, 3)
wp := Twordp(4)
CheckI("&wp", &wp, 4)
bv := Tbigv([2]uintptr{5, 6})
CheckI("bv", bv, 11)
CheckI("&bv", &bv, 11)
bp := Tbigp([2]uintptr{7, 8})
CheckI("&bp", &bp, 15)
t4 := Tm4{}
t3 := Tm3{&t4}
t2 := Tm2{&t3}
t1 := Tm1{t2}
CheckI("t4", t4, 40)
CheckI("&t4", &t4, 40)
CheckI("t3", t3, 40)
CheckI("&t3", &t3, 40)
CheckI("t2", t2, 40)
CheckI("&t2", &t2, 40)
CheckI("t1", t1, 40)
CheckI("&t1", &t1, 40)
var tnil Tinter
vnil := ValueOf(&tnil).Elem()
shouldPanic("Method", func() { vnil.Method(0) })
}
func TestInterfaceSet(t *testing.T) {
p := &Point{3, 4}
var s struct {
I interface{}
P interface {
Dist(int) int
}
}
sv := ValueOf(&s).Elem()
sv.Field(0).Set(ValueOf(p))
if q := s.I.(*Point); q != p {
t.Errorf("i: have %p want %p", q, p)
}
pv := sv.Field(1)
pv.Set(ValueOf(p))
if q := s.P.(*Point); q != p {
t.Errorf("i: have %p want %p", q, p)
}
i := pv.Method(0).Call([]Value{ValueOf(10)})[0].Int()
if i != 250 {
t.Errorf("Interface Method returned %d; want 250", i)
}
}
type T1 struct {
a string
int
}
func TestAnonymousFields(t *testing.T) {
var field StructField
var ok bool
var t1 T1
type1 := TypeOf(t1)
if field, ok = type1.FieldByName("int"); !ok {
t.Fatal("no field 'int'")
}
if field.Index[0] != 1 {
t.Error("field index should be 1; is", field.Index)
}
}
type FTest struct {
s interface{}
name string
index []int
value int
}
type D1 struct {
d int
}
type D2 struct {
d int
}
type S0 struct {
A, B, C int
D1
D2
}
type S1 struct {
B int
S0
}
type S2 struct {
A int
*S1
}
type S1x struct {
S1
}
type S1y struct {
S1
}
type S3 struct {
S1x
S2
D, E int
*S1y
}
type S4 struct {
*S4
A int
}
// The X in S6 and S7 annihilate, but they also block the X in S8.S9.
type S5 struct {
S6
S7
S8
}
type S6 struct {
X int
}
type S7 S6
type S8 struct {
S9
}
type S9 struct {
X int
Y int
}
// The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9.
type S10 struct {
S11
S12
S13
}
type S11 struct {
S6
}
type S12 struct {
S6
}
type S13 struct {
S8
}
// The X in S15.S11.S1 and S16.S11.S1 annihilate.
type S14 struct {
S15
S16
}
type S15 struct {
S11
}
type S16 struct {
S11
}
var fieldTests = []FTest{
{struct{}{}, "", nil, 0},
{struct{}{}, "Foo", nil, 0},
{S0{A: 'a'}, "A", []int{0}, 'a'},
{S0{}, "D", nil, 0},
{S1{S0: S0{A: 'a'}}, "A", []int{1, 0}, 'a'},
{S1{B: 'b'}, "B", []int{0}, 'b'},
{S1{}, "S0", []int{1}, 0},
{S1{S0: S0{C: 'c'}}, "C", []int{1, 2}, 'c'},
{S2{A: 'a'}, "A", []int{0}, 'a'},
{S2{}, "S1", []int{1}, 0},
{S2{S1: &S1{B: 'b'}}, "B", []int{1, 0}, 'b'},
{S2{S1: &S1{S0: S0{C: 'c'}}}, "C", []int{1, 1, 2}, 'c'},
{S2{}, "D", nil, 0},
{S3{}, "S1", nil, 0},
{S3{S2: S2{A: 'a'}}, "A", []int{1, 0}, 'a'},
{S3{}, "B", nil, 0},
{S3{D: 'd'}, "D", []int{2}, 0},
{S3{E: 'e'}, "E", []int{3}, 'e'},
{S4{A: 'a'}, "A", []int{1}, 'a'},
{S4{}, "B", nil, 0},
{S5{}, "X", nil, 0},
{S5{}, "Y", []int{2, 0, 1}, 0},
{S10{}, "X", nil, 0},
{S10{}, "Y", []int{2, 0, 0, 1}, 0},
{S14{}, "X", nil, 0},
}
func TestFieldByIndex(t *testing.T) {
for _, test := range fieldTests {
s := TypeOf(test.s)
f := s.FieldByIndex(test.index)
if f.Name != "" {
if test.index != nil {
if f.Name != test.name {
t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name)
}
} else {
t.Errorf("%s.%s found", s.Name(), f.Name)
}
} else if len(test.index) > 0 {
t.Errorf("%s.%s not found", s.Name(), test.name)
}
if test.value != 0 {
v := ValueOf(test.s).FieldByIndex(test.index)
if v.IsValid() {
if x, ok := v.Interface().(int); ok {
if x != test.value {
t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
}
} else {
t.Errorf("%s%v value not an int", s.Name(), test.index)
}
} else {
t.Errorf("%s%v value not found", s.Name(), test.index)
}
}
}
}
func TestFieldByName(t *testing.T) {
for _, test := range fieldTests {
s := TypeOf(test.s)
f, found := s.FieldByName(test.name)
if found {
if test.index != nil {
// Verify field depth and index.
if len(f.Index) != len(test.index) {
t.Errorf("%s.%s depth %d; want %d: %v vs %v", s.Name(), test.name, len(f.Index), len(test.index), f.Index, test.index)
} else {
for i, x := range f.Index {
if x != test.index[i] {
t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i])
}
}
}
} else {
t.Errorf("%s.%s found", s.Name(), f.Name)
}
} else if len(test.index) > 0 {
t.Errorf("%s.%s not found", s.Name(), test.name)
}
if test.value != 0 {
v := ValueOf(test.s).FieldByName(test.name)
if v.IsValid() {
if x, ok := v.Interface().(int); ok {
if x != test.value {
t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
}
} else {
t.Errorf("%s.%s value not an int", s.Name(), test.name)
}
} else {
t.Errorf("%s.%s value not found", s.Name(), test.name)
}
}
}
}
func TestImportPath(t *testing.T) {
tests := []struct {
t Type
path string
}{
{TypeOf(&base64.Encoding{}).Elem(), "encoding/base64"},
{TypeOf(int(0)), ""},
{TypeOf(int8(0)), ""},
{TypeOf(int16(0)), ""},
{TypeOf(int32(0)), ""},
{TypeOf(int64(0)), ""},
{TypeOf(uint(0)), ""},
{TypeOf(uint8(0)), ""},
{TypeOf(uint16(0)), ""},
{TypeOf(uint32(0)), ""},
{TypeOf(uint64(0)), ""},
{TypeOf(uintptr(0)), ""},
{TypeOf(float32(0)), ""},
{TypeOf(float64(0)), ""},
{TypeOf(complex64(0)), ""},
{TypeOf(complex128(0)), ""},
{TypeOf(byte(0)), ""},
{TypeOf(rune(0)), ""},
{TypeOf([]byte(nil)), ""},
{TypeOf([]rune(nil)), ""},
{TypeOf(string("")), ""},
{TypeOf((*interface{})(nil)).Elem(), ""},
{TypeOf((*byte)(nil)), ""},
{TypeOf((*rune)(nil)), ""},
{TypeOf((*int64)(nil)), ""},
{TypeOf(map[string]int{}), ""},
{TypeOf((*error)(nil)).Elem(), ""},
{TypeOf((*Point)(nil)), ""},
{TypeOf((*Point)(nil)).Elem(), "reflect_test"},
}
for _, test := range tests {
if path := test.t.PkgPath(); path != test.path {
t.Errorf("%v.PkgPath() = %q, want %q", test.t, path, test.path)
}
}
}
func TestFieldPkgPath(t *testing.T) {
type x int
typ := TypeOf(struct {
Exported string
unexported string
OtherPkgFields
int // issue 21702
*x // issue 21122
}{})
type pkgpathTest struct {
index []int
pkgPath string
embedded bool
exported bool
}
checkPkgPath := func(name string, s []pkgpathTest) {
for _, test := range s {
f := typ.FieldByIndex(test.index)
if got, want := f.PkgPath, test.pkgPath; got != want {
t.Errorf("%s: Field(%d).PkgPath = %q, want %q", name, test.index, got, want)
}
if got, want := f.Anonymous, test.embedded; got != want {
t.Errorf("%s: Field(%d).Anonymous = %v, want %v", name, test.index, got, want)
}
if got, want := f.IsExported(), test.exported; got != want {
t.Errorf("%s: Field(%d).IsExported = %v, want %v", name, test.index, got, want)
}
}
}
checkPkgPath("testStruct", []pkgpathTest{
{[]int{0}, "", false, true}, // Exported
{[]int{1}, "reflect_test", false, false}, // unexported
{[]int{2}, "", true, true}, // OtherPkgFields
{[]int{2, 0}, "", false, true}, // OtherExported
{[]int{2, 1}, "reflect", false, false}, // otherUnexported
{[]int{3}, "reflect_test", true, false}, // int
{[]int{4}, "reflect_test", true, false}, // *x
})
type localOtherPkgFields OtherPkgFields
typ = TypeOf(localOtherPkgFields{})
checkPkgPath("localOtherPkgFields", []pkgpathTest{
{[]int{0}, "", false, true}, // OtherExported
{[]int{1}, "reflect", false, false}, // otherUnexported
})
}
func TestMethodPkgPath(t *testing.T) {
type I interface {
x()
X()
}
typ := TypeOf((*interface {
I
y()
Y()
})(nil)).Elem()
tests := []struct {
name string
pkgPath string
exported bool
}{
{"X", "", true},
{"Y", "", true},
{"x", "reflect_test", false},
{"y", "reflect_test", false},
}
for _, test := range tests {
m, _ := typ.MethodByName(test.name)
if got, want := m.PkgPath, test.pkgPath; got != want {
t.Errorf("MethodByName(%q).PkgPath = %q, want %q", test.name, got, want)
}
if got, want := m.IsExported(), test.exported; got != want {
t.Errorf("MethodByName(%q).IsExported = %v, want %v", test.name, got, want)
}
}
}
func TestVariadicType(t *testing.T) {
// Test example from Type documentation.
var f func(x int, y ...float64)
typ := TypeOf(f)
if typ.NumIn() == 2 && typ.In(0) == TypeOf(int(0)) {
sl := typ.In(1)
if sl.Kind() == Slice {
if sl.Elem() == TypeOf(0.0) {
// ok
return
}
}
}
// Failed
t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64")
s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
for i := 0; i < typ.NumIn(); i++ {
s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i))
}
t.Error(s)
}
type inner struct {
x int
}
type outer struct {
y int
inner
}
func (*inner) M() {}
func (*outer) M() {}
func TestNestedMethods(t *testing.T) {
typ := TypeOf((*outer)(nil))
if typ.NumMethod() != 1 || typ.Method(0).Func.Pointer() != ValueOf((*outer).M).Pointer() {
t.Errorf("Wrong method table for outer: (M=%p)", (*outer).M)
for i := 0; i < typ.NumMethod(); i++ {
m := typ.Method(i)
t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Pointer())
}
}
}
type unexp struct{}
func (*unexp) f() (int32, int8) { return 7, 7 }
func (*unexp) g() (int64, int8) { return 8, 8 }
type unexpI interface {
f() (int32, int8)
}
var unexpi unexpI = new(unexp)
func TestUnexportedMethods(t *testing.T) {
typ := TypeOf(unexpi)
if got := typ.NumMethod(); got != 0 {
t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
}
}
type InnerInt struct {
X int
}
type OuterInt struct {
Y int
InnerInt
}
func (i *InnerInt) M() int {
return i.X
}
func TestEmbeddedMethods(t *testing.T) {
typ := TypeOf((*OuterInt)(nil))
if typ.NumMethod() != 1 || typ.Method(0).Func.Pointer() != ValueOf((*OuterInt).M).Pointer() {
t.Errorf("Wrong method table for OuterInt: (m=%p)", (*OuterInt).M)
for i := 0; i < typ.NumMethod(); i++ {
m := typ.Method(i)
t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Pointer())
}
}
i := &InnerInt{3}
if v := ValueOf(i).Method(0).Call(nil)[0].Int(); v != 3 {
t.Errorf("i.M() = %d, want 3", v)
}
o := &OuterInt{1, InnerInt{2}}
if v := ValueOf(o).Method(0).Call(nil)[0].Int(); v != 2 {
t.Errorf("i.M() = %d, want 2", v)
}
f := (*OuterInt).M
if v := f(o); v != 2 {
t.Errorf("f(o) = %d, want 2", v)
}
}
type FuncDDD func(...interface{}) error
func (f FuncDDD) M() {}
func TestNumMethodOnDDD(t *testing.T) {
rv := ValueOf((FuncDDD)(nil))
if n := rv.NumMethod(); n != 1 {
t.Fatalf("NumMethod()=%d, want 1", n)
}
}
func TestPtrTo(t *testing.T) {
// This block of code means that the ptrToThis field of the
// reflect data for *unsafe.Pointer is non zero, see
// https://golang.org/issue/19003
var x unsafe.Pointer
var y = &x
var z = &y
var i int
typ := TypeOf(z)
for i = 0; i < 100; i++ {
typ = PtrTo(typ)
}
for i = 0; i < 100; i++ {
typ = typ.Elem()
}
if typ != TypeOf(z) {
t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, TypeOf(z))
}
}
func TestPtrToGC(t *testing.T) {
type T *uintptr
tt := TypeOf(T(nil))
pt := PtrTo(tt)
const n = 100
var x []interface{}
for i := 0; i < n; i++ {
v := New(pt)
p := new(*uintptr)
*p = new(uintptr)
**p = uintptr(i)
v.Elem().Set(ValueOf(p).Convert(pt))
x = append(x, v.Interface())
}
runtime.GC()
for i, xi := range x {
k := ValueOf(xi).Elem().Elem().Elem().Interface().(uintptr)
if k != uintptr(i) {
t.Errorf("lost x[%d] = %d, want %d", i, k, i)
}
}
}
func BenchmarkPtrTo(b *testing.B) {
// Construct a type with a zero ptrToThis.
type T struct{ int }
t := SliceOf(TypeOf(T{}))
ptrToThis := ValueOf(t).Elem().FieldByName("ptrToThis")
if !ptrToThis.IsValid() {
b.Fatalf("%v has no ptrToThis field; was it removed from rtype?", t)
}
if ptrToThis.Int() != 0 {
b.Fatalf("%v.ptrToThis unexpectedly nonzero", t)
}
b.ResetTimer()
// Now benchmark calling PtrTo on it: we'll have to hit the ptrMap cache on
// every call.
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
PtrTo(t)
}
})
}
func TestAddr(t *testing.T) {
var p struct {
X, Y int
}
v := ValueOf(&p)
v = v.Elem()
v = v.Addr()
v = v.Elem()
v = v.Field(0)
v.SetInt(2)
if p.X != 2 {
t.Errorf("Addr.Elem.Set failed to set value")
}
// Again but take address of the ValueOf value.
// Exercises generation of PtrTypes not present in the binary.
q := &p
v = ValueOf(&q).Elem()
v = v.Addr()
v = v.Elem()
v = v.Elem()
v = v.Addr()
v = v.Elem()
v = v.Field(0)
v.SetInt(3)
if p.X != 3 {
t.Errorf("Addr.Elem.Set failed to set value")
}
// Starting without pointer we should get changed value
// in interface.
qq := p
v = ValueOf(&qq).Elem()
v0 := v
v = v.Addr()
v = v.Elem()
v = v.Field(0)
v.SetInt(4)
if p.X != 3 { // should be unchanged from last time
t.Errorf("somehow value Set changed original p")
}
p = v0.Interface().(struct {
X, Y int
})
if p.X != 4 {
t.Errorf("Addr.Elem.Set valued to set value in top value")
}
// Verify that taking the address of a type gives us a pointer
// which we can convert back using the usual interface
// notation.
var s struct {
B *bool
}
ps := ValueOf(&s).Elem().Field(0).Addr().Interface()
*(ps.(**bool)) = new(bool)
if s.B == nil {
t.Errorf("Addr.Interface direct assignment failed")
}
}
func noAlloc(t *testing.T, n int, f func(int)) {
if testing.Short() {
t.Skip("skipping malloc count in short mode")
}
if runtime.GOMAXPROCS(0) > 1 {
t.Skip("skipping; GOMAXPROCS>1")
}
i := -1
allocs := testing.AllocsPerRun(n, func() {
f(i)
i++
})
if allocs > 0 {
t.Errorf("%d iterations: got %v mallocs, want 0", n, allocs)
}
}
func TestAllocations(t *testing.T) {
noAlloc(t, 100, func(j int) {
var i interface{}
var v Value
// We can uncomment this when compiler escape analysis
// is good enough to see that the integer assigned to i
// does not escape and therefore need not be allocated.
//
// i = 42 + j
// v = ValueOf(i)
// if int(v.Int()) != 42+j {
// panic("wrong int")
// }
i = func(j int) int { return j }
v = ValueOf(i)
if v.Interface().(func(int) int)(j) != j {
panic("wrong result")
}
})
}
func TestSmallNegativeInt(t *testing.T) {
i := int16(-1)
v := ValueOf(i)
if v.Int() != -1 {
t.Errorf("int16(-1).Int() returned %v", v.Int())
}
}
func TestIndex(t *testing.T) {
xs := []byte{1, 2, 3, 4, 5, 6, 7, 8}
v := ValueOf(xs).Index(3).Interface().(byte)
if v != xs[3] {
t.Errorf("xs.Index(3) = %v; expected %v", v, xs[3])
}
xa := [8]byte{10, 20, 30, 40, 50, 60, 70, 80}
v = ValueOf(xa).Index(2).Interface().(byte)
if v != xa[2] {
t.Errorf("xa.Index(2) = %v; expected %v", v, xa[2])
}
s := "0123456789"
v = ValueOf(s).Index(3).Interface().(byte)
if v != s[3] {
t.Errorf("s.Index(3) = %v; expected %v", v, s[3])
}
}
func TestSlice(t *testing.T) {
xs := []int{1, 2, 3, 4, 5, 6, 7, 8}
v := ValueOf(xs).Slice(3, 5).Interface().([]int)
if len(v) != 2 {
t.Errorf("len(xs.Slice(3, 5)) = %d", len(v))
}
if cap(v) != 5 {
t.Errorf("cap(xs.Slice(3, 5)) = %d", cap(v))
}
if !DeepEqual(v[0:5], xs[3:]) {
t.Errorf("xs.Slice(3, 5)[0:5] = %v", v[0:5])
}
xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80}
v = ValueOf(&xa).Elem().Slice(2, 5).Interface().([]int)
if len(v) != 3 {
t.Errorf("len(xa.Slice(2, 5)) = %d", len(v))
}
if cap(v) != 6 {
t.Errorf("cap(xa.Slice(2, 5)) = %d", cap(v))
}
if !DeepEqual(v[0:6], xa[2:]) {
t.Errorf("xs.Slice(2, 5)[0:6] = %v", v[0:6])
}
s := "0123456789"
vs := ValueOf(s).Slice(3, 5).Interface().(string)
if vs != s[3:5] {
t.Errorf("s.Slice(3, 5) = %q; expected %q", vs, s[3:5])
}
rv := ValueOf(&xs).Elem()
rv = rv.Slice(3, 4)
ptr2 := rv.Pointer()
rv = rv.Slice(5, 5)
ptr3 := rv.Pointer()
if ptr3 != ptr2 {
t.Errorf("xs.Slice(3,4).Slice3(5,5).Pointer() = %#x, want %#x", ptr3, ptr2)
}
}
func TestSlice3(t *testing.T) {
xs := []int{1, 2, 3, 4, 5, 6, 7, 8}
v := ValueOf(xs).Slice3(3, 5, 7).Interface().([]int)
if len(v) != 2 {
t.Errorf("len(xs.Slice3(3, 5, 7)) = %d", len(v))
}
if cap(v) != 4 {
t.Errorf("cap(xs.Slice3(3, 5, 7)) = %d", cap(v))
}
if !DeepEqual(v[0:4], xs[3:7:7]) {
t.Errorf("xs.Slice3(3, 5, 7)[0:4] = %v", v[0:4])
}
rv := ValueOf(&xs).Elem()
shouldPanic("Slice3", func() { rv.Slice3(1, 2, 1) })
shouldPanic("Slice3", func() { rv.Slice3(1, 1, 11) })
shouldPanic("Slice3", func() { rv.Slice3(2, 2, 1) })
xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80}
v = ValueOf(&xa).Elem().Slice3(2, 5, 6).Interface().([]int)
if len(v) != 3 {
t.Errorf("len(xa.Slice(2, 5, 6)) = %d", len(v))
}
if cap(v) != 4 {
t.Errorf("cap(xa.Slice(2, 5, 6)) = %d", cap(v))
}
if !DeepEqual(v[0:4], xa[2:6:6]) {
t.Errorf("xs.Slice(2, 5, 6)[0:4] = %v", v[0:4])
}
rv = ValueOf(&xa).Elem()
shouldPanic("Slice3", func() { rv.Slice3(1, 2, 1) })
shouldPanic("Slice3", func() { rv.Slice3(1, 1, 11) })
shouldPanic("Slice3", func() { rv.Slice3(2, 2, 1) })
s := "hello world"
rv = ValueOf(&s).Elem()
shouldPanic("Slice3", func() { rv.Slice3(1, 2, 3) })
rv = ValueOf(&xs).Elem()
rv = rv.Slice3(3, 5, 7)
ptr2 := rv.Pointer()
rv = rv.Slice3(4, 4, 4)
ptr3 := rv.Pointer()
if ptr3 != ptr2 {
t.Errorf("xs.Slice3(3,5,7).Slice3(4,4,4).Pointer() = %#x, want %#x", ptr3, ptr2)
}
}
func TestSetLenCap(t *testing.T) {
xs := []int{1, 2, 3, 4, 5, 6, 7, 8}
xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80}
vs := ValueOf(&xs).Elem()
shouldPanic("SetLen", func() { vs.SetLen(10) })
shouldPanic("SetCap", func() { vs.SetCap(10) })
shouldPanic("SetLen", func() { vs.SetLen(-1) })
shouldPanic("SetCap", func() { vs.SetCap(-1) })
shouldPanic("SetCap", func() { vs.SetCap(6) }) // smaller than len
vs.SetLen(5)
if len(xs) != 5 || cap(xs) != 8 {
t.Errorf("after SetLen(5), len, cap = %d, %d, want 5, 8", len(xs), cap(xs))
}
vs.SetCap(6)
if len(xs) != 5 || cap(xs) != 6 {
t.Errorf("after SetCap(6), len, cap = %d, %d, want 5, 6", len(xs), cap(xs))
}
vs.SetCap(5)
if len(xs) != 5 || cap(xs) != 5 {
t.Errorf("after SetCap(5), len, cap = %d, %d, want 5, 5", len(xs), cap(xs))
}
shouldPanic("SetCap", func() { vs.SetCap(4) }) // smaller than len
shouldPanic("SetLen", func() { vs.SetLen(6) }) // bigger than cap
va := ValueOf(&xa).Elem()
shouldPanic("SetLen", func() { va.SetLen(8) })
shouldPanic("SetCap", func() { va.SetCap(8) })
}
func TestVariadic(t *testing.T) {
var b bytes.Buffer
V := ValueOf
b.Reset()
V(fmt.Fprintf).Call([]Value{V(&b), V("%s, %d world"), V("hello"), V(42)})
if b.String() != "hello, 42 world" {
t.Errorf("after Fprintf Call: %q != %q", b.String(), "hello 42 world")
}
b.Reset()
V(fmt.Fprintf).CallSlice([]Value{V(&b), V("%s, %d world"), V([]interface{}{"hello", 42})})
if b.String() != "hello, 42 world" {
t.Errorf("after Fprintf CallSlice: %q != %q", b.String(), "hello 42 world")
}
}
func TestFuncArg(t *testing.T) {
f1 := func(i int, f func(int) int) int { return f(i) }
f2 := func(i int) int { return i + 1 }
r := ValueOf(f1).Call([]Value{ValueOf(100), ValueOf(f2)})
if r[0].Int() != 101 {
t.Errorf("function returned %d, want 101", r[0].Int())
}
}
func TestStructArg(t *testing.T) {
type padded struct {
B string
C int32
}
var (
gotA padded
gotB uint32
wantA = padded{"3", 4}
wantB = uint32(5)
)
f := func(a padded, b uint32) {
gotA, gotB = a, b
}
ValueOf(f).Call([]Value{ValueOf(wantA), ValueOf(wantB)})
if gotA != wantA || gotB != wantB {
t.Errorf("function called with (%v, %v), want (%v, %v)", gotA, gotB, wantA, wantB)
}
}
var tagGetTests = []struct {
Tag StructTag
Key string
Value string
}{
{`protobuf:"PB(1,2)"`, `protobuf`, `PB(1,2)`},
{`protobuf:"PB(1,2)"`, `foo`, ``},
{`protobuf:"PB(1,2)"`, `rotobuf`, ``},
{`protobuf:"PB(1,2)" json:"name"`, `json`, `name`},
{`protobuf:"PB(1,2)" json:"name"`, `protobuf`, `PB(1,2)`},
{`k0:"values contain spaces" k1:"and\ttabs"`, "k0", "values contain spaces"},
{`k0:"values contain spaces" k1:"and\ttabs"`, "k1", "and\ttabs"},
}
func TestTagGet(t *testing.T) {
for _, tt := range tagGetTests {
if v := tt.Tag.Get(tt.Key); v != tt.Value {
t.Errorf("StructTag(%#q).Get(%#q) = %#q, want %#q", tt.Tag, tt.Key, v, tt.Value)
}
}
}
func TestBytes(t *testing.T) {
type B []byte
x := B{1, 2, 3, 4}
y := ValueOf(x).Bytes()
if !bytes.Equal(x, y) {
t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
}
if &x[0] != &y[0] {
t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
}
}
func TestSetBytes(t *testing.T) {
type B []byte
var x B
y := []byte{1, 2, 3, 4}
ValueOf(&x).Elem().SetBytes(y)
if !bytes.Equal(x, y) {
t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
}
if &x[0] != &y[0] {
t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
}
}
type Private struct {
x int
y **int
Z int
}
func (p *Private) m() {
}
type private struct {
Z int
z int
S string
A [1]Private
T []Private
}
func (p *private) P() {
}
type Public struct {
X int
Y **int
private
}
func (p *Public) M() {
}
func TestUnexported(t *testing.T) {
var pub Public
pub.S = "S"
pub.T = pub.A[:]
v := ValueOf(&pub)
isValid(v.Elem().Field(0))
isValid(v.Elem().Field(1))
isValid(v.Elem().Field(2))
isValid(v.Elem().FieldByName("X"))
isValid(v.Elem().FieldByName("Y"))
isValid(v.Elem().FieldByName("Z"))
isValid(v.Type().Method(0).Func)
m, _ := v.Type().MethodByName("M")
isValid(m.Func)
m, _ = v.Type().MethodByName("P")
isValid(m.Func)
isNonNil(v.Elem().Field(0).Interface())
isNonNil(v.Elem().Field(1).Interface())
isNonNil(v.Elem().Field(2).Field(2).Index(0))
isNonNil(v.Elem().FieldByName("X").Interface())
isNonNil(v.Elem().FieldByName("Y").Interface())
isNonNil(v.Elem().FieldByName("Z").Interface())
isNonNil(v.Elem().FieldByName("S").Index(0).Interface())
isNonNil(v.Type().Method(0).Func.Interface())
m, _ = v.Type().MethodByName("P")
isNonNil(m.Func.Interface())
var priv Private
v = ValueOf(&priv)
isValid(v.Elem().Field(0))
isValid(v.Elem().Field(1))
isValid(v.Elem().FieldByName("x"))
isValid(v.Elem().FieldByName("y"))
shouldPanic("Interface", func() { v.Elem().Field(0).Interface() })
shouldPanic("Interface", func() { v.Elem().Field(1).Interface() })
shouldPanic("Interface", func() { v.Elem().FieldByName("x").Interface() })
shouldPanic("Interface", func() { v.Elem().FieldByName("y").Interface() })
shouldPanic("Method", func() { v.Type().Method(0) })
}
func TestSetPanic(t *testing.T) {
ok := func(f func()) { f() }
bad := func(f func()) { shouldPanic("Set", f) }
clear := func(v Value) { v.Set(Zero(v.Type())) }
type t0 struct {
W int
}
type t1 struct {
Y int
t0
}
type T2 struct {
Z int
namedT0 t0
}
type T struct {
X int
t1
T2
NamedT1 t1
NamedT2 T2
namedT1 t1
namedT2 T2
}
// not addressable
v := ValueOf(T{})
bad(func() { clear(v.Field(0)) }) // .X
bad(func() { clear(v.Field(1)) }) // .t1
bad(func() { clear(v.Field(1).Field(0)) }) // .t1.Y
bad(func() { clear(v.Field(1).Field(1)) }) // .t1.t0
bad(func() { clear(v.Field(1).Field(1).Field(0)) }) // .t1.t0.W
bad(func() { clear(v.Field(2)) }) // .T2
bad(func() { clear(v.Field(2).Field(0)) }) // .T2.Z
bad(func() { clear(v.Field(2).Field(1)) }) // .T2.namedT0
bad(func() { clear(v.Field(2).Field(1).Field(0)) }) // .T2.namedT0.W
bad(func() { clear(v.Field(3)) }) // .NamedT1
bad(func() { clear(v.Field(3).Field(0)) }) // .NamedT1.Y
bad(func() { clear(v.Field(3).Field(1)) }) // .NamedT1.t0
bad(func() { clear(v.Field(3).Field(1).Field(0)) }) // .NamedT1.t0.W
bad(func() { clear(v.Field(4)) }) // .NamedT2
bad(func() { clear(v.Field(4).Field(0)) }) // .NamedT2.Z
bad(func() { clear(v.Field(4).Field(1)) }) // .NamedT2.namedT0
bad(func() { clear(v.Field(4).Field(1).Field(0)) }) // .NamedT2.namedT0.W
bad(func() { clear(v.Field(5)) }) // .namedT1
bad(func() { clear(v.Field(5).Field(0)) }) // .namedT1.Y
bad(func() { clear(v.Field(5).Field(1)) }) // .namedT1.t0
bad(func() { clear(v.Field(5).Field(1).Field(0)) }) // .namedT1.t0.W
bad(func() { clear(v.Field(6)) }) // .namedT2
bad(func() { clear(v.Field(6).Field(0)) }) // .namedT2.Z
bad(func() { clear(v.Field(6).Field(1)) }) // .namedT2.namedT0
bad(func() { clear(v.Field(6).Field(1).Field(0)) }) // .namedT2.namedT0.W
// addressable
v = ValueOf(&T{}).Elem()
ok(func() { clear(v.Field(0)) }) // .X
bad(func() { clear(v.Field(1)) }) // .t1
ok(func() { clear(v.Field(1).Field(0)) }) // .t1.Y
bad(func() { clear(v.Field(1).Field(1)) }) // .t1.t0
ok(func() { clear(v.Field(1).Field(1).Field(0)) }) // .t1.t0.W
ok(func() { clear(v.Field(2)) }) // .T2
ok(func() { clear(v.Field(2).Field(0)) }) // .T2.Z
bad(func() { clear(v.Field(2).Field(1)) }) // .T2.namedT0
bad(func() { clear(v.Field(2).Field(1).Field(0)) }) // .T2.namedT0.W
ok(func() { clear(v.Field(3)) }) // .NamedT1
ok(func() { clear(v.Field(3).Field(0)) }) // .NamedT1.Y
bad(func() { clear(v.Field(3).Field(1)) }) // .NamedT1.t0
ok(func() { clear(v.Field(3).Field(1).Field(0)) }) // .NamedT1.t0.W
ok(func() { clear(v.Field(4)) }) // .NamedT2
ok(func() { clear(v.Field(4).Field(0)) }) // .NamedT2.Z
bad(func() { clear(v.Field(4).Field(1)) }) // .NamedT2.namedT0
bad(func() { clear(v.Field(4).Field(1).Field(0)) }) // .NamedT2.namedT0.W
bad(func() { clear(v.Field(5)) }) // .namedT1
bad(func() { clear(v.Field(5).Field(0)) }) // .namedT1.Y
bad(func() { clear(v.Field(5).Field(1)) }) // .namedT1.t0
bad(func() { clear(v.Field(5).Field(1).Field(0)) }) // .namedT1.t0.W
bad(func() { clear(v.Field(6)) }) // .namedT2
bad(func() { clear(v.Field(6).Field(0)) }) // .namedT2.Z
bad(func() { clear(v.Field(6).Field(1)) }) // .namedT2.namedT0
bad(func() { clear(v.Field(6).Field(1).Field(0)) }) // .namedT2.namedT0.W
}
type timp int
func (t timp) W() {}
func (t timp) Y() {}
func (t timp) w() {}
func (t timp) y() {}
func TestCallPanic(t *testing.T) {
type t0 interface {
W()
w()
}
type T1 interface {
Y()
y()
}
type T2 struct {
T1
t0
}
type T struct {
t0 // 0
T1 // 1
NamedT0 t0 // 2
NamedT1 T1 // 3
NamedT2 T2 // 4
namedT0 t0 // 5
namedT1 T1 // 6
namedT2 T2 // 7
}
ok := func(f func()) { f() }
badCall := func(f func()) { shouldPanic("Call", f) }
badMethod := func(f func()) { shouldPanic("Method", f) }
call := func(v Value) { v.Call(nil) }
i := timp(0)
v := ValueOf(T{i, i, i, i, T2{i, i}, i, i, T2{i, i}})
badCall(func() { call(v.Field(0).Method(0)) }) // .t0.W
badCall(func() { call(v.Field(0).Elem().Method(0)) }) // .t0.W
badCall(func() { call(v.Field(0).Method(1)) }) // .t0.w
badMethod(func() { call(v.Field(0).Elem().Method(2)) }) // .t0.w
ok(func() { call(v.Field(1).Method(0)) }) // .T1.Y
ok(func() { call(v.Field(1).Elem().Method(0)) }) // .T1.Y
badCall(func() { call(v.Field(1).Method(1)) }) // .T1.y
badMethod(func() { call(v.Field(1).Elem().Method(2)) }) // .T1.y
ok(func() { call(v.Field(2).Method(0)) }) // .NamedT0.W
ok(func() { call(v.Field(2).Elem().Method(0)) }) // .NamedT0.W
badCall(func() { call(v.Field(2).Method(1)) }) // .NamedT0.w
badMethod(func() { call(v.Field(2).Elem().Method(2)) }) // .NamedT0.w
ok(func() { call(v.Field(3).Method(0)) }) // .NamedT1.Y
ok(func() { call(v.Field(3).Elem().Method(0)) }) // .NamedT1.Y
badCall(func() { call(v.Field(3).Method(1)) }) // .NamedT1.y
badMethod(func() { call(v.Field(3).Elem().Method(3)) }) // .NamedT1.y
ok(func() { call(v.Field(4).Field(0).Method(0)) }) // .NamedT2.T1.Y
ok(func() { call(v.Field(4).Field(0).Elem().Method(0)) }) // .NamedT2.T1.W
badCall(func() { call(v.Field(4).Field(1).Method(0)) }) // .NamedT2.t0.W
badCall(func() { call(v.Field(4).Field(1).Elem().Method(0)) }) // .NamedT2.t0.W
badCall(func() { call(v.Field(5).Method(0)) }) // .namedT0.W
badCall(func() { call(v.Field(5).Elem().Method(0)) }) // .namedT0.W
badCall(func() { call(v.Field(5).Method(1)) }) // .namedT0.w
badMethod(func() { call(v.Field(5).Elem().Method(2)) }) // .namedT0.w
badCall(func() { call(v.Field(6).Method(0)) }) // .namedT1.Y
badCall(func() { call(v.Field(6).Elem().Method(0)) }) // .namedT1.Y
badCall(func() { call(v.Field(6).Method(0)) }) // .namedT1.y
badCall(func() { call(v.Field(6).Elem().Method(0)) }) // .namedT1.y
badCall(func() { call(v.Field(7).Field(0).Method(0)) }) // .namedT2.T1.Y
badCall(func() { call(v.Field(7).Field(0).Elem().Method(0)) }) // .namedT2.T1.W
badCall(func() { call(v.Field(7).Field(1).Method(0)) }) // .namedT2.t0.W
badCall(func() { call(v.Field(7).Field(1).Elem().Method(0)) }) // .namedT2.t0.W
}
func shouldPanic(expect string, f func()) {
defer func() {
r := recover()
if r == nil {
panic("did not panic")
}
if expect != "" {
var s string
switch r := r.(type) {
case string:
s = r
case *ValueError:
s = r.Error()
default:
panic(fmt.Sprintf("panicked with unexpected type %T", r))
}
if !strings.HasPrefix(s, "reflect") {
panic(`panic string does not start with "reflect": ` + s)
}
if !strings.Contains(s, expect) {
panic(`panic string does not contain "` + expect + `": ` + s)
}
}
}()
f()
}
func isNonNil(x interface{}) {
if x == nil {
panic("nil interface")
}
}
func isValid(v Value) {
if !v.IsValid() {
panic("zero Value")
}
}
func TestAlias(t *testing.T) {
x := string("hello")
v := ValueOf(&x).Elem()
oldvalue := v.Interface()
v.SetString("world")
newvalue := v.Interface()
if oldvalue != "hello" || newvalue != "world" {
t.Errorf("aliasing: old=%q new=%q, want hello, world", oldvalue, newvalue)
}
}
var V = ValueOf
func EmptyInterfaceV(x interface{}) Value {
return ValueOf(&x).Elem()
}
func ReaderV(x io.Reader) Value {
return ValueOf(&x).Elem()
}
func ReadWriterV(x io.ReadWriter) Value {
return ValueOf(&x).Elem()
}
type Empty struct{}
type MyStruct struct {
x int `some:"tag"`
}
type MyStruct1 struct {
x struct {
int `some:"bar"`
}
}
type MyStruct2 struct {
x struct {
int `some:"foo"`
}
}
type MyString string
type MyBytes []byte
type MyBytesArrayPtr0 *[0]byte
type MyBytesArrayPtr *[4]byte
type MyBytesArray0 [0]byte
type MyBytesArray [4]byte
type MyRunes []int32
type MyFunc func()
type MyByte byte
type IntChan chan int
type IntChanRecv <-chan int
type IntChanSend chan<- int
type BytesChan chan []byte
type BytesChanRecv <-chan []byte
type BytesChanSend chan<- []byte
var convertTests = []struct {
in Value
out Value
}{
// numbers
/*
Edit .+1,/\*\//-1>cat >/tmp/x.go && go run /tmp/x.go
package main
import "fmt"
var numbers = []string{
"int8", "uint8", "int16", "uint16",
"int32", "uint32", "int64", "uint64",
"int", "uint", "uintptr",
"float32", "float64",
}
func main() {
// all pairs but in an unusual order,
// to emit all the int8, uint8 cases
// before n grows too big.
n := 1
for i, f := range numbers {
for _, g := range numbers[i:] {
fmt.Printf("\t{V(%s(%d)), V(%s(%d))},\n", f, n, g, n)
n++
if f != g {
fmt.Printf("\t{V(%s(%d)), V(%s(%d))},\n", g, n, f, n)
n++
}
}
}
}
*/
{V(int8(1)), V(int8(1))},
{V(int8(2)), V(uint8(2))},
{V(uint8(3)), V(int8(3))},
{V(int8(4)), V(int16(4))},
{V(int16(5)), V(int8(5))},
{V(int8(6)), V(uint16(6))},
{V(uint16(7)), V(int8(7))},
{V(int8(8)), V(int32(8))},
{V(int32(9)), V(int8(9))},
{V(int8(10)), V(uint32(10))},
{V(uint32(11)), V(int8(11))},
{V(int8(12)), V(int64(12))},
{V(int64(13)), V(int8(13))},
{V(int8(14)), V(uint64(14))},
{V(uint64(15)), V(int8(15))},
{V(int8(16)), V(int(16))},
{V(int(17)), V(int8(17))},
{V(int8(18)), V(uint(18))},
{V(uint(19)), V(int8(19))},
{V(int8(20)), V(uintptr(20))},
{V(uintptr(21)), V(int8(21))},
{V(int8(22)), V(float32(22))},
{V(float32(23)), V(int8(23))},
{V(int8(24)), V(float64(24))},
{V(float64(25)), V(int8(25))},
{V(uint8(26)), V(uint8(26))},
{V(uint8(27)), V(int16(27))},
{V(int16(28)), V(uint8(28))},
{V(uint8(29)), V(uint16(29))},
{V(uint16(30)), V(uint8(30))},
{V(uint8(31)), V(int32(31))},
{V(int32(32)), V(uint8(32))},
{V(uint8(33)), V(uint32(33))},
{V(uint32(34)), V(uint8(34))},
{V(uint8(35)), V(int64(35))},
{V(int64(36)), V(uint8(36))},
{V(uint8(37)), V(uint64(37))},
{V(uint64(38)), V(uint8(38))},
{V(uint8(39)), V(int(39))},
{V(int(40)), V(uint8(40))},
{V(uint8(41)), V(uint(41))},
{V(uint(42)), V(uint8(42))},
{V(uint8(43)), V(uintptr(43))},
{V(uintptr(44)), V(uint8(44))},
{V(uint8(45)), V(float32(45))},
{V(float32(46)), V(uint8(46))},
{V(uint8(47)), V(float64(47))},
{V(float64(48)), V(uint8(48))},
{V(int16(49)), V(int16(49))},
{V(int16(50)), V(uint16(50))},
{V(uint16(51)), V(int16(51))},
{V(int16(52)), V(int32(52))},
{V(int32(53)), V(int16(53))},
{V(int16(54)), V(uint32(54))},
{V(uint32(55)), V(int16(55))},
{V(int16(56)), V(int64(56))},
{V(int64(57)), V(int16(57))},
{V(int16(58)), V(uint64(58))},
{V(uint64(59)), V(int16(59))},
{V(int16(60)), V(int(60))},
{V(int(61)), V(int16(61))},
{V(int16(62)), V(uint(62))},
{V(uint(63)), V(int16(63))},
{V(int16(64)), V(uintptr(64))},
{V(uintptr(65)), V(int16(65))},
{V(int16(66)), V(float32(66))},
{V(float32(67)), V(int16(67))},
{V(int16(68)), V(float64(68))},
{V(float64(69)), V(int16(69))},
{V(uint16(70)), V(uint16(70))},
{V(uint16(71)), V(int32(71))},
{V(int32(72)), V(uint16(72))},
{V(uint16(73)), V(uint32(73))},
{V(uint32(74)), V(uint16(74))},
{V(uint16(75)), V(int64(75))},
{V(int64(76)), V(uint16(76))},
{V(uint16(77)), V(uint64(77))},
{V(uint64(78)), V(uint16(78))},
{V(uint16(79)), V(int(79))},
{V(int(80)), V(uint16(80))},
{V(uint16(81)), V(uint(81))},
{V(uint(82)), V(uint16(82))},
{V(uint16(83)), V(uintptr(83))},
{V(uintptr(84)), V(uint16(84))},
{V(uint16(85)), V(float32(85))},
{V(float32(86)), V(uint16(86))},
{V(uint16(87)), V(float64(87))},
{V(float64(88)), V(uint16(88))},
{V(int32(89)), V(int32(89))},
{V(int32(90)), V(uint32(90))},
{V(uint32(91)), V(int32(91))},
{V(int32(92)), V(int64(92))},
{V(int64(93)), V(int32(93))},
{V(int32(94)), V(uint64(94))},
{V(uint64(95)), V(int32(95))},
{V(int32(96)), V(int(96))},
{V(int(97)), V(int32(97))},
{V(int32(98)), V(uint(98))},
{V(uint(99)), V(int32(99))},
{V(int32(100)), V(uintptr(100))},
{V(uintptr(101)), V(int32(101))},
{V(int32(102)), V(float32(102))},
{V(float32(103)), V(int32(103))},
{V(int32(104)), V(float64(104))},
{V(float64(105)), V(int32(105))},
{V(uint32(106)), V(uint32(106))},
{V(uint32(107)), V(int64(107))},
{V(int64(108)), V(uint32(108))},
{V(uint32(109)), V(uint64(109))},
{V(uint64(110)), V(uint32(110))},
{V(uint32(111)), V(int(111))},
{V(int(112)), V(uint32(112))},
{V(uint32(113)), V(uint(113))},
{V(uint(114)), V(uint32(114))},
{V(uint32(115)), V(uintptr(115))},
{V(uintptr(116)), V(uint32(116))},
{V(uint32(117)), V(float32(117))},
{V(float32(118)), V(uint32(118))},
{V(uint32(119)), V(float64(119))},
{V(float64(120)), V(uint32(120))},
{V(int64(121)), V(int64(121))},
{V(int64(122)), V(uint64(122))},
{V(uint64(123)), V(int64(123))},
{V(int64(124)), V(int(124))},
{V(int(125)), V(int64(125))},
{V(int64(126)), V(uint(126))},
{V(uint(127)), V(int64(127))},
{V(int64(128)), V(uintptr(128))},
{V(uintptr(129)), V(int64(129))},
{V(int64(130)), V(float32(130))},
{V(float32(131)), V(int64(131))},
{V(int64(132)), V(float64(132))},
{V(float64(133)), V(int64(133))},
{V(uint64(134)), V(uint64(134))},
{V(uint64(135)), V(int(135))},
{V(int(136)), V(uint64(136))},
{V(uint64(137)), V(uint(137))},
{V(uint(138)), V(uint64(138))},
{V(uint64(139)), V(uintptr(139))},
{V(uintptr(140)), V(uint64(140))},
{V(uint64(141)), V(float32(141))},
{V(float32(142)), V(uint64(142))},
{V(uint64(143)), V(float64(143))},
{V(float64(144)), V(uint64(144))},
{V(int(145)), V(int(145))},
{V(int(146)), V(uint(146))},
{V(uint(147)), V(int(147))},
{V(int(148)), V(uintptr(148))},
{V(uintptr(149)), V(int(149))},
{V(int(150)), V(float32(150))},
{V(float32(151)), V(int(151))},
{V(int(152)), V(float64(152))},
{V(float64(153)), V(int(153))},
{V(uint(154)), V(uint(154))},
{V(uint(155)), V(uintptr(155))},
{V(uintptr(156)), V(uint(156))},
{V(uint(157)), V(float32(157))},
{V(float32(158)), V(uint(158))},
{V(uint(159)), V(float64(159))},
{V(float64(160)), V(uint(160))},
{V(uintptr(161)), V(uintptr(161))},
{V(uintptr(162)), V(float32(162))},
{V(float32(163)), V(uintptr(163))},
{V(uintptr(164)), V(float64(164))},
{V(float64(165)), V(uintptr(165))},
{V(float32(166)), V(float32(166))},
{V(float32(167)), V(float64(167))},
{V(float64(168)), V(float32(168))},
{V(float64(169)), V(float64(169))},
// truncation
{V(float64(1.5)), V(int(1))},
// complex
{V(complex64(1i)), V(complex64(1i))},
{V(complex64(2i)), V(complex128(2i))},
{V(complex128(3i)), V(complex64(3i))},
{V(complex128(4i)), V(complex128(4i))},
// string
{V(string("hello")), V(string("hello"))},
{V(string("bytes1")), V([]byte("bytes1"))},
{V([]byte("bytes2")), V(string("bytes2"))},
{V([]byte("bytes3")), V([]byte("bytes3"))},
{V(string("runes♝")), V([]rune("runes♝"))},
{V([]rune("runes♕")), V(string("runes♕"))},
{V([]rune("runes🙈🙉🙊")), V([]rune("runes🙈🙉🙊"))},
{V(int('a')), V(string("a"))},
{V(int8('a')), V(string("a"))},
{V(int16('a')), V(string("a"))},
{V(int32('a')), V(string("a"))},
{V(int64('a')), V(string("a"))},
{V(uint('a')), V(string("a"))},
{V(uint8('a')), V(string("a"))},
{V(uint16('a')), V(string("a"))},
{V(uint32('a')), V(string("a"))},
{V(uint64('a')), V(string("a"))},
{V(uintptr('a')), V(string("a"))},
{V(int(-1)), V(string("\uFFFD"))},
{V(int8(-2)), V(string("\uFFFD"))},
{V(int16(-3)), V(string("\uFFFD"))},
{V(int32(-4)), V(string("\uFFFD"))},
{V(int64(-5)), V(string("\uFFFD"))},
{V(int64(-1 << 32)), V(string("\uFFFD"))},
{V(int64(1 << 32)), V(string("\uFFFD"))},
{V(uint(0x110001)), V(string("\uFFFD"))},
{V(uint32(0x110002)), V(string("\uFFFD"))},
{V(uint64(0x110003)), V(string("\uFFFD"))},
{V(uint64(1 << 32)), V(string("\uFFFD"))},
{V(uintptr(0x110004)), V(string("\uFFFD"))},
// named string
{V(MyString("hello")), V(string("hello"))},
{V(string("hello")), V(MyString("hello"))},
{V(string("hello")), V(string("hello"))},
{V(MyString("hello")), V(MyString("hello"))},
{V(MyString("bytes1")), V([]byte("bytes1"))},
{V([]byte("bytes2")), V(MyString("bytes2"))},
{V([]byte("bytes3")), V([]byte("bytes3"))},
{V(MyString("runes♝")), V([]rune("runes♝"))},
{V([]rune("runes♕")), V(MyString("runes♕"))},
{V([]rune("runes🙈🙉🙊")), V([]rune("runes🙈🙉🙊"))},
{V([]rune("runes🙈🙉🙊")), V(MyRunes("runes🙈🙉🙊"))},
{V(MyRunes("runes🙈🙉🙊")), V([]rune("runes🙈🙉🙊"))},
{V(int('a')), V(MyString("a"))},
{V(int8('a')), V(MyString("a"))},
{V(int16('a')), V(MyString("a"))},
{V(int32('a')), V(MyString("a"))},
{V(int64('a')), V(MyString("a"))},
{V(uint('a')), V(MyString("a"))},
{V(uint8('a')), V(MyString("a"))},
{V(uint16('a')), V(MyString("a"))},
{V(uint32('a')), V(MyString("a"))},
{V(uint64('a')), V(MyString("a"))},
{V(uintptr('a')), V(MyString("a"))},
{V(int(-1)), V(MyString("\uFFFD"))},
{V(int8(-2)), V(MyString("\uFFFD"))},
{V(int16(-3)), V(MyString("\uFFFD"))},
{V(int32(-4)), V(MyString("\uFFFD"))},
{V(int64(-5)), V(MyString("\uFFFD"))},
{V(uint(0x110001)), V(MyString("\uFFFD"))},
{V(uint32(0x110002)), V(MyString("\uFFFD"))},
{V(uint64(0x110003)), V(MyString("\uFFFD"))},
{V(uintptr(0x110004)), V(MyString("\uFFFD"))},
// named []byte
{V(string("bytes1")), V(MyBytes("bytes1"))},
{V(MyBytes("bytes2")), V(string("bytes2"))},
{V(MyBytes("bytes3")), V(MyBytes("bytes3"))},
{V(MyString("bytes1")), V(MyBytes("bytes1"))},
{V(MyBytes("bytes2")), V(MyString("bytes2"))},
// named []rune
{V(string("runes♝")), V(MyRunes("runes♝"))},
{V(MyRunes("runes♕")), V(string("runes♕"))},
{V(MyRunes("runes🙈🙉🙊")), V(MyRunes("runes🙈🙉🙊"))},
{V(MyString("runes♝")), V(MyRunes("runes♝"))},
{V(MyRunes("runes♕")), V(MyString("runes♕"))},
// slice to array pointer
{V([]byte(nil)), V((*[0]byte)(nil))},
{V([]byte{}), V(new([0]byte))},
{V([]byte{7}), V(&[1]byte{7})},
{V(MyBytes([]byte(nil))), V((*[0]byte)(nil))},
{V(MyBytes([]byte{})), V(new([0]byte))},
{V(MyBytes([]byte{9})), V(&[1]byte{9})},
{V([]byte(nil)), V(MyBytesArrayPtr0(nil))},
{V([]byte{}), V(MyBytesArrayPtr0(new([0]byte)))},
{V([]byte{1, 2, 3, 4}), V(MyBytesArrayPtr(&[4]byte{1, 2, 3, 4}))},
{V(MyBytes([]byte{})), V(MyBytesArrayPtr0(new([0]byte)))},
{V(MyBytes([]byte{5, 6, 7, 8})), V(MyBytesArrayPtr(&[4]byte{5, 6, 7, 8}))},
{V([]byte(nil)), V((*MyBytesArray0)(nil))},
{V([]byte{}), V((*MyBytesArray0)(new([0]byte)))},
{V([]byte{1, 2, 3, 4}), V(&MyBytesArray{1, 2, 3, 4})},
{V(MyBytes([]byte(nil))), V((*MyBytesArray0)(nil))},
{V(MyBytes([]byte{})), V((*MyBytesArray0)(new([0]byte)))},
{V(MyBytes([]byte{5, 6, 7, 8})), V(&MyBytesArray{5, 6, 7, 8})},
{V(new([0]byte)), V(new(MyBytesArray0))},
{V(new(MyBytesArray0)), V(new([0]byte))},
{V(MyBytesArrayPtr0(nil)), V((*[0]byte)(nil))},
{V((*[0]byte)(nil)), V(MyBytesArrayPtr0(nil))},
// named types and equal underlying types
{V(new(int)), V(new(integer))},
{V(new(integer)), V(new(int))},
{V(Empty{}), V(struct{}{})},
{V(new(Empty)), V(new(struct{}))},
{V(struct{}{}), V(Empty{})},
{V(new(struct{})), V(new(Empty))},
{V(Empty{}), V(Empty{})},
{V(MyBytes{}), V([]byte{})},
{V([]byte{}), V(MyBytes{})},
{V((func())(nil)), V(MyFunc(nil))},
{V((MyFunc)(nil)), V((func())(nil))},
// structs with different tags
{V(struct {
x int `some:"foo"`
}{}), V(struct {
x int `some:"bar"`
}{})},
{V(struct {
x int `some:"bar"`
}{}), V(struct {
x int `some:"foo"`
}{})},
{V(MyStruct{}), V(struct {
x int `some:"foo"`
}{})},
{V(struct {
x int `some:"foo"`
}{}), V(MyStruct{})},
{V(MyStruct{}), V(struct {
x int `some:"bar"`
}{})},
{V(struct {
x int `some:"bar"`
}{}), V(MyStruct{})},
{V(MyStruct1{}), V(MyStruct2{})},
{V(MyStruct2{}), V(MyStruct1{})},
// can convert *byte and *MyByte
{V((*byte)(nil)), V((*MyByte)(nil))},
{V((*MyByte)(nil)), V((*byte)(nil))},
// cannot convert mismatched array sizes
{V([2]byte{}), V([2]byte{})},
{V([3]byte{}), V([3]byte{})},
// cannot convert other instances
{V((**byte)(nil)), V((**byte)(nil))},
{V((**MyByte)(nil)), V((**MyByte)(nil))},
{V((chan byte)(nil)), V((chan byte)(nil))},
{V((chan MyByte)(nil)), V((chan MyByte)(nil))},
{V(([]byte)(nil)), V(([]byte)(nil))},
{V(([]MyByte)(nil)), V(([]MyByte)(nil))},
{V((map[int]byte)(nil)), V((map[int]byte)(nil))},
{V((map[int]MyByte)(nil)), V((map[int]MyByte)(nil))},
{V((map[byte]int)(nil)), V((map[byte]int)(nil))},
{V((map[MyByte]int)(nil)), V((map[MyByte]int)(nil))},
{V([2]byte{}), V([2]byte{})},
{V([2]MyByte{}), V([2]MyByte{})},
// other
{V((***int)(nil)), V((***int)(nil))},
{V((***byte)(nil)), V((***byte)(nil))},
{V((***int32)(nil)), V((***int32)(nil))},
{V((***int64)(nil)), V((***int64)(nil))},
{V((chan byte)(nil)), V((chan byte)(nil))},
{V((chan MyByte)(nil)), V((chan MyByte)(nil))},
{V((map[int]bool)(nil)), V((map[int]bool)(nil))},
{V((map[int]byte)(nil)), V((map[int]byte)(nil))},
{V((map[uint]bool)(nil)), V((map[uint]bool)(nil))},
{V([]uint(nil)), V([]uint(nil))},
{V([]int(nil)), V([]int(nil))},
{V(new(interface{})), V(new(interface{}))},
{V(new(io.Reader)), V(new(io.Reader))},
{V(new(io.Writer)), V(new(io.Writer))},
// channels
{V(IntChan(nil)), V((chan<- int)(nil))},
{V(IntChan(nil)), V((<-chan int)(nil))},
{V((chan int)(nil)), V(IntChanRecv(nil))},
{V((chan int)(nil)), V(IntChanSend(nil))},
{V(IntChanRecv(nil)), V((<-chan int)(nil))},
{V((<-chan int)(nil)), V(IntChanRecv(nil))},
{V(IntChanSend(nil)), V((chan<- int)(nil))},
{V((chan<- int)(nil)), V(IntChanSend(nil))},
{V(IntChan(nil)), V((chan int)(nil))},
{V((chan int)(nil)), V(IntChan(nil))},
{V((chan int)(nil)), V((<-chan int)(nil))},
{V((chan int)(nil)), V((chan<- int)(nil))},
{V(BytesChan(nil)), V((chan<- []byte)(nil))},
{V(BytesChan(nil)), V((<-chan []byte)(nil))},
{V((chan []byte)(nil)), V(BytesChanRecv(nil))},
{V((chan []byte)(nil)), V(BytesChanSend(nil))},
{V(BytesChanRecv(nil)), V((<-chan []byte)(nil))},
{V((<-chan []byte)(nil)), V(BytesChanRecv(nil))},
{V(BytesChanSend(nil)), V((chan<- []byte)(nil))},
{V((chan<- []byte)(nil)), V(BytesChanSend(nil))},
{V(BytesChan(nil)), V((chan []byte)(nil))},
{V((chan []byte)(nil)), V(BytesChan(nil))},
{V((chan []byte)(nil)), V((<-chan []byte)(nil))},
{V((chan []byte)(nil)), V((chan<- []byte)(nil))},
// cannot convert other instances (channels)
{V(IntChan(nil)), V(IntChan(nil))},
{V(IntChanRecv(nil)), V(IntChanRecv(nil))},
{V(IntChanSend(nil)), V(IntChanSend(nil))},
{V(BytesChan(nil)), V(BytesChan(nil))},
{V(BytesChanRecv(nil)), V(BytesChanRecv(nil))},
{V(BytesChanSend(nil)), V(BytesChanSend(nil))},
// interfaces
{V(int(1)), EmptyInterfaceV(int(1))},
{V(string("hello")), EmptyInterfaceV(string("hello"))},
{V(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))},
{ReadWriterV(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))},
{V(new(bytes.Buffer)), ReadWriterV(new(bytes.Buffer))},
}
func TestConvert(t *testing.T) {
canConvert := map[[2]Type]bool{}
all := map[Type]bool{}
for _, tt := range convertTests {
t1 := tt.in.Type()
if !t1.ConvertibleTo(t1) {
t.Errorf("(%s).ConvertibleTo(%s) = false, want true", t1, t1)
continue
}
t2 := tt.out.Type()
if !t1.ConvertibleTo(t2) {
t.Errorf("(%s).ConvertibleTo(%s) = false, want true", t1, t2)
continue
}
all[t1] = true
all[t2] = true
canConvert[[2]Type{t1, t2}] = true
// vout1 represents the in value converted to the in type.
v1 := tt.in
if !v1.CanConvert(t1) {
t.Errorf("ValueOf(%T(%[1]v)).CanConvert(%s) = false, want true", tt.in.Interface(), t1)
}
vout1 := v1.Convert(t1)
out1 := vout1.Interface()
if vout1.Type() != tt.in.Type() || !DeepEqual(out1, tt.in.Interface()) {
t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t1, out1, tt.in.Interface())
}
// vout2 represents the in value converted to the out type.
if !v1.CanConvert(t2) {
t.Errorf("ValueOf(%T(%[1]v)).CanConvert(%s) = false, want true", tt.in.Interface(), t2)
}
vout2 := v1.Convert(t2)
out2 := vout2.Interface()
if vout2.Type() != tt.out.Type() || !DeepEqual(out2, tt.out.Interface()) {
t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t2, out2, tt.out.Interface())
}
if got, want := vout2.Kind(), vout2.Type().Kind(); got != want {
t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) has internal kind %v want %v", tt.in.Interface(), t1, got, want)
}
// vout3 represents a new value of the out type, set to vout2. This makes
// sure the converted value vout2 is really usable as a regular value.
vout3 := New(t2).Elem()
vout3.Set(vout2)
out3 := vout3.Interface()
if vout3.Type() != tt.out.Type() || !DeepEqual(out3, tt.out.Interface()) {
t.Errorf("Set(ValueOf(%T(%[1]v)).Convert(%s)) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t2, out3, tt.out.Interface())
}
if IsRO(v1) {
t.Errorf("table entry %v is RO, should not be", v1)
}
if IsRO(vout1) {
t.Errorf("self-conversion output %v is RO, should not be", vout1)
}
if IsRO(vout2) {
t.Errorf("conversion output %v is RO, should not be", vout2)
}
if IsRO(vout3) {
t.Errorf("set(conversion output) %v is RO, should not be", vout3)
}
if !IsRO(MakeRO(v1).Convert(t1)) {
t.Errorf("RO self-conversion output %v is not RO, should be", v1)
}
if !IsRO(MakeRO(v1).Convert(t2)) {
t.Errorf("RO conversion output %v is not RO, should be", v1)
}
}
// Assume that of all the types we saw during the tests,