Skip to content

Commit

Permalink
Fix float64 comparison in stats_helpers_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed May 13, 2017
1 parent 7633e2c commit b1780c6
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions cli/command/container/stats_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import (
"testing"
)

func float64Equals(x, y float64) bool {
epsilon := 1e-9
return x-y < epsilon && y-x < epsilon
}

func TestCalculateMemUsageUnixNoCache(t *testing.T) {
// Given
stats := types.MemoryStats{Usage: 500, Stats: map[string]uint64{"cache": 400}}

// When
result := calculateMemUsageUnixNoCache(stats)
expected := float64(100.0)

// Then
if result != 100 {
t.Errorf("mem = %d, want 100", result)
if !float64Equals(result, expected) {
t.Errorf("mem = %f, want %f", result, expected)
}
}

Expand All @@ -28,14 +34,14 @@ func TestCalculateMemPercentUnixNoCache(t *testing.T) {
t.Run("Limit is set", func(t *testing.T) {
result := calculateMemPercentUnixNoCache(someLimit, used)
expected := float64(70.0)
if result != expected {
if !float64Equals(result, expected) {
t.Errorf("percent = %f, want %f", result, expected)
}
})
t.Run("No limit, no cgroup data", func(t *testing.T) {
result := calculateMemPercentUnixNoCache(noLimit, used)
expected := float64(0.0)
if result != expected {
if !float64Equals(result, expected) {
t.Errorf("percent = %f, want %f", result, expected)
}
})
Expand Down

0 comments on commit b1780c6

Please sign in to comment.