github.com/undoio/delve@v1.9.0/pkg/dwarf/frame/entries_test.go (about)

     1  package frame
     2  
     3  import (
     4  	"encoding/binary"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  	"unsafe"
     9  )
    10  
    11  func ptrSizeByRuntimeArch() int {
    12  	return int(unsafe.Sizeof(uintptr(0)))
    13  }
    14  
    15  func TestFDEForPC(t *testing.T) {
    16  	frames := newFrameIndex()
    17  	frames = append(frames,
    18  		&FrameDescriptionEntry{begin: 10, size: 40},
    19  		&FrameDescriptionEntry{begin: 50, size: 50},
    20  		&FrameDescriptionEntry{begin: 100, size: 100},
    21  		&FrameDescriptionEntry{begin: 300, size: 10})
    22  
    23  	for _, test := range []struct {
    24  		pc  uint64
    25  		fde *FrameDescriptionEntry
    26  	}{
    27  		{0, nil},
    28  		{9, nil},
    29  		{10, frames[0]},
    30  		{35, frames[0]},
    31  		{49, frames[0]},
    32  		{50, frames[1]},
    33  		{75, frames[1]},
    34  		{100, frames[2]},
    35  		{199, frames[2]},
    36  		{200, nil},
    37  		{299, nil},
    38  		{300, frames[3]},
    39  		{309, frames[3]},
    40  		{310, nil},
    41  		{400, nil}} {
    42  
    43  		out, err := frames.FDEForPC(test.pc)
    44  		if test.fde != nil {
    45  			if err != nil {
    46  				t.Fatal(err)
    47  			}
    48  			if out != test.fde {
    49  				t.Errorf("[pc = %#x] got incorrect fde\noutput:\t%#v\nexpected:\t%#v", test.pc, out, test.fde)
    50  			}
    51  		} else {
    52  			if err == nil {
    53  				t.Errorf("[pc = %#x] expected error got fde %#v", test.pc, out)
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  func BenchmarkFDEForPC(b *testing.B) {
    60  	f, err := os.Open("testdata/frame")
    61  	if err != nil {
    62  		b.Fatal(err)
    63  	}
    64  	defer f.Close()
    65  
    66  	data, err := ioutil.ReadAll(f)
    67  	if err != nil {
    68  		b.Fatal(err)
    69  	}
    70  	fdes, _ := Parse(data, binary.BigEndian, 0, ptrSizeByRuntimeArch(), 0)
    71  
    72  	for i := 0; i < b.N; i++ {
    73  		// bench worst case, exhaustive search
    74  		_, _ = fdes.FDEForPC(0x455555555)
    75  	}
    76  }