github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/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 TestAppend(t *testing.T) { 60 equal := func(x, y FrameDescriptionEntries) bool { 61 if len(x) != len(y) { 62 return false 63 } 64 for i := range x { 65 if x[i].Begin() != y[i].Begin() || x[i].End() != y[i].End() { 66 return false 67 } 68 } 69 return true 70 } 71 var appendTests = []struct { 72 name string 73 f1 FrameDescriptionEntries 74 f2 FrameDescriptionEntries 75 want FrameDescriptionEntries 76 }{ 77 { 78 name: "nil", 79 f1: nil, 80 f2: nil, 81 want: nil, 82 }, 83 84 { 85 name: "one", 86 f1: FrameDescriptionEntries{ 87 &FrameDescriptionEntry{begin: 10, size: 40}, 88 }, 89 f2: FrameDescriptionEntries{ 90 &FrameDescriptionEntry{begin: 10, size: 40}, 91 }, 92 want: FrameDescriptionEntries{ 93 &FrameDescriptionEntry{begin: 10, size: 40}, 94 }, 95 }, 96 { 97 name: "1 item", 98 f1: FrameDescriptionEntries{ 99 &FrameDescriptionEntry{begin: 10, size: 40}, 100 &FrameDescriptionEntry{begin: 10, size: 40}, 101 &FrameDescriptionEntry{begin: 50, size: 50}, 102 }, 103 f2: FrameDescriptionEntries{ 104 &FrameDescriptionEntry{begin: 10, size: 40}, 105 &FrameDescriptionEntry{begin: 50, size: 50}, 106 }, 107 want: FrameDescriptionEntries{ 108 &FrameDescriptionEntry{begin: 10, size: 40}, 109 &FrameDescriptionEntry{begin: 50, size: 50}, 110 }, 111 }, 112 { 113 name: "many", 114 f1: FrameDescriptionEntries{ 115 &FrameDescriptionEntry{begin: 10, size: 40}, 116 &FrameDescriptionEntry{begin: 100, size: 100}, 117 &FrameDescriptionEntry{begin: 50, size: 50}, 118 &FrameDescriptionEntry{begin: 50, size: 50}, 119 &FrameDescriptionEntry{begin: 300, size: 10}, 120 &FrameDescriptionEntry{begin: 300, size: 10}, 121 }, 122 f2: FrameDescriptionEntries{ 123 &FrameDescriptionEntry{begin: 10, size: 40}, 124 &FrameDescriptionEntry{begin: 100, size: 100}, 125 &FrameDescriptionEntry{begin: 100, size: 100}, 126 }, 127 want: FrameDescriptionEntries{ 128 &FrameDescriptionEntry{begin: 10, size: 40}, 129 &FrameDescriptionEntry{begin: 50, size: 50}, 130 &FrameDescriptionEntry{begin: 100, size: 100}, 131 &FrameDescriptionEntry{begin: 300, size: 10}, 132 }, 133 }, 134 } 135 for _, test := range appendTests { 136 if got := test.f1.Append(test.f2); !equal(got, test.want) { 137 t.Errorf("%v.Append(%v) = %v, want %v", test.f1, test.f2, got, test.want) 138 } 139 } 140 } 141 142 func BenchmarkFDEForPC(b *testing.B) { 143 f, err := os.Open("testdata/frame") 144 if err != nil { 145 b.Fatal(err) 146 } 147 defer f.Close() 148 149 data, err := ioutil.ReadAll(f) 150 if err != nil { 151 b.Fatal(err) 152 } 153 fdes, _ := Parse(data, binary.BigEndian, 0, ptrSizeByRuntimeArch(), 0) 154 155 for i := 0; i < b.N; i++ { 156 // bench worst case, exhaustive search 157 _, _ = fdes.FDEForPC(0x455555555) 158 } 159 }