Codebase list golang-github-vbauerster-mpb / de5bc8f
optimize CalcPercentage Vladimir Bauer 8 years ago
2 changed file(s) with 13 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
348348 if total <= 0 {
349349 return 0
350350 }
351 if current > total {
352 current = total
353 }
354
355 p := float64(width) * float64(current) / float64(total)
351 p := float64(width*current) / float64(total)
356352 return int64(round(p))
357353 }
358354
00 package decor
11
2 import "testing"
2 import (
3 "testing"
4 )
35
4 func TestCalcPersentage(t *testing.T) {
6 func TestCalcPercentage(t *testing.T) {
57 // key is barWidth
68 testSuite := map[int64]map[string]struct {
79 total, current, expected int64
1719 "t,c,e{100,50,50}": {100, 50, 50},
1820 "t,c,e{100,99,99}": {100, 99, 99},
1921 "t,c,e{100,100,100}": {100, 100, 100},
20 "t,c,e{100,101,100}": {100, 101, 100},
22 "t,c,e{100,101,101}": {100, 101, 101},
23 "t,c,e{100,102,101}": {100, 102, 102},
2124 "t,c,e{120,0,0}": {120, 0, 0},
2225 "t,c,e{120,10,8}": {120, 10, 8},
2326 "t,c,e{120,15,13}": {120, 15, 13},
2831 "t,c,e{120,118,98}": {120, 118, 98},
2932 "t,c,e{120,119,99}": {120, 119, 99},
3033 "t,c,e{120,120,100}": {120, 120, 100},
31 "t,c,e{120,121,100}": {120, 121, 100},
34 "t,c,e{120,121,101}": {120, 121, 101},
35 "t,c,e{120,122,101}": {120, 122, 102},
3236 },
3337 80: {
3438 "t,c,e{-1,-1,0}": {-1, -1, 0},
4145 "t,c,e{100,50,40}": {100, 50, 40},
4246 "t,c,e{100,99,79}": {100, 99, 79},
4347 "t,c,e{100,100,80}": {100, 100, 80},
44 "t,c,e{100,101,80}": {100, 101, 80},
48 "t,c,e{100,101,81}": {100, 101, 81},
49 "t,c,e{100,102,82}": {100, 102, 82},
4550 "t,c,e{120,0,0}": {120, 0, 0},
4651 "t,c,e{120,10,7}": {120, 10, 7},
4752 "t,c,e{120,15,10}": {120, 15, 10},
5257 "t,c,e{120,118,79}": {120, 118, 79},
5358 "t,c,e{120,119,79}": {120, 119, 79},
5459 "t,c,e{120,120,80}": {120, 120, 80},
55 "t,c,e{120,121,80}": {120, 121, 80},
60 "t,c,e{120,121,81}": {120, 121, 81},
61 "t,c,e{120,122,81}": {120, 122, 81},
5662 },
5763 }
5864