Home | History | Annotate | Download | only in interface
      1 // run
      2 
      3 // Copyright 2009 The Go Authors. All rights reserved.
      4 // Use of this source code is governed by a BSD-style
      5 // license that can be found in the LICENSE file.
      6 
      7 // Test big vs. small, pointer vs. value interface methods.
      8 
      9 package main
     10 
     11 type I interface { M() int64 }
     12 
     13 type BigPtr struct { a, b, c, d int64 }
     14 func (z *BigPtr) M() int64 { return z.a+z.b+z.c+z.d }
     15 
     16 type SmallPtr struct { a int32 }
     17 func (z *SmallPtr) M() int64 { return int64(z.a) }
     18 
     19 type IntPtr int32
     20 func (z *IntPtr) M() int64 { return int64(*z) }
     21 
     22 var bad bool
     23 
     24 func test(name string, i I) {
     25 	m := i.M()
     26 	if m != 12345 {
     27 		println(name, m)
     28 		bad = true
     29 	}
     30 }
     31 
     32 func ptrs() {
     33 	var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 }
     34 	var smallptr SmallPtr = SmallPtr{ 12345 }
     35 	var intptr IntPtr = 12345
     36 
     37 //	test("bigptr", bigptr)
     38 	test("&bigptr", &bigptr)
     39 //	test("smallptr", smallptr)
     40 	test("&smallptr", &smallptr)
     41 //	test("intptr", intptr)
     42 	test("&intptr", &intptr)
     43 }
     44 
     45 type Big struct { a, b, c, d int64 }
     46 func (z Big) M() int64 { return z.a+z.b+z.c+z.d }
     47 
     48 type Small struct { a int32 }
     49 func (z Small) M() int64 { return int64(z.a) }
     50 
     51 type Int int32
     52 func (z Int) M() int64 { return int64(z) }
     53 
     54 func nonptrs() {
     55 	var big Big = Big{ 10000, 2000, 300, 45 }
     56 	var small Small = Small{ 12345 }
     57 	var int Int = 12345
     58 
     59 	test("big", big)
     60 	test("&big", &big)
     61 	test("small", small)
     62 	test("&small", &small)
     63 	test("int", int)
     64 	test("&int", &int)
     65 }
     66 
     67 func main() {
     68 	ptrs()
     69 	nonptrs()
     70 
     71 	if bad {
     72 		println("BUG: interface4")
     73 	}
     74 }
     75