-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte_size_test.go
79 lines (72 loc) · 1.03 KB
/
byte_size_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"testing"
)
func TestByteSizeSet(t *testing.T) {
conversions := []struct {
s string
p int64
}{
{
s: "B",
p: 1e0,
},
{
s: "kB",
p: 1e3,
},
{
s: "MB",
p: 1e6,
},
{
s: "GB",
p: 1e9,
},
{
s: "TB",
p: 1e12,
},
{
s: "PB",
p: 1e15,
},
{
s: "KiB",
p: 1024,
},
{
s: "MiB",
p: 1024 * 1024,
},
{
s: "GiB",
p: 1024 * 1024 * 1024,
},
{
s: "TiB",
p: 1024 * 1024 * 1024 * 1024,
},
{
s: "PiB",
p: 1024 * 1024 * 1024 * 1024 * 1024,
},
}
for i, c := range conversions {
for _, f := range []float64{1, 2, 3, 4, 5, 6} {
input := fmt.Sprintf("%.0f%s", f, c.s)
expect := f * float64(c.p)
var actual ByteSize
actual.Set(input)
if int64(expect) != int64(actual) {
t.Errorf("%d test parsing [%s] expected [%.6f] got [%s]",
i, input, expect, actual)
}
if input != actual.String() {
t.Errorf("%d test serializing [%s] expected [%s] got [%s]",
i, input, input, actual)
}
}
}
}