1 package humanize
2
3 import (
4 "testing"
5 )
6
7 func TestByteParsing(t *testing.T) {
8 tests := []struct {
9 in string
10 exp uint64
11 }{
12 {"42", 42},
13 {"42MB", 42000000},
14 {"42MiB", 44040192},
15 {"42mb", 42000000},
16 {"42mib", 44040192},
17 {"42MIB", 44040192},
18 {"42 MB", 42000000},
19 {"42 MiB", 44040192},
20 {"42 mb", 42000000},
21 {"42 mib", 44040192},
22 {"42 MIB", 44040192},
23 {"42.5MB", 42500000},
24 {"42.5MiB", 44564480},
25 {"42.5 MB", 42500000},
26 {"42.5 MiB", 44564480},
27
28 {"42M", 42000000},
29 {"42Mi", 44040192},
30 {"42m", 42000000},
31 {"42mi", 44040192},
32 {"42MI", 44040192},
33 {"42 M", 42000000},
34 {"42 Mi", 44040192},
35 {"42 m", 42000000},
36 {"42 mi", 44040192},
37 {"42 MI", 44040192},
38 {"42.5M", 42500000},
39 {"42.5Mi", 44564480},
40 {"42.5 M", 42500000},
41 {"42.5 Mi", 44564480},
42
43 {"1,005.03 MB", 1005030000},
44
45
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 := ParseBytes(p.in)
53 if err != nil {
54 t.Errorf("Couldn't parse %v: %v", p.in, err)
55 }
56 if got != p.exp {
57 t.Errorf("Expected %v for %v, got %v",
58 p.exp, p.in, got)
59 }
60 }
61 }
62
63 func TestByteErrors(t *testing.T) {
64 got, err := ParseBytes("84 JB")
65 if err == nil {
66 t.Errorf("Expected error, got %v", got)
67 }
68 _, err = ParseBytes("")
69 if err == nil {
70 t.Errorf("Expected error parsing nothing")
71 }
72 got, err = ParseBytes("16 EiB")
73 if err == nil {
74 t.Errorf("Expected error, got %v", got)
75 }
76 }
77
78 func TestBytes(t *testing.T) {
79 testList{
80 {"bytes(0)", Bytes(0), "0 B"},
81 {"bytes(1)", Bytes(1), "1 B"},
82 {"bytes(803)", Bytes(803), "803 B"},
83 {"bytes(999)", Bytes(999), "999 B"},
84
85 {"bytes(1024)", Bytes(1024), "1.0 kB"},
86 {"bytes(9999)", Bytes(9999), "10 kB"},
87 {"bytes(1MB - 1)", Bytes(MByte - Byte), "1000 kB"},
88
89 {"bytes(1MB)", Bytes(1024 * 1024), "1.0 MB"},
90 {"bytes(1GB - 1K)", Bytes(GByte - KByte), "1000 MB"},
91
92 {"bytes(1GB)", Bytes(GByte), "1.0 GB"},
93 {"bytes(1TB - 1M)", Bytes(TByte - MByte), "1000 GB"},
94 {"bytes(10MB)", Bytes(9999 * 1000), "10 MB"},
95
96 {"bytes(1TB)", Bytes(TByte), "1.0 TB"},
97 {"bytes(1PB - 1T)", Bytes(PByte - TByte), "999 TB"},
98
99 {"bytes(1PB)", Bytes(PByte), "1.0 PB"},
100 {"bytes(1PB - 1T)", Bytes(EByte - PByte), "999 PB"},
101
102 {"bytes(1EB)", Bytes(EByte), "1.0 EB"},
103
104
105
106 {"bytes(0)", IBytes(0), "0 B"},
107 {"bytes(1)", IBytes(1), "1 B"},
108 {"bytes(803)", IBytes(803), "803 B"},
109 {"bytes(1023)", IBytes(1023), "1023 B"},
110
111 {"bytes(1024)", IBytes(1024), "1.0 KiB"},
112 {"bytes(1MB - 1)", IBytes(MiByte - IByte), "1024 KiB"},
113
114 {"bytes(1MB)", IBytes(1024 * 1024), "1.0 MiB"},
115 {"bytes(1GB - 1K)", IBytes(GiByte - KiByte), "1024 MiB"},
116
117 {"bytes(1GB)", IBytes(GiByte), "1.0 GiB"},
118 {"bytes(1TB - 1M)", IBytes(TiByte - MiByte), "1024 GiB"},
119
120 {"bytes(1TB)", IBytes(TiByte), "1.0 TiB"},
121 {"bytes(1PB - 1T)", IBytes(PiByte - TiByte), "1023 TiB"},
122
123 {"bytes(1PB)", IBytes(PiByte), "1.0 PiB"},
124 {"bytes(1PB - 1T)", IBytes(EiByte - PiByte), "1023 PiB"},
125
126 {"bytes(1EiB)", IBytes(EiByte), "1.0 EiB"},
127
128
129
130 {"bytes(5.5GiB)", IBytes(5.5 * GiByte), "5.5 GiB"},
131
132 {"bytes(5.5GB)", Bytes(5.5 * GByte), "5.5 GB"},
133 }.validate(t)
134 }
135
136 func BenchmarkParseBytes(b *testing.B) {
137 for i := 0; i < b.N; i++ {
138 ParseBytes("16.5 GB")
139 }
140 }
141
142 func BenchmarkBytes(b *testing.B) {
143 for i := 0; i < b.N; i++ {
144 Bytes(16.5 * GByte)
145 }
146 }
147
View as plain text