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 // These are marked noinline so that we can use FuncForPC
37 func testCallerFoo(t
*testing
.T
) {
42 func testCallerBar(t
*testing
.T
) {
43 for i
:= 0; i
< 2; i
++ {
44 pc
, file
, line
, ok
:= runtime
.Caller(i
)
45 f
:= runtime
.FuncForPC(pc
)
47 !strings
.HasSuffix(file
, "symtab_test.go") ||
48 // FuncForPC doesn't work gccgo, because of inlining.
49 // (i == 0 && !strings.HasSuffix(f.Name(), "testCallerBar")) ||
50 // (i == 1 && !strings.HasSuffix(f.Name(), "testCallerFoo")) ||
51 line
< 5 || line
> 1000 ||
53 t
.Errorf("incorrect symbol info %d: %t %d %d %s %s %d",
54 i
, ok
, f
.Entry(), pc
, f
.Name(), file
, line
)
59 func lineNumber() int {
60 _
, _
, line
, _
:= runtime
.Caller(1)
61 return line
// return 0 for error
64 // Do not add/remove lines in this block without updating the line numbers.
65 var firstLine
= lineNumber() // 0
67 lineVar1
= lineNumber() // 2
68 lineVar2a
, lineVar2b
= lineNumber(), lineNumber() // 3
70 var compLit
= []struct { // 5
74 lineNumber(), lineNumber(), // 9
81 lineB
: lineNumber(), // 16
82 lineA
: lineNumber(), // 17
85 var arrayLit
= [...]int{lineNumber(), // 20
86 lineNumber(), lineNumber(), // 21
89 var sliceLit
= []int{lineNumber(), // 24
90 lineNumber(), lineNumber(), // 25
93 var mapLit
= map[int]int{ // 28
94 29: lineNumber(), // 29
95 30: lineNumber(), // 30
96 lineNumber(): 31, // 31
97 lineNumber(): 32, // 32
99 var intLit
= lineNumber() + // 34
102 func trythis() { // 37
103 recordLines(lineNumber(), // 38
108 // Modifications below this line are okay.
110 var l38
, l39
, l40
int
112 func recordLines(a
, b
, c
int) {
118 func TestLineNumber(t
*testing
.T
) {
120 for _
, test
:= range []struct {
125 {"firstLine", firstLine
, 0},
126 {"lineVar1", lineVar1
, 2},
127 {"lineVar2a", lineVar2a
, 3},
128 {"lineVar2b", lineVar2b
, 3},
129 {"compLit[0].lineA", compLit
[0].lineA
, 9},
130 {"compLit[0].lineB", compLit
[0].lineB
, 9},
131 {"compLit[1].lineA", compLit
[1].lineA
, 12},
132 {"compLit[1].lineB", compLit
[1].lineB
, 13},
133 {"compLit[2].lineA", compLit
[2].lineA
, 17},
134 {"compLit[2].lineB", compLit
[2].lineB
, 16},
136 {"arrayLit[0]", arrayLit
[0], 20},
137 {"arrayLit[1]", arrayLit
[1], 21},
138 {"arrayLit[2]", arrayLit
[2], 21},
139 {"arrayLit[3]", arrayLit
[3], 22},
141 {"sliceLit[0]", sliceLit
[0], 24},
142 {"sliceLit[1]", sliceLit
[1], 25},
143 {"sliceLit[2]", sliceLit
[2], 25},
144 {"sliceLit[3]", sliceLit
[3], 26},
146 {"mapLit[29]", mapLit
[29], 29},
147 {"mapLit[30]", mapLit
[30], 30},
148 {"mapLit[31]", mapLit
[31+firstLine
] + firstLine
, 31}, // nb it's the key not the value
149 {"mapLit[32]", mapLit
[32+firstLine
] + firstLine
, 32}, // nb it's the key not the value
151 {"intLit", intLit
- 2*firstLine
, 34 + 35 + 36},
157 if got
:= test
.val
- firstLine
; got
!= test
.want
{
158 t
.Errorf("%s on firstLine+%d want firstLine+%d (firstLine=%d, val=%d)",
159 test
.name
, got
, test
.want
, firstLine
, test
.val
)
164 func TestNilName(t
*testing
.T
) {
166 if ex
:= recover(); ex
!= nil {
167 t
.Fatalf("expected no nil panic, got=%v", ex
)
170 if got
:= (*runtime
.Func
)(nil).Name(); got
!= "" {
171 t
.Errorf("Name() = %q, want %q", got
, "")