github.com/ungtb10d/git-lfs@v2.5.2+incompatible/tools/humanize/humanize_test.go (about)

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