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.

267 lines
5.7 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. "encoding/json"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func randBitArray(bits int) (*BitArray, []byte) {
  10. src := RandBytes((bits + 7) / 8)
  11. bA := NewBitArray(bits)
  12. for i := 0; i < len(src); i++ {
  13. for j := 0; j < 8; j++ {
  14. if i*8+j >= bits {
  15. return bA, src
  16. }
  17. setBit := src[i]&(1<<uint(j)) > 0
  18. bA.SetIndex(i*8+j, setBit)
  19. }
  20. }
  21. return bA, src
  22. }
  23. func TestAnd(t *testing.T) {
  24. bA1, _ := randBitArray(51)
  25. bA2, _ := randBitArray(31)
  26. bA3 := bA1.And(bA2)
  27. var bNil *BitArray
  28. require.Equal(t, bNil.And(bA1), (*BitArray)(nil))
  29. require.Equal(t, bA1.And(nil), (*BitArray)(nil))
  30. require.Equal(t, bNil.And(nil), (*BitArray)(nil))
  31. if bA3.Bits != 31 {
  32. t.Error("Expected min bits", bA3.Bits)
  33. }
  34. if len(bA3.Elems) != len(bA2.Elems) {
  35. t.Error("Expected min elems length")
  36. }
  37. for i := 0; i < bA3.Bits; i++ {
  38. expected := bA1.GetIndex(i) && bA2.GetIndex(i)
  39. if bA3.GetIndex(i) != expected {
  40. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  41. }
  42. }
  43. }
  44. func TestOr(t *testing.T) {
  45. bA1, _ := randBitArray(51)
  46. bA2, _ := randBitArray(31)
  47. bA3 := bA1.Or(bA2)
  48. bNil := (*BitArray)(nil)
  49. require.Equal(t, bNil.Or(bA1), bA1)
  50. require.Equal(t, bA1.Or(nil), bA1)
  51. require.Equal(t, bNil.Or(nil), (*BitArray)(nil))
  52. if bA3.Bits != 51 {
  53. t.Error("Expected max bits")
  54. }
  55. if len(bA3.Elems) != len(bA1.Elems) {
  56. t.Error("Expected max elems length")
  57. }
  58. for i := 0; i < bA3.Bits; i++ {
  59. expected := bA1.GetIndex(i) || bA2.GetIndex(i)
  60. if bA3.GetIndex(i) != expected {
  61. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  62. }
  63. }
  64. }
  65. func TestSub1(t *testing.T) {
  66. bA1, _ := randBitArray(31)
  67. bA2, _ := randBitArray(51)
  68. bA3 := bA1.Sub(bA2)
  69. bNil := (*BitArray)(nil)
  70. require.Equal(t, bNil.Sub(bA1), (*BitArray)(nil))
  71. require.Equal(t, bA1.Sub(nil), (*BitArray)(nil))
  72. require.Equal(t, bNil.Sub(nil), (*BitArray)(nil))
  73. if bA3.Bits != bA1.Bits {
  74. t.Error("Expected bA1 bits")
  75. }
  76. if len(bA3.Elems) != len(bA1.Elems) {
  77. t.Error("Expected bA1 elems length")
  78. }
  79. for i := 0; i < bA3.Bits; i++ {
  80. expected := bA1.GetIndex(i)
  81. if bA2.GetIndex(i) {
  82. expected = false
  83. }
  84. if bA3.GetIndex(i) != expected {
  85. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  86. }
  87. }
  88. }
  89. func TestSub2(t *testing.T) {
  90. bA1, _ := randBitArray(51)
  91. bA2, _ := randBitArray(31)
  92. bA3 := bA1.Sub(bA2)
  93. bNil := (*BitArray)(nil)
  94. require.Equal(t, bNil.Sub(bA1), (*BitArray)(nil))
  95. require.Equal(t, bA1.Sub(nil), (*BitArray)(nil))
  96. require.Equal(t, bNil.Sub(nil), (*BitArray)(nil))
  97. if bA3.Bits != bA1.Bits {
  98. t.Error("Expected bA1 bits")
  99. }
  100. if len(bA3.Elems) != len(bA1.Elems) {
  101. t.Error("Expected bA1 elems length")
  102. }
  103. for i := 0; i < bA3.Bits; i++ {
  104. expected := bA1.GetIndex(i)
  105. if i < bA2.Bits && bA2.GetIndex(i) {
  106. expected = false
  107. }
  108. if bA3.GetIndex(i) != expected {
  109. t.Error("Wrong bit from bA3")
  110. }
  111. }
  112. }
  113. func TestPickRandom(t *testing.T) {
  114. for idx := 0; idx < 123; idx++ {
  115. bA1 := NewBitArray(123)
  116. bA1.SetIndex(idx, true)
  117. index, ok := bA1.PickRandom()
  118. if !ok {
  119. t.Fatal("Expected to pick element but got none")
  120. }
  121. if index != idx {
  122. t.Fatalf("Expected to pick element at %v but got wrong index", idx)
  123. }
  124. }
  125. }
  126. func TestBytes(t *testing.T) {
  127. bA := NewBitArray(4)
  128. bA.SetIndex(0, true)
  129. check := func(bA *BitArray, bz []byte) {
  130. if !bytes.Equal(bA.Bytes(), bz) {
  131. panic(Fmt("Expected %X but got %X", bz, bA.Bytes()))
  132. }
  133. }
  134. check(bA, []byte{0x01})
  135. bA.SetIndex(3, true)
  136. check(bA, []byte{0x09})
  137. bA = NewBitArray(9)
  138. check(bA, []byte{0x00, 0x00})
  139. bA.SetIndex(7, true)
  140. check(bA, []byte{0x80, 0x00})
  141. bA.SetIndex(8, true)
  142. check(bA, []byte{0x80, 0x01})
  143. bA = NewBitArray(16)
  144. check(bA, []byte{0x00, 0x00})
  145. bA.SetIndex(7, true)
  146. check(bA, []byte{0x80, 0x00})
  147. bA.SetIndex(8, true)
  148. check(bA, []byte{0x80, 0x01})
  149. bA.SetIndex(9, true)
  150. check(bA, []byte{0x80, 0x03})
  151. }
  152. func TestEmptyFull(t *testing.T) {
  153. ns := []int{47, 123}
  154. for _, n := range ns {
  155. bA := NewBitArray(n)
  156. if !bA.IsEmpty() {
  157. t.Fatal("Expected bit array to be empty")
  158. }
  159. for i := 0; i < n; i++ {
  160. bA.SetIndex(i, true)
  161. }
  162. if !bA.IsFull() {
  163. t.Fatal("Expected bit array to be full")
  164. }
  165. }
  166. }
  167. func TestUpdateNeverPanics(t *testing.T) {
  168. newRandBitArray := func(n int) *BitArray {
  169. ba, _ := randBitArray(n)
  170. return ba
  171. }
  172. pairs := []struct {
  173. a, b *BitArray
  174. }{
  175. {nil, nil},
  176. {newRandBitArray(10), newRandBitArray(12)},
  177. {newRandBitArray(23), newRandBitArray(23)},
  178. {newRandBitArray(37), nil},
  179. {nil, NewBitArray(10)},
  180. }
  181. for _, pair := range pairs {
  182. a, b := pair.a, pair.b
  183. a.Update(b)
  184. b.Update(a)
  185. }
  186. }
  187. func TestNewBitArrayNeverCrashesOnNegatives(t *testing.T) {
  188. bitList := []int{-127, -128, -1 << 31}
  189. for _, bits := range bitList {
  190. _ = NewBitArray(bits)
  191. }
  192. }
  193. func TestJSONMarshalUnmarshal(t *testing.T) {
  194. bA1 := NewBitArray(0)
  195. bA2 := NewBitArray(1)
  196. bA3 := NewBitArray(1)
  197. bA3.SetIndex(0, true)
  198. bA4 := NewBitArray(5)
  199. bA4.SetIndex(0, true)
  200. bA4.SetIndex(1, true)
  201. testCases := []struct {
  202. bA *BitArray
  203. marshalledBA string
  204. }{
  205. {nil, `null`},
  206. {bA1, `null`},
  207. {bA2, `"_"`},
  208. {bA3, `"x"`},
  209. {bA4, `"xx___"`},
  210. }
  211. for _, tc := range testCases {
  212. t.Run(tc.bA.String(), func(t *testing.T) {
  213. bz, err := json.Marshal(tc.bA)
  214. require.NoError(t, err)
  215. assert.Equal(t, tc.marshalledBA, string(bz))
  216. var unmarshalledBA *BitArray
  217. err = json.Unmarshal(bz, &unmarshalledBA)
  218. require.NoError(t, err)
  219. if tc.bA == nil {
  220. require.Nil(t, unmarshalledBA)
  221. } else {
  222. require.NotNil(t, unmarshalledBA)
  223. assert.EqualValues(t, tc.bA.Bits, unmarshalledBA.Bits)
  224. if assert.EqualValues(t, tc.bA.String(), unmarshalledBA.String()) {
  225. assert.EqualValues(t, tc.bA.Elems, unmarshalledBA.Elems)
  226. }
  227. }
  228. })
  229. }
  230. }