seaweedfs/weed/util/skiplist/skiplist_test.go

291 lines
5.8 KiB
Go
Raw Normal View History

2021-10-02 21:02:56 +00:00
package skiplist
import (
"bytes"
"math/rand"
"strconv"
"testing"
)
const (
maxN = 10000
)
2021-10-03 09:19:21 +00:00
var (
memStore = newMemStore()
)
2021-10-04 09:30:24 +00:00
func TestReverseInsert(t *testing.T) {
list := NewSeed(100, memStore)
2021-10-07 01:18:24 +00:00
list.InsertByKey([]byte("zzz"), 0, []byte("zzz"))
list.DeleteByKey([]byte("zzz"))
2021-10-04 09:30:24 +00:00
2021-10-07 01:18:24 +00:00
list.InsertByKey([]byte("aaa"), 0, []byte("aaa"))
2021-10-04 09:30:24 +00:00
if list.IsEmpty() {
t.Fail()
}
}
2021-10-02 21:02:56 +00:00
func TestInsertAndFind(t *testing.T) {
k0 := []byte("0")
var list *SkipList
var listPointer *SkipList
2021-10-07 01:18:24 +00:00
listPointer.InsertByKey(k0, 0, k0)
2021-10-03 20:50:52 +00:00
if _, _, ok, _ := listPointer.Find(k0); ok {
2021-10-02 21:02:56 +00:00
t.Fail()
}
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-03 20:50:52 +00:00
if _, _, ok, _ := list.Find(k0); ok {
2021-10-02 21:02:56 +00:00
t.Fail()
}
if !list.IsEmpty() {
t.Fail()
}
// Test at the beginning of the list.
for i := 0; i < maxN; i++ {
2021-10-03 09:19:21 +00:00
key := []byte(strconv.Itoa(maxN - i))
2021-10-07 01:18:24 +00:00
list.InsertByKey(key, 0, key)
2021-10-02 21:02:56 +00:00
}
for i := 0; i < maxN; i++ {
2021-10-03 09:19:21 +00:00
key := []byte(strconv.Itoa(maxN - i))
2021-10-03 20:50:52 +00:00
if _, _, ok, _ := list.Find(key); !ok {
2021-10-02 21:02:56 +00:00
t.Fail()
}
}
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-02 21:02:56 +00:00
// Test at the end of the list.
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i))
2021-10-07 01:18:24 +00:00
list.InsertByKey(key, 0, key)
2021-10-02 21:02:56 +00:00
}
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i))
2021-10-03 20:50:52 +00:00
if _, _, ok, _ := list.Find(key); !ok {
2021-10-02 21:02:56 +00:00
t.Fail()
}
}
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-02 21:02:56 +00:00
// Test at random positions in the list.
rList := rand.Perm(maxN)
for _, e := range rList {
key := []byte(strconv.Itoa(e))
2021-10-03 08:07:35 +00:00
// println("insert", e)
2021-10-07 01:18:24 +00:00
list.InsertByKey(key, 0, key)
2021-10-02 21:02:56 +00:00
}
for _, e := range rList {
key := []byte(strconv.Itoa(e))
2021-10-03 08:07:35 +00:00
// println("find", e)
2021-10-03 20:50:52 +00:00
if _, _, ok, _ := list.Find(key); !ok {
2021-10-02 21:02:56 +00:00
t.Fail()
}
}
2021-10-03 08:07:35 +00:00
// println("print list")
2022-09-27 20:12:31 +00:00
// list.println()
2021-10-02 21:02:56 +00:00
}
func Element(x int) []byte {
return []byte(strconv.Itoa(x))
}
func TestDelete(t *testing.T) {
k0 := []byte("0")
var list *SkipList
// Delete on empty list
2021-10-07 01:18:24 +00:00
list.DeleteByKey(k0)
2021-10-02 21:02:56 +00:00
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-02 21:02:56 +00:00
2021-10-07 01:18:24 +00:00
list.DeleteByKey(k0)
2021-10-02 21:02:56 +00:00
if !list.IsEmpty() {
t.Fail()
}
2021-10-07 01:18:24 +00:00
list.InsertByKey(k0, 0, k0)
list.DeleteByKey(k0)
2021-10-02 21:02:56 +00:00
if !list.IsEmpty() {
t.Fail()
}
// Delete elements at the beginning of the list.
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(i), 0, Element(i))
2021-10-02 21:02:56 +00:00
}
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.DeleteByKey(Element(i))
2021-10-02 21:02:56 +00:00
}
if !list.IsEmpty() {
t.Fail()
}
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-02 21:02:56 +00:00
// Delete elements at the end of the list.
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(i), 0, Element(i))
2021-10-02 21:02:56 +00:00
}
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.DeleteByKey(Element(maxN - i - 1))
2021-10-02 21:02:56 +00:00
}
if !list.IsEmpty() {
t.Fail()
}
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-02 21:02:56 +00:00
// Delete elements at random positions in the list.
rList := rand.Perm(maxN)
for _, e := range rList {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(e), 0, Element(e))
2021-10-02 21:02:56 +00:00
}
for _, e := range rList {
2021-10-07 01:18:24 +00:00
list.DeleteByKey(Element(e))
2021-10-02 21:02:56 +00:00
}
if !list.IsEmpty() {
t.Fail()
}
}
func TestNext(t *testing.T) {
2021-10-03 09:19:21 +00:00
list := New(memStore)
2021-10-02 21:02:56 +00:00
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(i), 0, Element(i))
2021-10-02 21:02:56 +00:00
}
2021-10-03 09:19:21 +00:00
smallest, _ := list.GetSmallestNode()
largest, _ := list.GetLargestNode()
2021-10-02 21:02:56 +00:00
lastNode := smallest
node := lastNode
for node != largest {
2021-10-03 09:19:21 +00:00
node, _ = list.Next(node)
2021-10-02 21:02:56 +00:00
// Must always be incrementing here!
2021-10-03 08:07:35 +00:00
if bytes.Compare(node.Key, lastNode.Key) <= 0 {
2021-10-02 21:02:56 +00:00
t.Fail()
}
// Next.Prev must always point to itself!
2021-10-03 09:19:21 +00:00
prevNode, _ := list.Prev(node)
nextNode, _ := list.Next(prevNode)
if nextNode != node {
2021-10-02 21:02:56 +00:00
t.Fail()
}
lastNode = node
}
2021-10-03 09:19:21 +00:00
if nextNode, _ := list.Next(largest); nextNode != smallest {
2021-10-02 21:02:56 +00:00
t.Fail()
}
}
func TestPrev(t *testing.T) {
2021-10-03 09:19:21 +00:00
list := New(memStore)
2021-10-02 21:02:56 +00:00
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(i), 0, Element(i))
2021-10-02 21:02:56 +00:00
}
2021-10-03 09:19:21 +00:00
smallest, _ := list.GetSmallestNode()
largest, _ := list.GetLargestNode()
2021-10-02 21:02:56 +00:00
lastNode := largest
node := lastNode
for node != smallest {
2021-10-03 09:19:21 +00:00
node, _ = list.Prev(node)
2021-10-02 21:02:56 +00:00
// Must always be incrementing here!
2021-10-03 08:07:35 +00:00
if bytes.Compare(node.Key, lastNode.Key) >= 0 {
2021-10-02 21:02:56 +00:00
t.Fail()
}
// Next.Prev must always point to itself!
2021-10-03 09:19:21 +00:00
nextNode, _ := list.Next(node)
prevNode, _ := list.Prev(nextNode)
if prevNode != node {
2021-10-02 21:02:56 +00:00
t.Fail()
}
lastNode = node
}
2021-10-03 09:19:21 +00:00
if prevNode, _ := list.Prev(smallest); prevNode != largest {
2021-10-02 21:02:56 +00:00
t.Fail()
}
}
2021-10-02 21:15:49 +00:00
func TestFindGreaterOrEqual(t *testing.T) {
maxNumber := maxN * 100
var list *SkipList
var listPointer *SkipList
// Test on empty list.
2021-10-03 20:50:52 +00:00
if _, _, ok, _ := listPointer.FindGreaterOrEqual(Element(0)); ok {
2021-10-14 03:42:16 +00:00
t.Errorf("found element 0 in an empty list")
2021-10-02 21:15:49 +00:00
}
2021-10-03 09:19:21 +00:00
list = New(memStore)
2021-10-02 21:15:49 +00:00
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(rand.Intn(maxNumber)), 0, Element(i))
2021-10-02 21:15:49 +00:00
}
for i := 0; i < maxN; i++ {
key := Element(rand.Intn(maxNumber))
2021-10-03 20:50:52 +00:00
if _, v, ok, _ := list.FindGreaterOrEqual(key); ok {
2021-10-02 21:15:49 +00:00
// if f is v should be bigger than the element before
2021-10-14 05:38:47 +00:00
if v.Prev != nil && bytes.Compare(key, v.Prev.Key) < 0 {
t.Errorf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key))
2021-10-02 21:15:49 +00:00
}
// v should be bigger or equal to f
// If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails.
2021-10-03 08:07:35 +00:00
if bytes.Compare(v.Key, key) < 0 {
2021-10-14 03:42:16 +00:00
t.Errorf("v: %s\n key: %s\n\n", string(v.Key), string(key))
2021-10-02 21:15:49 +00:00
}
} else {
2021-10-03 09:19:21 +00:00
lastNode, _ := list.GetLargestNode()
lastV := lastNode.GetValue()
2021-10-02 21:15:49 +00:00
// It is OK, to fail, as long as f is bigger than the last element.
if bytes.Compare(key, lastV) <= 0 {
2021-10-14 03:42:16 +00:00
t.Errorf("lastV: %s\n key: %s\n\n", string(lastV), string(key))
2021-10-02 21:15:49 +00:00
}
}
}
2021-10-03 08:15:14 +00:00
}
func TestChangeValue(t *testing.T) {
2021-10-03 09:19:21 +00:00
list := New(memStore)
2021-10-03 08:15:14 +00:00
for i := 0; i < maxN; i++ {
2021-10-07 01:18:24 +00:00
list.InsertByKey(Element(i), 0, []byte("value"))
2021-10-03 08:15:14 +00:00
}
for i := 0; i < maxN; i++ {
// The key only looks at the int so the string doesn't matter here!
2021-10-03 20:50:52 +00:00
_, f1, ok, _ := list.Find(Element(i))
2021-10-03 08:15:14 +00:00
if !ok {
t.Fail()
}
2021-10-03 09:19:21 +00:00
err := list.ChangeValue(f1, []byte("different value"))
if err != nil {
2021-10-03 08:15:14 +00:00
t.Fail()
}
2021-10-03 20:50:52 +00:00
_, f2, ok, _ := list.Find(Element(i))
2021-10-03 08:15:14 +00:00
if !ok {
t.Fail()
}
if bytes.Compare(f2.GetValue(), []byte("different value")) != 0 {
t.Fail()
}
}
}