Skip to content

Commit

Permalink
chore: Move mock struct to test file (#3177)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls authored Apr 6, 2024
1 parent e3e48ee commit 94564fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
36 changes: 0 additions & 36 deletions ebpf/symtab/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,6 @@ import (
"sort"
)

type SymTab struct {
Symbols []Sym
base uint64
}

type Sym struct {
Start uint64
Name string
}

func NewSymTab(symbols []Sym) *SymTab {
return &SymTab{Symbols: symbols}
}

func (t *SymTab) Rebase(base uint64) {
t.base = base
}

func (t *SymTab) Resolve(addr uint64) *Sym {
if len(t.Symbols) == 0 {
return nil
}
addr -= t.base
if addr < t.Symbols[0].Start {
return nil
}
i := sort.Search(len(t.Symbols), func(i int) bool {
return addr < t.Symbols[i].Start
})
i--
return &t.Symbols[i]
}
func (t *SymTab) Length() int {
return len(t.Symbols)
}

type SymbolTab struct {
symbols []Symbol
base uint64
Expand Down
40 changes: 39 additions & 1 deletion ebpf/symtab/table_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
package symtab

import (
"sort"
"testing"

"github.com/stretchr/testify/require"
)

type MockSymTab struct {
Symbols []MockSym
base uint64
}

type MockSym struct {
Start uint64
Name string
}

func NewSymTab(symbols []MockSym) *MockSymTab {
return &MockSymTab{Symbols: symbols}
}

func (t *MockSymTab) Rebase(base uint64) {
t.base = base
}

func (t *MockSymTab) Resolve(addr uint64) *MockSym {
if len(t.Symbols) == 0 {
return nil
}
addr -= t.base
if addr < t.Symbols[0].Start {
return nil
}
i := sort.Search(len(t.Symbols), func(i int) bool {
return addr < t.Symbols[i].Start
})
i--
return &t.Symbols[i]
}

func (t *MockSymTab) Length() int {
return len(t.Symbols)
}

func TestSymTab(t *testing.T) {
sym := NewSymTab([]Sym{
sym := NewSymTab([]MockSym{
{0x1000, "0x1000"},
{0x1200, "0x1200"},
{0x1300, "0x1300"},
Expand Down

0 comments on commit 94564fa

Please sign in to comment.