Debian Codegraph @

datasize_test.go

raw symbols SCIP
1 package datasize_test
2
3 import (
4 "testing"
5
6 . "github.com/c2h5oh/datasize"
7 )
8
9 func 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
39 func 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 t.Run("UnmarshalText "+tt.in, func(t *testing.T) {
63 var s ByteSize
64 err := s.UnmarshalText([]byte(tt.in))
65
66 if (err != nil) != tt.err {
67 t.Errorf("UnmarshalText(%s) => %v, want no error", tt.in, err)
68 }
69
70 if s != tt.out {
71 t.Errorf("UnmarshalText(%s) => %d bytes, want %d bytes", tt.in, s, tt.out)
72 }
73 })
74 t.Run("Parse "+tt.in, func(t *testing.T) {
75 s, err := Parse([]byte(tt.in))
76
77 if (err != nil) != tt.err {
78 t.Errorf("Parse(%s) => %v, want no error", tt.in, err)
79 }
80
81 if s != tt.out {
82 t.Errorf("Parse(%s) => %d bytes, want %d bytes", tt.in, s, tt.out)
83 }
84 })
85 t.Run("MustParse "+tt.in, func(t *testing.T) {
86 defer func() {
87 if err := recover(); (err != nil) != tt.err {
88 t.Errorf("MustParse(%s) => %v, want no error", tt.in, err)
89 }
90 }()
91
92 s := MustParse([]byte(tt.in))
93 if s != tt.out {
94 t.Errorf("MustParse(%s) => %d bytes, want %d bytes", tt.in, s, tt.out)
95 }
96 })
97 t.Run("ParseString "+tt.in, func(t *testing.T) {
98 s, err := ParseString(tt.in)
99
100 if (err != nil) != tt.err {
101 t.Errorf("ParseString(%s) => %v, want no error", tt.in, err)
102 }
103
104 if s != tt.out {
105 t.Errorf("ParseString(%s) => %d bytes, want %d bytes", tt.in, s, tt.out)
106 }
107 })
108 t.Run("MustParseString "+tt.in, func(t *testing.T) {
109 defer func() {
110 if err := recover(); (err != nil) != tt.err {
111 t.Errorf("MustParseString(%s) => %v, want no error", tt.in, err)
112 }
113 }()
114
115 s := MustParseString(tt.in)
116 if s != tt.out {
117 t.Errorf("MustParseString(%s) => %d bytes, want %d bytes", tt.in, s, tt.out)
118 }
119 })
120 }
121 }