You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
4.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func randBitArray(bits int) (*BitArray, []byte) {
  8. src := RandBytes((bits + 7) / 8)
  9. bA := NewBitArray(bits)
  10. for i := 0; i < len(src); i++ {
  11. for j := 0; j < 8; j++ {
  12. if i*8+j >= bits {
  13. return bA, src
  14. }
  15. setBit := src[i]&(1<<uint(j)) > 0
  16. bA.SetIndex(i*8+j, setBit)
  17. }
  18. }
  19. return bA, src
  20. }
  21. func TestAnd(t *testing.T) {
  22. bA1, _ := randBitArray(51)
  23. bA2, _ := randBitArray(31)
  24. bA3 := bA1.And(bA2)
  25. var bNil *BitArray
  26. require.Equal(t, bNil.And(bA1), (*BitArray)(nil))
  27. require.Equal(t, bA1.And(nil), (*BitArray)(nil))
  28. require.Equal(t, bNil.And(nil), (*BitArray)(nil))
  29. if bA3.Bits != 31 {
  30. t.Error("Expected min bits", bA3.Bits)
  31. }
  32. if len(bA3.Elems) != len(bA2.Elems) {
  33. t.Error("Expected min elems length")
  34. }
  35. for i := 0; i < bA3.Bits; i++ {
  36. expected := bA1.GetIndex(i) && bA2.GetIndex(i)
  37. if bA3.GetIndex(i) != expected {
  38. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  39. }
  40. }
  41. }
  42. func TestOr(t *testing.T) {
  43. bA1, _ := randBitArray(51)
  44. bA2, _ := randBitArray(31)
  45. bA3 := bA1.Or(bA2)
  46. bNil := (*BitArray)(nil)
  47. require.Equal(t, bNil.Or(bA1), bA1)
  48. require.Equal(t, bA1.Or(nil), bA1)
  49. require.Equal(t, bNil.Or(nil), (*BitArray)(nil))
  50. if bA3.Bits != 51 {
  51. t.Error("Expected max bits")
  52. }
  53. if len(bA3.Elems) != len(bA1.Elems) {
  54. t.Error("Expected max elems length")
  55. }
  56. for i := 0; i < bA3.Bits; i++ {
  57. expected := bA1.GetIndex(i) || bA2.GetIndex(i)
  58. if bA3.GetIndex(i) != expected {
  59. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  60. }
  61. }
  62. }
  63. func TestSub1(t *testing.T) {
  64. bA1, _ := randBitArray(31)
  65. bA2, _ := randBitArray(51)
  66. bA3 := bA1.Sub(bA2)
  67. bNil := (*BitArray)(nil)
  68. require.Equal(t, bNil.Sub(bA1), (*BitArray)(nil))
  69. require.Equal(t, bA1.Sub(nil), (*BitArray)(nil))
  70. require.Equal(t, bNil.Sub(nil), (*BitArray)(nil))
  71. if bA3.Bits != bA1.Bits {
  72. t.Error("Expected bA1 bits")
  73. }
  74. if len(bA3.Elems) != len(bA1.Elems) {
  75. t.Error("Expected bA1 elems length")
  76. }
  77. for i := 0; i < bA3.Bits; i++ {
  78. expected := bA1.GetIndex(i)
  79. if bA2.GetIndex(i) {
  80. expected = false
  81. }
  82. if bA3.GetIndex(i) != expected {
  83. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  84. }
  85. }
  86. }
  87. func TestSub2(t *testing.T) {
  88. bA1, _ := randBitArray(51)
  89. bA2, _ := randBitArray(31)
  90. bA3 := bA1.Sub(bA2)
  91. bNil := (*BitArray)(nil)
  92. require.Equal(t, bNil.Sub(bA1), (*BitArray)(nil))
  93. require.Equal(t, bA1.Sub(nil), (*BitArray)(nil))
  94. require.Equal(t, bNil.Sub(nil), (*BitArray)(nil))
  95. if bA3.Bits != bA1.Bits {
  96. t.Error("Expected bA1 bits")
  97. }
  98. if len(bA3.Elems) != len(bA1.Elems) {
  99. t.Error("Expected bA1 elems length")
  100. }
  101. for i := 0; i < bA3.Bits; i++ {
  102. expected := bA1.GetIndex(i)
  103. if i < bA2.Bits && bA2.GetIndex(i) {
  104. expected = false
  105. }
  106. if bA3.GetIndex(i) != expected {
  107. t.Error("Wrong bit from bA3")
  108. }
  109. }
  110. }
  111. func TestPickRandom(t *testing.T) {
  112. for idx := 0; idx < 123; idx++ {
  113. bA1 := NewBitArray(123)
  114. bA1.SetIndex(idx, true)
  115. index, ok := bA1.PickRandom()
  116. if !ok {
  117. t.Fatal("Expected to pick element but got none")
  118. }
  119. if index != idx {
  120. t.Fatalf("Expected to pick element at %v but got wrong index", idx)
  121. }
  122. }
  123. }
  124. func TestBytes(t *testing.T) {
  125. bA := NewBitArray(4)
  126. bA.SetIndex(0, true)
  127. check := func(bA *BitArray, bz []byte) {
  128. if !bytes.Equal(bA.Bytes(), bz) {
  129. panic(Fmt("Expected %X but got %X", bz, bA.Bytes()))
  130. }
  131. }
  132. check(bA, []byte{0x01})
  133. bA.SetIndex(3, true)
  134. check(bA, []byte{0x09})
  135. bA = NewBitArray(9)
  136. check(bA, []byte{0x00, 0x00})
  137. bA.SetIndex(7, true)
  138. check(bA, []byte{0x80, 0x00})
  139. bA.SetIndex(8, true)
  140. check(bA, []byte{0x80, 0x01})
  141. bA = NewBitArray(16)
  142. check(bA, []byte{0x00, 0x00})
  143. bA.SetIndex(7, true)
  144. check(bA, []byte{0x80, 0x00})
  145. bA.SetIndex(8, true)
  146. check(bA, []byte{0x80, 0x01})
  147. bA.SetIndex(9, true)
  148. check(bA, []byte{0x80, 0x03})
  149. }
  150. func TestEmptyFull(t *testing.T) {
  151. ns := []int{47, 123}
  152. for _, n := range ns {
  153. bA := NewBitArray(n)
  154. if !bA.IsEmpty() {
  155. t.Fatal("Expected bit array to be empty")
  156. }
  157. for i := 0; i < n; i++ {
  158. bA.SetIndex(i, true)
  159. }
  160. if !bA.IsFull() {
  161. t.Fatal("Expected bit array to be full")
  162. }
  163. }
  164. }
  165. func TestUpdateNeverPanics(t *testing.T) {
  166. newRandBitArray := func(n int) *BitArray {
  167. ba, _ := randBitArray(n)
  168. return ba
  169. }
  170. pairs := []struct {
  171. a, b *BitArray
  172. }{
  173. {nil, nil},
  174. {newRandBitArray(10), newRandBitArray(12)},
  175. {newRandBitArray(23), newRandBitArray(23)},
  176. {newRandBitArray(37), nil},
  177. {nil, NewBitArray(10)},
  178. }
  179. for _, pair := range pairs {
  180. a, b := pair.a, pair.b
  181. a.Update(b)
  182. b.Update(a)
  183. }
  184. }