Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup the test suite #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions forceexport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package forceexport

import (
"fmt"
"reflect"
"runtime"
"testing"
)

// funcName resolves the name of a given function
func funcName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}

func TestTimeNow(t *testing.T) {
var timeNowFunc func() (int64, int32)
GetFunc(&timeNowFunc, "time.now")
Expand Down Expand Up @@ -41,19 +48,19 @@ func TestGetSelf(t *testing.T) {
var getFunc func(interface{}, string) error
err := GetFunc(&getFunc, "github.com/alangpierce/go-forceexport.GetFunc")
if err != nil {
t.Error("Error: %s", err)
t.Errorf("Error: %s", err)
}
// The two functions should share the same code pointer, so they should
// have the same string representation.
if fmt.Sprint(getFunc) != fmt.Sprint(GetFunc) {
if fmt.Sprint(funcName(getFunc)) != fmt.Sprint(funcName(GetFunc)) {
t.Errorf("Expected ")
}
// Call it again on itself!
err = getFunc(&getFunc, "github.com/alangpierce/go-forceexport.GetFunc")
if err != nil {
t.Error("Error: %s", err)
t.Errorf("Error: %s", err)
}
if fmt.Sprint(getFunc) != fmt.Sprint(GetFunc) {
if fmt.Sprint(funcName(getFunc)) != fmt.Sprint(funcName(GetFunc)) {
t.Errorf("Expected ")
}
}
Expand All @@ -68,3 +75,12 @@ func TestInvalidFunc(t *testing.T) {
t.Error("Expected a nil function.")
}
}

// BenchmarkGetMainInit check how long it takes to find the symbol main.init,
// which is typically the last func symbol(by experiment).
func BenchmarkGetMainInit(b *testing.B) {
for i := 0; i < b.N; i++ {
var main_init func()
GetFunc(&main_init, "main.init")
}
}