github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/units/bytesize_test.go (about) 1 package units_test 2 3 import ( 4 "testing" 5 6 "github.com/xmplusdev/xmcore/common/units" 7 ) 8 9 func TestByteSizes(t *testing.T) { 10 size := units.ByteSize(0) 11 assertSizeString(t, size, "0") 12 size++ 13 assertSizeValue(t, 14 assertSizeString(t, size, "1.00B"), 15 size, 16 ) 17 size <<= 10 18 assertSizeValue(t, 19 assertSizeString(t, size, "1.00KB"), 20 size, 21 ) 22 size <<= 10 23 assertSizeValue(t, 24 assertSizeString(t, size, "1.00MB"), 25 size, 26 ) 27 size <<= 10 28 assertSizeValue(t, 29 assertSizeString(t, size, "1.00GB"), 30 size, 31 ) 32 size <<= 10 33 assertSizeValue(t, 34 assertSizeString(t, size, "1.00TB"), 35 size, 36 ) 37 size <<= 10 38 assertSizeValue(t, 39 assertSizeString(t, size, "1.00PB"), 40 size, 41 ) 42 size <<= 10 43 assertSizeValue(t, 44 assertSizeString(t, size, "1.00EB"), 45 size, 46 ) 47 } 48 49 func assertSizeValue(t *testing.T, size string, expected units.ByteSize) { 50 actual := units.ByteSize(0) 51 err := actual.Parse(size) 52 if err != nil { 53 t.Error(err) 54 } 55 if actual != expected { 56 t.Errorf("expect %s, but got %s", expected, actual) 57 } 58 } 59 60 func assertSizeString(t *testing.T, size units.ByteSize, expected string) string { 61 actual := size.String() 62 if actual != expected { 63 t.Errorf("expect %s, but got %s", expected, actual) 64 } 65 return expected 66 }