1package datasize_test 2 3import ( 4 "testing" 5 6 . "github.com/c2h5oh/datasize" 7) 8 9func TestMarshalText(t *testing.T) { 10 table := []struct { 11 in ByteSize 12 out string 13 }{ 14 {0, "0B"}, 15 {B, "1B"}, 16 {KB, "1KB"}, 17 {MB, "1MB"}, 18 {GB, "1GB"}, 19 {TB, "1TB"}, 20 {PB, "1PB"}, 21 {EB, "1EB"}, 22 {400 * TB, "400TB"}, 23 {2048 * MB, "2GB"}, 24 {B + KB, "1025B"}, 25 {MB + 20*KB, "1044KB"}, 26 {100*MB + KB, "102401KB"}, 27 } 28 29 for _, tt := range table { 30 b, _ := tt.in.MarshalText() 31 s := string(b) 32 33 if s != tt.out { 34 t.Errorf("MarshalText(%d) => %s, want %s", tt.in, s, tt.out) 35 } 36 } 37} 38 39func TestUnmarshalText(t *testing.T) { 40 table := []struct { 41 in string 42 err bool 43 out ByteSize 44 }{ 45 {"0", false, ByteSize(0)}, 46 {"0B", false, ByteSize(0)}, 47 {"0 KB", false, ByteSize(0)}, 48 {"1", false, B}, 49 {"1K", false, KB}, 50 {"2MB", false, 2 * MB}, 51 {"5 GB", false, 5 * GB}, 52 {"20480 G", false, 20 * TB}, 53 {"50 eB", true, ByteSize((1 << 64) - 1)}, 54 {"200000 pb", true, ByteSize((1 << 64) - 1)}, 55 {"10 Mb", true, ByteSize(0)}, 56 {"g", true, ByteSize(0)}, 57 {"10 kB ", false, 10 * KB}, 58 {"10 kBs ", true, ByteSize(0)}, 59 } 60 61 for _, tt := range table { 62 var s ByteSize 63 err := s.UnmarshalText([]byte(tt.in)) 64 65 if (err != nil) != tt.err { 66 t.Errorf("UnmarshalText(%s) => %v, want no error", tt.in, err) 67 } 68 69 if s != tt.out { 70 t.Errorf("UnmarshalText(%s) => %d bytes, want %d bytes", tt.in, s, tt.out) 71 } 72 } 73} 74