github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/man/bigbytes_test.go (about)

     1  package man
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  )
     7  
     8  func TestBigByteParsing(t *testing.T) {
     9  	tests := []struct {
    10  		in  string
    11  		exp uint64
    12  	}{
    13  		{"42", 42},
    14  		{"42MB", 42000000},
    15  		{"42MiB", 44040192},
    16  		{"42mb", 42000000},
    17  		{"42mib", 44040192},
    18  		{"42MIB", 44040192},
    19  		{"42 MB", 42000000},
    20  		{"42 MiB", 44040192},
    21  		{"42 mb", 42000000},
    22  		{"42 mib", 44040192},
    23  		{"42 MIB", 44040192},
    24  		{"42.5MB", 42500000},
    25  		{"42.5MiB", 44564480},
    26  		{"42.5 MB", 42500000},
    27  		{"42.5 MiB", 44564480},
    28  		// No need to say B
    29  		{"42M", 42000000},
    30  		{"42Mi", 44040192},
    31  		{"42m", 42000000},
    32  		{"42mi", 44040192},
    33  		{"42MI", 44040192},
    34  		{"42 M", 42000000},
    35  		{"42 Mi", 44040192},
    36  		{"42 m", 42000000},
    37  		{"42 mi", 44040192},
    38  		{"42 MI", 44040192},
    39  		{"42.5M", 42500000},
    40  		{"42.5Mi", 44564480},
    41  		{"42.5 M", 42500000},
    42  		{"42.5 Mi", 44564480},
    43  		{"1,005.03 MB", 1005030000},
    44  		// Large testing, breaks when too much larger than
    45  		// this.
    46  		{"12.5 EB", uint64(12.5 * float64(EByte))},
    47  		{"12.5 E", uint64(12.5 * float64(EByte))},
    48  		{"12.5 EiB", uint64(12.5 * float64(EiByte))},
    49  	}
    50  
    51  	for _, p := range tests {
    52  		got, err := ParseBigBytes(p.in)
    53  		if err != nil {
    54  			t.Errorf("Couldn't parse %v: %v", p.in, err)
    55  		} else {
    56  			if got.Uint64() != p.exp {
    57  				t.Errorf("Expected %v for %v, got %v",
    58  					p.exp, p.in, got)
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  func TestBigByteErrors(t *testing.T) {
    65  	got, err := ParseBigBytes("84 JB")
    66  	if err == nil {
    67  		t.Errorf("Expected error, got %v", got)
    68  	}
    69  	got, err = ParseBigBytes("")
    70  	if err == nil {
    71  		t.Errorf("Expected error parsing nothing")
    72  	}
    73  }
    74  
    75  func bbyte(in uint64) string {
    76  	return BigBytes((&big.Int{}).SetUint64(in))
    77  }
    78  
    79  func bibyte(in uint64) string {
    80  	return BigIBytes((&big.Int{}).SetUint64(in))
    81  }
    82  
    83  func TestBigBytes(t *testing.T) {
    84  	testList{
    85  		{"bytes(0)", bbyte(0), "0B"},
    86  		{"bytes(1)", bbyte(1), "1B"},
    87  		{"bytes(803)", bbyte(803), "803B"},
    88  		{"bytes(999)", bbyte(999), "999B"},
    89  
    90  		{"bytes(1024)", bbyte(1024), "1kB"},
    91  		{"bytes(1MB - 1)", bbyte(MByte - Byte), "1000kB"},
    92  
    93  		{"bytes(1MB)", bbyte(1024 * 1024), "1MB"},
    94  		{"bytes(1GB - 1K)", bbyte(GByte - KByte), "1000MB"},
    95  
    96  		{"bytes(1GB)", bbyte(GByte), "1GB"},
    97  		{"bytes(1TB - 1M)", bbyte(TByte - MByte), "1000GB"},
    98  
    99  		{"bytes(1TB)", bbyte(TByte), "1TB"},
   100  		{"bytes(1PB - 1T)", bbyte(PByte - TByte), "999TB"},
   101  
   102  		{"bytes(1PB)", bbyte(PByte), "1PB"},
   103  		{"bytes(1PB - 1T)", bbyte(EByte - PByte), "999PB"},
   104  
   105  		{"bytes(1EB)", bbyte(EByte), "1EB"},
   106  		// Overflows.
   107  		// {"bytes(1EB - 1P)", Bytes((KByte*EByte)-PByte), "1023EB"},
   108  
   109  		{"bytes(0)", bibyte(0), "0B"},
   110  		{"bytes(1)", bibyte(1), "1B"},
   111  		{"bytes(803)", bibyte(803), "803B"},
   112  		{"bytes(1023)", bibyte(1023), "1023B"},
   113  
   114  		{"bytes(1024)", bibyte(1024), "1KiB"},
   115  		{"bytes(1MB - 1)", bibyte(MiByte - IByte), "1024KiB"},
   116  
   117  		{"bytes(1MB)", bibyte(1024 * 1024), "1MiB"},
   118  		{"bytes(1GB - 1K)", bibyte(GiByte - KiByte), "1024MiB"},
   119  
   120  		{"bytes(1GB)", bibyte(GiByte), "1GiB"},
   121  		{"bytes(1TB - 1M)", bibyte(TiByte - MiByte), "1024GiB"},
   122  
   123  		{"bytes(1TB)", bibyte(TiByte), "1TiB"},
   124  		{"bytes(1PB - 1T)", bibyte(PiByte - TiByte), "1023TiB"},
   125  
   126  		{"bytes(1PB)", bibyte(PiByte), "1PiB"},
   127  		{"bytes(1PB - 1T)", bibyte(EiByte - PiByte), "1023PiB"},
   128  
   129  		{"bytes(1EiB)", bibyte(EiByte), "1EiB"},
   130  		// Overflows.
   131  		// {"bytes(1EB - 1P)", bibyte((KIByte*EIByte)-PiByte), "1023EB"},
   132  
   133  		{"bytes(5.5GiB)", bibyte(5.5 * GiByte), "5.5GiB"},
   134  
   135  		{"bytes(5.5GB)", bbyte(5.5 * GByte), "5.5GB"},
   136  	}.validate(t)
   137  }
   138  
   139  func TestVeryBigBytes(t *testing.T) {
   140  	b, _ := (&big.Int{}).SetString("15347691069326346944512", 10)
   141  	s := BigBytes(b)
   142  	if s != "15.3ZB" {
   143  		t.Errorf("Expected 15.3ZB, got %v", s)
   144  	}
   145  	s = BigIBytes(b)
   146  	if s != "13ZiB" {
   147  		t.Errorf("Expected 13 ZiB, got %v", s)
   148  	}
   149  
   150  	b, _ = (&big.Int{}).SetString("15716035654990179271180288", 10)
   151  	s = BigBytes(b)
   152  	if s != "15.7YB" {
   153  		t.Errorf("Expected 15.7YB, got %v", s)
   154  	}
   155  	s = BigIBytes(b)
   156  	if s != "13YiB" {
   157  		t.Errorf("Expected 13 YiB, got %v", s)
   158  	}
   159  }
   160  
   161  func TestVeryVeryBigBytes(t *testing.T) {
   162  	b, _ := (&big.Int{}).SetString("16093220510709943573688614912", 10)
   163  	s := BigBytes(b)
   164  	if s != "16093.2YB" {
   165  		t.Errorf("Expected 16093.2YB, got %v", s)
   166  	}
   167  	s = BigIBytes(b)
   168  	if s != "13312YiB" {
   169  		t.Errorf("Expected 13312 YiB, got %v", s)
   170  	}
   171  }
   172  
   173  func TestParseVeryBig(t *testing.T) {
   174  	tests := []struct {
   175  		in  string
   176  		out string
   177  	}{
   178  		{"16 ZB", "16000000000000000000000"},
   179  		{"16 ZiB", "18889465931478580854784"},
   180  		{"16.5 ZB", "16500000000000000000000"},
   181  		{"16.5 ZiB", "19479761741837286506496"},
   182  		{"16 Z", "16000000000000000000000"},
   183  		{"16 Zi", "18889465931478580854784"},
   184  		{"16.5 Z", "16500000000000000000000"},
   185  		{"16.5 Zi", "19479761741837286506496"},
   186  
   187  		{"16 YB", "16000000000000000000000000"},
   188  		{"16 YiB", "19342813113834066795298816"},
   189  		{"16.5 YB", "16500000000000000000000000"},
   190  		{"16.5 YiB", "19947276023641381382651904"},
   191  		{"16 Y", "16000000000000000000000000"},
   192  		{"16 Yi", "19342813113834066795298816"},
   193  		{"16.5 Y", "16500000000000000000000000"},
   194  		{"16.5 Yi", "19947276023641381382651904"},
   195  	}
   196  
   197  	for _, test := range tests {
   198  		x, err := ParseBigBytes(test.in)
   199  		if err != nil {
   200  			t.Errorf("Error parsing %q: %v", test.in, err)
   201  			continue
   202  		}
   203  
   204  		if x.String() != test.out {
   205  			t.Errorf("Expected %q for %q, got %v", test.out, test.in, x)
   206  		}
   207  	}
   208  }
   209  
   210  func BenchmarkParseBigBytes(b *testing.B) {
   211  	for i := 0; i < b.N; i++ {
   212  		ParseBigBytes("16.5 Z")
   213  	}
   214  }
   215  
   216  func BenchmarkBigBytes(b *testing.B) {
   217  	for i := 0; i < b.N; i++ {
   218  		bibyte(16.5 * GByte)
   219  	}
   220  }