|
|
@ -1,9 +1,12 @@
|
|
|
|
package search
|
|
|
|
package search
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/stringx"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type mockedRoute struct {
|
|
|
|
type mockedRoute struct {
|
|
|
@ -139,11 +142,9 @@ func TestStrictSearchSibling(t *testing.T) {
|
|
|
|
tree.Add(r.route, r.value)
|
|
|
|
tree.Add(r.route, r.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
result, ok := tree.Search(query)
|
|
|
|
result, ok := tree.Search(query)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, 3, result.Item.(int))
|
|
|
|
assert.Equal(t, 3, result.Item.(int))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestAddDuplicate(t *testing.T) {
|
|
|
|
func TestAddDuplicate(t *testing.T) {
|
|
|
@ -185,3 +186,41 @@ func TestSearchInvalidItem(t *testing.T) {
|
|
|
|
err := tree.Add("/", nil)
|
|
|
|
err := tree.Add("/", nil)
|
|
|
|
assert.Equal(t, errEmptyItem, err)
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|