Codebase list golang-github-vbauerster-mpb / aba0562
drop internal.Round, use math.Round Vladimir Bauer 7 years ago
4 changed file(s) with 58 addition(s) and 107 deletion(s). Raw diff Collapse all Expand all
55 "time"
66
77 "github.com/VividCortex/ewma"
8 "github.com/vbauerster/mpb/internal"
98 )
109
1110 type TimeNormalizer func(time.Duration) time.Duration
5857 return d.FormatMsg(*d.completeMsg)
5958 }
6059
61 v := internal.Round(d.average.Value())
60 v := math.Round(d.average.Value())
6261 remaining := d.normalizer(time.Duration((st.Total - st.Current) * int64(v)))
6362 hours := int64((remaining / time.Hour) % 60)
6463 minutes := int64((remaining / time.Minute) % 60)
132131
133132 var str string
134133 timeElapsed := time.Since(d.startTime)
135 v := internal.Round(float64(timeElapsed) / float64(st.Current))
134 v := math.Round(float64(timeElapsed) / float64(st.Current))
136135 if math.IsInf(v, 0) || math.IsNaN(v) {
137136 v = 0
138137 }
00 package internal
1
2 import "math"
13
24 // Percentage is a helper function, to calculate percentage.
35 func Percentage(total, current, width int64) int64 {
57 return 0
68 }
79 p := float64(width*current) / float64(total)
8 return int64(Round(p))
10 return int64(math.Round(p))
911 }
00 package internal
11
2 import (
3 "testing"
4 )
2 import "testing"
53
64 func TestPercentage(t *testing.T) {
75 // key is barWidth
8 testSuite := map[int64]map[string]struct {
6 testSuite := map[int64][]struct {
7 name string
98 total, current, expected int64
109 }{
1110 100: {
12 "t,c,e{-1,-1,0}": {-1, -1, 0},
13 "t,c,e{0,-1,0}": {0, -1, 0},
14 "t,c,e{0,0,0}": {0, 0, 0},
15 "t,c,e{0,1,0}": {0, 1, 0},
16 "t,c,e{100,0,0}": {100, 0, 0},
17 "t,c,e{100,10,10}": {100, 10, 10},
18 "t,c,e{100,15,15}": {100, 15, 15},
19 "t,c,e{100,50,50}": {100, 50, 50},
20 "t,c,e{100,99,99}": {100, 99, 99},
21 "t,c,e{100,100,100}": {100, 100, 100},
22 "t,c,e{100,101,101}": {100, 101, 101},
23 "t,c,e{100,102,101}": {100, 102, 102},
24 "t,c,e{120,0,0}": {120, 0, 0},
25 "t,c,e{120,10,8}": {120, 10, 8},
26 "t,c,e{120,15,13}": {120, 15, 13},
27 "t,c,e{120,50,42}": {120, 50, 42},
28 "t,c,e{120,60,50}": {120, 60, 50},
29 "t,c,e{120,99,83}": {120, 99, 83},
30 "t,c,e{120,101,84}": {120, 101, 84},
31 "t,c,e{120,118,98}": {120, 118, 98},
32 "t,c,e{120,119,99}": {120, 119, 99},
33 "t,c,e{120,120,100}": {120, 120, 100},
34 "t,c,e{120,121,101}": {120, 121, 101},
35 "t,c,e{120,122,101}": {120, 122, 102},
11 {"t,c,e{-1,-1,0}", -1, -1, 0},
12 {"t,c,e{0,-1,0}", 0, -1, 0},
13 {"t,c,e{0,0,0}", 0, 0, 0},
14 {"t,c,e{0,1,0}", 0, 1, 0},
15 {"t,c,e{100,0,0}", 100, 0, 0},
16 {"t,c,e{100,10,10}", 100, 10, 10},
17 {"t,c,e{100,15,15}", 100, 15, 15},
18 {"t,c,e{100,50,50}", 100, 50, 50},
19 {"t,c,e{100,99,99}", 100, 99, 99},
20 {"t,c,e{100,100,100}", 100, 100, 100},
21 {"t,c,e{100,101,101}", 100, 101, 101},
22 {"t,c,e{100,102,101}", 100, 102, 102},
23 {"t,c,e{120,0,0}", 120, 0, 0},
24 {"t,c,e{120,10,8}", 120, 10, 8},
25 {"t,c,e{120,15,13}", 120, 15, 13},
26 {"t,c,e{120,50,42}", 120, 50, 42},
27 {"t,c,e{120,60,50}", 120, 60, 50},
28 {"t,c,e{120,99,83}", 120, 99, 83},
29 {"t,c,e{120,101,84}", 120, 101, 84},
30 {"t,c,e{120,118,98}", 120, 118, 98},
31 {"t,c,e{120,119,99}", 120, 119, 99},
32 {"t,c,e{120,120,100}", 120, 120, 100},
33 {"t,c,e{120,121,101}", 120, 121, 101},
34 {"t,c,e{120,122,101}", 120, 122, 102},
3635 },
3736 80: {
38 "t,c,e{-1,-1,0}": {-1, -1, 0},
39 "t,c,e{0,-1,0}": {0, -1, 0},
40 "t,c,e{0,0,0}": {0, 0, 0},
41 "t,c,e{0,1,0}": {0, 1, 0},
42 "t,c,e{100,0,0}": {100, 0, 0},
43 "t,c,e{100,10,8}": {100, 10, 8},
44 "t,c,e{100,15,12}": {100, 15, 12},
45 "t,c,e{100,50,40}": {100, 50, 40},
46 "t,c,e{100,99,79}": {100, 99, 79},
47 "t,c,e{100,100,80}": {100, 100, 80},
48 "t,c,e{100,101,81}": {100, 101, 81},
49 "t,c,e{100,102,82}": {100, 102, 82},
50 "t,c,e{120,0,0}": {120, 0, 0},
51 "t,c,e{120,10,7}": {120, 10, 7},
52 "t,c,e{120,15,10}": {120, 15, 10},
53 "t,c,e{120,50,33}": {120, 50, 33},
54 "t,c,e{120,60,40}": {120, 60, 40},
55 "t,c,e{120,99,66}": {120, 99, 66},
56 "t,c,e{120,101,67}": {120, 101, 67},
57 "t,c,e{120,118,79}": {120, 118, 79},
58 "t,c,e{120,119,79}": {120, 119, 79},
59 "t,c,e{120,120,80}": {120, 120, 80},
60 "t,c,e{120,121,81}": {120, 121, 81},
61 "t,c,e{120,122,81}": {120, 122, 81},
37 {"t,c,e{-1,-1,0}", -1, -1, 0},
38 {"t,c,e{0,-1,0}", 0, -1, 0},
39 {"t,c,e{0,0,0}", 0, 0, 0},
40 {"t,c,e{0,1,0}", 0, 1, 0},
41 {"t,c,e{100,0,0}", 100, 0, 0},
42 {"t,c,e{100,10,8}", 100, 10, 8},
43 {"t,c,e{100,15,12}", 100, 15, 12},
44 {"t,c,e{100,50,40}", 100, 50, 40},
45 {"t,c,e{100,99,79}", 100, 99, 79},
46 {"t,c,e{100,100,80}", 100, 100, 80},
47 {"t,c,e{100,101,81}", 100, 101, 81},
48 {"t,c,e{100,102,82}", 100, 102, 82},
49 {"t,c,e{120,0,0}", 120, 0, 0},
50 {"t,c,e{120,10,7}", 120, 10, 7},
51 {"t,c,e{120,15,10}", 120, 15, 10},
52 {"t,c,e{120,50,33}", 120, 50, 33},
53 {"t,c,e{120,60,40}", 120, 60, 40},
54 {"t,c,e{120,99,66}", 120, 99, 66},
55 {"t,c,e{120,101,67}", 120, 101, 67},
56 {"t,c,e{120,118,79}", 120, 118, 79},
57 {"t,c,e{120,119,79}", 120, 119, 79},
58 {"t,c,e{120,120,80}", 120, 120, 80},
59 {"t,c,e{120,121,81}", 120, 121, 81},
60 {"t,c,e{120,122,81}", 120, 122, 81},
6261 },
6362 }
6463
6564 for width, cases := range testSuite {
66 for name, tc := range cases {
65 for _, tc := range cases {
6766 got := Percentage(tc.total, tc.current, width)
6867 if got != tc.expected {
69 t.Errorf("width %d; %s: Expected: %d, got: %d\n", width, name, tc.expected, got)
68 t.Errorf("width %d; %s: Expected: %d, got: %d\n", width, tc.name, tc.expected, got)
7069 }
7170 }
7271 }
+0
-49
internal/round.go less more
0 package internal
1
2 import "math"
3
4 const (
5 uvone = 0x3FF0000000000000
6 mask = 0x7FF
7 shift = 64 - 11 - 1
8 bias = 1023
9 signMask = 1 << 63
10 fracMask = 1<<shift - 1
11 )
12
13 // Round returns the nearest integer, rounding half away from zero.
14 //
15 // Special cases are:
16 // Round(±0) = ±0
17 // Round(±Inf) = ±Inf
18 // Round(NaN) = NaN
19 func Round(x float64) float64 {
20 // Round is a faster implementation of:
21 //
22 // func Round(x float64) float64 {
23 // t := Trunc(x)
24 // if Abs(x-t) >= 0.5 {
25 // return t + Copysign(1, x)
26 // }
27 // return t
28 // }
29 bits := math.Float64bits(x)
30 e := uint(bits>>shift) & mask
31 if e < bias {
32 // Round abs(x) < 1 including denormals.
33 bits &= signMask // +-0
34 if e == bias-1 {
35 bits |= uvone // +-1
36 }
37 } else if e < bias+shift {
38 // Round any abs(x) >= 1 containing a fractional component [0,1).
39 //
40 // Numbers with larger exponents are returned unchanged since they
41 // must be either an integer, infinity, or NaN.
42 const half = 1 << (shift - 1)
43 e -= bias
44 bits += half >> e
45 bits &^= fracMask >> e
46 }
47 return math.Float64frombits(bits)
48 }