blob: 7e7b1ff3b96f0ff2c5db5e4780ff71bc6a34f60b [file] [log] [blame]
Ian Lance Taylor59e7a022012-02-03 16:38:59 -08001// $G $D/method4a.go && $G $D/$F.go && $L $F.$A && ./$A.out
2
Russ Coxcd22afa2012-09-23 13:16:14 -04003// NOTE: This test is not run by 'run.go' and so not run by all.bash.
4// To run this test you must use the ./run shell script.
5
Ian Lance Taylor59e7a022012-02-03 16:38:59 -08006// Copyright 2012 The Go Authors. All rights reserved.
7// Use of this source code is governed by a BSD-style
8// license that can be found in the LICENSE file.
9
10// Test method expressions with arguments.
11
12package main
13
14import "./method4a"
15
16type T1 int
17
18type T2 struct {
19 f int
20}
21
22type I1 interface {
23 Sum([]int, int) int
24}
25
26type I2 interface {
27 Sum(a []int, b int) int
28}
29
30func (i T1) Sum(a []int, b int) int {
31 r := int(i) + b
32 for _, v := range a {
33 r += v
34 }
35 return r
36}
37
38func (p *T2) Sum(a []int, b int) int {
39 r := p.f + b
40 for _, v := range a {
41 r += v
42 }
43 return r
44}
45
46func eq(v1, v2 int) {
47 if v1 != v2 {
48 panic(0)
49 }
50}
51
52func main() {
53 a := []int{1, 2, 3}
54 t1 := T1(4)
55 t2 := &T2{4}
56
57 eq(t1.Sum(a, 5), 15)
58 eq(t2.Sum(a, 6), 16)
59
60 eq(T1.Sum(t1, a, 7), 17)
61 eq((*T2).Sum(t2, a, 8), 18)
62
63 f1 := T1.Sum
64 eq(f1(t1, a, 9), 19)
65 f2 := (*T2).Sum
66 eq(f2(t2, a, 10), 20)
67
68 eq(I1.Sum(t1, a, 11), 21)
69 eq(I1.Sum(t2, a, 12), 22)
70
71 f3 := I1.Sum
72 eq(f3(t1, a, 13), 23)
73 eq(f3(t2, a, 14), 24)
74
75 eq(I2.Sum(t1, a, 15), 25)
76 eq(I2.Sum(t2, a, 16), 26)
77
78 f4 := I2.Sum
79 eq(f4(t1, a, 17), 27)
80 eq(f4(t2, a, 18), 28)
81
82 mt1 := method4a.T1(4)
83 mt2 := &method4a.T2{4}
84
85 eq(mt1.Sum(a, 30), 40)
86 eq(mt2.Sum(a, 31), 41)
87
88 eq(method4a.T1.Sum(mt1, a, 32), 42)
89 eq((*method4a.T2).Sum(mt2, a, 33), 43)
90
91 g1 := method4a.T1.Sum
92 eq(g1(mt1, a, 34), 44)
93 g2 := (*method4a.T2).Sum
94 eq(g2(mt2, a, 35), 45)
95
96 eq(method4a.I1.Sum(mt1, a, 36), 46)
97 eq(method4a.I1.Sum(mt2, a, 37), 47)
98
99 g3 := method4a.I1.Sum
100 eq(g3(mt1, a, 38), 48)
101 eq(g3(mt2, a, 39), 49)
102
103 eq(method4a.I2.Sum(mt1, a, 40), 50)
104 eq(method4a.I2.Sum(mt2, a, 41), 51)
105
106 g4 := method4a.I2.Sum
107 eq(g4(mt1, a, 42), 52)
108 eq(g4(mt2, a, 43), 53)
109}