OSDN Git Service

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