2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-02-20 19:56:48 +00:00
|
|
|
package structs
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
2022-03-15 12:42:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
2023-10-24 07:08:35 +00:00
|
|
|
"github.com/shoenig/test/must"
|
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)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Error(t, err)
|
2016-02-20 19:56:48 +00:00
|
|
|
_, err = NewBitmap(7)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Error(t, err)
|
2016-02-20 19:56:48 +00:00
|
|
|
|
|
|
|
// Create a normal bitmap
|
2016-08-05 23:08:35 +00:00
|
|
|
var s uint = 256
|
|
|
|
b, err := NewBitmap(s)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, s, b.Size())
|
2016-08-05 23:08:35 +00:00
|
|
|
|
2016-02-20 19:56:48 +00:00
|
|
|
// Set a few bits
|
|
|
|
b.Set(0)
|
|
|
|
b.Set(255)
|
|
|
|
|
|
|
|
// Verify the bytes
|
2023-10-24 07:08:35 +00:00
|
|
|
must.NotEq(t, 0, b[0])
|
|
|
|
must.True(t, b.Check(0))
|
2016-02-20 19:56:48 +00:00
|
|
|
|
|
|
|
// Verify the bytes
|
2023-10-24 07:08:35 +00:00
|
|
|
must.NotEq(t, 0, b[len(b)-1])
|
|
|
|
must.True(t, b.Check(255))
|
2016-02-20 19:56:48 +00:00
|
|
|
|
|
|
|
// All other bits should be unset
|
|
|
|
for i := 1; i < 255; i++ {
|
2023-10-24 07:08:35 +00:00
|
|
|
must.False(t, b.Check(uint(i)))
|
2016-02-20 19:56:48 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Eq(t, []int{0, 255}, idxs)
|
2016-08-05 23:08:35 +00:00
|
|
|
|
2016-08-16 22:16:35 +00:00
|
|
|
idxs = b.IndexesInRange(true, 1, 255)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Eq(t, []int{255}, idxs)
|
2016-08-05 23:08:35 +00:00
|
|
|
|
2016-08-10 23:37:26 +00:00
|
|
|
idxs = b.IndexesInRange(false, 0, 256)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Len(t, 254, idxs)
|
2016-08-05 23:08:35 +00:00
|
|
|
|
2016-08-10 23:37:26 +00:00
|
|
|
idxs = b.IndexesInRange(false, 100, 200)
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Len(t, 101, idxs)
|
2016-08-10 23:37:26 +00:00
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
// Check the copy is correct
|
|
|
|
b2, err := b.Copy()
|
2023-10-24 07:08:35 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, b, b2)
|
2016-08-05 23:08:35 +00:00
|
|
|
|
2016-02-20 19:56:48 +00:00
|
|
|
// Clear
|
|
|
|
b.Clear()
|
|
|
|
|
|
|
|
// All bits should be unset
|
|
|
|
for i := 0; i < 256; i++ {
|
2023-10-24 07:08:35 +00:00
|
|
|
must.False(t, b.Check(uint(i)))
|
2016-02-20 19:56:48 +00:00
|
|
|
}
|
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
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Eq(t, 0, b[0])
|
|
|
|
must.False(t, b.Check(0))
|
2017-05-31 18:34:46 +00:00
|
|
|
|
|
|
|
// Verify the bytes
|
2023-10-24 07:08:35 +00:00
|
|
|
must.Eq(t, 0, b[len(b)-1])
|
|
|
|
must.False(t, b.Check(255))
|
2016-02-20 19:56:48 +00:00
|
|
|
}
|