2013-05-06 15:42:58 +04:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2013-05-13 09:36:54 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-05-06 15:42:58 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-05-13 09:36:54 +04:00
|
|
|
func TestInsertRefs(t *testing.T) {
|
2013-05-06 15:42:58 +04:00
|
|
|
|
2013-05-13 09:36:54 +04:00
|
|
|
refs := make([]int64, 0, 1)
|
2013-05-06 15:42:58 +04:00
|
|
|
|
2013-05-13 09:36:54 +04:00
|
|
|
refs = insertRefs(refs, 1)
|
|
|
|
if refs[0] != 1 {
|
2013-05-06 15:42:58 +04:00
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
|
2013-05-13 09:36:54 +04:00
|
|
|
refs = insertRefs(refs, 10)
|
|
|
|
if refs[0] != 1 && refs[1] != 10 {
|
2013-05-06 15:42:58 +04:00
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert twice
|
2013-05-13 09:36:54 +04:00
|
|
|
refs = insertRefs(refs, 10)
|
|
|
|
if refs[0] != 1 && refs[1] != 10 {
|
2013-05-06 15:42:58 +04:00
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert before
|
2013-05-13 09:36:54 +04:00
|
|
|
refs = insertRefs(refs, 0)
|
|
|
|
if refs[0] != 0 && refs[1] != 1 && refs[2] != 10 {
|
2013-05-06 15:42:58 +04:00
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert after
|
2013-05-13 09:36:54 +04:00
|
|
|
refs = insertRefs(refs, 12)
|
|
|
|
if refs[0] != 0 && refs[1] != 1 && refs[2] != 10 && refs[3] != 12 {
|
2013-05-06 15:42:58 +04:00
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert between
|
2013-05-13 09:36:54 +04:00
|
|
|
refs = insertRefs(refs, 11)
|
|
|
|
if refs[0] != 0 && refs[1] != 1 && refs[2] != 10 && refs[3] != 11 && refs[4] != 12 {
|
2013-05-06 15:42:58 +04:00
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-05-13 09:36:54 +04:00
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
// TODO write test for (Un)MarshalBunch
|
|
|
|
// func TestMarshalRefs(t *testing.T) {
|
|
|
|
// refs := []int64{1890166659, -1890166659, 0, 1890166, 1890167, 1890167, 1890165}
|
|
|
|
// buf := MarshalRefs(refs)
|
2013-05-13 09:36:54 +04:00
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
// t.Log(len(refs), len(buf))
|
|
|
|
// result := UnmarshalRefs(buf)
|
2013-05-13 09:36:54 +04:00
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
// if len(result) != len(refs) {
|
|
|
|
// t.Fatal(result)
|
|
|
|
// }
|
|
|
|
// for i, ref := range refs {
|
|
|
|
// if result[i] != ref {
|
|
|
|
// t.Fatal(result)
|
|
|
|
// }
|
|
|
|
// }
|
2013-05-13 09:36:54 +04:00
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
// }
|
2013-05-13 09:36:54 +04:00
|
|
|
|
|
|
|
func TestWriteDiff(t *testing.T) {
|
|
|
|
cache_dir, _ := ioutil.TempDir("", "goposm_test")
|
|
|
|
defer os.RemoveAll(cache_dir)
|
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
cache, err := newRefIndex(cache_dir, &globalCacheOptions.CoordsIndex)
|
2013-05-13 09:36:54 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
|
|
|
|
for w := 0; w < 5; w++ {
|
|
|
|
for n := 0; n < 200; n++ {
|
2013-07-05 10:03:13 +04:00
|
|
|
cache.buffer.add(cache.getBunchId(int64(n)), int64(n), int64(w))
|
2013-05-13 09:36:54 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-14 13:54:39 +04:00
|
|
|
cache.Close()
|
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
cache, err = newRefIndex(cache_dir, &globalCacheOptions.CoordsIndex)
|
2013-05-14 13:54:39 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cache.Close()
|
2013-05-13 09:36:54 +04:00
|
|
|
|
|
|
|
for n := 0; n < 200; n++ {
|
|
|
|
refs := cache.Get(int64(n))
|
|
|
|
if len(refs) != 5 {
|
|
|
|
t.Fatal(refs)
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 17:52:45 +04:00
|
|
|
|
2013-05-13 09:36:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkWriteDiff(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
cache_dir, _ := ioutil.TempDir("", "goposm_test")
|
|
|
|
defer os.RemoveAll(cache_dir)
|
|
|
|
|
2013-07-05 10:03:13 +04:00
|
|
|
cache, err := newRefIndex(cache_dir, &globalCacheOptions.CoordsIndex)
|
2013-05-13 09:36:54 +04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal()
|
|
|
|
}
|
|
|
|
defer cache.Close()
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for w := 0; w < 5; w++ {
|
|
|
|
for n := 0; n < 200; n++ {
|
2013-07-05 10:03:13 +04:00
|
|
|
cache.buffer.add(1, int64(n), int64(w))
|
2013-05-13 09:36:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|