mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00: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
@ -247,4 +247,61 @@ func main() {
|
|||||||
println("zv.val():", zv.val())
|
println("zv.val():", zv.val())
|
||||||
panic("fail")
|
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,7 +55,7 @@ func testslice() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 15 {
|
if s != 15 {
|
||||||
println("wrong sum ranging over makeslice")
|
println("wrong sum ranging over makeslice", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ func testslice1() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 10 {
|
if s != 10 {
|
||||||
println("wrong sum ranging over makeslice")
|
println("wrong sum ranging over makeslice", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ func testarray() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 15 {
|
if s != 15 {
|
||||||
println("wrong sum ranging over makearray")
|
println("wrong sum ranging over makearray", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ func testarray1() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 10 {
|
if s != 10 {
|
||||||
println("wrong sum ranging over makearray")
|
println("wrong sum ranging over makearray", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,7 +155,7 @@ func testarrayptr() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 15 {
|
if s != 15 {
|
||||||
println("wrong sum ranging over makearrayptr")
|
println("wrong sum ranging over makearrayptr", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,7 +171,7 @@ func testarrayptr1() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 10 {
|
if s != 10 {
|
||||||
println("wrong sum ranging over makearrayptr")
|
println("wrong sum ranging over makearrayptr", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -195,7 +195,7 @@ func teststring() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 'a'+'b'+'c'+'d'+'☺' {
|
if s != 'a'+'b'+'c'+'d'+'☺' {
|
||||||
println("wrong sum ranging over makestring")
|
println("wrong sum ranging over makestring", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -211,7 +211,7 @@ func teststring1() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 10 {
|
if s != 10 {
|
||||||
println("wrong sum ranging over makestring")
|
println("wrong sum ranging over makestring", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,7 +235,7 @@ func testmap() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 'a'+'b'+'c'+'d'+'☺' {
|
if s != 'a'+'b'+'c'+'d'+'☺' {
|
||||||
println("wrong sum ranging over makemap")
|
println("wrong sum ranging over makemap", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -251,7 +251,7 @@ func testmap1() {
|
|||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
if s != 10 {
|
if s != 10 {
|
||||||
println("wrong sum ranging over makemap")
|
println("wrong sum ranging over makemap", s)
|
||||||
panic("fail")
|
panic("fail")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ func main() {
|
|||||||
p6()
|
p6()
|
||||||
p7()
|
p7()
|
||||||
p8()
|
p8()
|
||||||
p9()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var gx []int
|
var gx []int
|
||||||
@ -107,21 +106,6 @@ func p7() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func p8() {
|
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 := make(map[int]int)
|
||||||
m[0] = len(m)
|
m[0] = len(m)
|
||||||
if m[0] != 0 {
|
if m[0] != 0 {
|
||||||
|
@ -327,6 +327,51 @@ func main() {
|
|||||||
assert(false, "c1 did not match itself")
|
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
|
i := 0
|
||||||
switch x := 5; {
|
switch x := 5; {
|
||||||
case i < x:
|
case i < x:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user