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.

120 lines
2.5 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
  1. package common
  2. import (
  3. "testing"
  4. )
  5. func randBitArray(bits uint) (*BitArray, []byte) {
  6. src := RandBytes(int((bits + 7) / 8))
  7. bA := NewBitArray(bits)
  8. for i := uint(0); i < uint(len(src)); i++ {
  9. for j := uint(0); j < 8; j++ {
  10. if i*8+j >= bits {
  11. return bA, src
  12. }
  13. setBit := src[i]&(1<<j) > 0
  14. bA.SetIndex(i*8+j, setBit)
  15. }
  16. }
  17. return bA, src
  18. }
  19. func TestAnd(t *testing.T) {
  20. bA1, _ := randBitArray(51)
  21. bA2, _ := randBitArray(31)
  22. bA3 := bA1.And(bA2)
  23. if bA3.Bits != 31 {
  24. t.Error("Expected min bits", bA3.Bits)
  25. }
  26. if len(bA3.Elems) != len(bA2.Elems) {
  27. t.Error("Expected min elems length")
  28. }
  29. for i := uint(0); i < bA3.Bits; i++ {
  30. expected := bA1.GetIndex(i) && bA2.GetIndex(i)
  31. if bA3.GetIndex(i) != expected {
  32. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  33. }
  34. }
  35. }
  36. func TestOr(t *testing.T) {
  37. bA1, _ := randBitArray(51)
  38. bA2, _ := randBitArray(31)
  39. bA3 := bA1.Or(bA2)
  40. if bA3.Bits != 51 {
  41. t.Error("Expected max bits")
  42. }
  43. if len(bA3.Elems) != len(bA1.Elems) {
  44. t.Error("Expected max elems length")
  45. }
  46. for i := uint(0); i < bA3.Bits; i++ {
  47. expected := bA1.GetIndex(i) || bA2.GetIndex(i)
  48. if bA3.GetIndex(i) != expected {
  49. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  50. }
  51. }
  52. }
  53. func TestSub1(t *testing.T) {
  54. bA1, _ := randBitArray(31)
  55. bA2, _ := randBitArray(51)
  56. bA3 := bA1.Sub(bA2)
  57. if bA3.Bits != bA1.Bits {
  58. t.Error("Expected bA1 bits")
  59. }
  60. if len(bA3.Elems) != len(bA1.Elems) {
  61. t.Error("Expected bA1 elems length")
  62. }
  63. for i := uint(0); i < bA3.Bits; i++ {
  64. expected := bA1.GetIndex(i)
  65. if bA2.GetIndex(i) {
  66. expected = false
  67. }
  68. if bA3.GetIndex(i) != expected {
  69. t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
  70. }
  71. }
  72. }
  73. func TestSub2(t *testing.T) {
  74. bA1, _ := randBitArray(51)
  75. bA2, _ := randBitArray(31)
  76. bA3 := bA1.Sub(bA2)
  77. if bA3.Bits != bA1.Bits {
  78. t.Error("Expected bA1 bits")
  79. }
  80. if len(bA3.Elems) != len(bA1.Elems) {
  81. t.Error("Expected bA1 elems length")
  82. }
  83. for i := uint(0); i < bA3.Bits; i++ {
  84. expected := bA1.GetIndex(i)
  85. if i < bA2.Bits && bA2.GetIndex(i) {
  86. expected = false
  87. }
  88. if bA3.GetIndex(i) != expected {
  89. t.Error("Wrong bit from bA3")
  90. }
  91. }
  92. }
  93. func TestPickRandom(t *testing.T) {
  94. for idx := 0; idx < 123; idx++ {
  95. bA1 := NewBitArray(123)
  96. bA1.SetIndex(uint(idx), true)
  97. index, ok := bA1.PickRandom()
  98. if !ok {
  99. t.Fatal("Expected to pick element but got none")
  100. }
  101. if index != uint(idx) {
  102. t.Fatalf("Expected to pick element at %v but got wrong index", idx)
  103. }
  104. }
  105. }