-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes_test.go
56 lines (53 loc) · 1.19 KB
/
bytes_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
package flagx
import (
"testing"
)
func TestBytesSetFailure(t *testing.T) {
f := func(value string) {
t.Helper()
var b Bytes
if err := b.Set(value); err == nil {
t.Fatalf("expecting non-nil error in b.Set(%q)", value)
}
}
f("")
f("foobar")
f("5foobar")
f("aKB")
f("134xMB")
f("2.43sdfGb")
f("aKiB")
f("134xMiB")
f("2.43sdfGIb")
}
func TestBytesSetSuccess(t *testing.T) {
f := func(value string, expectedResult int64) {
t.Helper()
var b Bytes
if err := b.Set(value); err != nil {
t.Fatalf("unexpected error in b.Set(%q): %s", value, err)
}
if b.N != expectedResult {
t.Fatalf("unexpected result; got %d; want %d", b.N, expectedResult)
}
valueString := b.String()
valueExpected := normalizeBytesString(value)
if valueString != valueExpected {
t.Fatalf("unexpected valueString; got %q; want %q", valueString, valueExpected)
}
}
f("0", 0)
f("1", 1)
f("-1234", -1234)
f("123.456", 123)
f("1KiB", 1024)
f("1.5kib", 1.5*1024)
f("23MiB", 23*1024*1024)
f("0.25GiB", 0.25*1024*1024*1024)
f("1.25TiB", 1.25*1024*1024*1024*1024)
f("1KB", 1000)
f("1.5kb", 1.5*1000)
f("23MB", 23*1000*1000)
f("0.25GB", 0.25*1000*1000*1000)
f("1.25TB", 1.25*1000*1000*1000*1000)
}