-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitfield_test.go
215 lines (186 loc) · 5.51 KB
/
bitfield_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package bits
import (
"reflect"
"testing"
)
func TestSetBit(t *testing.T) {
bf := NewBitField(64)
bf.SetBit(5)
if !bf.IsSet(5) {
t.Errorf("Expected bit at position 5 to be set")
}
bf.SetBit(63)
if !bf.IsSet(63) {
t.Errorf("Expected bit at position 63 to be set")
}
// Check that other bits are still unset
if bf.IsSet(0) {
t.Errorf("Expected bit at position 0 to be unset")
}
if bf.IsSet(6) {
t.Errorf("Expected bit at position 6 to be unset")
}
}
func TestClearBit(t *testing.T) {
bf := NewBitField(64)
bf.SetBit(10)
bf.ClearBit(10)
if bf.IsSet(10) {
t.Errorf("Expected bit at position 10 to be cleared")
}
// Clearing an already cleared bit should not change the state
bf.ClearBit(10)
if bf.IsSet(10) {
t.Errorf("Expected bit at position 10 to remain cleared")
}
}
func TestIsSet(t *testing.T) {
bf := NewBitField(64)
// Initially all bits should be unset
if bf.IsSet(20) {
t.Errorf("Expected bit at position 20 to be unset")
}
bf.SetBit(20)
if !bf.IsSet(20) {
t.Errorf("Expected bit at position 20 to be set")
}
}
func TestAllocateNextAvailableBits(t *testing.T) {
// Test case 1: Normal operation with enough bits available
bf := NewBitField(10)
bf.SetBit(1)
bf.SetBit(3)
allocated, err := bf.AllocateNextAvailableBits(3)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expected := []int{0, 2, 4}
if !reflect.DeepEqual(allocated, expected) {
t.Errorf("Expected allocated bits %v, got %v", expected, allocated)
}
// Test case 2: Insufficient available bits
_, err = bf.AllocateNextAvailableBits(8) // 8 bits requested when only 7 are left
if err == nil {
t.Fatal("Expected error due to insufficient available bits")
}
// Test case 3: Request for only one bit
bf.ClearBit(5)
allocated, err = bf.AllocateNextAvailableBits(1)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expected = []int{5}
if !reflect.DeepEqual(allocated, expected) {
t.Errorf("Expected allocated bit %v, got %v", expected, allocated)
}
// Test case 4: Edge case with bitfield size of 1
bfSmall := NewBitField(1)
allocated, err = bfSmall.AllocateNextAvailableBits(1)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expected = []int{0}
if !reflect.DeepEqual(allocated, expected) {
t.Errorf("Expected allocated bit %v, got %v", expected, allocated)
}
if !bfSmall.IsSet(0) {
t.Error("Expected bit 0 to be set in bitfield of size 1")
}
// Test case 5: Out of range request for more bits than the size of bitfield
_, err = bfSmall.AllocateNextAvailableBits(2) // Only 1 bit is available in bfSmall
if err == nil {
t.Fatal("Expected error due to request exceeding bitfield size")
}
// Test case 6: Verify bits are non-consecutive
bf = NewBitField(10)
bf.SetBit(2)
bf.SetBit(5)
allocated, err = bf.AllocateNextAvailableBits(3)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expected = []int{0, 1, 3}
if !reflect.DeepEqual(allocated, expected) {
t.Errorf("Expected allocated bits %v, got %v", expected, allocated)
}
if !bf.IsSet(2) || !bf.IsSet(5) {
t.Errorf("Expected bits at positions 2 and 5 to remain set")
}
}
func TestAllocateNextAvailableBitsInvalidInput(t *testing.T) {
bf := NewBitField(64)
// Test invalid input (e.g., n <= 0 or n > size)
_, err := bf.AllocateNextAvailableBits(0)
if err == nil {
t.Errorf("Expected error for invalid bit count (0)")
}
_, err = bf.AllocateNextAvailableBits(-1)
if err == nil {
t.Errorf("Expected error for invalid bit count (-1)")
}
_, err = bf.AllocateNextAvailableBits(65)
if err == nil {
t.Errorf("Expected error for bit count exceeding BitField size")
}
}
func TestNextAvailableBitsInRange(t *testing.T) {
// Test case 1: Normal operation within bounds
bf := NewBitField(10)
bf.SetBit(1)
bf.SetBit(3)
bf.SetBit(7)
// Find the next 2 available bits in range [0, 5)
availableBits, err := bf.AllocateAvailableBitsInRange(0, 5, 2)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expected := []int{0, 2}
for i, pos := range availableBits {
if pos != expected[i] {
t.Errorf("Expected %d, got %d at index %d", expected[i], pos, i)
}
}
// Test case 2: Range with fewer than required bits
_, err = bf.AllocateAvailableBitsInRange(0, 5, 4)
if err == nil {
t.Fatal("Expected error due to insufficient consecutive bits")
}
// Test case 3: Sufficient bits in a later part of the range
availableBits, err = bf.AllocateAvailableBitsInRange(5, 10, 3)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
expected = []int{5, 6, 8}
for i, pos := range availableBits {
if pos != expected[i] {
t.Errorf("Expected %d, got %d at index %d", expected[i], pos, i)
}
}
// Test case 4: Entire range but no sequence found
bf.SetBit(0)
bf.SetBit(2)
bf.SetBit(4)
_, err = bf.AllocateAvailableBitsInRange(0, 5, 2)
if err == nil {
t.Fatal("Expected error due to lack of available consecutive bits")
}
// Test case 5: Out-of-bounds range
_, err = bf.AllocateAvailableBitsInRange(-1, 15, 2)
if err == nil {
t.Fatal("Expected error due to out-of-bounds range")
}
// Test case 6: Range where `n` is greater than available bits
_, err = bf.AllocateAvailableBitsInRange(0, 10, 11)
if err == nil {
t.Fatal("Expected error due to requesting more bits than available in range")
}
// Test case 7: Requesting 1 available bit
bf.ClearBit(0)
availableBits, err = bf.AllocateAvailableBitsInRange(0, 10, 1)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(availableBits) != 1 || availableBits[0] != 0 {
t.Errorf("Expected single available bit at position 0, got %v", availableBits)
}
}