vid_map_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package wdclient
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestLocationIndex(t *testing.T) {
  7. vm := vidMap{}
  8. // test must be failed
  9. mustFailed := func(length int) {
  10. _, err := vm.getLocationIndex(length)
  11. if err == nil {
  12. t.Errorf("length %d must be failed", length)
  13. }
  14. if err.Error() != fmt.Sprintf("invalid length: %d", length) {
  15. t.Errorf("length %d must be failed. error: %v", length, err)
  16. }
  17. }
  18. mustFailed(-1)
  19. mustFailed(0)
  20. mustOk := func(length, cursor, expect int) {
  21. if length <= 0 {
  22. t.Fatal("please don't do this")
  23. }
  24. vm.cursor = int32(cursor)
  25. got, err := vm.getLocationIndex(length)
  26. if err != nil {
  27. t.Errorf("length: %d, why? %v\n", length, err)
  28. return
  29. }
  30. if got != expect {
  31. t.Errorf("cursor: %d, length: %d, expect: %d, got: %d\n", cursor, length, expect, got)
  32. return
  33. }
  34. }
  35. for i := -1; i < 100; i++ {
  36. mustOk(7, i, (i+1)%7)
  37. }
  38. // when cursor reaches MaxInt64
  39. mustOk(7, maxCursorIndex, 0)
  40. // test with constructor
  41. vm = newVidMap()
  42. length := 7
  43. for i := 0; i < 100; i++ {
  44. got, err := vm.getLocationIndex(length)
  45. if err != nil {
  46. t.Errorf("length: %d, why? %v\n", length, err)
  47. return
  48. }
  49. if got != i%length {
  50. t.Errorf("length: %d, i: %d, got: %d\n", length, i, got)
  51. }
  52. }
  53. }
  54. func BenchmarkLocationIndex(b *testing.B) {
  55. b.SetParallelism(8)
  56. vm := vidMap{
  57. cursor: maxCursorIndex - 4000,
  58. }
  59. b.ResetTimer()
  60. b.RunParallel(func(pb *testing.PB) {
  61. for pb.Next() {
  62. _, err := vm.getLocationIndex(3)
  63. if err != nil {
  64. b.Error(err)
  65. }
  66. }
  67. })
  68. }