From 757729404372bf47de3d48296d20c3be862d6714 Mon Sep 17 00:00:00 2001 From: Dan Rammer Date: Wed, 23 Feb 2022 10:54:36 -0600 Subject: [PATCH] Allow creation of bitset with 0 size (#116) * allow creation of bitset with 0 size Signed-off-by: Daniel Rammer * removed unecessary math import Signed-off-by: Daniel Rammer * added unit test and fixed lint issue Signed-off-by: Daniel Rammer --- flytestdlib/bitarray/bitset.go | 7 +++++-- flytestdlib/bitarray/bitset_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/flytestdlib/bitarray/bitset.go b/flytestdlib/bitarray/bitset.go index 0fc6299e42..883b9ded65 100644 --- a/flytestdlib/bitarray/bitset.go +++ b/flytestdlib/bitarray/bitset.go @@ -67,7 +67,10 @@ func (s BitSet) DeepCopy() BitSet { // Initializes a new BitSet of the specified size. func NewBitSet(desiredCap uint) *BitSet { - // Create enough blocks to contain the number of intended bits. - a := make(BitSet, ((desiredCap-1)/blockSize)+1) + size := desiredCap / blockSize + if desiredCap%blockSize != 0 { + size++ + } + a := make(BitSet, size) return &a } diff --git a/flytestdlib/bitarray/bitset_test.go b/flytestdlib/bitarray/bitset_test.go index e2dfa42c2e..72e91f70d0 100644 --- a/flytestdlib/bitarray/bitset_test.go +++ b/flytestdlib/bitarray/bitset_test.go @@ -36,6 +36,14 @@ func TestBitSet_Set(t *testing.T) { } func TestNewBitSet(t *testing.T) { + t.Run("Empty", func(t *testing.T) { + b := new(BitSet) + assert.Equal(t, 0, b.BlockCount()) + + b = NewBitSet(0) + assert.Equal(t, 0, b.BlockCount()) + }) + t.Run("Block size", func(t *testing.T) { b := NewBitSet(63) assert.Equal(t, 2, b.BlockCount())