seaweedfs/weed/util/skiplist/name_list_test.go

74 lines
1.2 KiB
Go
Raw Normal View History

2021-10-04 08:54:25 +08:00
package skiplist
import (
"math/rand"
"strconv"
"testing"
)
const (
maxNameCount = 100
)
func String(x int) string {
return strconv.Itoa(x)
}
func TestNameList(t *testing.T) {
2021-10-04 16:01:31 +08:00
list := newNameList(memStore, 7)
2021-10-04 08:54:25 +08:00
for i := 0; i < maxNameCount; i++ {
list.WriteName(String(i))
}
counter := 0
list.ListNames("", func(name string) bool {
counter++
print(name, " ")
return true
})
if counter != maxNameCount {
t.Fail()
}
// list.skipList.println()
deleteBase := 5
2021-10-08 12:13:31 +08:00
deleteCount := maxNameCount - 3*deleteBase
2021-10-04 08:54:25 +08:00
for i := deleteBase; i < deleteBase+deleteCount; i++ {
list.DeleteName(String(i))
}
counter = 0
list.ListNames("", func(name string) bool {
counter++
return true
})
// list.skipList.println()
if counter != maxNameCount-deleteCount {
t.Fail()
}
// randomized deletion
2021-10-04 16:01:31 +08:00
list = newNameList(memStore, 7)
2021-10-04 08:54:25 +08:00
// Delete elements at random positions in the list.
rList := rand.Perm(maxN)
for _, i := range rList {
list.WriteName(String(i))
}
for _, i := range rList {
list.DeleteName(String(i))
}
counter = 0
list.ListNames("", func(name string) bool {
counter++
print(name, " ")
return true
})
if counter != 0 {
t.Fail()
}
}