Test CalcPercentage
Vladimir Bauer
8 years ago
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | func CalcPercentage(total, current, width int64) int64 { | |
| 207 | if total == 0 || current > total { | |
| 206 | func CalcPercentage(total, current, width int64) (perc int64) { | |
| 207 | if total <= 0 { | |
| 208 | 208 | return 0 |
| 209 | 209 | } |
| 210 | if current > total { | |
| 211 | current = total | |
| 212 | } | |
| 213 | ||
| 210 | 214 | num := float64(width) * float64(current) / float64(total) |
| 211 | 215 | ceil := math.Ceil(num) |
| 212 | 216 | diff := ceil - num |
| 0 | package decor | |
| 1 | ||
| 2 | import "testing" | |
| 3 | ||
| 4 | func TestCalcPersentage(t *testing.T) { | |
| 5 | // key is barWidth | |
| 6 | testSuite := map[int64]map[string]struct { | |
| 7 | total, current, expected int64 | |
| 8 | }{ | |
| 9 | 100: { | |
| 10 | "t,c,e{-1,-1,0}": {-1, -1, 0}, | |
| 11 | "t,c,e{0,-1,0}": {0, -1, 0}, | |
| 12 | "t,c,e{0,0,0}": {0, 0, 0}, | |
| 13 | "t,c,e{0,1,0}": {0, 1, 0}, | |
| 14 | "t,c,e{100,0,0}": {100, 0, 0}, | |
| 15 | "t,c,e{100,10,10}": {100, 10, 10}, | |
| 16 | "t,c,e{100,15,15}": {100, 15, 15}, | |
| 17 | "t,c,e{100,50,50}": {100, 50, 50}, | |
| 18 | "t,c,e{100,99,99}": {100, 99, 99}, | |
| 19 | "t,c,e{100,100,100}": {100, 100, 100}, | |
| 20 | "t,c,e{100,101,100}": {100, 101, 100}, | |
| 21 | "t,c,e{120,0,0}": {120, 0, 0}, | |
| 22 | "t,c,e{120,10,8}": {120, 10, 8}, | |
| 23 | "t,c,e{120,15,13}": {120, 15, 13}, | |
| 24 | "t,c,e{120,50,42}": {120, 50, 42}, | |
| 25 | "t,c,e{120,60,50}": {120, 60, 50}, | |
| 26 | "t,c,e{120,99,83}": {120, 99, 83}, | |
| 27 | "t,c,e{120,101,84}": {120, 101, 84}, | |
| 28 | "t,c,e{120,118,98}": {120, 118, 98}, | |
| 29 | "t,c,e{120,119,99}": {120, 119, 99}, | |
| 30 | "t,c,e{120,120,100}": {120, 120, 100}, | |
| 31 | "t,c,e{120,121,100}": {120, 121, 100}, | |
| 32 | }, | |
| 33 | 80: { | |
| 34 | "t,c,e{-1,-1,0}": {-1, -1, 0}, | |
| 35 | "t,c,e{0,-1,0}": {0, -1, 0}, | |
| 36 | "t,c,e{0,0,0}": {0, 0, 0}, | |
| 37 | "t,c,e{0,1,0}": {0, 1, 0}, | |
| 38 | "t,c,e{100,0,0}": {100, 0, 0}, | |
| 39 | "t,c,e{100,10,8}": {100, 10, 8}, | |
| 40 | "t,c,e{100,15,12}": {100, 15, 12}, | |
| 41 | "t,c,e{100,50,40}": {100, 50, 40}, | |
| 42 | "t,c,e{100,99,79}": {100, 99, 79}, | |
| 43 | "t,c,e{100,100,80}": {100, 100, 80}, | |
| 44 | "t,c,e{100,101,80}": {100, 101, 80}, | |
| 45 | "t,c,e{120,0,0}": {120, 0, 0}, | |
| 46 | "t,c,e{120,10,7}": {120, 10, 7}, | |
| 47 | "t,c,e{120,15,10}": {120, 15, 10}, | |
| 48 | "t,c,e{120,50,33}": {120, 50, 33}, | |
| 49 | "t,c,e{120,60,40}": {120, 60, 40}, | |
| 50 | "t,c,e{120,99,66}": {120, 99, 66}, | |
| 51 | "t,c,e{120,101,67}": {120, 101, 67}, | |
| 52 | "t,c,e{120,118,79}": {120, 118, 79}, | |
| 53 | "t,c,e{120,119,79}": {120, 119, 79}, | |
| 54 | "t,c,e{120,120,80}": {120, 120, 80}, | |
| 55 | "t,c,e{120,121,80}": {120, 121, 80}, | |
| 56 | }, | |
| 57 | } | |
| 58 | ||
| 59 | for width, cases := range testSuite { | |
| 60 | for name, tc := range cases { | |
| 61 | got := CalcPercentage(tc.total, tc.current, width) | |
| 62 | if got != tc.expected { | |
| 63 | t.Errorf("width %d; %s: Expected: %d, got: %d\n", width, name, tc.expected, got) | |
| 64 | } | |
| 65 | } | |
| 66 | } | |
| 67 | } |
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | func TestFillBar(t *testing.T) { |
| 8 | tests := []struct { | |
| 9 | termWidth int | |
| 10 | barWidth int | |
| 11 | total int64 | |
| 12 | current int64 | |
| 13 | barRefill *refill | |
| 14 | want string | |
| 8 | // key is termWidth | |
| 9 | testSuite := map[int]map[string]struct { | |
| 10 | total, current int64 | |
| 11 | barWidth int | |
| 12 | barRefill *refill | |
| 13 | want string | |
| 15 | 14 | }{ |
| 16 | { | |
| 17 | termWidth: 2, | |
| 18 | barWidth: 100, | |
| 19 | want: "[]", | |
| 20 | }, | |
| 21 | { | |
| 22 | termWidth: 3, | |
| 23 | barWidth: 100, | |
| 24 | total: 100, | |
| 25 | current: 20, | |
| 26 | want: "[-]", | |
| 27 | }, | |
| 28 | { | |
| 29 | termWidth: 5, | |
| 30 | barWidth: 100, | |
| 31 | total: 100, | |
| 32 | current: 20, | |
| 33 | want: "[>--]", | |
| 34 | }, | |
| 35 | { | |
| 36 | termWidth: 6, | |
| 37 | barWidth: 100, | |
| 38 | total: 100, | |
| 39 | current: 20, | |
| 40 | want: "[>---]", | |
| 41 | }, | |
| 42 | { | |
| 43 | termWidth: 20, | |
| 44 | barWidth: 100, | |
| 45 | total: 100, | |
| 46 | current: 20, | |
| 47 | want: "[===>--------------]", | |
| 48 | }, | |
| 49 | { | |
| 50 | termWidth: 50, | |
| 51 | barWidth: 100, | |
| 52 | total: 100, | |
| 53 | current: 20, | |
| 54 | want: "[=========>--------------------------------------]", | |
| 55 | }, | |
| 56 | { | |
| 57 | termWidth: 100, | |
| 58 | barWidth: 100, | |
| 59 | total: 100, | |
| 60 | current: 0, | |
| 61 | want: "[--------------------------------------------------------------------------------------------------]", | |
| 62 | }, | |
| 63 | { | |
| 64 | termWidth: 100, | |
| 65 | barWidth: 100, | |
| 66 | total: 100, | |
| 67 | current: 1, | |
| 68 | want: "[>-------------------------------------------------------------------------------------------------]", | |
| 69 | }, | |
| 70 | { | |
| 71 | termWidth: 100, | |
| 72 | barWidth: 100, | |
| 73 | total: 100, | |
| 74 | current: 40, | |
| 75 | want: "[======================================>-----------------------------------------------------------]", | |
| 76 | }, | |
| 77 | { | |
| 78 | termWidth: 100, | |
| 79 | barWidth: 100, | |
| 80 | total: 100, | |
| 81 | current: 40, | |
| 82 | barRefill: &refill{'+', 32}, | |
| 83 | want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]", | |
| 84 | }, | |
| 85 | { | |
| 86 | termWidth: 100, | |
| 87 | barWidth: 100, | |
| 88 | total: 100, | |
| 89 | current: 99, | |
| 90 | want: "[================================================================================================>-]", | |
| 91 | }, | |
| 92 | { | |
| 93 | termWidth: 100, | |
| 94 | barWidth: 100, | |
| 95 | total: 100, | |
| 96 | current: 100, | |
| 97 | want: "[==================================================================================================]", | |
| 15 | 100: { | |
| 16 | "t,c,bw{100,100,0}": { | |
| 17 | total: 100, | |
| 18 | current: 0, | |
| 19 | barWidth: 100, | |
| 20 | want: "[--------------------------------------------------------------------------------------------------]", | |
| 21 | }, | |
| 22 | "t,c,bw{100,1,100}": { | |
| 23 | total: 100, | |
| 24 | current: 1, | |
| 25 | barWidth: 100, | |
| 26 | want: "[>-------------------------------------------------------------------------------------------------]", | |
| 27 | }, | |
| 28 | "t,c,bw{100,40,100}": { | |
| 29 | total: 100, | |
| 30 | current: 40, | |
| 31 | barWidth: 100, | |
| 32 | want: "[======================================>-----------------------------------------------------------]", | |
| 33 | }, | |
| 34 | "t,c,bw{100,40,100}refill{'+', 32}": { | |
| 35 | total: 100, | |
| 36 | current: 40, | |
| 37 | barWidth: 100, | |
| 38 | barRefill: &refill{'+', 32}, | |
| 39 | want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]", | |
| 40 | }, | |
| 41 | "t,c,bw{100,99,100}": { | |
| 42 | total: 100, | |
| 43 | current: 99, | |
| 44 | barWidth: 100, | |
| 45 | want: "[================================================================================================>-]", | |
| 46 | }, | |
| 47 | "t,c,bw{100,100,100}": { | |
| 48 | total: 100, | |
| 49 | current: 100, | |
| 50 | barWidth: 100, | |
| 51 | want: "[==================================================================================================]", | |
| 52 | }, | |
| 53 | }, | |
| 54 | 2: { | |
| 55 | "t,c,bw{0,0,100}": { | |
| 56 | barWidth: 100, | |
| 57 | want: "[]", | |
| 58 | }, | |
| 59 | "t,c,bw{60,20,80}": { | |
| 60 | total: 60, | |
| 61 | current: 20, | |
| 62 | barWidth: 80, | |
| 63 | want: "[]", | |
| 64 | }, | |
| 65 | }, | |
| 66 | 3: { | |
| 67 | "t,c,bw{100,20,100}": { | |
| 68 | total: 100, | |
| 69 | current: 20, | |
| 70 | barWidth: 100, | |
| 71 | want: "[-]", | |
| 72 | }, | |
| 73 | "t,c,bw{100,98,100}": { | |
| 74 | total: 100, | |
| 75 | current: 98, | |
| 76 | barWidth: 100, | |
| 77 | want: "[=]", | |
| 78 | }, | |
| 79 | "t,c,bw{100,100,100}": { | |
| 80 | total: 100, | |
| 81 | current: 100, | |
| 82 | barWidth: 100, | |
| 83 | want: "[=]", | |
| 84 | }, | |
| 85 | }, | |
| 86 | 5: { | |
| 87 | "t,c,bw{100,20,100}": { | |
| 88 | total: 100, | |
| 89 | current: 20, | |
| 90 | barWidth: 100, | |
| 91 | want: "[>--]", | |
| 92 | }, | |
| 93 | "t,c,bw{100,98,100}": { | |
| 94 | total: 100, | |
| 95 | current: 98, | |
| 96 | barWidth: 100, | |
| 97 | want: "[===]", | |
| 98 | }, | |
| 99 | "t,c,bw{100,100,100}": { | |
| 100 | total: 100, | |
| 101 | current: 100, | |
| 102 | barWidth: 100, | |
| 103 | want: "[===]", | |
| 104 | }, | |
| 105 | }, | |
| 106 | 6: { | |
| 107 | "t,c,bw{100,20,100}": { | |
| 108 | total: 100, | |
| 109 | current: 20, | |
| 110 | barWidth: 100, | |
| 111 | want: "[>---]", | |
| 112 | }, | |
| 113 | "t,c,bw{100,98,100}": { | |
| 114 | total: 100, | |
| 115 | current: 98, | |
| 116 | barWidth: 100, | |
| 117 | want: "[====]", | |
| 118 | }, | |
| 119 | "t,c,bw{100,100,100}": { | |
| 120 | total: 100, | |
| 121 | current: 100, | |
| 122 | barWidth: 100, | |
| 123 | want: "[====]", | |
| 124 | }, | |
| 125 | }, | |
| 126 | 20: { | |
| 127 | "t,c,bw{100,20,100}": { | |
| 128 | total: 100, | |
| 129 | current: 20, | |
| 130 | barWidth: 100, | |
| 131 | want: "[===>--------------]", | |
| 132 | }, | |
| 133 | "t,c,bw{100,60,100}": { | |
| 134 | total: 100, | |
| 135 | current: 60, | |
| 136 | barWidth: 100, | |
| 137 | want: "[==========>-------]", | |
| 138 | }, | |
| 139 | "t,c,bw{100,98,100}": { | |
| 140 | total: 100, | |
| 141 | current: 98, | |
| 142 | barWidth: 100, | |
| 143 | want: "[==================]", | |
| 144 | }, | |
| 145 | "t,c,bw{100,100,100}": { | |
| 146 | total: 100, | |
| 147 | current: 100, | |
| 148 | barWidth: 100, | |
| 149 | want: "[==================]", | |
| 150 | }, | |
| 151 | }, | |
| 152 | 50: { | |
| 153 | "t,c,bw{100,20,100}": { | |
| 154 | total: 100, | |
| 155 | current: 20, | |
| 156 | barWidth: 100, | |
| 157 | want: "[=========>--------------------------------------]", | |
| 158 | }, | |
| 159 | "t,c,bw{100,60,100}": { | |
| 160 | total: 100, | |
| 161 | current: 60, | |
| 162 | barWidth: 100, | |
| 163 | want: "[============================>-------------------]", | |
| 164 | }, | |
| 165 | "t,c,bw{100,98,100}": { | |
| 166 | total: 100, | |
| 167 | current: 98, | |
| 168 | barWidth: 100, | |
| 169 | want: "[==============================================>-]", | |
| 170 | }, | |
| 171 | "t,c,bw{100,100,100}": { | |
| 172 | total: 100, | |
| 173 | current: 100, | |
| 174 | barWidth: 100, | |
| 175 | want: "[================================================]", | |
| 176 | }, | |
| 98 | 177 | }, |
| 99 | 178 | } |
| 100 | 179 | |
| 101 | 180 | prependWs := newWidthSync(nil, 1, 0) |
| 102 | 181 | appendWs := newWidthSync(nil, 1, 0) |
| 103 | for _, test := range tests { | |
| 104 | s := newTestState() | |
| 105 | s.width = test.barWidth | |
| 106 | s.total = test.total | |
| 107 | s.current = test.current | |
| 108 | if test.barRefill != nil { | |
| 109 | s.refill = test.barRefill | |
| 110 | } | |
| 111 | s.draw(test.termWidth, prependWs, appendWs) | |
| 112 | got := s.bufB.String() | |
| 113 | if got != test.want { | |
| 114 | t.Errorf("Want: %q, Got: %q\n", test.want, got) | |
| 182 | for termWidth, cases := range testSuite { | |
| 183 | for name, tc := range cases { | |
| 184 | s := newTestState() | |
| 185 | s.width = tc.barWidth | |
| 186 | s.total = tc.total | |
| 187 | s.current = tc.current | |
| 188 | if tc.barRefill != nil { | |
| 189 | s.refill = tc.barRefill | |
| 190 | } | |
| 191 | s.draw(termWidth, prependWs, appendWs) | |
| 192 | got := s.bufB.String() | |
| 193 | if got != tc.want { | |
| 194 | t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, tc.want, len(tc.want), got, len(got)) | |
| 195 | } | |
| 115 | 196 | } |
| 116 | 197 | } |
| 117 | 198 | } |