github.com/phuslu/log@v1.0.100/runtime_test.go (about)

     1  package log
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  )
     7  
     8  func TestPcFileLine(t *testing.T) {
     9  	var pcs [2]uintptr
    10  	runtime.Callers(1, pcs[:])
    11  	file1, line1 := pcFileLine(pcs[0])
    12  	_, file2, line2, _ := runtime.Caller(0)
    13  
    14  	if file1 != file2 {
    15  		t.Errorf("pcFileLine file error: %q != %q", file1, file2)
    16  	}
    17  
    18  	if int(line1)+2 != line2 {
    19  		t.Errorf("pcFileLine line error: %d+2 != %d", line1, line2)
    20  	}
    21  }
    22  
    23  func TestPcNameFileLine(t *testing.T) {
    24  	var pcs [2]uintptr
    25  	runtime.Callers(1, pcs[:])
    26  	file1, line1, name1 := pcFileLineName(pcs[0])
    27  	t.Log(name1, file1, line1)
    28  	pc, file2, line2, _ := runtime.Caller(0)
    29  	frame, _ := runtime.CallersFrames([]uintptr{pc}).Next()
    30  	name2 := frame.Function
    31  	t.Log(name2, file2, line2)
    32  
    33  	if name1 != name2 {
    34  		t.Errorf("pcFileLine file error: %q != %q", name1, name2)
    35  	}
    36  
    37  	if file1 != file2 {
    38  		t.Errorf("pcFileLine file error: %q != %q", file1, file2)
    39  	}
    40  
    41  	if int(line1)+3 != line2 {
    42  		t.Errorf("pcFileLine line error: %d+3 != %d", line1, line2)
    43  	}
    44  }
    45  
    46  func BenchmarkPcFileLine(b *testing.B) {
    47  	b.ReportAllocs()
    48  	for i := 0; i < b.N; i++ {
    49  		var pc uintptr
    50  		caller1(1, &pc, 1, 1)
    51  		pcFileLine(pc)
    52  	}
    53  }
    54  
    55  func BenchmarkPcNameFileLine(b *testing.B) {
    56  	b.ReportAllocs()
    57  	for i := 0; i < b.N; i++ {
    58  		var pc uintptr
    59  		caller1(1, &pc, 1, 1)
    60  		pcFileLineName(pc)
    61  	}
    62  }