Skip to content

Commit 368ed15

Browse files
authored
Merge pull request #6 from github/more-tests
Add some more tests
2 parents ca03c07 + b39ea67 commit 368ed15

34 files changed

+6196
-313
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
/.gopath
22
/bin
3-
/vendor

Gopkg.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
#
33
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
44
# for detailed Gopkg.toml documentation.
5+
6+
[prune]
7+
unused-packages = true
8+
go-tests = true
9+
non-go = true

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ all: bin/git-sizer
2121
.PHONY: bin/git-sizer
2222
bin/git-sizer:
2323
mkdir -p bin
24-
$(GO) build $(GOFLAGS) -o $@ $(PACKAGE)
24+
cd $(GOPATH)/src/$(PACKAGE) && $(GO) build $(GOFLAGS) -o $(ROOTDIR)/$@ $(PACKAGE)
2525

2626
# Cross-compile for a bunch of common platforms. Note that this
2727
# doesn't work with USE_ISATTY:
@@ -43,7 +43,7 @@ define PLATFORM_template =
4343
.PHONY: bin/git-sizer-$(1)-$(2)$(3)
4444
bin/git-sizer-$(1)-$(2)$(3):
4545
mkdir -p bin
46-
GOOS=$(1) GOARCH=$(2) $$(GO) build $$(GOFLAGS) -o $$@ $$(PACKAGE)
46+
cd $$(GOPATH)/src/$$(PACKAGE) && GOOS=$(1) GOARCH=$(2) $$(GO) build $$(GOFLAGS) -o $$(ROOTDIR)/$$@ $$(PACKAGE)
4747
endef
4848

4949
$(eval $(call PLATFORM_template,linux,amd64))
@@ -60,7 +60,7 @@ test: bin/git-sizer gotest
6060

6161
.PHONY: gotest
6262
gotest:
63-
$(GO) test -timeout 60s $(GOFLAGS) ./...
63+
cd $(GOPATH)/src/$(PACKAGE) && $(GO) test -timeout 60s $(GOFLAGS) ./...
6464

6565
.PHONY: gofmt
6666
gofmt:

sizes/count.go renamed to counts/counts.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sizes
1+
package counts
22

