codeberg.org/gruf/go-bytesize@v1.0.2/bytesize_test.go (about)

     1  package bytesize_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"codeberg.org/gruf/go-bytesize"
     7  )
     8  
     9  const (
    10  	KB = 1e3
    11  	MB = 1e6
    12  	GB = 1e9
    13  	TB = 1e12
    14  	PB = 1e15
    15  	EB = 1e18
    16  
    17  	KiB = 1024
    18  	MiB = KiB * 1024
    19  	GiB = MiB * 1024
    20  	TiB = GiB * 1024
    21  	PiB = TiB * 1024
    22  	EiB = PiB * 1024
    23  )
    24  
    25  var formatTests = []struct {
    26  	Value     bytesize.Size
    27  	StringSI  string
    28  	StringIEC string
    29  }{
    30  	{
    31  		Value:     1,
    32  		StringSI:  `1B`,
    33  		StringIEC: `1B`,
    34  	},
    35  	{
    36  		Value:     1 * KiB,
    37  		StringSI:  `1.02kB`,
    38  		StringIEC: `1.00kiB`,
    39  	},
    40  	{
    41  		Value:     1 * MiB,
    42  		StringSI:  `1.05MB`,
    43  		StringIEC: `1.00MiB`,
    44  	},
    45  	{
    46  		Value:     1 * GiB,
    47  		StringSI:  `1.07GB`,
    48  		StringIEC: `1.00GiB`,
    49  	},
    50  	{
    51  		Value:     1999,
    52  		StringSI:  `2.00kB`,
    53  		StringIEC: `1.95kiB`,
    54  	},
    55  }
    56  
    57  var parseTests = []struct {
    58  	String string
    59  	Value  bytesize.Size
    60  	Error  error
    61  }{
    62  	{
    63  		String: `0`,
    64  		Value:  0,
    65  	},
    66  	{
    67  		String: `1024`,
    68  		Value:  1024,
    69  	},
    70  	{
    71  		String: `1024B`,
    72  		Value:  1024,
    73  	},
    74  	{
    75  		String: `1kiB`,
    76  		Value:  1 * KiB,
    77  	},
    78  	{
    79  		String: `1kB`,
    80  		Value:  1 * KB,
    81  	},
    82  	{
    83  		String: `1.22kB`,
    84  		Value:  1.22 * KB,
    85  	},
    86  	{
    87  		String: `1.22kiB`,
    88  		Value:  floatSz(1.22 * float64(bytesize.KiB)),
    89  	},
    90  	{
    91  		String: `1.69GiB`,
    92  		Value:  floatSz(1.69 * float64(bytesize.GiB)),
    93  	},
    94  	{
    95  		String: `0.89TiB`,
    96  		Value:  floatSz(0.89 * float64(bytesize.TiB)),
    97  	},
    98  	{
    99  		String: `93.541PiB`,
   100  		Value:  floatSz(93.541 * float64(bytesize.PiB)),
   101  	},
   102  }
   103  
   104  func TestFormat(t *testing.T) {
   105  	for _, test := range formatTests {
   106  		if s := test.Value.StringSI(); s != test.StringSI {
   107  			t.Fatalf("expected SI strings do not match: expect=%q receive=%q", test.StringSI, s)
   108  		} else if s := test.Value.StringIEC(); s != test.StringIEC {
   109  			t.Fatalf("expected IEC strings do not match: expect=%q receive=%q", test.StringIEC, s)
   110  		}
   111  	}
   112  }
   113  
   114  func TestParse(t *testing.T) {
   115  	for _, test := range parseTests {
   116  		v, err := bytesize.ParseSize(test.String)
   117  		if (v != test.Value) || (err != test.Error) {
   118  			t.Fatalf("parsed size / error does not match expected: expect=(%v, %q) receive=(%v, %q)", test.Value, test.Error, v, err)
   119  		}
   120  	}
   121  }
   122  
   123  func BenchmarkFormatIEC(b *testing.B) {
   124  	b.ReportAllocs()
   125  	b.ResetTimer()
   126  	b.RunParallel(func(pb *testing.PB) {
   127  		var s1, s2 string
   128  
   129  		for pb.Next() {
   130  			for _, test := range formatTests {
   131  				s1 = test.Value.StringIEC()
   132  			}
   133  		}
   134  
   135  		s1, s2 = s2, s1
   136  		_ = s1
   137  		_ = s2
   138  	})
   139  }
   140  
   141  func BenchmarkFormatSI(b *testing.B) {
   142  	b.ReportAllocs()
   143  	b.ResetTimer()
   144  	b.RunParallel(func(pb *testing.PB) {
   145  		var s1, s2 string
   146  
   147  		for pb.Next() {
   148  			for _, test := range formatTests {
   149  				s2 = test.Value.StringSI()
   150  			}
   151  		}
   152  
   153  		s1, s2 = s2, s1
   154  		_ = s1
   155  		_ = s2
   156  	})
   157  }
   158  
   159  func BenchmarkParseIEC(b *testing.B) {
   160  	b.ReportAllocs()
   161  	b.ResetTimer()
   162  	b.RunParallel(func(pb *testing.PB) {
   163  		var sz1, sz2 bytesize.Size
   164  
   165  		for pb.Next() {
   166  			for _, test := range formatTests {
   167  				sz1, _ = bytesize.ParseSize(test.StringIEC)
   168  			}
   169  		}
   170  
   171  		sz1, sz2 = sz2, sz1
   172  		_ = sz1
   173  		_ = sz2
   174  	})
   175  }
   176  
   177  func BenchmarkParseSI(b *testing.B) {
   178  	b.ReportAllocs()
   179  	b.ResetTimer()
   180  	b.RunParallel(func(pb *testing.PB) {
   181  		var sz1, sz2 bytesize.Size
   182  
   183  		for pb.Next() {
   184  			for _, test := range formatTests {
   185  				sz2, _ = bytesize.ParseSize(test.StringSI)
   186  			}
   187  		}
   188  
   189  		sz1, sz2 = sz2, sz1
   190  		_ = sz1
   191  		_ = sz2
   192  	})
   193  }
   194  
   195  func floatSz(f float64) bytesize.Size {
   196  	return bytesize.Size(f)
   197  }