This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte_test.go
90 lines (78 loc) · 1.95 KB
/
byte_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
80
81
82
83
84
85
86
87
88
89
90
package unit_test
import (
"testing"
"github.com/deixis/pkg/unit"
)
func TestByteSizes(t *testing.T) {
t.Parallel()
table := []struct {
res unit.Byte
expect float64
}{
{res: unit.KB * 3, expect: 3072},
{res: unit.MB * 5, expect: 5242880},
{res: unit.GB * 7, expect: 7516192768},
{res: unit.TB * 9, expect: 9895604649984},
{res: unit.TB * 0, expect: 0},
{res: unit.TB * 1, expect: 1099511627776},
{res: unit.TB * -1, expect: -1099511627776},
}
for _, test := range table {
if float64(test.res) != test.expect {
t.Errorf("expect to get %f, but got %f", test.expect, test.res)
}
}
}
func TestByteParse(t *testing.T) {
t.Parallel()
table := []struct {
in string
expect unit.Byte
err error
}{
{in: "3 kB", expect: unit.KB * 3},
{in: "3.3 kB", expect: unit.KB * 3.3},
{in: "3kB", expect: unit.KB * 3},
{in: "3.3kB", expect: unit.KB * 3.3},
{in: "5 MB", expect: unit.MB * 5},
{in: "7 GB", expect: unit.GB * 7},
{in: "9 TB", expect: unit.TB * 9},
{in: "0 B", expect: unit.TB * 0},
{in: "1 TB", expect: unit.TB * 1},
{in: "-1 TB", expect: unit.TB * -1},
}
for _, test := range table {
res, err := unit.ParseByte(test.in)
if err != test.err {
t.Errorf("expect to get error %s, but got %s", test.err, err)
}
if err != nil {
continue
}
if res != test.expect {
t.Errorf("expect to get %f, but got %f", test.expect, res)
}
}
}
func TestByteStringFormat(t *testing.T) {
t.Parallel()
table := []struct {
in unit.Byte
expect string
}{
{in: unit.KB * 3, expect: "3 kB"},
{in: unit.KB * 3.3, expect: "3.3 kB"},
{in: unit.MB * 5, expect: "5 MB"},
{in: unit.GB * 7, expect: "7 GB"},
{in: unit.TB * 9, expect: "9 TB"},
{in: unit.TB * 0, expect: "0 B"},
{in: unit.TB * 1, expect: "1 TB"},
{in: unit.TB * -1, expect: "-1 TB"},
}
for _, test := range table {
res := test.in.String()
if res != test.expect {
t.Errorf("expect to get %s, but got %s", test.expect, res)
}
}
}