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.

322 lines
7.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package bits
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. tmrand "github.com/tendermint/tendermint/libs/rand"
  11. tmprotobits "github.com/tendermint/tendermint/proto/tendermint/libs/bits"
  12. )
  13. func randBitArray(bits int) (*BitArray, []byte) {
  14. src := tmrand.Bytes((bits + 7) / 8)
  15. bA := NewBitArray(bits)
  16. for i := 0; i < len(src); i++ {
  17. for j := 0; j < 8; j++ {
  18. if i*8+j >= bits {
  19. return bA, src
  20. }
  21. setBit := src[i]&(1<<uint(j)) > 0
  22. bA.SetIndex(i*8+j, setBit)
  23. }
  24. }
  25. return bA, src
  26. }
  27. func TestAnd(t *testing.T) {
  28. bA1, _ := randBitArray(51)
  29. bA2, _ := randBitArray(31)
  30. bA3 := bA1.And(bA2)
  31. var bNil *BitArray
  32. require.Equal(t, bNil.And(bA1), (*BitArray)(nil))
  33. require.Equal(t, bA1.And(nil), (*BitArray)(nil))
  34. require.Equal(t, bNil.And(nil), (*BitArray)(nil))
  35. if bA3.Bits != 31 {
  36. t.Error("Expected min bits", bA3.Bits)
  37. }
  38. if len(bA3.Elems) != len(bA2.Elems) {
  39. t.Error("Expected min elems length")
  40. }
  41. for i := 0; i < bA3.Bits; i++ {
  42. expected := bA1.GetIndex(i) && bA2.GetIndex(i)
  43. if bA3.GetIndex(i) != expected {
  44. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  45. }
  46. }
  47. }
  48. func TestOr(t *testing.T) {
  49. bA1, _ := randBitArray(51)
  50. bA2, _ := randBitArray(31)
  51. bA3 := bA1.Or(bA2)
  52. bNil := (*BitArray)(nil)
  53. require.Equal(t, bNil.Or(bA1), bA1)
  54. require.Equal(t, bA1.Or(nil), bA1)
  55. require.Equal(t, bNil.Or(nil), (*BitArray)(nil))
  56. if bA3.Bits != 51 {
  57. t.Error("Expected max bits")
  58. }
  59. if len(bA3.Elems) != len(bA1.Elems) {
  60. t.Error("Expected max elems length")
  61. }
  62. for i := 0; i < bA3.Bits; i++ {
  63. expected := bA1.GetIndex(i) || bA2.GetIndex(i)
  64. if bA3.GetIndex(i) != expected {
  65. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  66. }
  67. }
  68. }
  69. func TestSub(t *testing.T) {
  70. testCases := []struct {
  71. initBA string
  72. subtractingBA string
  73. expectedBA string
  74. }{
  75. {`null`, `null`, `null`},
  76. {`"x"`, `null`, `null`},
  77. {`null`, `"x"`, `null`},
  78. {`"x"`, `"x"`, `"_"`},
  79. {`"xxxxxx"`, `"x_x_x_"`, `"_x_x_x"`},
  80. {`"x_x_x_"`, `"xxxxxx"`, `"______"`},
  81. {`"xxxxxx"`, `"x_x_x_xxxx"`, `"_x_x_x"`},
  82. {`"x_x_x_xxxx"`, `"xxxxxx"`, `"______xxxx"`},
  83. {`"xxxxxxxxxx"`, `"x_x_x_"`, `"_x_x_xxxxx"`},
  84. {`"x_x_x_"`, `"xxxxxxxxxx"`, `"______"`},
  85. }
  86. for _, tc := range testCases {
  87. var bA *BitArray
  88. err := json.Unmarshal([]byte(tc.initBA), &bA)
  89. require.Nil(t, err)
  90. var o *BitArray
  91. err = json.Unmarshal([]byte(tc.subtractingBA), &o)
  92. require.Nil(t, err)
  93. got, _ := json.Marshal(bA.Sub(o))
  94. require.Equal(
  95. t,
  96. tc.expectedBA,
  97. string(got),
  98. "%s minus %s doesn't equal %s",
  99. tc.initBA,
  100. tc.subtractingBA,
  101. tc.expectedBA,
  102. )
  103. }
  104. }
  105. func TestPickRandom(t *testing.T) {
  106. empty16Bits := "________________"
  107. empty64Bits := empty16Bits + empty16Bits + empty16Bits + empty16Bits
  108. testCases := []struct {
  109. bA string
  110. ok bool
  111. }{
  112. {`null`, false},
  113. {`"x"`, true},
  114. {`"` + empty16Bits + `"`, false},
  115. {`"x` + empty16Bits + `"`, true},
  116. {`"` + empty16Bits + `x"`, true},
  117. {`"x` + empty16Bits + `x"`, true},
  118. {`"` + empty64Bits + `"`, false},
  119. {`"x` + empty64Bits + `"`, true},
  120. {`"` + empty64Bits + `x"`, true},
  121. {`"x` + empty64Bits + `x"`, true},
  122. }
  123. for _, tc := range testCases {
  124. var bitArr *BitArray
  125. err := json.Unmarshal([]byte(tc.bA), &bitArr)
  126. require.NoError(t, err)
  127. _, ok := bitArr.PickRandom()
  128. require.Equal(t, tc.ok, ok, "PickRandom got an unexpected result on input %s", tc.bA)
  129. }
  130. }
  131. func TestBytes(t *testing.T) {
  132. bA := NewBitArray(4)
  133. bA.SetIndex(0, true)
  134. check := func(bA *BitArray, bz []byte) {
  135. if !bytes.Equal(bA.Bytes(), bz) {
  136. panic(fmt.Sprintf("Expected %X but got %X", bz, bA.Bytes()))
  137. }
  138. }
  139. check(bA, []byte{0x01})
  140. bA.SetIndex(3, true)
  141. check(bA, []byte{0x09})
  142. bA = NewBitArray(9)
  143. check(bA, []byte{0x00, 0x00})
  144. bA.SetIndex(7, true)
  145. check(bA, []byte{0x80, 0x00})
  146. bA.SetIndex(8, true)
  147. check(bA, []byte{0x80, 0x01})
  148. bA = NewBitArray(16)
  149. check(bA, []byte{0x00, 0x00})
  150. bA.SetIndex(7, true)
  151. check(bA, []byte{0x80, 0x00})
  152. bA.SetIndex(8, true)
  153. check(bA, []byte{0x80, 0x01})
  154. bA.SetIndex(9, true)
  155. check(bA, []byte{0x80, 0x03})
  156. }
  157. func TestEmptyFull(t *testing.T) {
  158. ns := []int{47, 123}
  159. for _, n := range ns {
  160. bA := NewBitArray(n)
  161. if !bA.IsEmpty() {
  162. t.Fatal("Expected bit array to be empty")
  163. }
  164. for i := 0; i < n; i++ {
  165. bA.SetIndex(i, true)
  166. }
  167. if !bA.IsFull() {
  168. t.Fatal("Expected bit array to be full")
  169. }
  170. }
  171. }
  172. func TestUpdateNeverPanics(t *testing.T) {
  173. newRandBitArray := func(n int) *BitArray {
  174. ba, _ := randBitArray(n)
  175. return ba
  176. }
  177. pairs := []struct {
  178. a, b *BitArray
  179. }{
  180. {nil, nil},
  181. {newRandBitArray(10), newRandBitArray(12)},
  182. {newRandBitArray(23), newRandBitArray(23)},
  183. {newRandBitArray(37), nil},
  184. {nil, NewBitArray(10)},
  185. }
  186. for _, pair := range pairs {
  187. a, b := pair.a, pair.b
  188. a.Update(b)
  189. b.Update(a)
  190. }
  191. }
  192. func TestNewBitArrayNeverCrashesOnNegatives(t *testing.T) {
  193. bitList := []int{-127, -128, -1 << 31}
  194. for _, bits := range bitList {
  195. _ = NewBitArray(bits)
  196. }
  197. }
  198. func TestJSONMarshalUnmarshal(t *testing.T) {
  199. bA1 := NewBitArray(0)
  200. bA2 := NewBitArray(1)
  201. bA3 := NewBitArray(1)
  202. bA3.SetIndex(0, true)
  203. bA4 := NewBitArray(5)
  204. bA4.SetIndex(0, true)
  205. bA4.SetIndex(1, true)
  206. testCases := []struct {
  207. bA *BitArray
  208. marshalledBA string
  209. }{
  210. {nil, `null`},
  211. {bA1, `null`},
  212. {bA2, `"_"`},
  213. {bA3, `"x"`},
  214. {bA4, `"xx___"`},
  215. }
  216. for _, tc := range testCases {
  217. tc := tc
  218. t.Run(tc.bA.String(), func(t *testing.T) {
  219. bz, err := json.Marshal(tc.bA)
  220. require.NoError(t, err)
  221. assert.Equal(t, tc.marshalledBA, string(bz))
  222. var unmarshalledBA *BitArray
  223. err = json.Unmarshal(bz, &unmarshalledBA)
  224. require.NoError(t, err)
  225. if tc.bA == nil {
  226. require.Nil(t, unmarshalledBA)
  227. } else {
  228. require.NotNil(t, unmarshalledBA)
  229. assert.EqualValues(t, tc.bA.Bits, unmarshalledBA.Bits)
  230. if assert.EqualValues(t, tc.bA.String(), unmarshalledBA.String()) {
  231. assert.EqualValues(t, tc.bA.Elems, unmarshalledBA.Elems)
  232. }
  233. }
  234. })
  235. }
  236. }
  237. func TestBitArrayToFromProto(t *testing.T) {
  238. testCases := []struct {
  239. msg string
  240. bA1 *BitArray
  241. expPass bool
  242. }{
  243. {"success empty", &BitArray{}, true},
  244. {"success", NewBitArray(1), true},
  245. {"success", NewBitArray(2), true},
  246. {"negative", NewBitArray(-1), false},
  247. }
  248. for _, tc := range testCases {
  249. protoBA := tc.bA1.ToProto()
  250. ba := new(BitArray)
  251. err := ba.FromProto(protoBA)
  252. if tc.expPass {
  253. assert.NoError(t, err)
  254. require.Equal(t, tc.bA1, ba, tc.msg)
  255. } else {
  256. require.NotEqual(t, tc.bA1, ba, tc.msg)
  257. }
  258. }
  259. }
  260. func TestBitArrayFromProto(t *testing.T) {
  261. testCases := []struct {
  262. pbA *tmprotobits.BitArray
  263. resA *BitArray
  264. expErr bool
  265. }{
  266. 0: {nil, &BitArray{}, false},
  267. 1: {&tmprotobits.BitArray{}, &BitArray{}, false},
  268. 2: {&tmprotobits.BitArray{Bits: 1, Elems: make([]uint64, 1)}, &BitArray{Bits: 1, Elems: make([]uint64, 1)}, false},
  269. 3: {&tmprotobits.BitArray{Bits: -1, Elems: make([]uint64, 1)}, &BitArray{}, true},
  270. 4: {&tmprotobits.BitArray{Bits: math.MaxInt32 + 1, Elems: make([]uint64, 1)}, &BitArray{}, true},
  271. 5: {&tmprotobits.BitArray{Bits: 1, Elems: make([]uint64, 2)}, &BitArray{}, true},
  272. }
  273. for i, tc := range testCases {
  274. bA := new(BitArray)
  275. err := bA.FromProto(tc.pbA)
  276. if tc.expErr {
  277. assert.Error(t, err, "#%d", i)
  278. assert.Equal(t, tc.resA, bA, "#%d", i)
  279. } else {
  280. assert.NoError(t, err, "#%d", i)
  281. assert.Equal(t, tc.resA, bA, "#%d", i)
  282. }
  283. }
  284. }