33
import (
44
"math"
@@ -33,8 +33,8 @@ func (n1 *Count32) Increment(n2 Count32) {
3333
*n1 = n1.Plus(n2)
3434
}
3535

36-
// Adjust `*n1` to be `max(*n1, n2)` and return true iff n2 was
37-
// bigger. Favor `*n1` if they are equal.
36+
// AdjustMaxIfNecessary adjusts `*n1` to be `max(*n1, n2)`. Return
37+
// true iff `n2` was greater than `*n1`.
3838
func (n1 *Count32) AdjustMaxIfNecessary(n2 Count32) bool {
3939
if n2 > *n1 {
4040
*n1 = n2
@@ -44,8 +44,8 @@ func (n1 *Count32) AdjustMaxIfNecessary(n2 Count32) bool {
4444
}
4545
}
4646

47-
// Adjust `*n1` to be `max(*n1, n2)` and return true iff n2 was
48-
// bigger. Favor `n2` if they are equal.
47+
// AdjustMaxIfPossible adjusts `*n1` to be `max(*n1, n2)`. Return true
48+
// iff `n2` was greater than or equal to `*n1`.
4949
func (n1 *Count32) AdjustMaxIfPossible(n2 Count32) bool {
5050
if n2 >= *n1 {
5151
*n1 = n2
@@ -81,8 +81,8 @@ func (n1 *Count64) Increment(n2 Count64) {
8181
*n1 = n1.Plus(n2)
8282
}
8383

84-
// Adjust `*n1` to be `max(*n1, n2)` and return true iff n2 was
85-
// bigger. Favor `*n1` if they are equal.
84+
// AdjustMaxIfNecessary adjusts `*n1` to be `max(*n1, n2)`. Return
85+
// true iff `n2` was greater than `*n1`.
8686
func (n1 *Count64) AdjustMaxIfNecessary(n2 Count64) bool {
8787
if n2 > *n1 {
8888
*n1 = n2
@@ -92,8 +92,8 @@ func (n1 *Count64) AdjustMaxIfNecessary(n2 Count64) bool {
9292
}
9393
}
9494

95-
// Adjust `*n1` to be `max(*n1, n2)` and return true iff n2 was
96-
// bigger. Favor `n2` if they are equal.
95+
// AdjustMaxIfPossible adjusts `*n1` to be `max(*n1, n2)`. Return true
96+
// iff `n2` was greater than or equal to `*n1`.
9797
func (n1 *Count64) AdjustMaxIfPossible(n2 Count64) bool {
9898
if n2 > *n1 {
9999
*n1 = n2

counts/counts_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package counts_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/github/git-sizer/counts"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestCount32(t *testing.T) {
12+
assert := assert.New(t)
13+
14+
c := counts.NewCount32(0)
15+
assert.Equalf(uint64(0), c.ToUint64(), "NewCount32(0).ToUint64() should be 0")
16+
17+
c.Increment(counts.Count32(0xf0000000))
18+
assert.Equalf(uint64(0xf0000000), c.ToUint64(), "Count32(0xf0000000).ToUint64() value")
19+
20+
c.Increment(counts.Count32(0xf0000000))
21+
assert.Equalf(uint64(0xffffffff), c.ToUint64(), "Count32(0xffffffff).ToUint64() value")
22+
}
23+
24+
func TestCount64(t *testing.T) {
25+
assert := assert.New(t)
26+
27+
c := counts.NewCount64(0)
28+
assert.Equalf(uint64(0), c.ToUint64(), "NewCount64(0).ToUint64() should be 0")
29+
30+
c.Increment(counts.Count64(0xf000000000000000))
31+
assert.Equalf(uint64(0xf000000000000000), c.ToUint64(), "Count64(0xf000000000000000).ToUint64() value")
32+
33+
c.Increment(counts.Count64(0xf000000000000000))
34+
assert.Equalf(uint64(0xffffffffffffffff), c.ToUint64(), "Count64(0xffffffffffffffff).ToUint64() value")
35+
}

counts/human.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package counts
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
type Prefix struct {
9+
Name string
10+
Multiplier uint64
11+
}
12+
13+
type Humaner interface {
14+
Human([]Prefix, string) (string, string)
15+
ToUint64() uint64
16+
}
17+
18+
var MetricPrefixes []Prefix
19+
20+
func init() {
21+
MetricPrefixes = []Prefix{
22+
{"", 1},
23+
{"k", 1e3},
24+
{"M", 1e6},
25+
{"G", 1e9},
26+
{"T", 1e12},
27+
{"P", 1e15},
28+
}
29+
}
30+
31+
var BinaryPrefixes []Prefix
32+
33+
func init() {
34+
BinaryPrefixes = []Prefix{
35+
{"", 1 << (10 * 0)},
36+
{"Ki", 1 << (10 * 1)},
37+
{"Mi", 1 << (10 * 2)},
38+
{"Gi", 1 << (10 * 3)},
39+
{"Ti", 1 << (10 * 4)},
40+
{"Pi", 1 << (10 * 5)},
41+
}
42+
}
43+
44+
// Format values, aligned, in `len(unit) + 10` or fewer characters
45+
// (except for extremely large numbers).
46+
func Human(n uint64, prefixes []Prefix, unit string) (string, string) {
47+
prefix := prefixes[0]
48+
wholePart := n
49+
for _, p := range prefixes {
50+
w := n / p.Multiplier
51+
if w >= 1 {
52+
wholePart = w
53+
prefix = p
54+
}
55+
}
56+
57+
if prefix.Multiplier == 1 {
58+
return fmt.Sprintf("%d", n), unit
59+
} else {
60+
mantissa := float64(n) / float64(prefix.Multiplier)
61+
var format string
62+
63+
if wholePart >= 100 {
64+
// `mantissa` can actually be up to 1023.999.
65+
format = "%.0f"
66+
} else if wholePart >= 10 {
67+
format = "%.1f"
68+
} else {
69+
format = "%.2f"
70+
}
71+
return fmt.Sprintf(format, mantissa), prefix.Name + unit
72+
}
73+
}
74+
75+
func (n Count32) Human(prefixes []Prefix, unit string) (string, string) {
76+
if n == math.MaxUint32 {
77+
return "∞", unit
78+
} else {
79+
return Human(uint64(n), prefixes, unit)
80+
}
81+
}
82+
83+
func (n Count64) Human(prefixes []Prefix, unit string) (string, string) {
84+
if n == math.MaxUint64 {
85+
return "∞", unit
86+
} else {
87+
return Human(uint64(n), prefixes, unit)
88+
}
89+
}

counts/human_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package counts_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/github/git-sizer/counts"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type humanTest struct {
12+
n uint64
13+
number, unit string
14+
}
15+
16+
func TestMetric(t *testing.T) {
17+
assert := assert.New(t)
18+
19+
for _, ht := range []humanTest{
20+
{0, "0", "cd"},
21+
{1, "1", "cd"},
22+
{999, "999", "cd"},
23+
{1000, "1.00", "kcd"},
24+
{1094, "1.09", "kcd"},
25+
{1096, "1.10", "kcd"},
26+
{9990, "9.99", "kcd"},
27+
{9999, "10.00", "kcd"}, // Not ideal, but ok
28+
{10000, "10.0", "kcd"},
29+
{10060, "10.1", "kcd"},
30+
{99999, "100.0", "kcd"}, // Not ideal, but ok
31+
{100000, "100", "kcd"},
32+
{999999, "1000", "kcd"}, // Not ideal, but ok
33+
{1000000, "1.00", "Mcd"},
34+
{9999999, "10.00", "Mcd"}, // Not ideal, but ok
35+
{10000000, "10.0", "Mcd"},
36+
{99999999, "100.0", "Mcd"}, // Not ideal, but ok
37+
{100000000, "100", "Mcd"},
38+
{999999999, "1000", "Mcd"}, // Not ideal, but ok
39+
{1000000000, "1.00", "Gcd"},
40+
{9999999999, "10.00", "Gcd"}, // Not ideal, but ok
41+
{10000000000, "10.0", "Gcd"},
42+
{99999999999, "100.0", "Gcd"}, // Not ideal, but ok
43+
{100000000000, "100", "Gcd"},
44+
{999999999999, "1000", "Gcd"}, // Not ideal, but ok
45+
{1000000000000, "1.00", "Tcd"},
46+
{999999999999999, "1000", "Tcd"}, // Not ideal, but ok
47+
{1000000000000000, "1.00", "Pcd"},
48+
{999999999999999999, "1000", "Pcd"},
49+
{1000000000000000000, "1000", "Pcd"},
50+
{9999999999999999999, "10000", "Pcd"},
51+
{10000000000000000000, "10000", "Pcd"},
52+
{12345678900000000000, "12346", "Pcd"}, // Not ideal, but ok
53+
{0xffffffffffffffff, "18447", "Pcd"}, // Not ideal, but ok
54+
} {
55+
number, unit := counts.Human(ht.n, counts.MetricPrefixes, "cd")
56+
assert.Equalf(ht.number, number, "Number for %d in metric", ht.n)
57+
assert.Equalf(ht.unit, unit, "Unit for %d in metric", ht.n)
58+
if ht.n < 0xffffffff {
59+
c := counts.NewCount32(ht.n)
60+
number, unit := c.Human(counts.MetricPrefixes, "cd")
61+
assert.Equalf(ht.number, number, "Number for Count32(%d) in metric", ht.n)
62+
assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in metric", ht.n)
63+
}
64+
if ht.n < 0xffffffffffffffff {
65+
c := counts.NewCount64(ht.n)
66+
number, unit := c.Human(counts.MetricPrefixes, "cd")
67+
assert.Equalf(ht.number, number, "Number for Count64(%d) in metric", ht.n)
68+
assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in metric", ht.n)
69+
}
70+
}
71+
}
72+
73+
func TestBinary(t *testing.T) {
74+
assert := assert.New(t)
75+
76+
for _, ht := range []humanTest{
77+
{0, "0", "B"},
78+
{1, "1", "B"},
79+
{1023, "1023", "B"},
80+
{1024, "1.00", "KiB"},
81+
{1234, "1.21", "KiB"},
82+
{1048575, "1024", "KiB"}, // Not ideal, but ok
83+
{1048576, "1.00", "MiB"},
84+
{1073741823, "1024", "MiB"}, // Not ideal, but ok
85+
{1073741824, "1.00", "GiB"},
86+
{1099511627775, "1024", "GiB"}, // Not ideal, but ok
87+
{1099511627776, "1.00", "TiB"},
88+
{1125899906842623, "1024", "TiB"}, // Not ideal, but ok
89+
{1125899906842624, "1.00", "PiB"},
90+
{1152921504606846975, "1024", "PiB"},
91+
{1152921504606846976, "1024", "PiB"},
92+
{0xffffffffffffffff, "16384", "PiB"},
93+
} {
94+
number, unit := counts.Human(ht.n, counts.BinaryPrefixes, "B")
95+
assert.Equalf(ht.number, number, "Number for %d in binary", ht.n)
96+
assert.Equalf(ht.unit, unit, "Unit for %d in binary", ht.n)
97+
if ht.n < 0xffffffff {
98+
c := counts.NewCount32(ht.n)
99+
number, unit := c.Human(counts.BinaryPrefixes, "B")
100+
assert.Equalf(ht.number, number, "Number for Count32(%d) in binary", ht.n)
101+
assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in binary", ht.n)
102+
}
103+
if ht.n < 0xffffffffffffffff {
104+
c := counts.NewCount64(ht.n)
105+
number, unit := c.Human(counts.BinaryPrefixes, "B")
106+
assert.Equalf(ht.number, number, "Number for Count64(%d) in binary", ht.n)
107+
assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in binary", ht.n)
108+
}
109+
}
110+
}
111+
112+
func TestLimits32(t *testing.T) {
113+
assert := assert.New(t)
114+
115+
c := counts.NewCount32(0xffffffff)
116+
number, unit := c.Human(counts.MetricPrefixes, "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())
119+
}
120+
121+
func TestLimits64(t *testing.T) {
122+
assert := assert.New(t)
123+
124+
c := counts.NewCount64(0xffffffffffffffff)
125+
number, unit := c.Human(counts.MetricPrefixes, "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())
128+
}

0 commit comments

Comments
 (0)