util_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package chord
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. )
  7. func Test_isEqual(t *testing.T) {
  8. type args struct {
  9. a []byte
  10. b []byte
  11. }
  12. tests := []struct {
  13. name string
  14. args args
  15. want bool
  16. }{
  17. // TODO: Add test cases.
  18. }
  19. for _, tt := range tests {
  20. t.Run(tt.name, func(t *testing.T) {
  21. if got := isEqual(tt.args.a, tt.args.b); got != tt.want {
  22. t.Errorf("isEqual() = %v, want %v", got, tt.want)
  23. }
  24. })
  25. }
  26. }
  27. func Test_isPowerOfTwo(t *testing.T) {
  28. type args struct {
  29. num int
  30. }
  31. tests := []struct {
  32. name string
  33. args args
  34. want bool
  35. }{
  36. // TODO: Add test cases.
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. if got := isPowerOfTwo(tt.args.num); got != tt.want {
  41. t.Errorf("isPowerOfTwo() = %v, want %v", got, tt.want)
  42. }
  43. })
  44. }
  45. }
  46. func Test_randStabilize(t *testing.T) {
  47. type args struct {
  48. min time.Duration
  49. max time.Duration
  50. }
  51. tests := []struct {
  52. name string
  53. args args
  54. want time.Duration
  55. }{
  56. // TODO: Add test cases.
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. if got := randStabilize(tt.args.min, tt.args.max); got != tt.want {
  61. t.Errorf("randStabilize() = %v, want %v", got, tt.want)
  62. }
  63. })
  64. }
  65. }
  66. func TestRL(t *testing.T) {
  67. t.Parallel()
  68. min := GetHashID("0.0.0.0:8081")
  69. max := GetHashID("0.0.0.0:8083")
  70. for i := 2; i < 100; i++ {
  71. val := strconv.Itoa(i)
  72. key := GetHashID(val)
  73. if got := betweenRightIncl(key, min, max); got != true {
  74. t.Errorf("betweenRightIncl() %s %x = %v, want %v", val, key, got, true)
  75. }
  76. }
  77. }
  78. func Test_betweenRightIncl(t *testing.T) {
  79. t.Parallel()
  80. type args struct {
  81. key []byte
  82. a []byte
  83. b []byte
  84. }
  85. tests := []struct {
  86. name string
  87. args args
  88. want bool
  89. }{
  90. {"1", args{[]byte{1, 0, 0, 0}, []byte{0, 0, 0, 0}, []byte{1, 0, 0, 0}}, true},
  91. {"2", args{[]byte{1, 1, 1, 1}, []byte{1, 1, 1, 0}, []byte{1, 1, 1, 1}}, true},
  92. {"3", args{[]byte{1, 1, 1, 1, 1}, []byte{0}, []byte{1, 1, 1, 1}}, false},
  93. {"4", args{[]byte{1, 1, 1, 1, 1}, []byte{0}, []byte{1, 1, 1, 1, 1, 1}}, true},
  94. {
  95. "5",
  96. args{
  97. []byte{4, 40, 171},
  98. []byte{53, 106, 25, 43, 121, 19, 176, 76, 84, 87, 77, 24, 194, 141, 70, 230, 57, 84, 40, 171},
  99. []byte{4, 40, 171},
  100. },
  101. true,
  102. },
  103. {"6", args{GetHashID("11"), GetHashID("1"), GetHashID("20")}, true},
  104. }
  105. for _, tt := range tests {
  106. t.Run(tt.name, func(t *testing.T) {
  107. if got := betweenRightIncl(tt.args.key, tt.args.a, tt.args.b); got != tt.want {
  108. t.Errorf("betweenRightIncl() = %v, want %v", got, tt.want)
  109. }
  110. })
  111. }
  112. }
  113. func Test_between(t *testing.T) {
  114. type args struct {
  115. key []byte
  116. a []byte
  117. b []byte
  118. }
  119. tests := []struct {
  120. name string
  121. args args
  122. want bool
  123. }{
  124. // TODO: Add test cases.
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. if got := between(tt.args.key, tt.args.a, tt.args.b); got != tt.want {
  129. t.Errorf("between() = %v, want %v", got, tt.want)
  130. }
  131. })
  132. }
  133. }