Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
4d3f49ba
Commit
4d3f49ba
authored
Dec 18, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #18557 from timstclair/resource-math
Auto commit by PR queue bot
parents
1f24297e
b4116eb7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
4 deletions
+61
-4
quantity.go
pkg/api/resource/quantity.go
+17
-4
quantity_test.go
pkg/api/resource/quantity_test.go
+44
-0
No files found.
pkg/api/resource/quantity.go
View file @
4d3f49ba
...
@@ -328,15 +328,28 @@ func (q *Quantity) Cmp(y Quantity) int {
...
@@ -328,15 +328,28 @@ func (q *Quantity) Cmp(y Quantity) int {
}
}
func
(
q
*
Quantity
)
Add
(
y
Quantity
)
error
{
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
return
nil
}
}
func
(
q
*
Quantity
)
Sub
(
y
Quantity
)
error
{
func
(
q
*
Quantity
)
Sub
(
y
Quantity
)
error
{
if
q
.
Format
!=
y
.
Format
{
switch
{
return
fmt
.
Errorf
(
"format mismatch: %v vs. %v"
,
q
.
Format
,
y
.
Format
)
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
return
nil
}
}
...
...
pkg/api/resource/quantity_test.go
View file @
4d3f49ba
...
@@ -535,3 +535,47 @@ func TestQFlagIsPFlag(t *testing.T) {
...
@@ -535,3 +535,47 @@ func TestQFlagIsPFlag(t *testing.T) {
t
.
Errorf
(
"Unexpected result %v != %v"
,
e
,
a
)
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
())
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment