Skip to content

Commit 9f5f401

Browse files
committed
Add AnySet() to topologymanager bitmask API
1 parent d9c3d15 commit 9f5f401

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

pkg/kubelet/cm/topologymanager/bitmask/bitmask.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type BitMask interface {
3333
IsEqual(mask BitMask) bool
3434
IsEmpty() bool
3535
IsSet(bit int) bool
36+
AnySet(bits []int) bool
3637
IsNarrowerThan(mask BitMask) bool
3738
String() string
3839
Count() int
@@ -120,6 +121,16 @@ func (s *bitMask) IsSet(bit int) bool {
120121
return (*s & (1 << uint64(bit))) > 0
121122
}
122123

124+
// AnySet checks bit in mask to see if any provided bit is set to one
125+
func (s *bitMask) AnySet(bits []int) bool {
126+
for _, b := range bits {
127+
if s.IsSet(b) {
128+
return true
129+
}
130+
}
131+
return false
132+
}
133+
123134
// IsEqual checks if masks are equal
124135
func (s *bitMask) IsEqual(mask BitMask) bool {
125136
return *s == *mask.(*bitMask)

pkg/kubelet/cm/topologymanager/bitmask/bitmask_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,53 @@ func TestIsSet(t *testing.T) {
381381
}
382382
}
383383

384+
func TestAnySet(t *testing.T) {
385+
tcases := []struct {
386+
name string
387+
mask []int
388+
checkBits []int
389+
expectedSet bool
390+
}{
391+
{
392+
name: "Check if any bits from 11 in mask 00 is set",
393+
mask: nil,
394+
checkBits: []int{0, 1},
395+
expectedSet: false,
396+
},
397+
{
398+
name: "Check if any bits from 11 in mask 01 is set",
399+
mask: []int{0},
400+
checkBits: []int{0, 1},
401+
expectedSet: true,
402+
},
403+
{
404+
name: "Check if any bits from 11 in mask 11 is set",
405+
mask: []int{0, 1},
406+
checkBits: []int{0, 1},
407+
expectedSet: true,
408+
},
409+
{
410+
name: "Check if any bit outside range 0-63 is set",
411+
mask: []int{0, 1},
412+
checkBits: []int{64, 65},
413+
expectedSet: false,
414+
},
415+
{
416+
name: "Check if any bits from 1001 in mask 0110 is set",
417+
mask: []int{1, 2},
418+
checkBits: []int{0, 3},
419+
expectedSet: false,
420+
},
421+
}
422+
for _, tc := range tcases {
423+
mask, _ := NewBitMask(tc.mask...)
424+
set := mask.AnySet(tc.checkBits)
425+
if set != tc.expectedSet {
426+
t.Errorf("Expected value to be %v, got %v", tc.expectedSet, set)
427+
}
428+
}
429+
}
430+
384431
func TestIsEqual(t *testing.T) {
385432
tcases := []struct {
386433
name string

0 commit comments

Comments
 (0)