2016-02-20 19:56:48 +00:00
|
|
|
package structs
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
2016-02-20 19:56:48 +00:00
|
|
|
|
|
|
|
func TestBitmap(t *testing.T) {
|
|
|
|
// Check invalid sizes
|
|
|
|
_, err := NewBitmap(0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
_, err = NewBitmap(7)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a normal bitmap
|
2016-08-05 23:08:35 +00:00
|
|
|
var s uint = 256
|
|
|
|
b, err := NewBitmap(s)
|
2016-02-20 19:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
if b.Size() != s {
|
|
|
|
t.Fatalf("bad size")
|
|
|
|
}
|
|
|
|
|
2016-02-20 19:56:48 +00:00
|
|
|
// Set a few bits
|
|
|
|
b.Set(0)
|
|
|
|
b.Set(255)
|
|
|
|
|
|
|
|
// Verify the bytes
|
|
|
|
if b[0] == 0 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !b.Check(0) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the bytes
|
|
|
|
if b[len(b)-1] == 0 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !b.Check(255) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other bits should be unset
|
|
|
|
for i := 1; i < 255; i++ {
|
|
|
|
if b.Check(uint(i)) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
// Check the indexes
|
|
|
|
idxs := b.IndexesFrom(true, 0)
|
|
|
|
expected := []uint{0, 255}
|
|
|
|
if !reflect.DeepEqual(idxs, expected) {
|
|
|
|
t.Fatalf("bad: got %#v; want %#v", idxs, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
idxs = b.IndexesFrom(true, 1)
|
|
|
|
expected = []uint{255}
|
|
|
|
if !reflect.DeepEqual(idxs, expected) {
|
|
|
|
t.Fatalf("bad: got %#v; want %#v", idxs, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
idxs = b.IndexesFrom(false, 0)
|
|
|
|
if len(idxs) != 254 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the copy is correct
|
|
|
|
b2, err := b.Copy()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(b, b2) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2016-02-20 19:56:48 +00:00
|
|
|
// Clear
|
|
|
|
b.Clear()
|
|
|
|
|
|
|
|
// All bits should be unset
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
if b.Check(uint(i)) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|