diff --git a/decor/eta.go b/decor/eta.go index 44a1f03..b4ab6be 100644 --- a/decor/eta.go +++ b/decor/eta.go @@ -6,7 +6,6 @@ "time" "github.com/VividCortex/ewma" - "github.com/vbauerster/mpb/internal" ) type TimeNormalizer func(time.Duration) time.Duration @@ -59,7 +58,7 @@ return d.FormatMsg(*d.completeMsg) } - v := internal.Round(d.average.Value()) + v := math.Round(d.average.Value()) remaining := d.normalizer(time.Duration((st.Total - st.Current) * int64(v))) hours := int64((remaining / time.Hour) % 60) minutes := int64((remaining / time.Minute) % 60) @@ -133,7 +132,7 @@ var str string timeElapsed := time.Since(d.startTime) - v := internal.Round(float64(timeElapsed) / float64(st.Current)) + v := math.Round(float64(timeElapsed) / float64(st.Current)) if math.IsInf(v, 0) || math.IsNaN(v) { v = 0 } diff --git a/internal/percentage.go b/internal/percentage.go index 3c8defb..0483d25 100644 --- a/internal/percentage.go +++ b/internal/percentage.go @@ -1,4 +1,6 @@ package internal + +import "math" // Percentage is a helper function, to calculate percentage. func Percentage(total, current, width int64) int64 { @@ -6,5 +8,5 @@ return 0 } p := float64(width*current) / float64(total) - return int64(Round(p)) + return int64(math.Round(p)) } diff --git a/internal/percentage_test.go b/internal/percentage_test.go index ce5a25e..4a649ce 100644 --- a/internal/percentage_test.go +++ b/internal/percentage_test.go @@ -1,73 +1,72 @@ package internal -import ( - "testing" -) +import "testing" func TestPercentage(t *testing.T) { // key is barWidth - testSuite := map[int64]map[string]struct { + testSuite := map[int64][]struct { + name string total, current, expected int64 }{ 100: { - "t,c,e{-1,-1,0}": {-1, -1, 0}, - "t,c,e{0,-1,0}": {0, -1, 0}, - "t,c,e{0,0,0}": {0, 0, 0}, - "t,c,e{0,1,0}": {0, 1, 0}, - "t,c,e{100,0,0}": {100, 0, 0}, - "t,c,e{100,10,10}": {100, 10, 10}, - "t,c,e{100,15,15}": {100, 15, 15}, - "t,c,e{100,50,50}": {100, 50, 50}, - "t,c,e{100,99,99}": {100, 99, 99}, - "t,c,e{100,100,100}": {100, 100, 100}, - "t,c,e{100,101,101}": {100, 101, 101}, - "t,c,e{100,102,101}": {100, 102, 102}, - "t,c,e{120,0,0}": {120, 0, 0}, - "t,c,e{120,10,8}": {120, 10, 8}, - "t,c,e{120,15,13}": {120, 15, 13}, - "t,c,e{120,50,42}": {120, 50, 42}, - "t,c,e{120,60,50}": {120, 60, 50}, - "t,c,e{120,99,83}": {120, 99, 83}, - "t,c,e{120,101,84}": {120, 101, 84}, - "t,c,e{120,118,98}": {120, 118, 98}, - "t,c,e{120,119,99}": {120, 119, 99}, - "t,c,e{120,120,100}": {120, 120, 100}, - "t,c,e{120,121,101}": {120, 121, 101}, - "t,c,e{120,122,101}": {120, 122, 102}, + {"t,c,e{-1,-1,0}", -1, -1, 0}, + {"t,c,e{0,-1,0}", 0, -1, 0}, + {"t,c,e{0,0,0}", 0, 0, 0}, + {"t,c,e{0,1,0}", 0, 1, 0}, + {"t,c,e{100,0,0}", 100, 0, 0}, + {"t,c,e{100,10,10}", 100, 10, 10}, + {"t,c,e{100,15,15}", 100, 15, 15}, + {"t,c,e{100,50,50}", 100, 50, 50}, + {"t,c,e{100,99,99}", 100, 99, 99}, + {"t,c,e{100,100,100}", 100, 100, 100}, + {"t,c,e{100,101,101}", 100, 101, 101}, + {"t,c,e{100,102,101}", 100, 102, 102}, + {"t,c,e{120,0,0}", 120, 0, 0}, + {"t,c,e{120,10,8}", 120, 10, 8}, + {"t,c,e{120,15,13}", 120, 15, 13}, + {"t,c,e{120,50,42}", 120, 50, 42}, + {"t,c,e{120,60,50}", 120, 60, 50}, + {"t,c,e{120,99,83}", 120, 99, 83}, + {"t,c,e{120,101,84}", 120, 101, 84}, + {"t,c,e{120,118,98}", 120, 118, 98}, + {"t,c,e{120,119,99}", 120, 119, 99}, + {"t,c,e{120,120,100}", 120, 120, 100}, + {"t,c,e{120,121,101}", 120, 121, 101}, + {"t,c,e{120,122,101}", 120, 122, 102}, }, 80: { - "t,c,e{-1,-1,0}": {-1, -1, 0}, - "t,c,e{0,-1,0}": {0, -1, 0}, - "t,c,e{0,0,0}": {0, 0, 0}, - "t,c,e{0,1,0}": {0, 1, 0}, - "t,c,e{100,0,0}": {100, 0, 0}, - "t,c,e{100,10,8}": {100, 10, 8}, - "t,c,e{100,15,12}": {100, 15, 12}, - "t,c,e{100,50,40}": {100, 50, 40}, - "t,c,e{100,99,79}": {100, 99, 79}, - "t,c,e{100,100,80}": {100, 100, 80}, - "t,c,e{100,101,81}": {100, 101, 81}, - "t,c,e{100,102,82}": {100, 102, 82}, - "t,c,e{120,0,0}": {120, 0, 0}, - "t,c,e{120,10,7}": {120, 10, 7}, - "t,c,e{120,15,10}": {120, 15, 10}, - "t,c,e{120,50,33}": {120, 50, 33}, - "t,c,e{120,60,40}": {120, 60, 40}, - "t,c,e{120,99,66}": {120, 99, 66}, - "t,c,e{120,101,67}": {120, 101, 67}, - "t,c,e{120,118,79}": {120, 118, 79}, - "t,c,e{120,119,79}": {120, 119, 79}, - "t,c,e{120,120,80}": {120, 120, 80}, - "t,c,e{120,121,81}": {120, 121, 81}, - "t,c,e{120,122,81}": {120, 122, 81}, + {"t,c,e{-1,-1,0}", -1, -1, 0}, + {"t,c,e{0,-1,0}", 0, -1, 0}, + {"t,c,e{0,0,0}", 0, 0, 0}, + {"t,c,e{0,1,0}", 0, 1, 0}, + {"t,c,e{100,0,0}", 100, 0, 0}, + {"t,c,e{100,10,8}", 100, 10, 8}, + {"t,c,e{100,15,12}", 100, 15, 12}, + {"t,c,e{100,50,40}", 100, 50, 40}, + {"t,c,e{100,99,79}", 100, 99, 79}, + {"t,c,e{100,100,80}", 100, 100, 80}, + {"t,c,e{100,101,81}", 100, 101, 81}, + {"t,c,e{100,102,82}", 100, 102, 82}, + {"t,c,e{120,0,0}", 120, 0, 0}, + {"t,c,e{120,10,7}", 120, 10, 7}, + {"t,c,e{120,15,10}", 120, 15, 10}, + {"t,c,e{120,50,33}", 120, 50, 33}, + {"t,c,e{120,60,40}", 120, 60, 40}, + {"t,c,e{120,99,66}", 120, 99, 66}, + {"t,c,e{120,101,67}", 120, 101, 67}, + {"t,c,e{120,118,79}", 120, 118, 79}, + {"t,c,e{120,119,79}", 120, 119, 79}, + {"t,c,e{120,120,80}", 120, 120, 80}, + {"t,c,e{120,121,81}", 120, 121, 81}, + {"t,c,e{120,122,81}", 120, 122, 81}, }, } for width, cases := range testSuite { - for name, tc := range cases { + for _, tc := range cases { got := Percentage(tc.total, tc.current, width) if got != tc.expected { - t.Errorf("width %d; %s: Expected: %d, got: %d\n", width, name, tc.expected, got) + t.Errorf("width %d; %s: Expected: %d, got: %d\n", width, tc.name, tc.expected, got) } } } diff --git a/internal/round.go b/internal/round.go deleted file mode 100644 index c54a789..0000000 --- a/internal/round.go +++ /dev/null @@ -1,49 +0,0 @@ -package internal - -import "math" - -const ( - uvone = 0x3FF0000000000000 - mask = 0x7FF - shift = 64 - 11 - 1 - bias = 1023 - signMask = 1 << 63 - fracMask = 1<= 0.5 { - // return t + Copysign(1, x) - // } - // return t - // } - bits := math.Float64bits(x) - e := uint(bits>>shift) & mask - if e < bias { - // Round abs(x) < 1 including denormals. - bits &= signMask // +-0 - if e == bias-1 { - bits |= uvone // +-1 - } - } else if e < bias+shift { - // Round any abs(x) >= 1 containing a fractional component [0,1). - // - // Numbers with larger exponents are returned unchanged since they - // must be either an integer, infinity, or NaN. - const half = 1 << (shift - 1) - e -= bias - bits += half >> e - bits &^= fracMask >> e - } - return math.Float64frombits(bits) -}