Skip to content

Commit 1c1096e

Browse files
committed
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
1 parent d282532 commit 1c1096e

File tree

4 files changed

+133
-47
lines changed

4 files changed

+133
-47
lines changed

test/method.go

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ func main() {
128128
panic("fail")
129129
}
130130

131-
var zs struct { S }
132-
var zps struct { *S1 }
133-
var zi struct { I }
134-
var zpi struct { *I1 }
135-
var zpt struct { *T1 }
136-
var zt struct { T }
137-
var zv struct { Val }
131+
var zs struct{ S }
132+
var zps struct{ *S1 }
133+
var zi struct{ I }
134+
var zpi struct{ *I1 }
135+
var zpt struct{ *T1 }
136+
var zt struct{ T }
137+
var zv struct{ Val }
138138

139139
if zs.val() != 1 {
140140
println("zs.val:", zs.val())
@@ -247,4 +247,61 @@ func main() {
247247
println("zv.val():", zv.val())
248248
panic("fail")
249249
}
250+
251+
promotion()
252+
}
253+
254+
type A struct{ B }
255+
type B struct {
256+
C
257+
*D
258+
}
259+
type C int
260+
261+
func (C) f() {} // value receiver, direct field of A
262+
func (*C) g() {} // pointer receiver
263+
264+
type D int
265+
266+
func (D) h() {} // value receiver, indirect field of A
267+
func (*D) i() {} // pointer receiver
268+
269+
func expectPanic() {
270+
if r := recover(); r == nil {
271+
panic("expected nil dereference")
272+
}
273+
}
274+
275+
func promotion() {
276+
var a A
277+
// Addressable value receiver.
278+
a.f()
279+
a.g()
280+
func() {
281+
defer expectPanic()
282+
a.h() // dynamic error: nil dereference in a.B.D->f()
283+
}()
284+
a.i()
285+
286+
// Non-addressable value receiver.
287+
A(a).f()
288+
// A(a).g() // static error: cannot call pointer method on A literal.B.C
289+
func() {
290+
defer expectPanic()
291+
A(a).h() // dynamic error: nil dereference in A().B.D->f()
292+
}()
293+
A(a).i()
294+
295+
// Pointer receiver.
296+
(&a).f()
297+
(&a).g()
298+
func() {
299+
defer expectPanic()
300+
(&a).h() // dynamic error: nil deref: nil dereference in (&a).B.D->f()
301+
}()
302+
(&a).i()
303+
304+
c := new(C)
305+
c.f() // makes a copy
306+
c.g()
250307
}

test/range.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ func testslice() {
5555
panic("fail")
5656
}
5757
if s != 15 {
58-
println("wrong sum ranging over makeslice")
58+
println("wrong sum ranging over makeslice", s)
5959
panic("fail")
6060
}
61-
61+
6262
x := []int{10, 20}
6363
y := []int{99}
6464
i := 1
@@ -82,7 +82,7 @@ func testslice1() {
8282
panic("fail")
8383
}
8484
if s != 10 {
85-
println("wrong sum ranging over makeslice")
85+
println("wrong sum ranging over makeslice", s)
8686
panic("fail")
8787
}
8888
}
@@ -106,7 +106,7 @@ func testarray() {
106106
panic("fail")
107107
}
108108
if s != 15 {
109-
println("wrong sum ranging over makearray")
109+
println("wrong sum ranging over makearray", s)
110110
panic("fail")
111111
}
112112
}
@@ -122,7 +122,7 @@ func testarray1() {
122122
panic("fail")
123123
}
124124
if s != 10 {
125-
println("wrong sum ranging over makearray")
125+
println("wrong sum ranging over makearray", s)
126126
panic("fail")
127127
}
128128
}
@@ -155,7 +155,7 @@ func testarrayptr() {
155155
panic("fail")
156156
}
157157
if s != 15 {
158-
println("wrong sum ranging over makearrayptr")
158+
println("wrong sum ranging over makearrayptr", s)
159159
panic("fail")
160160
}
161161
}
@@ -171,7 +171,7 @@ func testarrayptr1() {
171171
panic("fail")
172172
}
173173
if s != 10 {
174-
println("wrong sum ranging over makearrayptr")
174+
println("wrong sum ranging over makearrayptr", s)
175175
panic("fail")
176176
}
177177
}
@@ -195,7 +195,7 @@ func teststring() {
195195
panic("fail")
196196
}
197197
if s != 'a'+'b'+'c'+'d'+'☺' {
198-
println("wrong sum ranging over makestring")
198+
println("wrong sum ranging over makestring", s)
199199
panic("fail")
200200
}
201201
}
@@ -211,7 +211,7 @@ func teststring1() {
211211
panic("fail")
212212
}
213213
if s != 10 {
214-
println("wrong sum ranging over makestring")
214+
println("wrong sum ranging over makestring", s)
215215
panic("fail")
216216
}
217217
}
@@ -235,7 +235,7 @@ func testmap() {
235235
panic("fail")
236236
}
237237
if s != 'a'+'b'+'c'+'d'+'☺' {
238-
println("wrong sum ranging over makemap")
238+
println("wrong sum ranging over makemap", s)
239239
panic("fail")
240240
}
241241
}
@@ -251,7 +251,7 @@ func testmap1() {
251251
panic("fail")
252252
}
253253
if s != 10 {
254-
println("wrong sum ranging over makemap")
254+
println("wrong sum ranging over makemap", s)
255255
panic("fail")
256256
}
257257
}

