github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/runtime/symtab_test.go (about)

     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  var _ = runtime.Caller
    14  var _ = strings.HasSuffix
    15  
    16  type _ testing.T
    17  
    18  func TestCaller(t *testing.T) {
    19  	procs := runtime.GOMAXPROCS(-1)
    20  	c := make(chan bool, procs)
    21  	for p := 0; p < procs; p++ {
    22  		go func() {
    23  			for i := 0; i < 1000; i++ {
    24  				testCallerFoo(t)
    25  			}
    26  			c <- true
    27  		}()
    28  		defer func() {
    29  			<-c
    30  		}()
    31  	}
    32  }
    33  
    34  func testCallerFoo(t *testing.T) {
    35  	testCallerBar(t)
    36  }
    37  
    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)
    42  		if !ok ||
    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 ||
    48  			f.Entry() >= pc {
    49  			t.Errorf("incorrect symbol info %d: %t %d %d %s %s %d",
    50  				i, ok, f.Entry(), pc, f.Name(), file, line)
    51  		}
    52  	}
    53  }
    54  
    55  func lineNumber() int {
    56  	_, _, line, _ := runtime.Caller(1)
    57  	return line // return 0 for error
    58  }
    59  
    60  // Do not add/remove lines in this block without updating the line numbers.
    61  var firstLine = lineNumber() // 0
    62  var (                        // 1
    63  	lineVar1             = lineNumber()               // 2
    64  	lineVar2a, lineVar2b = lineNumber(), lineNumber() // 3
    65  )                        // 4
    66  var compLit = []struct { // 5
    67  	lineA, lineB int // 6
    68  }{ // 7
    69  	{ // 8
    70  		lineNumber(), lineNumber(), // 9
    71  	}, // 10
    72  	{ // 11
    73  		lineNumber(), // 12
    74  		lineNumber(), // 13
    75  	}, // 14
    76  	{ // 15
    77  		lineB: lineNumber(), // 16
    78  		lineA: lineNumber(), // 17
    79  	}, // 18
    80  }                                     // 19
    81  var arrayLit = [...]int{lineNumber(), // 20
    82  	lineNumber(), lineNumber(), // 21
    83  	lineNumber(), // 22
    84  }                                  // 23
    85  var sliceLit = []int{lineNumber(), // 24
    86  	lineNumber(), lineNumber(), // 25
    87  	lineNumber(), // 26
    88  }                         // 27
    89  var mapLit = map[int]int{ // 28
    90  	29:           lineNumber(), // 29
    91  	30:           lineNumber(), // 30
    92  	lineNumber(): 31,           // 31
    93  	lineNumber(): 32,           // 32
    94  }                           // 33
    95  var intLit = lineNumber() + // 34
    96  	lineNumber() + // 35
    97  			lineNumber() // 36
    98  func trythis() { // 37
    99  	recordLines(lineNumber(), // 38
   100  		lineNumber(), // 39
   101  		lineNumber()) // 40
   102  }
   103  
   104  // Modifications below this line are okay.
   105  
   106  var l38, l39, l40 int
   107  
   108  func recordLines(a, b, c int) {
   109  	l38 = a
   110  	l39 = b
   111  	l40 = c
   112  }
   113  
   114  func TestLineNumber(t *testing.T) {
   115  	trythis()
   116  	for _, test := range []struct {
   117  		name string
   118  		val  int
   119  		want int
   120  	}{
   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},
   131  
   132  		{"arrayLit[0]", arrayLit[0], 20},
   133  		{"arrayLit[1]", arrayLit[1], 21},
   134  		{"arrayLit[2]", arrayLit[2], 21},
   135  		{"arrayLit[3]", arrayLit[3], 22},
   136  
   137  		{"sliceLit[0]", sliceLit[0], 24},
   138  		{"sliceLit[1]", sliceLit[1], 25},
   139  		{"sliceLit[2]", sliceLit[2], 25},
   140  		{"sliceLit[3]", sliceLit[3], 26},
   141  
   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
   146  
   147  		{"intLit", intLit - 2*firstLine, 34 + 35 + 36},
   148  
   149  		{"l38", l38, 38},
   150  		{"l39", l39, 39},
   151  		{"l40", l40, 40},
   152  	} {
   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)
   156  		}
   157  	}
   158  }