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.

166 lines
3.4 KiB

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