Skip to content

Commit

Permalink
test: add more tests (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Dec 30, 2021
1 parent dfc67b5 commit e4ab518
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions core/search/tree_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package search

import (
"math/rand"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/stringx"
)

type mockedRoute struct {
Expand Down Expand Up @@ -139,11 +142,9 @@ func TestStrictSearchSibling(t *testing.T) {
tree.Add(r.route, r.value)
}

for i := 0; i < 1000; i++ {
result, ok := tree.Search(query)
assert.True(t, ok)
assert.Equal(t, 3, result.Item.(int))
}
result, ok := tree.Search(query)
assert.True(t, ok)
assert.Equal(t, 3, result.Item.(int))
}

func TestAddDuplicate(t *testing.T) {
Expand Down Expand Up @@ -185,3 +186,41 @@ func TestSearchInvalidItem(t *testing.T) {
err := tree.Add("/", nil)
assert.Equal(t, errEmptyItem, err)
}

func BenchmarkSearchTree(b *testing.B) {
const (
avgLen = 1000
entries = 10000
)

tree := NewTree()
generate := func() string {
var buf strings.Builder
size := rand.Intn(avgLen) + avgLen/2
val := stringx.Randn(size)
prev := 0
for j := rand.Intn(9) + 1; j < size; j += rand.Intn(9) + 1 {
buf.WriteRune('/')
buf.WriteString(val[prev:j])
prev = j
}
if prev < size {
buf.WriteRune('/')
buf.WriteString(val[prev:])
}
return buf.String()
}
index := rand.Intn(entries)
var query string
for i := 0; i < entries; i++ {
val := generate()
if i == index {
query = val
}
tree.Add(val, i)
}

for i := 0; i < b.N; i++ {
tree.Search(query)
}
}

0 comments on commit e4ab518

Please sign in to comment.