blob: 44f6ab127ea38026b0ac396aa8e8500c09065d67 [file] [log] [blame]
Russ Coxe5124812009-01-08 18:06:06 -08001// $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
10package main
11
Russ Cox839a6842009-01-20 14:40:40 -080012type I interface { M() int64 }
Russ Coxe5124812009-01-08 18:06:06 -080013
Russ Cox839a6842009-01-20 14:40:40 -080014type BigPtr struct { a, b, c, d int64 }
Russ Coxe5124812009-01-08 18:06:06 -080015func (z *BigPtr) M() int64 { return z.a+z.b+z.c+z.d }
16
Russ Cox839a6842009-01-20 14:40:40 -080017type SmallPtr struct { a int32 }
Russ Coxe5124812009-01-08 18:06:06 -080018func (z *SmallPtr) M() int64 { return int64(z.a) }
19
Russ Cox839a6842009-01-20 14:40:40 -080020type IntPtr int32
Russ Coxe5124812009-01-08 18:06:06 -080021func (z *IntPtr) M() int64 { return int64(*z) }
22
23var bad bool
24
25func test(name string, i I) {
Rob Pike4f61fc92010-09-04 10:36:13 +100026 m := i.M()
Russ Coxe5124812009-01-08 18:06:06 -080027 if m != 12345 {
Rob Pike4f61fc92010-09-04 10:36:13 +100028 println(name, m)
29 bad = true
Russ Coxe5124812009-01-08 18:06:06 -080030 }
31}
32
33func ptrs() {
Rob Pike4f61fc92010-09-04 10:36:13 +100034 var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 }
35 var smallptr SmallPtr = SmallPtr{ 12345 }
36 var intptr IntPtr = 12345
Russ Coxe5124812009-01-08 18:06:06 -080037
Rob Pike4f61fc92010-09-04 10:36:13 +100038// test("bigptr", bigptr)
39 test("&bigptr", &bigptr)
40// test("smallptr", smallptr)
41 test("&smallptr", &smallptr)
42// test("intptr", intptr)
43 test("&intptr", &intptr)
Russ Coxe5124812009-01-08 18:06:06 -080044}
45
Russ Cox839a6842009-01-20 14:40:40 -080046type Big struct { a, b, c, d int64 }
Russ Coxe5124812009-01-08 18:06:06 -080047func (z Big) M() int64 { return z.a+z.b+z.c+z.d }
48
Russ Cox839a6842009-01-20 14:40:40 -080049type Small struct { a int32 }
Russ Coxe5124812009-01-08 18:06:06 -080050func (z Small) M() int64 { return int64(z.a) }
51
Russ Cox839a6842009-01-20 14:40:40 -080052type Int int32
Russ Coxe5124812009-01-08 18:06:06 -080053func (z Int) M() int64 { return int64(z) }
54
55func nonptrs() {
Rob Pike4f61fc92010-09-04 10:36:13 +100056 var big Big = Big{ 10000, 2000, 300, 45 }
57 var small Small = Small{ 12345 }
58 var int Int = 12345
Russ Coxe5124812009-01-08 18:06:06 -080059
Rob Pike4f61fc92010-09-04 10:36:13 +100060 test("big", big)
61 test("&big", &big)
62 test("small", small)
63 test("&small", &small)
64 test("int", int)
65 test("&int", &int)
Russ Coxe5124812009-01-08 18:06:06 -080066}
67
68func main() {
Rob Pike4f61fc92010-09-04 10:36:13 +100069 ptrs()
70 nonptrs()
Russ Coxe5124812009-01-08 18:06:06 -080071
72 if bad {
Rob Pike4f61fc92010-09-04 10:36:13 +100073 println("BUG: interface4")
Russ Coxe5124812009-01-08 18:06:06 -080074 }
75}