mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
cpuset: Convert Union arguments to variadic
This allows Union to implement UnionAll easily.
This commit is contained in:
parent
0e49c542d7
commit
824bd57ad6
@ -217,7 +217,7 @@ func (p *staticPolicy) validateState(s state.State) error {
|
|||||||
tmpCPUSets = append(tmpCPUSets, cset)
|
tmpCPUSets = append(tmpCPUSets, cset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalKnownCPUs = totalKnownCPUs.UnionAll(tmpCPUSets)
|
totalKnownCPUs = totalKnownCPUs.Union(tmpCPUSets...)
|
||||||
if !totalKnownCPUs.Equals(p.topology.CPUDetails.CPUs()) {
|
if !totalKnownCPUs.Equals(p.topology.CPUDetails.CPUs()) {
|
||||||
return fmt.Errorf("current set of available CPUs \"%s\" doesn't match with CPUs in state \"%s\"",
|
return fmt.Errorf("current set of available CPUs \"%s\" doesn't match with CPUs in state \"%s\"",
|
||||||
p.topology.CPUDetails.CPUs().String(), totalKnownCPUs.String())
|
p.topology.CPUDetails.CPUs().String(), totalKnownCPUs.String())
|
||||||
|
@ -145,23 +145,9 @@ func (s CPUSet) IsSubsetOf(s2 CPUSet) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Union returns a new CPU set that contains all of the elements from this
|
// Union returns a new CPU set that contains all of the elements from this
|
||||||
// set and all of the elements from the supplied set, without mutating
|
|
||||||
// either source set.
|
|
||||||
func (s CPUSet) Union(s2 CPUSet) CPUSet {
|
|
||||||
b := NewBuilder()
|
|
||||||
for cpu := range s.elems {
|
|
||||||
b.Add(cpu)
|
|
||||||
}
|
|
||||||
for cpu := range s2.elems {
|
|
||||||
b.Add(cpu)
|
|
||||||
}
|
|
||||||
return b.Result()
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnionAll returns a new CPU set that contains all of the elements from this
|
|
||||||
// set and all of the elements from the supplied sets, without mutating
|
// set and all of the elements from the supplied sets, without mutating
|
||||||
// either source set.
|
// either source set.
|
||||||
func (s CPUSet) UnionAll(s2 []CPUSet) CPUSet {
|
func (s CPUSet) Union(s2 ...CPUSet) CPUSet {
|
||||||
b := NewBuilder()
|
b := NewBuilder()
|
||||||
for cpu := range s.elems {
|
for cpu := range s.elems {
|
||||||
b.Add(cpu)
|
b.Add(cpu)
|
||||||
|
@ -175,55 +175,39 @@ func TestCPUSetIsSubsetOf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCPUSetUnionAll(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
s1 CPUSet
|
|
||||||
s2 CPUSet
|
|
||||||
s3 CPUSet
|
|
||||||
expected CPUSet
|
|
||||||
}{
|
|
||||||
{NewCPUSet(), NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(4, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
|
||||||
{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(), NewCPUSet(4), NewCPUSet(1, 2, 3, 4, 5)},
|
|
||||||
{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
|
||||||
}
|
|
||||||
for _, c := range testCases {
|
|
||||||
s := []CPUSet{}
|
|
||||||
s = append(s, c.s2)
|
|
||||||
s = append(s, c.s3)
|
|
||||||
result := c.s1.UnionAll(s)
|
|
||||||
if !result.Equals(c.expected) {
|
|
||||||
t.Fatalf("expected the union of s1 and s2 to be [%v] (got [%v]), s1: [%v], s2: [%v]", c.expected, result, c.s1, c.s2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCPUSetUnion(t *testing.T) {
|
func TestCPUSetUnion(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
s1 CPUSet
|
s1 CPUSet
|
||||||
s2 CPUSet
|
others []CPUSet
|
||||||
expected CPUSet
|
expected CPUSet
|
||||||
}{
|
}{
|
||||||
{NewCPUSet(), NewCPUSet(), NewCPUSet()},
|
{NewCPUSet(5), []CPUSet{}, NewCPUSet(5)},
|
||||||
|
|
||||||
{NewCPUSet(), NewCPUSet(5), NewCPUSet(5)},
|
{NewCPUSet(), []CPUSet{NewCPUSet()}, NewCPUSet()},
|
||||||
{NewCPUSet(5), NewCPUSet(), NewCPUSet(5)},
|
|
||||||
{NewCPUSet(5), NewCPUSet(5), NewCPUSet(5)},
|
|
||||||
|
|
||||||
{NewCPUSet(), NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(), []CPUSet{NewCPUSet(5)}, NewCPUSet(5)},
|
||||||
{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(5), []CPUSet{NewCPUSet()}, NewCPUSet(5)},
|
||||||
{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(5), []CPUSet{NewCPUSet(5)}, NewCPUSet(5)},
|
||||||
|
|
||||||
{NewCPUSet(5), NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(), []CPUSet{NewCPUSet(1, 2, 3, 4, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(5), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(1, 2, 3, 4, 5), []CPUSet{NewCPUSet()}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
{NewCPUSet(1, 2, 3, 4, 5), []CPUSet{NewCPUSet(1, 2, 3, 4, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
|
||||||
{NewCPUSet(1, 2), NewCPUSet(3, 4, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(5), []CPUSet{NewCPUSet(1, 2, 3, 4, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
{NewCPUSet(1, 2, 3), NewCPUSet(3, 4, 5), NewCPUSet(1, 2, 3, 4, 5)},
|
{NewCPUSet(1, 2, 3, 4, 5), []CPUSet{NewCPUSet(5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
|
||||||
|
{NewCPUSet(1, 2), []CPUSet{NewCPUSet(3, 4, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
{NewCPUSet(1, 2, 3), []CPUSet{NewCPUSet(3, 4, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
|
||||||
|
{NewCPUSet(), []CPUSet{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(4, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
{NewCPUSet(1, 2, 3, 4, 5), []CPUSet{NewCPUSet(), NewCPUSet(4)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
|
{NewCPUSet(1, 2, 3, 4, 5), []CPUSet{NewCPUSet(1, 2, 3, 4, 5), NewCPUSet(1, 5)}, NewCPUSet(1, 2, 3, 4, 5)},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range testCases {
|
for _, c := range testCases {
|
||||||
result := c.s1.Union(c.s2)
|
result := c.s1.Union(c.others...)
|
||||||
if !result.Equals(c.expected) {
|
if !result.Equals(c.expected) {
|
||||||
t.Fatalf("expected the union of s1 and s2 to be [%v] (got [%v]), s1: [%v], s2: [%v]", c.expected, result, c.s1, c.s2)
|
t.Fatalf("expected the union of s1 and s2 to be [%v] (got [%v]), others: [%v]", c.expected, result, c.others)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user