2019-09-13 12:06:02 +00:00
|
|
|
package wdclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocationIndex(t *testing.T) {
|
|
|
|
vm := vidMap{}
|
|
|
|
// test must be failed
|
2019-09-14 08:21:51 +00:00
|
|
|
mustFailed := func(length int) {
|
2019-09-13 12:06:02 +00:00
|
|
|
_, err := vm.getLocationIndex(length)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("length %d must be failed", length)
|
|
|
|
}
|
|
|
|
if err.Error() != fmt.Sprintf("invalid length: %d", length) {
|
|
|
|
t.Errorf("length %d must be failed. error: %v", length, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mustFailed(-1)
|
|
|
|
mustFailed(0)
|
|
|
|
|
2019-09-14 08:21:51 +00:00
|
|
|
mustOk := func(length, cursor, expect int) {
|
2019-09-13 12:06:02 +00:00
|
|
|
if length <= 0 {
|
|
|
|
t.Fatal("please don't do this")
|
|
|
|
}
|
2019-09-14 08:21:51 +00:00
|
|
|
vm.cursor = int32(cursor)
|
2019-09-13 12:06:02 +00:00
|
|
|
got, err := vm.getLocationIndex(length)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("length: %d, why? %v\n", length, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != expect {
|
|
|
|
t.Errorf("cursor: %d, length: %d, expect: %d, got: %d\n", cursor, length, expect, got)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 08:21:51 +00:00
|
|
|
for i := -1; i < 100; i++ {
|
2019-09-13 12:06:02 +00:00
|
|
|
mustOk(7, i, (i+1)%7)
|
|
|
|
}
|
|
|
|
|
|
|
|
// when cursor reaches MaxInt64
|
2019-09-14 08:21:51 +00:00
|
|
|
mustOk(7, maxCursorIndex, 0)
|
2019-09-13 12:06:02 +00:00
|
|
|
|
|
|
|
// test with constructor
|
2020-11-11 21:13:33 +00:00
|
|
|
vm = newVidMap("")
|
2019-09-14 08:21:51 +00:00
|
|
|
length := 7
|
|
|
|
for i := 0; i < 100; i++ {
|
2019-09-13 12:06:02 +00:00
|
|
|
got, err := vm.getLocationIndex(length)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("length: %d, why? %v\n", length, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != i%length {
|
|
|
|
t.Errorf("length: %d, i: %d, got: %d\n", length, i, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLocationIndex(b *testing.B) {
|
|
|
|
b.SetParallelism(8)
|
|
|
|
vm := vidMap{
|
2019-09-14 08:21:51 +00:00
|
|
|
cursor: maxCursorIndex - 4000,
|
2019-09-13 12:06:02 +00:00
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
_, err := vm.getLocationIndex(3)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|