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.

161 lines
3.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package binary
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. . "github.com/tendermint/tendermint/common"
  7. )
  8. func randBitArray(bits uint) (BitArray, []byte) {
  9. src := RandBytes(int((bits + 7) / 8))
  10. bA := NewBitArray(bits)
  11. for i := uint(0); i < uint(len(src)); i++ {
  12. for j := uint(0); j < 8; j++ {
  13. if i*8+j >= bits {
  14. return bA, src
  15. }
  16. setBit := src[i]&(1<<j) > 0
  17. bA.SetIndex(i*8+j, setBit)
  18. }
  19. }
  20. return bA, src
  21. }
  22. func TestReadWriteEmptyBitarray(t *testing.T) {
  23. bA1 := BitArray{}
  24. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  25. WriteBinary(bA1, buf, n, err)
  26. if *err != nil {
  27. t.Error("Failed to write empty bitarray")
  28. }
  29. bA2 := ReadBinary(BitArray{}, buf, n, err).(BitArray)
  30. if *err != nil {
  31. t.Error("Failed to read empty bitarray")
  32. }
  33. if bA2.Bits != 0 {
  34. t.Error("Expected to get bA2.Bits 0")
  35. }
  36. }
  37. func TestReadWriteBitarray(t *testing.T) {
  38. // Make random bA1
  39. bA1, testData := randBitArray(64*10 + 8) // not divisible by 64
  40. // Write it
  41. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  42. WriteBinary(bA1, buf, n, err)
  43. if *err != nil {
  44. t.Error("Failed to write bitarray")
  45. }
  46. fmt.Printf("Bytes: %X", buf.Bytes())
  47. // Read it
  48. bA2 := ReadBinary(BitArray{}, buf, n, err).(BitArray)
  49. if *err != nil {
  50. t.Error("Failed to read bitarray")
  51. }
  52. testData2 := make([]byte, len(testData))
  53. for i := uint(0); i < uint(len(testData)); i++ {
  54. for j := uint(0); j < 8; j++ {
  55. if bA2.GetIndex(i*8 + j) {
  56. testData2[i] |= 1 << j
  57. }
  58. }
  59. }
  60. // Compare testData
  61. if !bytes.Equal(testData, testData2) {
  62. t.Errorf("Not the same:\n%X\n%X", testData, testData2)
  63. }
  64. }
  65. func TestAnd(t *testing.T) {
  66. bA1, _ := randBitArray(51)
  67. bA2, _ := randBitArray(31)
  68. bA3 := bA1.And(bA2)
  69. if bA3.Bits != 31 {
  70. t.Error("Expected min bits", bA3.Bits)
  71. }
  72. if len(bA3.Elems) != len(bA2.Elems) {
  73. t.Error("Expected min elems length")
  74. }
  75. for i := uint(0); i < bA3.Bits; i++ {
  76. expected := bA1.GetIndex(i) && bA2.GetIndex(i)
  77. if bA3.GetIndex(i) != expected {
  78. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  79. }
  80. }
  81. }
  82. func TestOr(t *testing.T) {
  83. bA1, _ := randBitArray(51)
  84. bA2, _ := randBitArray(31)
  85. bA3 := bA1.Or(bA2)
  86. if bA3.Bits != 51 {
  87. t.Error("Expected max bits")
  88. }
  89. if len(bA3.Elems) != len(bA1.Elems) {
  90. t.Error("Expected max elems length")
  91. }
  92. for i := uint(0); i < bA3.Bits; i++ {
  93. expected := bA1.GetIndex(i) || bA2.GetIndex(i)
  94. if bA3.GetIndex(i) != expected {
  95. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  96. }
  97. }
  98. }
  99. func TestSub1(t *testing.T) {
  100. bA1, _ := randBitArray(31)
  101. bA2, _ := randBitArray(51)
  102. bA3 := bA1.Sub(bA2)
  103. if bA3.Bits != bA1.Bits {
  104. t.Error("Expected bA1 bits")
  105. }
  106. if len(bA3.Elems) != len(bA1.Elems) {
  107. t.Error("Expected bA1 elems length")
  108. }
  109. for i := uint(0); i < bA3.Bits; i++ {
  110. expected := bA1.GetIndex(i)
  111. if bA2.GetIndex(i) {
  112. expected = false
  113. }
  114. if bA3.GetIndex(i) != expected {
  115. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  116. }
  117. }
  118. }
  119. func TestSub2(t *testing.T) {
  120. bA1, _ := randBitArray(51)
  121. bA2, _ := randBitArray(31)
  122. bA3 := bA1.Sub(bA2)
  123. if bA3.Bits != bA1.Bits {
  124. t.Error("Expected bA1 bits")
  125. }
  126. if len(bA3.Elems) != len(bA1.Elems) {
  127. t.Error("Expected bA1 elems length")
  128. }
  129. for i := uint(0); i < bA3.Bits; i++ {
  130. expected := bA1.GetIndex(i)
  131. if i < bA2.Bits && bA2.GetIndex(i) {
  132. expected = false
  133. }
  134. if bA3.GetIndex(i) != expected {
  135. t.Error("Wrong bit from bA3")
  136. }
  137. }
  138. }