2021-10-02 21:02:56 +00:00
|
|
|
package skiplist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-02 21:15:49 +00:00
|
|
|
"fmt"
|
2021-10-02 21:02:56 +00:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxN = 10000
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInsertAndFind(t *testing.T) {
|
|
|
|
|
|
|
|
k0 := []byte("0")
|
|
|
|
var list *SkipList
|
|
|
|
|
|
|
|
var listPointer *SkipList
|
2021-10-03 08:07:35 +00:00
|
|
|
listPointer.Insert(k0, k0)
|
2021-10-02 21:02:56 +00:00
|
|
|
if _, ok := listPointer.Find(k0); ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
if _, ok := list.Find(k0); ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !list.IsEmpty() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at the beginning of the list.
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
key := []byte(strconv.Itoa(maxN-i))
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(key, key)
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
key := []byte(strconv.Itoa(maxN-i))
|
|
|
|
if _, ok := list.Find(key); !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
// Test at the end of the list.
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
key := []byte(strconv.Itoa(i))
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(key, key)
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
key := []byte(strconv.Itoa(i))
|
|
|
|
if _, ok := list.Find(key); !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
// 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)
|
|
|
|
list.Insert(key, 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-02 21:02:56 +00:00
|
|
|
if _, ok := list.Find(key); !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 08:07:35 +00:00
|
|
|
// println("print list")
|
2021-10-02 21:02:56 +00:00
|
|
|
list.println()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
list.Delete(k0)
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
|
|
|
|
list.Delete(k0)
|
|
|
|
if !list.IsEmpty() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(k0, k0)
|
2021-10-02 21:02:56 +00:00
|
|
|
list.Delete(k0)
|
|
|
|
if !list.IsEmpty() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete elements at the beginning of the list.
|
|
|
|
for i := 0; i < maxN; i++ {
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(Element(i), Element(i))
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
list.Delete(Element(i))
|
|
|
|
}
|
|
|
|
if !list.IsEmpty() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
// Delete elements at the end of the list.
|
|
|
|
for i := 0; i < maxN; i++ {
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(Element(i), Element(i))
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
list.Delete(Element(maxN - i - 1))
|
|
|
|
}
|
|
|
|
if !list.IsEmpty() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
// Delete elements at random positions in the list.
|
|
|
|
rList := rand.Perm(maxN)
|
|
|
|
for _, e := range rList {
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(Element(e), Element(e))
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
for _, e := range rList {
|
|
|
|
list.Delete(Element(e))
|
|
|
|
}
|
|
|
|
if !list.IsEmpty() {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNext(t *testing.T) {
|
|
|
|
list := New()
|
|
|
|
|
|
|
|
for i := 0; i < maxN; i++ {
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(Element(i), Element(i))
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
smallest := list.GetSmallestNode()
|
|
|
|
largest := list.GetLargestNode()
|
|
|
|
|
|
|
|
lastNode := smallest
|
|
|
|
node := lastNode
|
|
|
|
for node != largest {
|
|
|
|
node = list.Next(node)
|
|
|
|
// 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!
|
|
|
|
if list.Next(list.Prev(node)) != node {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
lastNode = node
|
|
|
|
}
|
|
|
|
|
|
|
|
if list.Next(largest) != smallest {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrev(t *testing.T) {
|
|
|
|
list := New()
|
|
|
|
|
|
|
|
for i := 0; i < maxN; i++ {
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(Element(i), Element(i))
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
smallest := list.GetSmallestNode()
|
|
|
|
largest := list.GetLargestNode()
|
|
|
|
|
|
|
|
lastNode := largest
|
|
|
|
node := lastNode
|
|
|
|
for node != smallest {
|
|
|
|
node = list.Prev(node)
|
|
|
|
// 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!
|
|
|
|
if list.Prev(list.Next(node)) != node {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
lastNode = node
|
|
|
|
}
|
|
|
|
|
|
|
|
if list.Prev(smallest) != largest {
|
|
|
|
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.
|
|
|
|
if _, ok := listPointer.FindGreaterOrEqual(Element(0)); ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
list = New()
|
|
|
|
|
|
|
|
for i := 0; i < maxN; i++ {
|
2021-10-03 08:07:35 +00:00
|
|
|
list.Insert(Element(rand.Intn(maxNumber)), Element(i))
|
2021-10-02 21:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
key := Element(rand.Intn(maxNumber))
|
|
|
|
if v, ok := list.FindGreaterOrEqual(key); ok {
|
|
|
|
// if f is v should be bigger than the element before
|
2021-10-03 08:07:35 +00:00
|
|
|
if v.Prev != nil && bytes.Compare(v.Prev.Key, key) >= 0 {
|
2021-10-02 21:15:49 +00:00
|
|
|
fmt.Printf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key))
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
// 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 {
|
|
|
|
fmt.Printf("v: %s\n key: %s\n\n", string(v.Key), string(key))
|
2021-10-02 21:15:49 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lastV := list.GetLargestNode().GetValue()
|
|
|
|
// It is OK, to fail, as long as f is bigger than the last element.
|
|
|
|
if bytes.Compare(key, lastV) <= 0 {
|
|
|
|
fmt.Printf("lastV: %s\n key: %s\n\n", string(lastV), string(key))
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 08:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeValue(t *testing.T) {
|
|
|
|
list := New()
|
|
|
|
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
list.Insert(Element(i), []byte("value"))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < maxN; i++ {
|
|
|
|
// The key only looks at the int so the string doesn't matter here!
|
|
|
|
f1, ok := list.Find(Element(i))
|
|
|
|
if !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
ok = list.ChangeValue(f1, []byte("different value"))
|
|
|
|
if !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
f2, ok := list.Find(Element(i))
|
|
|
|
if !ok {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if bytes.Compare(f2.GetValue(), []byte("different value")) != 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|