summaryrefslogtreecommitdiff
path: root/src/reflect
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2014-10-08 15:48:46 -0700
committerIan Lance Taylor <iant@golang.org>2014-10-08 15:48:46 -0700
commitd5169fcdbca1592b8c6c7565c6b7f0e85f5deef2 (patch)
tree3445fd3e6a889142678e9836197a115443d6ae76 /src/reflect
parent2212223399bf5410ae9bffc56253cc9afc454d5b (diff)
downloadgo-d5169fcdbca1592b8c6c7565c6b7f0e85f5deef2.tar.gz
reflect: add tests for variadic method calls
These tests fail when using gccgo. In gccgo using Interface on the value of a method function is implemented using a variant of MakeFunc. That approach did not correctly handle variadic functions. LGTM=r R=golang-codereviews, r CC=golang-codereviews https://codereview.appspot.com/151280043
Diffstat (limited to 'src/reflect')
-rw-r--r--src/reflect/all_test.go54
1 files changed, 49 insertions, 5 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index d17ef5c5e..f13b91b74 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -1569,6 +1569,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}
@@ -1751,6 +1769,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.
@@ -3770,11 +3819,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")