mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Merge pull request #18557 from timstclair/resource-math
Auto commit by PR queue bot
This commit is contained in:
commit
4d3f49baf0
@ -328,15 +328,28 @@ func (q *Quantity) Cmp(y Quantity) int {
|
||||
}
|
||||
|
||||
func (q *Quantity) Add(y Quantity) error {
|
||||
q.Amount.Add(q.Amount, y.Amount)
|
||||
switch {
|
||||
case y.Amount == nil:
|
||||
// Adding 0: do nothing.
|
||||
case q.Amount == nil:
|
||||
q.Amount = &inf.Dec{}
|
||||
return q.Add(y)
|
||||
default:
|
||||
q.Amount.Add(q.Amount, y.Amount)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Quantity) Sub(y Quantity) error {
|
||||
if q.Format != y.Format {
|
||||
return fmt.Errorf("format mismatch: %v vs. %v", q.Format, y.Format)
|
||||
switch {
|
||||
case y.Amount == nil:
|
||||
// Subtracting 0: do nothing.
|
||||
case q.Amount == nil:
|
||||
q.Amount = &inf.Dec{}
|
||||
return q.Sub(y)
|
||||
default:
|
||||
q.Amount.Sub(q.Amount, y.Amount)
|
||||
}
|
||||
q.Amount.Sub(q.Amount, y.Amount)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -535,3 +535,47 @@ func TestQFlagIsPFlag(t *testing.T) {
|
||||
t.Errorf("Unexpected result %v != %v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSub(t *testing.T) {
|
||||
tests := []struct {
|
||||
a Quantity
|
||||
b Quantity
|
||||
expected Quantity
|
||||
}{
|
||||
{Quantity{dec(10, 0), DecimalSI}, Quantity{dec(1, 1), DecimalSI}, Quantity{dec(0, 0), DecimalSI}},
|
||||
{Quantity{dec(10, 0), DecimalSI}, Quantity{dec(1, 0), BinarySI}, Quantity{dec(9, 0), DecimalSI}},
|
||||
{Quantity{dec(10, 0), BinarySI}, Quantity{dec(1, 0), DecimalSI}, Quantity{dec(9, 0), BinarySI}},
|
||||
{Quantity{nil, DecimalSI}, Quantity{dec(50, 0), DecimalSI}, Quantity{dec(-50, 0), DecimalSI}},
|
||||
{Quantity{dec(50, 0), DecimalSI}, Quantity{nil, DecimalSI}, Quantity{dec(50, 0), DecimalSI}},
|
||||
{Quantity{nil, DecimalSI}, Quantity{nil, DecimalSI}, Quantity{dec(0, 0), DecimalSI}},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
test.a.Sub(test.b)
|
||||
if test.a.Cmp(test.expected) != 0 {
|
||||
t.Errorf("[%d] Expected %q, got %q", i, test.expected.String(), test.a.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
tests := []struct {
|
||||
a Quantity
|
||||
b Quantity
|
||||
expected Quantity
|
||||
}{
|
||||
{Quantity{dec(10, 0), DecimalSI}, Quantity{dec(1, 1), DecimalSI}, Quantity{dec(20, 0), DecimalSI}},
|
||||
{Quantity{dec(10, 0), DecimalSI}, Quantity{dec(1, 0), BinarySI}, Quantity{dec(11, 0), DecimalSI}},
|
||||
{Quantity{dec(10, 0), BinarySI}, Quantity{dec(1, 0), DecimalSI}, Quantity{dec(11, 0), BinarySI}},
|
||||
{Quantity{nil, DecimalSI}, Quantity{dec(50, 0), DecimalSI}, Quantity{dec(50, 0), DecimalSI}},
|
||||
{Quantity{dec(50, 0), DecimalSI}, Quantity{nil, DecimalSI}, Quantity{dec(50, 0), DecimalSI}},
|
||||
{Quantity{nil, DecimalSI}, Quantity{nil, DecimalSI}, Quantity{dec(0, 0), DecimalSI}},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
test.a.Add(test.b)
|
||||
if test.a.Cmp(test.expected) != 0 {
|
||||
t.Errorf("[%d] Expected %q, got %q", i, test.expected.String(), test.a.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user