Skip to content

Commit 95f2966

Browse files
committed
Change Humanable.ToUint64() to report whether it overflowed
1 parent cdf6b1d commit 95f2966

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

counts/counts.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func NewCount32(n uint64) Count32 {
1414
return Count32(n)
1515
}
1616

17-
func (n Count32) ToUint64() uint64 {
18-
return uint64(n)
17+
func (n Count32) ToUint64() (uint64, bool) {
18+
return uint64(n), n == math.MaxUint32
1919
}
2020

2121
// Return the sum of two Count32s, capped at math.MaxUint32.
@@ -62,8 +62,8 @@ func NewCount64(n uint64) Count64 {
6262
return Count64(n)
6363
}
6464

65-
func (n Count64) ToUint64() uint64 {
66-
return uint64(n)
65+
func (n Count64) ToUint64() (uint64, bool) {
66+
return uint64(n), n == math.MaxUint64
6767
}
6868

6969
// Return the sum of two Count64s, capped at math.MaxUint64.

counts/counts_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,43 @@ import (
1111
func TestCount32(t *testing.T) {
1212
assert := assert.New(t)
1313

14+
var value uint64
15+
var overflow bool
16+
1417
c := counts.NewCount32(0)
15-
assert.Equalf(uint64(0), c.ToUint64(), "NewCount32(0).ToUint64() should be 0")
18+
value, overflow = c.ToUint64()
19+
assert.Equalf(uint64(0), value, "NewCount32(0).ToUint64() should be 0")
20+
assert.False(overflow, "NewCount32(0).ToUint64() does not overflow")
1621

1722
c.Increment(counts.Count32(0xf0000000))
18-
assert.Equalf(uint64(0xf0000000), c.ToUint64(), "Count32(0xf0000000).ToUint64() value")
23+
value, overflow = c.ToUint64()
24+
assert.Equalf(uint64(0xf0000000), value, "Count32(0xf0000000).ToUint64() value")
25+
assert.False(overflow, "NewCount32(0xf0000000).ToUint64() does not overflow")
1926

2027
c.Increment(counts.Count32(0xf0000000))
21-
assert.Equalf(uint64(0xffffffff), c.ToUint64(), "Count32(0xffffffff).ToUint64() value")
28+
value, overflow = c.ToUint64()
29+
assert.Equalf(uint64(0xffffffff), value, "Count32(0xffffffff).ToUint64() value")
30+
assert.True(overflow, "NewCount32(0xffffffff).ToUint64() overflows")
2231
}
2332

2433
func TestCount64(t *testing.T) {
2534
assert := assert.New(t)
2635

36+
var value uint64
37+
var overflow bool
38+
2739
c := counts.NewCount64(0)
28-
assert.Equalf(uint64(0), c.ToUint64(), "NewCount64(0).ToUint64() should be 0")
40+
value, overflow = c.ToUint64()
41+
assert.Equalf(uint64(0), value, "NewCount64(0).ToUint64() should be 0")
42+
assert.False(overflow, "NewCount64(0).ToUint64() does not overflow")
2943

3044
c.Increment(counts.Count64(0xf000000000000000))
31-
assert.Equalf(uint64(0xf000000000000000), c.ToUint64(), "Count64(0xf000000000000000).ToUint64() value")
45+
value, overflow = c.ToUint64()
46+
assert.Equalf(uint64(0xf000000000000000), value, "Count64(0xf000000000000000).ToUint64() value")
47+
assert.False(overflow, "NewCount64(0xf000000000000000).ToUint64() does not overflow")
3248

3349
c.Increment(counts.Count64(0xf000000000000000))
34-
assert.Equalf(uint64(0xffffffffffffffff), c.ToUint64(), "Count64(0xffffffffffffffff).ToUint64() value")
50+
value, overflow = c.ToUint64()
51+
assert.Equalf(uint64(0xffffffffffffffff), value, "Count64(0xffffffffffffffff).ToUint64() value")
52+
assert.True(overflow, "NewCount64(0xffffffffffffffff).ToUint64() overflows")
3553
}

counts/human.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import (
77

88
// A quantity that can be made human-readable using Human().
99
type Humanable interface {
10+
// Return the value and units as separate strings.
1011
Human(Humaner, string) (string, string)
11-
ToUint64() uint64
12+
13+
// Return the value as a uint64, and a boolean telling whether it
14+
// overflowed.
15+
ToUint64() (uint64, bool)
1216
}
1317

1418
// An object that can format a Humanable in human-readable format.

counts/human_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ func TestLimits32(t *testing.T) {
114114

115115
c := counts.NewCount32(0xffffffff)
116116
number, unit := c.Human(counts.Metric, "cd")
117-
assert.Equalf("∞", number, "Number for Count32(%d) in metric", c.ToUint64())
118-
assert.Equalf("cd", unit, "Unit for Count32(%d) in metric", c.ToUint64())
117+
assert.Equalf("∞", number, "Number for Count32(0xffffffff) in metric")
118+
assert.Equalf("cd", unit, "Unit for Count32(0xffffffff) in metric")
119119
}
120120

121121
func TestLimits64(t *testing.T) {
122122
assert := assert.New(t)
123123

124124
c := counts.NewCount64(0xffffffffffffffff)
125125
number, unit := c.Human(counts.Metric, "B")
126-
assert.Equalf("∞", number, "Number for Count64(%d) in metric", c.ToUint64())
127-
assert.Equalf("B", unit, "Unit for Count64(%d) in metric", c.ToUint64())
126+
assert.Equalf("∞", number, "Number for Count64(0xffffffffffffffff) in metric")
127+
assert.Equalf("B", unit, "Unit for Count64(0xffffffffffffffff) in metric")
128128
}

sizes/output.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ func (l *item) Footnote(nameStyle NameStyle) string {
159159
// return the string that should be used as its "level of concern" and
160160
// `true`; otherwise, return `"", false`.
161161
func (l *item) levelOfConcern(threshold Threshold) (string, bool) {
162-
alert := Threshold(float64(l.value.ToUint64()) / l.scale)
162+
value, _ := l.value.ToUint64()
163+
alert := Threshold(float64(value) / l.scale)
163164
if alert < threshold {
164165
return "", false
165166
}

0 commit comments

Comments
 (0)