[dev.power64] all: merge default into dev.power64

This brings dev.power64 up-to-date with the current tip of
default.  go_bootstrap is still panicking with a bad defer
when initializing the runtime (even on amd64).

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/152570049
This commit is contained in:
Austin Clements 2014-10-22 15:51:54 -04:00
commit f0bd539c59
663 changed files with 60112 additions and 34009 deletions

View file

@ -1548,7 +1548,17 @@ func TestMakeFuncVariadic(t *testing.T) {
fv := MakeFunc(TypeOf(fn), func(in []Value) []Value { return in[1:2] })
ValueOf(&fn).Elem().Set(fv)
r := fv.Call([]Value{ValueOf(1), ValueOf(2), ValueOf(3)})[0].Interface().([]int)
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])
}
@ -1557,6 +1567,17 @@ func TestMakeFuncVariadic(t *testing.T) {
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])
}
}
type Point struct {
@ -1574,6 +1595,24 @@ func (p Point) Dist(scale int) int {
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) 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
}
func TestMethod(t *testing.T) {
// Non-curried method of type.
p := Point{3, 4}
@ -1756,6 +1795,37 @@ func TestMethodValue(t *testing.T) {
}
}
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]))
// Curried method of value.
tfunc := TypeOf((func(...Point) int)(nil))
v := ValueOf(p).Method(3)
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)
}
}
// Reflect version of $GOROOT/test/method5.go
// Concrete types implementing M method.
@ -2437,10 +2507,21 @@ func TestAllocations(t *testing.T) {
noAlloc(t, 100, func(j int) {
var i interface{}
var v Value
i = 42 + j
// 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 int(v.Int()) != 42+j {
panic("wrong int")
if v.Interface().(func(int) int)(j) != j {
panic("wrong result")
}
})
}
@ -2613,6 +2694,26 @@ func TestFuncArg(t *testing.T) {
}
}
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
@ -3190,6 +3291,44 @@ func TestConvert(t *testing.T) {
}
}
type ComparableStruct struct {
X int
}
type NonComparableStruct struct {
X int
Y map[string]int
}
var comparableTests = []struct {
typ Type
ok bool
}{
{TypeOf(1), true},
{TypeOf("hello"), true},
{TypeOf(new(byte)), true},
{TypeOf((func())(nil)), false},
{TypeOf([]byte{}), false},
{TypeOf(map[string]int{}), false},
{TypeOf(make(chan int)), true},
{TypeOf(1.5), true},
{TypeOf(false), true},
{TypeOf(1i), true},
{TypeOf(ComparableStruct{}), true},
{TypeOf(NonComparableStruct{}), false},
{TypeOf([10]map[string]int{}), false},
{TypeOf([10]string{}), true},
{TypeOf(new(interface{})).Elem(), true},
}
func TestComparable(t *testing.T) {
for _, tt := range comparableTests {
if ok := tt.typ.Comparable(); ok != tt.ok {
t.Errorf("TypeOf(%v).Comparable() = %v, want %v", tt.typ, ok, tt.ok)
}
}
}
func TestOverflow(t *testing.T) {
if ovf := V(float64(0)).OverflowFloat(1e300); ovf {
t.Errorf("%v wrongly overflows float64", 1e300)
@ -3737,11 +3876,6 @@ func TestReflectFuncTraceback(t *testing.T) {
f.Call([]Value{})
}
func (p Point) GCMethod(k int) int {
runtime.GC()
return k + p.x
}
func TestReflectMethodTraceback(t *testing.T) {
p := Point{3, 4}
m := ValueOf(p).MethodByName("GCMethod")
@ -3865,3 +3999,64 @@ func TestCallMethodJump(t *testing.T) {
// Stop garbage collecting during reflect.call.
*CallGC = false
}
func TestMakeFuncStackCopy(t *testing.T) {
target := func(in []Value) []Value {
runtime.GC()
useStack(16)
return []Value{ValueOf(9)}
}
var concrete func(*int, int) int
fn := MakeFunc(ValueOf(concrete).Type(), target)
ValueOf(&concrete).Elem().Set(fn)
x := concrete(nil, 7)
if x != 9 {
t.Errorf("have %#q want 9", x)
}
}
// use about n KB of stack
func useStack(n int) {
if n == 0 {
return
}
var b [1024]byte // makes frame about 1KB
useStack(n - 1 + int(b[99]))
}
type Impl struct{}
func (Impl) f() {}
func TestValueString(t *testing.T) {
rv := ValueOf(Impl{})
if rv.String() != "<reflect_test.Impl Value>" {
t.Errorf("ValueOf(Impl{}).String() = %q, want %q", rv.String(), "<reflect_test.Impl Value>")
}
method := rv.Method(0)
if method.String() != "<func() Value>" {
t.Errorf("ValueOf(Impl{}).Method(0).String() = %q, want %q", method.String(), "<func() Value>")
}
}
func TestInvalid(t *testing.T) {
// Used to have inconsistency between IsValid() and Kind() != Invalid.
type T struct{ v interface{} }
v := ValueOf(T{}).Field(0)
if v.IsValid() != true || v.Kind() != Interface {
t.Errorf("field: IsValid=%v, Kind=%v, want true, Interface", v.IsValid(), v.Kind())
}
v = v.Elem()
if v.IsValid() != false || v.Kind() != Invalid {
t.Errorf("field elem: IsValid=%v, Kind=%v, want false, Invalid", v.IsValid(), v.Kind())
}
}
// Issue 8917.
func TestLargeGCProg(t *testing.T) {
fv := ValueOf(func([256]*byte) {})
fv.Call([]Value{ValueOf([256]*byte{})})
}