Home | History | Annotate | Download | only in method4.dir
      1 // Copyright 2012 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // Test method expressions with arguments.
      6 
      7 package main
      8 
      9 import "./method4a"
     10 
     11 type T1 int
     12 
     13 type T2 struct {
     14 	f int
     15 }
     16 
     17 type I1 interface {
     18 	Sum([]int, int) int
     19 }
     20 
     21 type I2 interface {
     22 	Sum(a []int, b int) int
     23 }
     24 
     25 func (i T1) Sum(a []int, b int) int {
     26 	r := int(i) + b
     27 	for _, v := range a {
     28 		r += v
     29 	}
     30 	return r
     31 }
     32 
     33 func (p *T2) Sum(a []int, b int) int {
     34 	r := p.f + b
     35 	for _, v := range a {
     36 		r += v
     37 	}
     38 	return r
     39 }
     40 
     41 func eq(v1, v2 int) {
     42 	if v1 != v2 {
     43 		panic(0)
     44 	}
     45 }
     46 
     47 func main() {
     48 	a := []int{1, 2, 3}
     49 	t1 := T1(4)
     50 	t2 := &T2{4}
     51 
     52 	eq(t1.Sum(a, 5), 15)
     53 	eq(t2.Sum(a, 6), 16)
     54 
     55 	eq(T1.Sum(t1, a, 7), 17)
     56 	eq((*T2).Sum(t2, a, 8), 18)
     57 
     58 	f1 := T1.Sum
     59 	eq(f1(t1, a, 9), 19)
     60 	f2 := (*T2).Sum
     61 	eq(f2(t2, a, 10), 20)
     62 
     63 	eq(I1.Sum(t1, a, 11), 21)
     64 	eq(I1.Sum(t2, a, 12), 22)
     65 
     66 	f3 := I1.Sum
     67 	eq(f3(t1, a, 13), 23)
     68 	eq(f3(t2, a, 14), 24)
     69 
     70 	eq(I2.Sum(t1, a, 15), 25)
     71 	eq(I2.Sum(t2, a, 16), 26)
     72 
     73 	f4 := I2.Sum
     74 	eq(f4(t1, a, 17), 27)
     75 	eq(f4(t2, a, 18), 28)
     76 
     77 	// issue 6723
     78 	f5 := (interface {
     79 		I2
     80 	}).Sum
     81 	eq(f5(t1, a, 19), 29)
     82 	eq(f5(t2, a, 20), 30)
     83 
     84 	mt1 := method4a.T1(4)
     85 	mt2 := &method4a.T2{4}
     86 
     87 	eq(mt1.Sum(a, 30), 40)
     88 	eq(mt2.Sum(a, 31), 41)
     89 
     90 	eq(method4a.T1.Sum(mt1, a, 32), 42)
     91 	eq((*method4a.T2).Sum(mt2, a, 33), 43)
     92 
     93 	g1 := method4a.T1.Sum
     94 	eq(g1(mt1, a, 34), 44)
     95 	g2 := (*method4a.T2).Sum
     96 	eq(g2(mt2, a, 35), 45)
     97 
     98 	eq(method4a.I1.Sum(mt1, a, 36), 46)
     99 	eq(method4a.I1.Sum(mt2, a, 37), 47)
    100 
    101 	g3 := method4a.I1.Sum
    102 	eq(g3(mt1, a, 38), 48)
    103 	eq(g3(mt2, a, 39), 49)
    104 
    105 	eq(method4a.I2.Sum(mt1, a, 40), 50)
    106 	eq(method4a.I2.Sum(mt2, a, 41), 51)
    107 
    108 	g4 := method4a.I2.Sum
    109 	eq(g4(mt1, a, 42), 52)
    110 	eq(g4(mt2, a, 43), 53)
    111 }
    112