1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
package humanize_test
import (
"math"
"testing"
"time"
"github.com/git-lfs/git-lfs/v3/tools/humanize"
"github.com/stretchr/testify/assert"
)
type ParseBytesTestCase struct {
Given string
Expected uint64
Err error
}
func (c *ParseBytesTestCase) Assert(t *testing.T) {
got, err := humanize.ParseBytes(c.Given)
if c.Err == nil {
assert.NoError(t, err, "unexpected error: %s", err)
assert.EqualValues(t, c.Expected, got)
} else {
assert.Equal(t, c.Err, err)
}
}
type FormatBytesTestCase struct {
Given uint64
Expected string
}
func (c *FormatBytesTestCase) Assert(t *testing.T) {
assert.Equal(t, c.Expected, humanize.FormatBytes(c.Given))
}
type ParseByteUnitTestCase struct {
Given string
Expected uint64
Err string
}
func (c *ParseByteUnitTestCase) Assert(t *testing.T) {
got, err := humanize.ParseByteUnit(c.Given)
if len(c.Err) == 0 {
assert.NoError(t, err, "unexpected error: %s", err)
assert.EqualValues(t, c.Expected, got)
} else {
assert.EqualError(t, err, c.Err)
}
}
type FormatBytesUnitTestCase struct {
Given uint64
Unit uint64
Expected string
}
func (c *FormatBytesUnitTestCase) Assert(t *testing.T) {
assert.Equal(t, c.Expected, humanize.FormatBytesUnit(c.Given, c.Unit))
}
type FormatByteRateTestCase struct {
Given uint64
Over time.Duration
Expected string
}
func (c *FormatByteRateTestCase) Assert(t *testing.T) {
assert.Equal(t, c.Expected, humanize.FormatByteRate(c.Given, c.Over))
}
func TestParseBytes(t *testing.T) {
for desc, c := range map[string]*ParseBytesTestCase{
"parse byte (zero, empty)": {"", uint64(0), nil},
"parse byte (empty)": {"10", uint64(10 * math.Pow(2, 0)), nil},
"parse byte": {"10B", uint64(10 * math.Pow(2, 0)), nil},
"parse kibibyte": {"20KIB", uint64(20 * math.Pow(2, 10)), nil},
"parse mebibyte": {"30MIB", uint64(30 * math.Pow(2, 20)), nil},
"parse gibibyte": {"40GIB", uint64(40 * math.Pow(2, 30)), nil},
"parse tebibyte": {"50TIB", uint64(50 * math.Pow(2, 40)), nil},
"parse pebibyte": {"60PIB", uint64(60 * math.Pow(2, 50)), nil},
"parse byte (lowercase)": {"10b", uint64(10 * math.Pow(2, 0)), nil},
"parse kibibyte (lowercase)": {"20kib", uint64(20 * math.Pow(2, 10)), nil},
"parse mebibyte (lowercase)": {"30mib", uint64(30 * math.Pow(2, 20)), nil},
"parse gibibyte (lowercase)": {"40gib", uint64(40 * math.Pow(2, 30)), nil},
"parse tebibyte (lowercase)": {"50tib", uint64(50 * math.Pow(2, 40)), nil},
"parse pebibyte (lowercase)": {"60pib", uint64(60 * math.Pow(2, 50)), nil},
"parse byte (with space)": {"10 B", uint64(10 * math.Pow(2, 0)), nil},
"parse kibibyte (with space)": {"20 KIB", uint64(20 * math.Pow(2, 10)), nil},
"parse mebibyte (with space)": {"30 MIB", uint64(30 * math.Pow(2, 20)), nil},
"parse gibibyte (with space)": {"40 GIB", uint64(40 * math.Pow(2, 30)), nil},
"parse tebibyte (with space)": {"50 TIB", uint64(50 * math.Pow(2, 40)), nil},
"parse pebibyte (with space)": {"60 PIB", uint64(60 * math.Pow(2, 50)), nil},
"parse byte (with space, lowercase)": {"10 b", uint64(10 * math.Pow(2, 0)), nil},
"parse kibibyte (with space, lowercase)": {"20 kib", uint64(20 * math.Pow(2, 10)), nil},
"parse mebibyte (with space, lowercase)": {"30 mib", uint64(30 * math.Pow(2, 20)), nil},
"parse gibibyte (with space, lowercase)": {"40 gib", uint64(40 * math.Pow(2, 30)), nil},
"parse tebibyte (with space, lowercase)": {"50 tib", uint64(50 * math.Pow(2, 40)), nil},
"parse pebibyte (with space, lowercase)": {"60 pib", uint64(60 * math.Pow(2, 50)), nil},
"parse kilobyte": {"20KB", uint64(20 * math.Pow(10, 3)), nil},
"parse megabyte": {"30MB", uint64(30 * math.Pow(10, 6)), nil},
"parse gigabyte": {"40GB", uint64(40 * math.Pow(10, 9)), nil},
"parse terabyte": {"50TB", uint64(50 * math.Pow(10, 12)), nil},
"parse petabyte": {"60PB", uint64(60 * math.Pow(10, 15)), nil},
"parse kilobyte (lowercase)": {"20kb", uint64(20 * math.Pow(10, 3)), nil},
"parse megabyte (lowercase)": {"30mb", uint64(30 * math.Pow(10, 6)), nil},
"parse gigabyte (lowercase)": {"40gb", uint64(40 * math.Pow(10, 9)), nil},
"parse terabyte (lowercase)": {"50tb", uint64(50 * math.Pow(10, 12)), nil},
"parse petabyte (lowercase)": {"60pb", uint64(60 * math.Pow(10, 15)), nil},
"parse kilobyte (with space)": {"20 KB", uint64(20 * math.Pow(10, 3)), nil},
"parse megabyte (with space)": {"30 MB", uint64(30 * math.Pow(10, 6)), nil},
"parse gigabyte (with space)": {"40 GB", uint64(40 * math.Pow(10, 9)), nil},
"parse terabyte (with space)": {"50 TB", uint64(50 * math.Pow(10, 12)), nil},
"parse petabyte (with space)": {"60 PB", uint64(60 * math.Pow(10, 15)), nil},
"parse kilobyte (with space, lowercase)": {"20 kb", uint64(20 * math.Pow(10, 3)), nil},
"parse megabyte (with space, lowercase)": {"30 mb", uint64(30 * math.Pow(10, 6)), nil},
"parse gigabyte (with space, lowercase)": {"40 gb", uint64(40 * math.Pow(10, 9)), nil},
"parse terabyte (with space, lowercase)": {"50 tb", uint64(50 * math.Pow(10, 12)), nil},
"parse petabyte (with space, lowercase)": {"60 pb", uint64(60 * math.Pow(10, 15)), nil},
} {
t.Run(desc, c.Assert)
}
}
func TestFormatBytes(t *testing.T) {
for desc, c := range map[string]*FormatBytesTestCase{
"format bytes": {uint64(1 * math.Pow(10, 0)), "1 B"},
"format kilobytes": {uint64(1 * math.Pow(10, 3)), "1.0 KB"},
"format megabytes": {uint64(1 * math.Pow(10, 6)), "1.0 MB"},
"format gigabytes": {uint64(1 * math.Pow(10, 9)), "1.0 GB"},
"format petabytes": {uint64(1 * math.Pow(10, 12)), "1.0 TB"},
"format terabytes": {uint64(1 * math.Pow(10, 15)), "1.0 PB"},
"format kilobytes under": {uint64(1.49 * math.Pow(10, 3)), "1.5 KB"},
"format megabytes under": {uint64(1.49 * math.Pow(10, 6)), "1.5 MB"},
"format gigabytes under": {uint64(1.49 * math.Pow(10, 9)), "1.5 GB"},
"format petabytes under": {uint64(1.49 * math.Pow(10, 12)), "1.5 TB"},
"format terabytes under": {uint64(1.49 * math.Pow(10, 15)), "1.5 PB"},
"format kilobytes over": {uint64(1.51 * math.Pow(10, 3)), "1.5 KB"},
"format megabytes over": {uint64(1.51 * math.Pow(10, 6)), "1.5 MB"},
"format gigabytes over": {uint64(1.51 * math.Pow(10, 9)), "1.5 GB"},
"format petabytes over": {uint64(1.51 * math.Pow(10, 12)), "1.5 TB"},
"format terabytes over": {uint64(1.51 * math.Pow(10, 15)), "1.5 PB"},
"format kilobytes exact": {uint64(1.3 * math.Pow(10, 3)), "1.3 KB"},
"format megabytes exact": {uint64(1.3 * math.Pow(10, 6)), "1.3 MB"},
"format gigabytes exact": {uint64(1.3 * math.Pow(10, 9)), "1.3 GB"},
"format petabytes exact": {uint64(1.3 * math.Pow(10, 12)), "1.3 TB"},
"format terabytes exact": {uint64(1.3 * math.Pow(10, 15)), "1.3 PB"},
} {
t.Run(desc, c.Assert)
}
}
func TestParseByteUnit(t *testing.T) {
for desc, c := range map[string]*ParseByteUnitTestCase{
"parse byte": {"B", uint64(math.Pow(2, 0)), ""},
"parse kibibyte": {"KIB", uint64(math.Pow(2, 10)), ""},
"parse mebibyte": {"MIB", uint64(math.Pow(2, 20)), ""},
"parse gibibyte": {"GIB", uint64(math.Pow(2, 30)), ""},
"parse tebibyte": {"TIB", uint64(math.Pow(2, 40)), ""},
"parse pebibyte": {"PIB", uint64(math.Pow(2, 50)), ""},
"parse byte (lowercase)": {"b", uint64(math.Pow(2, 0)), ""},
"parse kibibyte (lowercase)": {"kib", uint64(math.Pow(2, 10)), ""},
"parse mebibyte (lowercase)": {"mib", uint64(math.Pow(2, 20)), ""},
"parse gibibyte (lowercase)": {"gib", uint64(math.Pow(2, 30)), ""},
"parse tebibyte (lowercase)": {"tib", uint64(math.Pow(2, 40)), ""},
"parse pebibyte (lowercase)": {"pib", uint64(math.Pow(2, 50)), ""},
"parse byte (with space)": {" B", uint64(math.Pow(2, 0)), ""},
"parse kibibyte (with space)": {" KIB", uint64(math.Pow(2, 10)), ""},
"parse mebibyte (with space)": {" MIB", uint64(math.Pow(2, 20)), ""},
"parse gibibyte (with space)": {" GIB", uint64(math.Pow(2, 30)), ""},
"parse tebibyte (with space)": {" TIB", uint64(math.Pow(2, 40)), ""},
"parse pebibyte (with space)": {" PIB", uint64(math.Pow(2, 50)), ""},
"parse byte (with space, lowercase)": {" b", uint64(math.Pow(2, 0)), ""},
"parse kibibyte (with space, lowercase)": {" kib", uint64(math.Pow(2, 10)), ""},
"parse mebibyte (with space, lowercase)": {" mib", uint64(math.Pow(2, 20)), ""},
"parse gibibyte (with space, lowercase)": {" gib", uint64(math.Pow(2, 30)), ""},
"parse tebibyte (with space, lowercase)": {" tib", uint64(math.Pow(2, 40)), ""},
"parse pebibyte (with space, lowercase)": {" pib", uint64(math.Pow(2, 50)), ""},
"parse kilobyte": {"KB", uint64(math.Pow(10, 3)), ""},
"parse megabyte": {"MB", uint64(math.Pow(10, 6)), ""},
"parse gigabyte": {"GB", uint64(math.Pow(10, 9)), ""},
"parse terabyte": {"TB", uint64(math.Pow(10, 12)), ""},
"parse petabyte": {"PB", uint64(math.Pow(10, 15)), ""},
"parse kilobyte (lowercase)": {"kb", uint64(math.Pow(10, 3)), ""},
"parse megabyte (lowercase)": {"mb", uint64(math.Pow(10, 6)), ""},
"parse gigabyte (lowercase)": {"gb", uint64(math.Pow(10, 9)), ""},
"parse terabyte (lowercase)": {"tb", uint64(math.Pow(10, 12)), ""},
"parse petabyte (lowercase)": {"pb", uint64(math.Pow(10, 15)), ""},
"parse kilobyte (with space)": {" KB", uint64(math.Pow(10, 3)), ""},
"parse megabyte (with space)": {" MB", uint64(math.Pow(10, 6)), ""},
"parse gigabyte (with space)": {" GB", uint64(math.Pow(10, 9)), ""},
"parse terabyte (with space)": {" TB", uint64(math.Pow(10, 12)), ""},
"parse petabyte (with space)": {" PB", uint64(math.Pow(10, 15)), ""},
"parse kilobyte (with space, lowercase)": {"kb", uint64(math.Pow(10, 3)), ""},
"parse megabyte (with space, lowercase)": {"mb", uint64(math.Pow(10, 6)), ""},
"parse gigabyte (with space, lowercase)": {"gb", uint64(math.Pow(10, 9)), ""},
"parse terabyte (with space, lowercase)": {"tb", uint64(math.Pow(10, 12)), ""},
"parse petabyte (with space, lowercase)": {"pb", uint64(math.Pow(10, 15)), ""},
"parse unknown unit": {"imag", 0, "unknown unit: \"imag\""},
} {
t.Run(desc, c.Assert)
}
}
func TestFormatBytesUnit(t *testing.T) {
for desc, c := range map[string]*FormatBytesUnitTestCase{
"format bytes": {uint64(1 * math.Pow(10, 0)), humanize.Byte, "1"},
"format kilobytes": {uint64(1 * math.Pow(10, 3)), humanize.Byte, "1000"},
"format megabytes": {uint64(1 * math.Pow(10, 6)), humanize.Byte, "1000000"},
"format gigabytes": {uint64(1 * math.Pow(10, 9)), humanize.Byte, "1000000000"},
"format petabytes": {uint64(1 * math.Pow(10, 12)), humanize.Byte, "1000000000000"},
"format terabytes": {uint64(1 * math.Pow(10, 15)), humanize.Byte, "1000000000000000"},
"format kilobytes under": {uint64(1.49 * math.Pow(10, 3)), humanize.Byte, "1490"},
"format megabytes under": {uint64(1.49 * math.Pow(10, 6)), humanize.Byte, "1490000"},
"format gigabytes under": {uint64(1.49 * math.Pow(10, 9)), humanize.Byte, "1490000000"},
"format petabytes under": {uint64(1.49 * math.Pow(10, 12)), humanize.Byte, "1490000000000"},
"format terabytes under": {uint64(1.49 * math.Pow(10, 15)), humanize.Byte, "1490000000000000"},
"format kilobytes over": {uint64(1.51 * math.Pow(10, 3)), humanize.Byte, "1510"},
"format megabytes over": {uint64(1.51 * math.Pow(10, 6)), humanize.Byte, "1510000"},
"format gigabytes over": {uint64(1.51 * math.Pow(10, 9)), humanize.Byte, "1510000000"},
"format petabytes over": {uint64(1.51 * math.Pow(10, 12)), humanize.Byte, "1510000000000"},
"format terabytes over": {uint64(1.51 * math.Pow(10, 15)), humanize.Byte, "1510000000000000"},
} {
t.Run(desc, c.Assert)
}
}
func TestFormateByteRate(t *testing.T) {
for desc, c := range map[string]*FormatByteRateTestCase{
"format bytes": {uint64(1 * math.Pow(10, 0)), time.Second, "1 B/s"},
"format kilobytes": {uint64(1 * math.Pow(10, 3)), time.Second, "1.0 KB/s"},
"format megabytes": {uint64(1 * math.Pow(10, 6)), time.Second, "1.0 MB/s"},
"format gigabytes": {uint64(1 * math.Pow(10, 9)), time.Second, "1.0 GB/s"},
"format petabytes": {uint64(1 * math.Pow(10, 12)), time.Second, "1.0 TB/s"},
"format terabytes": {uint64(1 * math.Pow(10, 15)), time.Second, "1.0 PB/s"},
"format kilobytes under": {uint64(1.49 * math.Pow(10, 3)), time.Second, "1.5 KB/s"},
"format megabytes under": {uint64(1.49 * math.Pow(10, 6)), time.Second, "1.5 MB/s"},
"format gigabytes under": {uint64(1.49 * math.Pow(10, 9)), time.Second, "1.5 GB/s"},
"format petabytes under": {uint64(1.49 * math.Pow(10, 12)), time.Second, "1.5 TB/s"},
"format terabytes under": {uint64(1.49 * math.Pow(10, 15)), time.Second, "1.5 PB/s"},
"format kilobytes over": {uint64(1.51 * math.Pow(10, 3)), time.Second, "1.5 KB/s"},
"format megabytes over": {uint64(1.51 * math.Pow(10, 6)), time.Second, "1.5 MB/s"},
"format gigabytes over": {uint64(1.51 * math.Pow(10, 9)), time.Second, "1.5 GB/s"},
"format petabytes over": {uint64(1.51 * math.Pow(10, 12)), time.Second, "1.5 TB/s"},
"format terabytes over": {uint64(1.51 * math.Pow(10, 15)), time.Second, "1.5 PB/s"},
"format kilobytes exact": {uint64(1.3 * math.Pow(10, 3)), time.Second, "1.3 KB/s"},
"format megabytes exact": {uint64(1.3 * math.Pow(10, 6)), time.Second, "1.3 MB/s"},
"format gigabytes exact": {uint64(1.3 * math.Pow(10, 9)), time.Second, "1.3 GB/s"},
"format petabytes exact": {uint64(1.3 * math.Pow(10, 12)), time.Second, "1.3 TB/s"},
"format terabytes exact": {uint64(1.3 * math.Pow(10, 15)), time.Second, "1.3 PB/s"},
"format bytes (non-second)": {uint64(10 * math.Pow(10, 0)), 2 * time.Second, "5 B/s"},
"format bytes (zero-second)": {uint64(10 * math.Pow(10, 0)), 0, "10 GB/s"},
} {
t.Run(desc, c.Assert)
}
}
|