mirror of
https://github.com/golang/go
synced 2024-11-25 09:47:57 -07:00
test: a number of fixes.
Details: - reorder.go: delete p8. (Once expectation is changed per b/4627 it is identical to p1.) - switch.go: added some more (degenerate) switches. - range.go: improved error messages in a few cases. - method.go: added tests of calls to promoted methods. R=iant CC=golang-dev https://golang.org/cl/7306087
This commit is contained in:
parent
d282532901
commit
1c1096ea31
@ -128,13 +128,13 @@ func main() {
|
||||
panic("fail")
|
||||
}
|
||||
|
||||
var zs struct { S }
|
||||
var zps struct { *S1 }
|
||||
var zi struct { I }
|
||||
var zpi struct { *I1 }
|
||||
var zpt struct { *T1 }
|
||||
var zt struct { T }
|
||||
var zv struct { Val }
|
||||
var zs struct{ S }
|
||||
var zps struct{ *S1 }
|
||||
var zi struct{ I }
|
||||
var zpi struct{ *I1 }
|
||||
var zpt struct{ *T1 }
|
||||
var zt struct{ T }
|
||||
var zv struct{ Val }
|
||||
|
||||
if zs.val() != 1 {
|
||||
println("zs.val:", zs.val())
|
||||
@ -247,4 +247,61 @@ func main() {
|
||||
println("zv.val():", zv.val())
|
||||
panic("fail")
|
||||
}
|
||||
|
||||
promotion()
|
||||
}
|
||||
|
||||
type A struct{ B }
|
||||
type B struct {
|
||||
C
|
||||
*D
|
||||
}
|
||||
type C int
|
||||
|
||||
func (C) f() {} // value receiver, direct field of A
|
||||
func (*C) g() {} // pointer receiver
|
||||
|
||||
type D int
|
||||
|
||||
func (D) h() {} // value receiver, indirect field of A
|
||||
func (*D) i() {} // pointer receiver
|
||||
|
||||
func expectPanic() {
|
||||
if r := recover(); r == nil {
|
||||
panic("expected nil dereference")
|
||||
}
|
||||
}
|
||||
|
||||
func promotion() {
|
||||
var a A
|
||||
// Addressable value receiver.
|
||||
a.f()
|
||||
a.g()
|
||||
func() {
|
||||
defer expectPanic()
|
||||
a.h() // dynamic error: nil dereference in a.B.D->f()
|
||||
}()
|
||||
a.i()
|
||||
|
||||
// Non-addressable value receiver.
|
||||
A(a).f()
|
||||
// A(a).g() // static error: cannot call pointer method on A literal.B.C
|
||||
func() {
|
||||
defer expectPanic()
|
||||
A(a).h() // dynamic error: nil dereference in A().B.D->f()
|
||||
}()
|
||||
A(a).i()
|
||||
|
||||
// Pointer receiver.
|
||||
(&a).f()
|
||||
(&a).g()
|
||||
func() {
|
||||
defer expectPanic()
|
||||
(&a).h() // dynamic error: nil deref: nil dereference in (&a).B.D->f()
|
||||
}()
|
||||
(&a).i()
|
||||
|
||||
c := new(C)
|
||||
c.f() // makes a copy
|
||||
c.g()
|
||||
}
|
||||
|
@ -55,10 +55,10 @@ func testslice() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 15 {
|
||||
println("wrong sum ranging over makeslice")
|
||||
println("wrong sum ranging over makeslice", s)
|
||||
panic("fail")
|
||||
}
|
||||
|
||||
|
||||
x := []int{10, 20}
|
||||
y := []int{99}
|
||||
i := 1
|
||||
@ -82,7 +82,7 @@ func testslice1() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 10 {
|
||||
println("wrong sum ranging over makeslice")
|
||||
println("wrong sum ranging over makeslice", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -106,7 +106,7 @@ func testarray() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 15 {
|
||||
println("wrong sum ranging over makearray")
|
||||
println("wrong sum ranging over makearray", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -122,7 +122,7 @@ func testarray1() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 10 {
|
||||
println("wrong sum ranging over makearray")
|
||||
println("wrong sum ranging over makearray", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -155,7 +155,7 @@ func testarrayptr() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 15 {
|
||||
println("wrong sum ranging over makearrayptr")
|
||||
println("wrong sum ranging over makearrayptr", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -171,7 +171,7 @@ func testarrayptr1() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 10 {
|
||||
println("wrong sum ranging over makearrayptr")
|
||||
println("wrong sum ranging over makearrayptr", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -195,7 +195,7 @@ func teststring() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 'a'+'b'+'c'+'d'+'☺' {
|
||||
println("wrong sum ranging over makestring")
|
||||
println("wrong sum ranging over makestring", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -211,7 +211,7 @@ func teststring1() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 10 {
|
||||
println("wrong sum ranging over makestring")
|
||||
println("wrong sum ranging over makestring", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -235,7 +235,7 @@ func testmap() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 'a'+'b'+'c'+'d'+'☺' {
|
||||
println("wrong sum ranging over makemap")
|
||||
println("wrong sum ranging over makemap", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
@ -251,7 +251,7 @@ func testmap1() {
|
||||
panic("fail")
|
||||
}
|
||||
if s != 10 {
|
||||
println("wrong sum ranging over makemap")
|
||||
println("wrong sum ranging over makemap", s)
|
||||
panic("fail")
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ func main() {
|
||||
p6()
|
||||
p7()
|
||||
p8()
|
||||
p9()
|
||||
}
|
||||
|
||||
var gx []int
|
||||
@ -43,7 +42,7 @@ func check3(x, y, z, xx, yy, zz int) {
|
||||
}
|
||||
|
||||
func p1() {
|
||||
x := []int{1,2,3}
|
||||
x := []int{1, 2, 3}
|
||||
i := 0
|
||||
i, x[i] = 1, 100
|
||||
_ = i
|
||||
@ -51,7 +50,7 @@ func p1() {
|
||||
}
|
||||
|
||||
func p2() {
|
||||
x := []int{1,2,3}
|
||||
x := []int{1, 2, 3}
|
||||
i := 0
|
||||
x[i], i = 100, 1
|
||||
_ = i
|
||||
@ -59,7 +58,7 @@ func p2() {
|
||||
}
|
||||
|
||||
func p3() {
|
||||
x := []int{1,2,3}
|
||||
x := []int{1, 2, 3}
|
||||
y := x
|
||||
gx = x
|
||||
x[1], y[0] = f(0), f(1)
|
||||
@ -67,7 +66,7 @@ func p3() {
|
||||
}
|
||||
|
||||
func p4() {
|
||||
x := []int{1,2,3}
|
||||
x := []int{1, 2, 3}
|
||||
y := x
|
||||
gx = x
|
||||
x[1], y[0] = gx[0], gx[1]
|
||||
@ -75,7 +74,7 @@ func p4() {
|
||||
}
|
||||
|
||||
func p5() {
|
||||
x := []int{1,2,3}
|
||||
x := []int{1, 2, 3}
|
||||
y := x
|
||||
p := &x[0]
|
||||
q := &x[1]
|
||||
@ -90,7 +89,7 @@ func p6() {
|
||||
px := &x
|
||||
py := &y
|
||||
*px, *py = y, x
|
||||
check3(x, y, z, 2, 1, 3)
|
||||
check3(x, y, z, 2, 1, 3)
|
||||
}
|
||||
|
||||
func f1(x, y, z int) (xx, yy, zz int) {
|
||||
@ -107,21 +106,6 @@ func p7() {
|
||||
}
|
||||
|
||||
func p8() {
|
||||
x := []int{1,2,3}
|
||||
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err == nil {
|
||||
panic("not panicking")
|
||||
}
|
||||
check(x, 100, 2, 3)
|
||||
}()
|
||||
|
||||
i := 0
|
||||
i, x[i], x[5] = 1, 100, 500
|
||||
}
|
||||
|
||||
func p9() {
|
||||
m := make(map[int]int)
|
||||
m[0] = len(m)
|
||||
if m[0] != 0 {
|
||||
|
@ -307,9 +307,9 @@ func main() {
|
||||
|
||||
// switch on array.
|
||||
switch ar := [3]int{1, 2, 3}; ar {
|
||||
case [3]int{1,2,3}:
|
||||
case [3]int{1, 2, 3}:
|
||||
assert(true, "[1 2 3]")
|
||||
case [3]int{4,5,6}:
|
||||
case [3]int{4, 5, 6}:
|
||||
assert(false, "ar should be [1 2 3]")
|
||||
default:
|
||||
assert(false, "ar should be [1 2 3]")
|
||||
@ -327,12 +327,57 @@ func main() {
|
||||
assert(false, "c1 did not match itself")
|
||||
}
|
||||
|
||||
// empty switch
|
||||
switch {
|
||||
}
|
||||
|
||||
// empty switch with default case.
|
||||
fired = false
|
||||
switch {
|
||||
default:
|
||||
fired = true
|
||||
}
|
||||
assert(fired, "fail")
|
||||
|
||||
// Default and fallthrough.
|
||||
count = 0
|
||||
switch {
|
||||
default:
|
||||
count++
|
||||
fallthrough
|
||||
case false:
|
||||
count++
|
||||
}
|
||||
assert(count == 2, "fail")
|
||||
|
||||
// fallthrough to default, which is not at end.
|
||||
count = 0
|
||||
switch i5 {
|
||||
case 5:
|
||||
count++
|
||||
fallthrough
|
||||
default:
|
||||
count++
|
||||
case 6:
|
||||
count++
|
||||
}
|
||||
assert(count == 2, "fail")
|
||||
|
||||
// fallthrough in final case.
|
||||
count = 0
|
||||
switch i5 {
|
||||
case 5:
|
||||
count++
|
||||
fallthrough
|
||||
}
|
||||
assert(count == 1, "fail")
|
||||
|
||||
i := 0
|
||||
switch x := 5; {
|
||||
case i < x:
|
||||
os.Exit(0)
|
||||
case i == x:
|
||||
case i > x:
|
||||
os.Exit(1)
|
||||
case i < x:
|
||||
os.Exit(0)
|
||||
case i == x:
|
||||
case i > x:
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user