2016-02-20 19:56:48 +00:00
|
|
|
package structs
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-03-15 12:42:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
2016-08-05 23:08:35 +00:00
|
|
|
)
|
2016-02-20 19:56:48 +00:00
|
|
|
|
|
|
|
func TestBitmap(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2016-02-20 19:56:48 +00:00
|
|
|
// 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
|
2016-08-16 22:16:35 +00:00
|
|
|
idxs := b.IndexesInRange(true, 0, 500)
|
2016-08-10 18:47:20 +00:00
|
|
|
expected := []int{0, 255}
|
2016-08-05 23:08:35 +00:00
|
|
|
if !reflect.DeepEqual(idxs, expected) {
|
|
|
|
t.Fatalf("bad: got %#v; want %#v", idxs, expected)
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:16:35 +00:00
|
|
|
idxs = b.IndexesInRange(true, 1, 255)
|
2016-08-10 18:47:20 +00:00
|
|
|
expected = []int{255}
|
2016-08-05 23:08:35 +00:00
|
|
|
if !reflect.DeepEqual(idxs, expected) {
|
|
|
|
t.Fatalf("bad: got %#v; want %#v", idxs, expected)
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:37:26 +00:00
|
|
|
idxs = b.IndexesInRange(false, 0, 256)
|
2016-08-05 23:08:35 +00:00
|
|
|
if len(idxs) != 254 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:37:26 +00:00
|
|
|
idxs = b.IndexesInRange(false, 100, 200)
|
2016-08-16 22:16:35 +00:00
|
|
|
if len(idxs) != 101 {
|
2016-08-10 23:37:26 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 18:34:46 +00:00
|
|
|
|
|
|
|
// Set a few bits
|
|
|
|
b.Set(0)
|
|
|
|
b.Set(255)
|
|
|
|
b.Unset(0)
|
|
|
|
b.Unset(255)
|
|
|
|
|
|
|
|
// Clear the bits
|
|
|
|
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")
|
|
|
|
}
|
2016-02-20 19:56:48 +00:00
|
|
|
}
|