test/reorder.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func main() {
1919
p6()
2020
p7()
2121
p8()
22-
p9()
2322
}
2423

2524
var gx []int
@@ -43,39 +42,39 @@ func check3(x, y, z, xx, yy, zz int) {
4342
}
4443

4544
func p1() {
46-
x := []int{1,2,3}
45+
x := []int{1, 2, 3}
4746
i := 0
4847
i, x[i] = 1, 100
4948
_ = i
5049
check(x, 100, 2, 3)
5150
}
5251

5352
func p2() {
54-
x := []int{1,2,3}
53+
x := []int{1, 2, 3}
5554
i := 0
5655
x[i], i = 100, 1
5756
_ = i
5857
check(x, 100, 2, 3)
5958
}
6059

6160
func p3() {
62-
x := []int{1,2,3}
61+
x := []int{1, 2, 3}
6362
y := x
6463
gx = x
6564
x[1], y[0] = f(0), f(1)
6665
check(x, 2, 1, 3)
6766
}
6867

6968
func p4() {
70-
x := []int{1,2,3}
69+
x := []int{1, 2, 3}
7170
y := x
7271
gx = x
7372
x[1], y[0] = gx[0], gx[1]
7473
check(x, 2, 1, 3)
7574
}
7675

7776
func p5() {
78-
x := []int{1,2,3}
77+
x := []int{1, 2, 3}
7978
y := x
8079
p := &x[0]
8180
q := &x[1]
@@ -90,7 +89,7 @@ func p6() {
9089
px := &x
9190
py := &y
9291
*px, *py = y, x
93-
check3(x, y, z, 2, 1, 3)
92+
check3(x, y, z, 2, 1, 3)
9493
}
9594

9695
func f1(x, y, z int) (xx, yy, zz int) {
@@ -107,21 +106,6 @@ func p7() {
107106
}
108107

109108
func p8() {
110-
x := []int{1,2,3}
111-
112-
defer func() {
113-
err := recover()
114-
if err == nil {
115-
panic("not panicking")
116-
}
117-
check(x, 100, 2, 3)
118-
}()
119-
120-
i := 0
121-
i, x[i], x[5] = 1, 100, 500
122-
}
123-
124-
func p9() {
125109
m := make(map[int]int)
126110
m[0] = len(m)
127111
if m[0] != 0 {

test/switch.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ func main() {
307307

308308
// switch on array.
309309
switch ar := [3]int{1, 2, 3}; ar {
310-
case [3]int{1,2,3}:
310+
case [3]int{1, 2, 3}:
311311
assert(true, "[1 2 3]")
312-
case [3]int{4,5,6}:
312+
case [3]int{4, 5, 6}:
313313
assert(false, "ar should be [1 2 3]")
314314
default:
315315
assert(false, "ar should be [1 2 3]")
@@ -327,12 +327,57 @@ func main() {
327327
assert(false, "c1 did not match itself")
328328
}
329329

330+
// empty switch
331+
switch {
332+
}
333+
334+
// empty switch with default case.
335+
fired = false
336+
switch {
337+
default:
338+
fired = true
339+
}
340+
assert(fired, "fail")
341+
342+
// Default and fallthrough.
343+
count = 0
344+
switch {
345+
default:
346+
count++
347+
fallthrough
348+
case false:
349+
count++
350+
}
351+
assert(count == 2, "fail")
352+
353+
// fallthrough to default, which is not at end.
354+
count = 0
355+
switch i5 {
356+
case 5:
357+
count++
358+
fallthrough
359+
default:
360+
count++
361+
case 6:
362+
count++
363+
}
364+
assert(count == 2, "fail")
365+
366+
// fallthrough in final case.
367+
count = 0
368+
switch i5 {
369+
case 5:
370+
count++
371+
fallthrough
372+
}
373+
assert(count == 1, "fail")
374+
330375
i := 0
331376
switch x := 5; {
332-
case i < x:
333-
os.Exit(0)
334-
case i == x:
335-
case i > x:
336-
os.Exit(1)
377+
case i < x:
378+
os.Exit(0)
379+
case i == x:
380+
case i > x:
381+
os.Exit(1)
337382
}
338383
}

0 commit comments

Comments
 (0)