2020-02-06 04:08:36 +00:00
|
|
|
package dbutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHistoryIndex_Search1(t *testing.T) {
|
2020-04-25 14:50:32 +00:00
|
|
|
index := NewHistoryIndex().Append(3, false).Append(5, false).Append(8, false)
|
|
|
|
v, _, _ := index.Search(1)
|
2020-02-06 04:08:36 +00:00
|
|
|
if v != 3 {
|
|
|
|
t.Fatal("must be 3 but", v)
|
|
|
|
}
|
2020-04-25 14:50:32 +00:00
|
|
|
v, _, _ = index.Search(3)
|
2020-02-06 04:08:36 +00:00
|
|
|
if v != 3 {
|
|
|
|
t.Fatal("must be 3")
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:50:32 +00:00
|
|
|
v, _, _ = index.Search(4)
|
2020-02-06 04:08:36 +00:00
|
|
|
if v != 5 {
|
|
|
|
t.Fatal("must be 5")
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:50:32 +00:00
|
|
|
v, _, _ = index.Search(5)
|
2020-02-06 04:08:36 +00:00
|
|
|
if v != 5 {
|
|
|
|
t.Fatal("must be 5")
|
|
|
|
}
|
2020-04-25 14:50:32 +00:00
|
|
|
v, _, _ = index.Search(7)
|
2020-02-06 04:08:36 +00:00
|
|
|
if v != 8 {
|
|
|
|
t.Fatal("must be 8")
|
|
|
|
}
|
2020-04-25 14:50:32 +00:00
|
|
|
v, _, _ = index.Search(8)
|
2020-02-06 04:08:36 +00:00
|
|
|
if v != 8 {
|
|
|
|
t.Fatal("must be 8")
|
|
|
|
}
|
2020-04-25 14:50:32 +00:00
|
|
|
_, _, b := index.Search(9)
|
2020-02-06 04:08:36 +00:00
|
|
|
if b {
|
|
|
|
t.Fatal("must be not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryIndex_Search_EmptyIndex(t *testing.T) {
|
2020-04-25 14:50:32 +00:00
|
|
|
index := NewHistoryIndex()
|
|
|
|
_, _, b := index.Search(1)
|
2020-02-06 04:08:36 +00:00
|
|
|
if b {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistoryIndex_Append(t *testing.T) {
|
|
|
|
index := NewHistoryIndex()
|
|
|
|
for i := uint64(1); i < 10; i++ {
|
2020-04-25 14:50:32 +00:00
|
|
|
index = index.Append(i, false)
|
2020-02-06 04:08:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 14:50:32 +00:00
|
|
|
res, _, err := index.Decode()
|
2020-02-06 04:08:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}) {
|
|
|
|
t.Fatal("Not equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if index.Len() != 9 {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 10:22:48 +00:00
|
|
|
|
|
|
|
func TestHistoryIndex_Idempotent(t *testing.T) {
|
|
|
|
index := NewHistoryIndex()
|
|
|
|
for i := uint64(1); i < 10; i++ {
|
|
|
|
index = index.Append(i, false)
|
|
|
|
}
|
|
|
|
oldLen := len(index)
|
|
|
|
for i := uint64(1); i < 10; i++ {
|
|
|
|
index = index.Append(i, false)
|
|
|
|
if len(index) != oldLen {
|
|
|
|
t.Errorf("index is not idempotent, managed to append %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index = index.Append(11, false)
|
|
|
|
if len(index) != oldLen+3 {
|
|
|
|
t.Errorf("appending after the last element should still work: %d != %d", len(index), oldLen+3)
|
|
|
|
}
|
|
|
|
}
|