forked from p53841790/wheat-cache
test(util): test skiplist
This commit is contained in:
parent
5776e0ba6e
commit
b61794d74b
|
@ -0,0 +1,68 @@
|
|||
package skiplist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/timedb/wheatCache/pkg/logx"
|
||||
"github.com/stretchr/testify/require"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 时间测试
|
||||
func TestSkiplist_InsertTimeB(t *testing.T) {
|
||||
// 测试 达到 maxLevel = 18 时性能 OK
|
||||
list := NewSkipList(18)
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
list.Insert(float64(i), i)
|
||||
}
|
||||
list.debugPrint()
|
||||
|
||||
}
|
||||
|
||||
func TestSkiplist_InsertTimeA(t *testing.T) {
|
||||
// 测试 达到 maxLevel = 18 时性能 OK
|
||||
list := NewSkipList(20)
|
||||
|
||||
for i := 0; i < 200; i++ {
|
||||
list.Insert(float64(rand.Intn(100)), i)
|
||||
}
|
||||
list.debugPrint()
|
||||
|
||||
start := time.Now()
|
||||
list.Insert(7890, 1)
|
||||
end := time.Now()
|
||||
|
||||
fmt.Println(end.UnixNano() - start.UnixNano())
|
||||
}
|
||||
|
||||
func TestSkipList_Insert(t *testing.T) {
|
||||
s := NewSkipList(18)
|
||||
s.Insert(20, 1)
|
||||
s.Insert(30, 2)
|
||||
s.Insert(11, 3)
|
||||
s.Insert(20, 11)
|
||||
val := s.Get(30)
|
||||
require.Equal(t, 2, val)
|
||||
val = s.Get(11)
|
||||
require.Equal(t, 3, val)
|
||||
|
||||
require.Equal(t, s.GetAll(20), []interface{}{1, 11})
|
||||
|
||||
s.RemoveAll(20)
|
||||
require.Equal(t, s.GetAll(20), []interface{}{})
|
||||
}
|
||||
|
||||
func Test_skipList_ClearLeft(t *testing.T) {
|
||||
s := NewSkipList(18)
|
||||
for i := 0; i < 100; i++ {
|
||||
s.Insert(float64(rand.Intn(100)), i)
|
||||
}
|
||||
|
||||
val := s.PopLeft(50)
|
||||
logx.Debug("val:%v", val)
|
||||
s.debugPrint()
|
||||
require.Equal(t, s.Get(20), nil)
|
||||
|
||||
}
|
Loading…
Reference in New Issue