Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 1 | // $G $D/$F.go && $L $F.$A && ./$A.out |
| 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 | // check that big vs small, pointer vs not |
| 8 | // interface methods work. |
| 9 | |
| 10 | package main |
| 11 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 12 | type I interface { M() int64 } |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 13 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 14 | type BigPtr struct { a, b, c, d int64 } |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 15 | func (z *BigPtr) M() int64 { return z.a+z.b+z.c+z.d } |
| 16 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 17 | type SmallPtr struct { a int32 } |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 18 | func (z *SmallPtr) M() int64 { return int64(z.a) } |
| 19 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 20 | type IntPtr int32 |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 21 | func (z *IntPtr) M() int64 { return int64(*z) } |
| 22 | |
| 23 | var bad bool |
| 24 | |
| 25 | func test(name string, i I) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 26 | m := i.M() |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 27 | if m != 12345 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 28 | println(name, m) |
| 29 | bad = true |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | func ptrs() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 34 | var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 } |
| 35 | var smallptr SmallPtr = SmallPtr{ 12345 } |
| 36 | var intptr IntPtr = 12345 |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 37 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 38 | // test("bigptr", bigptr) |
| 39 | test("&bigptr", &bigptr) |
| 40 | // test("smallptr", smallptr) |
| 41 | test("&smallptr", &smallptr) |
| 42 | // test("intptr", intptr) |
| 43 | test("&intptr", &intptr) |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 46 | type Big struct { a, b, c, d int64 } |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 47 | func (z Big) M() int64 { return z.a+z.b+z.c+z.d } |
| 48 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 49 | type Small struct { a int32 } |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 50 | func (z Small) M() int64 { return int64(z.a) } |
| 51 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 52 | type Int int32 |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 53 | func (z Int) M() int64 { return int64(z) } |
| 54 | |
| 55 | func nonptrs() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 56 | var big Big = Big{ 10000, 2000, 300, 45 } |
| 57 | var small Small = Small{ 12345 } |
| 58 | var int Int = 12345 |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 59 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 60 | test("big", big) |
| 61 | test("&big", &big) |
| 62 | test("small", small) |
| 63 | test("&small", &small) |
| 64 | test("int", int) |
| 65 | test("&int", &int) |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 69 | ptrs() |
| 70 | nonptrs() |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 71 | |
| 72 | if bad { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 73 | println("BUG: interface4") |
Russ Cox | e512481 | 2009-01-08 18:06:06 -0800 | [diff] [blame] | 74 | } |
| 75 | } |