github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/args/fmt_duration_test.go (about) 1 // Copyright 2020 Insolar Network Ltd. 2 // All rights reserved. 3 // This material is licensed under the Insolar License version 1.0, 4 // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md. 5 6 package args 7 8 import ( 9 "math/big" 10 "testing" 11 "time" 12 ) 13 14 func TestBitCountToMaxDecimalCount(t *testing.T) { 15 bitStart := 1 16 v := big.NewInt(1) 17 v.Lsh(v, uint(bitStart-1)) 18 19 for bitCount := bitStart; bitCount < 10000; bitCount++ { 20 decimal := v.String() 21 want := len(decimal) 22 if got := BitCountToMaxDecimalCount(bitCount); got != want { 23 t.Errorf("BitCountToMaxDecimalCount() = %v, want %v", got, want) 24 } 25 v.Lsh(v, 1) 26 } 27 } 28 29 func TestDurationFixedLen(t *testing.T) { 30 type args struct { 31 d time.Duration 32 fixedLen int 33 } 34 tests := []struct { 35 name string 36 args args 37 }{ 38 {"0µs", args{0, 0}}, 39 {"0.00µs", args{0, 7}}, 40 {"0.50ms", args{500*time.Microsecond + 1, 6}}, 41 {"1.00ms", args{1 * time.Millisecond, 6}}, 42 {"1.000s", args{1 * time.Second, 6}}, 43 {"60.00s", args{1 * time.Minute, 6}}, 44 {"60m00s", args{1 * time.Hour, 6}}, 45 } 46 for _, tt := range tests { 47 t.Run(tt.name, func(t *testing.T) { 48 if got := DurationFixedLen(tt.args.d, tt.args.fixedLen); got != tt.name { 49 t.Errorf("DurationFixedLen() = %v, want %v", got, tt.name) 50 } 51 }) 52 } 53 } 54 55 func Test_fmtAboveSeconds(t *testing.T) { 56 type args struct { 57 d time.Duration 58 fixedLen int 59 } 60 tests := []struct { 61 name string 62 args args 63 }{ 64 {"0s", args{0, 0}}, 65 {"1s", args{1 * time.Second, 0}}, 66 {"0m", args{10 * time.Second, 0}}, 67 {"10s", args{10 * time.Second, 3}}, 68 {"2m", args{100 * time.Second, 0}}, 69 {"2m", args{100 * time.Second, 4}}, 70 {"1m40s", args{100 * time.Second, 5}}, 71 {"1m40s", args{100 * time.Second, 100}}, 72 73 {"17m", args{1000 * time.Second, 0}}, 74 {"16m40s", args{1000 * time.Second, 6}}, 75 {"16m40s", args{1000 * time.Second, 100}}, 76 {"167m", args{10000 * time.Second, 0}}, 77 {"166m40s", args{10000 * time.Second, 7}}, 78 {"166m40s", args{10000 * time.Second, 100}}, 79 {"28h", args{100000 * time.Second, 0}}, 80 {"27h46m", args{100000 * time.Second, 6}}, 81 {"27h46m", args{100000 * time.Second, 100}}, 82 {"278h", args{1000000 * time.Second, 0}}, 83 {"277h46m", args{1000000 * time.Second, 7}}, 84 } 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 if got := fmtAboveSeconds(tt.args.d, tt.args.fixedLen); got != tt.name { 88 t.Errorf("fmtAboveSeconds() = %v, want %v", got, tt.name) 89 } 90 }) 91 } 92 } 93 94 // 95 func Test_fmtPortions(t *testing.T) { 96 type args struct { 97 valueLo uint64 98 valueHi uint64 99 fixedLen int 100 } 101 tests := []struct { 102 name string 103 args args 104 }{ 105 {"0min", args{0, 0, 0}}, 106 {"0min", args{0, 0, 100}}, 107 {"1hr", args{31, 0, 0}}, 108 {"2hr", args{31, 1, 0}}, 109 {"3hr", args{31, 2, 1}}, 110 {"8hr", args{31, 7, 7}}, 111 {"8hr59min", args{59, 8, 8}}, 112 {"9hr59min", args{59, 9, 9}}, 113 {"80hr", args{29, 80, 8}}, 114 {"90hr29min", args{29, 90, 9}}, 115 {"100hr", args{29, 100, 9}}, 116 {"100hr29min", args{29, 100, 100}}, 117 } 118 for _, tt := range tests { 119 t.Run(tt.name, func(t *testing.T) { 120 if got := fmtPortions(tt.args.valueLo, "min", tt.args.valueHi, "hr", tt.args.fixedLen); got != tt.name { 121 t.Errorf("fmtPortions() = %v, want %v", got, tt.name) 122 } 123 }) 124 } 125 } 126 127 type args struct { 128 d time.Duration 129 } 130 131 var metricTests = []struct { 132 name string 133 args args 134 want time.Duration 135 want1, want2, want3 string 136 }{ 137 /* 138 NB! "µs" require 3 bytes, not 2 bytes, hence the difference with "ms" on formatting 139 */ 140 {"0.000µs", args{0}, time.Microsecond, "µs", "0µs", "0.0µs"}, 141 {"0.001µs", args{1 * time.Nanosecond}, time.Microsecond, "µs", "0µs", "0.0µs"}, 142 {"0.999µs", args{1*time.Microsecond - 1}, time.Microsecond, "µs", "1µs", "0.9µs"}, 143 {"1.000µs", args{1 * time.Microsecond}, time.Microsecond, "µs", "1µs", "1.0µs"}, 144 {"99.00µs", args{99 * time.Microsecond}, time.Microsecond, "µs", "0ms", "99µs"}, 145 {"99.87µs", args{99876 * time.Nanosecond}, time.Microsecond, "µs", "0ms", "100µs"}, 146 {"499.9µs", args{500*time.Microsecond - 1}, time.Microsecond, "µs", "0ms", "500µs"}, 147 {"500.0µs", args{500 * time.Microsecond}, time.Microsecond, "µs", "0ms", "500µs"}, 148 {"0.5000ms", args{500*time.Microsecond + 1}, time.Millisecond, "ms", "1ms", "0.5ms"}, 149 {"1.0000ms", args{1 * time.Millisecond}, time.Millisecond, "ms", "1ms", "1.0ms"}, 150 {"99.000ms", args{99 * time.Millisecond}, time.Millisecond, "ms", "0s", "99ms"}, 151 {"99.876ms", args{99876 * time.Microsecond}, time.Millisecond, "ms", "0s", "100ms"}, 152 {"499.99ms", args{500*time.Millisecond - time.Microsecond}, time.Millisecond, "ms", "0s", "500ms"}, 153 {"499.99ms", args{500*time.Millisecond - 1}, time.Millisecond, "ms", "0s", "500ms"}, 154 {"500.00ms", args{500 * time.Millisecond}, time.Millisecond, "ms", "0s", "500ms"}, 155 {"0.50000s", args{500*time.Millisecond + 1}, time.Second, "s", "1s", "0.50s"}, 156 {"1.00000s", args{1 * time.Second}, time.Second, "s", "1s", "1.00s"}, 157 {"30.0000s", args{30 * time.Second}, time.Second, "s", "0m", "30.0s"}, 158 {"59.0000s", args{59 * time.Second}, time.Second, "s", "1m", "59.0s"}, 159 {"300.000s", args{300 * time.Second}, time.Second, "s", "5m", "300s"}, 160 {"599.000s", args{10*time.Minute - time.Second}, time.Second, "s", "10m", "599s"}, 161 {"599.999s", args{10*time.Minute - 1}, time.Second, "s", "10m", "600s"}, 162 {"10m00s", args{10 * time.Minute}, time.Minute, "m", "", ""}, 163 {"10000m", args{10 * time.Minute}, time.Minute, "m", "", ""}, 164 } 165 166 func Test_metricDurationBase(t *testing.T) { 167 for _, tt := range metricTests { 168 t.Run(tt.name, func(t *testing.T) { 169 got, got1 := metricDurationBase(tt.args.d) 170 if got != tt.want { 171 t.Errorf("metricDurationBase() got = %v, want %v", got, tt.want) 172 } 173 if got1 != tt.want1 { 174 t.Errorf("metricDurationBase() got1 = %v, want %v", got1, tt.want1) 175 } 176 }) 177 } 178 } 179 180 func Test_fmtMetric(t *testing.T) { 181 for _, tt := range metricTests { 182 if tt.want == time.Minute { 183 continue 184 } 185 t.Run(tt.name, func(t *testing.T) { 186 if got3 := fmtMetric(tt.args.d, tt.want, tt.want1, 8); got3 != tt.name { 187 t.Errorf("fmtMetric() = %v, want %v", got3, tt.name) 188 } 189 }) 190 } 191 } 192 193 func Test_fmtMetricTooShort(t *testing.T) { 194 for _, tt := range metricTests { 195 if tt.want == time.Minute { 196 continue 197 } 198 t.Run(tt.name, func(t *testing.T) { 199 if got3 := fmtMetric(tt.args.d, tt.want, tt.want1, 2); got3 != tt.want2 { 200 t.Errorf("fmtMetric() = %v, want %v", got3, tt.want2) 201 } 202 }) 203 } 204 } 205 206 func Test_fmtMetricTooShort2(t *testing.T) { 207 for _, tt := range metricTests { 208 if tt.want == time.Minute { 209 continue 210 } 211 t.Run(tt.name, func(t *testing.T) { 212 expectedLen := 5 213 if tt.want == time.Microsecond { 214 /* 215 NB! "µs" require 3 bytes, not 2 bytes, hence the difference with "ms" on formatting 216 */ 217 expectedLen++ 218 } 219 if got3 := fmtMetric(tt.args.d, tt.want, tt.want1, expectedLen); got3 != tt.want3 { 220 t.Errorf("fmtMetric() = %v, want %v", got3, tt.want3) 221 } 222 }) 223 } 224 }