2009-01-15 11:15:34 -07:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out || echo BUG interface6
|
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-05-12 17:09:47 -06:00
|
|
|
// Interface values containing structures.
|
|
|
|
|
2009-01-15 11:15:34 -07:00
|
|
|
package main
|
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
import "os"
|
|
|
|
|
2009-01-15 11:15:34 -07:00
|
|
|
var fail int
|
|
|
|
|
|
|
|
func check(b bool, msg string) {
|
|
|
|
if (!b) {
|
|
|
|
println("failure in", msg);
|
|
|
|
fail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type I1 interface { Get() int; Put(int); }
|
2009-01-15 11:15:34 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type S1 struct { i int }
|
2009-01-15 11:15:34 -07:00
|
|
|
func (p S1) Get() int { return p.i }
|
|
|
|
func (p S1) Put(i int) { p.i = i }
|
|
|
|
|
|
|
|
func f1() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := S1{1};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I1 = s;
|
|
|
|
i.Put(2);
|
|
|
|
check(i.Get() == 1, "f1 i");
|
|
|
|
check(s.i == 1, "f1 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func f2() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := S1{1};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I1 = &s;
|
|
|
|
i.Put(2);
|
|
|
|
check(i.Get() == 1, "f2 i");
|
|
|
|
check(s.i == 1, "f2 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func f3() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := &S1{1};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I1 = s;
|
|
|
|
i.Put(2);
|
|
|
|
check(i.Get() == 1, "f3 i");
|
|
|
|
check(s.i == 1, "f3 s");
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type S2 struct { i int }
|
2009-01-15 11:15:34 -07:00
|
|
|
func (p *S2) Get() int { return p.i }
|
|
|
|
func (p *S2) Put(i int) { p.i = i }
|
|
|
|
|
2009-03-11 17:06:17 -06:00
|
|
|
// func f4() {
|
|
|
|
// s := S2{1};
|
|
|
|
// var i I1 = s;
|
|
|
|
// i.Put(2);
|
|
|
|
// check(i.Get() == 2, "f4 i");
|
|
|
|
// check(s.i == 1, "f4 s");
|
|
|
|
// }
|
2009-01-15 11:15:34 -07:00
|
|
|
|
|
|
|
func f5() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := S2{1};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I1 = &s;
|
|
|
|
i.Put(2);
|
|
|
|
check(i.Get() == 2, "f5 i");
|
|
|
|
check(s.i == 2, "f5 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func f6() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := &S2{1};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I1 = s;
|
|
|
|
i.Put(2);
|
|
|
|
check(i.Get() == 2, "f6 i");
|
|
|
|
check(s.i == 2, "f6 s");
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type I2 interface { Get() int64; Put(int64); }
|
2009-01-15 11:15:34 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type S3 struct { i, j, k, l int64 }
|
2009-01-15 11:15:34 -07:00
|
|
|
func (p S3) Get() int64 { return p.l }
|
|
|
|
func (p S3) Put(i int64) { p.l = i }
|
|
|
|
|
|
|
|
func f7() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := S3{1, 2, 3, 4};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I2 = s;
|
|
|
|
i.Put(5);
|
|
|
|
check(i.Get() == 4, "f7 i");
|
|
|
|
check(s.l == 4, "f7 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func f8() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := S3{1, 2, 3, 4};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I2 = &s;
|
|
|
|
i.Put(5);
|
|
|
|
check(i.Get() == 4, "f8 i");
|
|
|
|
check(s.l == 4, "f8 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func f9() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := &S3{1, 2, 3, 4};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I2 = s;
|
|
|
|
i.Put(5);
|
|
|
|
check(i.Get() == 4, "f9 i");
|
|
|
|
check(s.l == 4, "f9 s");
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type S4 struct { i, j, k, l int64 }
|
2009-01-15 11:15:34 -07:00
|
|
|
func (p *S4) Get() int64 { return p.l }
|
|
|
|
func (p *S4) Put(i int64) { p.l = i }
|
|
|
|
|
2009-03-11 17:06:17 -06:00
|
|
|
// func f10() {
|
|
|
|
// s := S4{1, 2, 3, 4};
|
|
|
|
// var i I2 = s;
|
|
|
|
// i.Put(5);
|
|
|
|
// check(i.Get() == 5, "f10 i");
|
|
|
|
// check(s.l == 4, "f10 s");
|
|
|
|
// }
|
2009-01-15 11:15:34 -07:00
|
|
|
|
|
|
|
func f11() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := S4{1, 2, 3, 4};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I2 = &s;
|
|
|
|
i.Put(5);
|
|
|
|
check(i.Get() == 5, "f11 i");
|
|
|
|
check(s.l == 5, "f11 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func f12() {
|
2009-03-03 09:39:12 -07:00
|
|
|
s := &S4{1, 2, 3, 4};
|
2009-01-15 11:15:34 -07:00
|
|
|
var i I2 = s;
|
|
|
|
i.Put(5);
|
|
|
|
check(i.Get() == 5, "f12 i");
|
|
|
|
check(s.l == 5, "f12 s");
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
f1();
|
|
|
|
f2();
|
|
|
|
f3();
|
2009-03-11 17:06:17 -06:00
|
|
|
// f4();
|
2009-01-15 11:15:34 -07:00
|
|
|
f5();
|
|
|
|
f6();
|
|
|
|
f7();
|
|
|
|
f8();
|
|
|
|
f9();
|
2009-03-11 17:06:17 -06:00
|
|
|
// f10();
|
2009-01-15 11:15:34 -07:00
|
|
|
f11();
|
|
|
|
f12();
|
|
|
|
if fail > 0 {
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1)
|
2009-01-15 11:15:34 -07:00
|
|
|
}
|
|
|
|
}
|