Fixed panic, added some tests

This commit is contained in:
Stephen McQuay 2015-10-31 16:16:27 -07:00
parent 1c4f1a6f95
commit fc1f5ce2bc
2 changed files with 63 additions and 25 deletions

7
bps.go
View File

@ -133,7 +133,7 @@ func (b *BPS) HumanRate() string {
// Sparkline returns a human-friendly sprakline of history // Sparkline returns a human-friendly sprakline of history
func (b *BPS) Sparkline(count int) string { func (b *BPS) Sparkline(count int) string {
if count > len(b.buckets) { if count <= 0 || count > len(b.buckets) {
return "bucket count inappropriate" return "bucket count inappropriate"
} }
sparks := []string{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"} sparks := []string{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}
@ -147,10 +147,13 @@ func (b *BPS) Sparkline(count int) string {
// find max // find max
for i := 0; i < len(b.buckets); i++ { for i := 0; i < len(b.buckets); i++ {
cur = b.buckets[i] cur += b.buckets[i]
if i%bucketsPer == 0 {
if cur > max { if cur > max {
max = cur max = cur
} }
cur = 0
}
} }
if max == 0 { if max == 0 {

View File

@ -94,32 +94,67 @@ func TestHumanBytes(t *testing.T) {
} }
} }
func TestSparkline(t *testing.T) { // TestSparkBadBucketCounts just makes sure that bad inputs are reported
func TestSparkBadBucketCounts(t *testing.T) {
bw := BPS{ bw := BPS{
buckets: make([]int64, 10), buckets: make([]int64, 10),
} }
t.Logf("%+v", bw.Sparkline(100)) sizes := []int{-10, -1, 0, 11, 12, 100, 1000}
for _, size := range sizes {
got := bw.Sparkline(size)
want := "bucket count inappropriate"
if got != want {
t.Fatalf("got: %v, want: %v", got, want)
}
}
}
// TestSparkline just creates a simple BPS and traverses all reasonable count
// sizes. It's primarily a test for panics which plagued us early on.
func TestSparkline(t *testing.T) {
buckets := 20
bw := BPS{
buckets: make([]int64, buckets),
}
t.Logf("all 0")
for i := 0; i < buckets; i++ {
bw.buckets[i] = 0
}
for i := 1; i < buckets; i++ {
line := bw.Sparkline(i)
t.Logf("count: %d, len %d: %+v", i, len([]rune(line)), line)
}
t.Logf("increasing")
for i := 0; i < buckets; i++ {
bw.buckets[i] = int64(i * buckets)
}
for i := 1; i < buckets; i++ {
line := bw.Sparkline(i)
t.Logf("count: %d, len %d: %+v", i, len([]rune(line)), line)
}
t.Logf("decreasing")
for i := 0; i < buckets; i++ {
bw.buckets[i] = int64((buckets - i) * buckets)
}
for i := 1; i < buckets; i++ {
line := bw.Sparkline(i)
t.Logf("count: %d, len %d: %+v", i, len([]rune(line)), line)
}
}
func TestSparkRandom(t *testing.T) {
buckets := 20
bw := BPS{
buckets: make([]int64, buckets),
}
bw.buckets[3] = 100 bw.buckets[3] = 100
t.Logf("%+v", bw.Sparkline(10)) t.Logf("%+v", bw.Sparkline(10))
bw.buckets[7] = 300 bw.buckets[7] = 300
for i := 1; i < 10; i++ { for i := 1; i < buckets; i++ {
t.Logf("%+v", bw.Sparkline(i)) line := bw.Sparkline(i)
t.Logf("count: %d, len: %d: %+v", i, len([]rune(line)), line)
} }
for i := 0; i < 10; i++ {
bw.buckets[i] = int64(i * 10)
t.Logf("%+v", bw.Sparkline(10))
}
t.Logf("%+v", bw.Sparkline(10))
for i := 0; i < 10; i++ {
bw.buckets[i] = 0
}
for i := 0; i < 10; i++ {
bw.buckets[i] = int64((10 - i) * 10)
t.Logf("%+v", bw.Sparkline(10))
}
t.Logf("%+v", bw.Sparkline(10))
bw.buckets[5] = 200
t.Logf("%+v", bw.Sparkline(10))
} }