1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 var _
= runtime
.Caller
14 var _
= strings
.HasSuffix
18 func TestCaller(t
*testing
.T
) {
19 procs
:= runtime
.GOMAXPROCS(-1)
20 c
:= make(chan bool, procs
)
21 for p
:= 0; p
< procs
; p
++ {
23 for i
:= 0; i
< 1000; i
++ {
34 func testCallerFoo(t
*testing
.T
) {
38 func testCallerBar(t
*testing
.T
) {
39 for i
:= 0; i
< 2; i
++ {
40 pc
, file
, line
, ok
:= runtime
.Caller(i
)
41 f
:= runtime
.FuncForPC(pc
)
43 !strings
.HasSuffix(file
, "symtab_test.go") ||
44 // FuncForPC doesn't work gccgo, because of inlining.
45 // (i == 0 && !strings.HasSuffix(f.Name(), "testCallerBar")) ||
46 // (i == 1 && !strings.HasSuffix(f.Name(), "testCallerFoo")) ||
47 line
< 5 || line
> 1000 ||
49 t
.Errorf("incorrect symbol info %d: %t %d %d %s %s %d",
50 i
, ok
, f
.Entry(), pc
, f
.Name(), file
, line
)
55 func lineNumber() int {
56 _
, _
, line
, _
:= runtime
.Caller(1)
57 return line
// return 0 for error
60 // Do not add/remove lines in this block without updating the line numbers.
61 var firstLine
= lineNumber() // 0
63 lineVar1
= lineNumber() // 2
64 lineVar2a
, lineVar2b
= lineNumber(), lineNumber() // 3
66 var compLit
= []struct { // 5
70 lineNumber(), lineNumber(), // 9
77 lineB
: lineNumber(), // 16
78 lineA
: lineNumber(), // 17
81 var arrayLit
= [...]int{lineNumber(), // 20
82 lineNumber(), lineNumber(), // 21
85 var sliceLit
= []int{lineNumber(), // 24
86 lineNumber(), lineNumber(), // 25
89 var mapLit
= map[int]int{ // 28
90 29: lineNumber(), // 29
91 30: lineNumber(), // 30
92 lineNumber(): 31, // 31
93 lineNumber(): 32, // 32
95 var intLit
= lineNumber() + // 34
98 func trythis() { // 37
99 recordLines(lineNumber(), // 38
104 // Modifications below this line are okay.
106 var l38
, l39
, l40
int
108 func recordLines(a
, b
, c
int) {
114 func TestLineNumber(t
*testing
.T
) {
116 for _
, test
:= range []struct {
121 {"firstLine", firstLine
, 0},
122 {"lineVar1", lineVar1
, 2},
123 {"lineVar2a", lineVar2a
, 3},
124 {"lineVar2b", lineVar2b
, 3},
125 {"compLit[0].lineA", compLit
[0].lineA
, 9},
126 {"compLit[0].lineB", compLit
[0].lineB
, 9},
127 {"compLit[1].lineA", compLit
[1].lineA
, 12},
128 {"compLit[1].lineB", compLit
[1].lineB
, 13},
129 {"compLit[2].lineA", compLit
[2].lineA
, 17},
130 {"compLit[2].lineB", compLit
[2].lineB
, 16},
132 {"arrayLit[0]", arrayLit
[0], 20},
133 {"arrayLit[1]", arrayLit
[1], 21},
134 {"arrayLit[2]", arrayLit
[2], 21},
135 {"arrayLit[3]", arrayLit
[3], 22},
137 {"sliceLit[0]", sliceLit
[0], 24},
138 {"sliceLit[1]", sliceLit
[1], 25},
139 {"sliceLit[2]", sliceLit
[2], 25},
140 {"sliceLit[3]", sliceLit
[3], 26},
142 {"mapLit[29]", mapLit
[29], 29},
143 {"mapLit[30]", mapLit
[30], 30},
144 {"mapLit[31]", mapLit
[31+firstLine
] + firstLine
, 31}, // nb it's the key not the value
145 {"mapLit[32]", mapLit
[32+firstLine
] + firstLine
, 32}, // nb it's the key not the value
147 {"intLit", intLit
- 2*firstLine
, 34 + 35 + 36},
153 if got
:= test
.val
- firstLine
; got
!= test
.want
{
154 t
.Errorf("%s on firstLine+%d want firstLine+%d (firstLine=%d, val=%d)",
155 test
.name
, got
, test
.want
, firstLine
, test
.val
)