...
Run Format

Source file src/runtime/symtab_test.go

Documentation: runtime

     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.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestCaller(t *testing.T) {
    14  	procs := runtime.GOMAXPROCS(-1)
    15  	c := make(chan bool, procs)
    16  	for p := 0; p < procs; p++ {
    17  		go func() {
    18  			for i := 0; i < 1000; i++ {
    19  				testCallerFoo(t)
    20  			}
    21  			c <- true
    22  		}()
    23  		defer func() {
    24  			<-c
    25  		}()
    26  	}
    27  }
    28  
    29  // These are marked noinline so that we can use FuncForPC
    30  // in testCallerBar.
    31  //go:noinline
    32  func testCallerFoo(t *testing.T) {
    33  	testCallerBar(t)
    34  }
    35  
    36  //go:noinline
    37  func testCallerBar(t *testing.T) {
    38  	for i := 0; i < 2; i++ {
    39  		pc, file, line, ok := runtime.Caller(i)
    40  		f := runtime.FuncForPC(pc)
    41  		if !ok ||
    42  			!strings.HasSuffix(file, "symtab_test.go") ||
    43  			(i == 0 && !strings.HasSuffix(f.Name(), "testCallerBar")) ||
    44  			(i == 1 && !strings.HasSuffix(f.Name(), "testCallerFoo")) ||
    45  			line < 5 || line > 1000 ||
    46  			f.Entry() >= pc {
    47  			t.Errorf("incorrect symbol info %d: %t %d %d %s %s %d",
    48  				i, ok, f.Entry(), pc, f.Name(), file, line)
    49  		}
    50  	}
    51  }
    52  
    53  func lineNumber() int {
    54  	_, _, line, _ := runtime.Caller(1)
    55  	return line // return 0 for error
    56  }
    57  
    58  // Do not add/remove lines in this block without updating the line numbers.
    59  var firstLine = lineNumber() // 0
    60  var (                        // 1
    61  	lineVar1             = lineNumber()               // 2
    62  	lineVar2a, lineVar2b = lineNumber(), lineNumber() // 3
    63  )                        // 4
    64  var compLit = []struct { // 5
    65  	lineA, lineB int // 6
    66  }{ // 7
    67  	{ // 8
    68  		lineNumber(), lineNumber(), // 9
    69  	}, // 10
    70  	{ // 11
    71  		lineNumber(), // 12
    72  		lineNumber(), // 13
    73  	}, // 14
    74  	{ // 15
    75  		lineB: lineNumber(), // 16
    76  		lineA: lineNumber(), // 17
    77  	}, // 18
    78  }                                     // 19
    79  var arrayLit = [...]int{lineNumber(), // 20
    80  	lineNumber(), lineNumber(), // 21
    81  	lineNumber(), // 22
    82  }                                  // 23
    83  var sliceLit = []int{lineNumber(), // 24
    84  	lineNumber(), lineNumber(), // 25
    85  	lineNumber(), // 26
    86  }                         // 27
    87  var mapLit = map[int]int{ // 28
    88  	29:           lineNumber(), // 29
    89  	30:           lineNumber(), // 30
    90  	lineNumber(): 31,           // 31
    91  	lineNumber(): 32,           // 32
    92  }                           // 33
    93  var intLit = lineNumber() + // 34
    94  	lineNumber() + // 35
    95  	lineNumber() // 36
    96  func trythis() { // 37
    97  	recordLines(lineNumber(), // 38
    98  		lineNumber(), // 39
    99  		lineNumber()) // 40
   100  }
   101  
   102  // Modifications below this line are okay.
   103  
   104  var l38, l39, l40 int
   105  
   106  func recordLines(a, b, c int) {
   107  	l38 = a
   108  	l39 = b
   109  	l40 = c
   110  }
   111  
   112  func TestLineNumber(t *testing.T) {
   113  	trythis()
   114  	for _, test := range []struct {
   115  		name string
   116  		val  int
   117  		want int
   118  	}{
   119  		{"firstLine", firstLine, 0},
   120  		{"lineVar1", lineVar1, 2},
   121  		{"lineVar2a", lineVar2a, 3},
   122  		{"lineVar2b", lineVar2b, 3},
   123  		{"compLit[0].lineA", compLit[0].lineA, 9},
   124  		{"compLit[0].lineB", compLit[0].lineB, 9},
   125  		{"compLit[1].lineA", compLit[1].lineA, 12},
   126  		{"compLit[1].lineB", compLit[1].lineB, 13},
   127  		{"compLit[2].lineA", compLit[2].lineA, 17},
   128  		{"compLit[2].lineB", compLit[2].lineB, 16},
   129  
   130  		{"arrayLit[0]", arrayLit[0], 20},
   131  		{"arrayLit[1]", arrayLit[1], 21},
   132  		{"arrayLit[2]", arrayLit[2], 21},
   133  		{"arrayLit[3]", arrayLit[3], 22},
   134  
   135  		{"sliceLit[0]", sliceLit[0], 24},
   136  		{"sliceLit[1]", sliceLit[1], 25},
   137  		{"sliceLit[2]", sliceLit[2], 25},
   138  		{"sliceLit[3]", sliceLit[3], 26},
   139  
   140  		{"mapLit[29]", mapLit[29], 29},
   141  		{"mapLit[30]", mapLit[30], 30},
   142  		{"mapLit[31]", mapLit[31+firstLine] + firstLine, 31}, // nb it's the key not the value
   143  		{"mapLit[32]", mapLit[32+firstLine] + firstLine, 32}, // nb it's the key not the value
   144  
   145  		{"intLit", intLit - 2*firstLine, 34 + 35 + 36},
   146  
   147  		{"l38", l38, 38},
   148  		{"l39", l39, 39},
   149  		{"l40", l40, 40},
   150  	} {
   151  		if got := test.val - firstLine; got != test.want {
   152  			t.Errorf("%s on firstLine+%d want firstLine+%d (firstLine=%d, val=%d)",
   153  				test.name, got, test.want, firstLine, test.val)
   154  		}
   155  	}
   156  }
   157  
   158  func TestNilName(t *testing.T) {
   159  	defer func() {
   160  		if ex := recover(); ex != nil {
   161  			t.Fatalf("expected no nil panic, got=%v", ex)
   162  		}
   163  	}()
   164  	if got := (*runtime.Func)(nil).Name(); got != "" {
   165  		t.Errorf("Name() = %q, want %q", got, "")
   166  	}
   167  }
   168  

View as plain text