-
Notifications
You must be signed in to change notification settings - Fork 89
/
size_test.go
149 lines (131 loc) · 3.01 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"io"
"io/ioutil"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/testing/filetesting"
gc "gopkg.in/check.v1"
"github.com/juju/utils/v4"
)
var _ = gc.Suite(&sizeSuite{})
type sizeSuite struct {
testing.IsolationSuite
}
func (*sizeSuite) TestParseSize(c *gc.C) {
type test struct {
in string
out uint64
err string
}
tests := []test{{
in: "",
err: `expected a non-negative number, got ""`,
}, {
in: "-1",
err: `expected a non-negative number, got "-1"`,
}, {
in: "1MZ",
err: `invalid multiplier suffix "MZ", expected one of MGTPEZY`,
}, {
in: "0",
out: 0,
}, {
in: "123",
out: 123,
}, {
in: "1M",
out: 1,
}, {
in: "0.5G",
out: 512,
}, {
in: "0.5GB",
out: 512,
}, {
in: "0.5GiB",
out: 512,
}, {
in: "0.5T",
out: 524288,
}, {
in: "0.5P",
out: 536870912,
}, {
in: "0.0009765625E",
out: 1073741824,
}, {
in: "1Z",
out: 1125899906842624,
}, {
in: "1Y",
out: 1152921504606846976,
}}
for i, test := range tests {
c.Logf("test %d: %+v", i, test)
size, err := utils.ParseSize(test.in)
if test.err != "" {
c.Assert(err, gc.NotNil)
c.Assert(err, gc.ErrorMatches, test.err)
} else {
c.Assert(err, gc.IsNil)
c.Assert(size, gc.Equals, test.out)
}
}
}
func (*sizeSuite) TestSizingReaderOkay(c *gc.C) {
expected := "some data"
stub := &testing.Stub{}
reader := filetesting.NewStubReader(stub, expected)
var st utils.SizeTracker
sizingReader := io.TeeReader(reader, &st)
data, err := ioutil.ReadAll(sizingReader)
c.Assert(err, jc.ErrorIsNil)
stub.CheckCallNames(c, "Read", "Read")
c.Check(string(data), gc.Equals, expected)
c.Check(st.Size(), gc.Equals, int64(len(expected)))
}
func (*sizeSuite) TestSizingReaderMixedEOF(c *gc.C) {
expected := "some data"
stub := &testing.Stub{}
reader := &filetesting.StubReader{
Stub: stub,
ReturnRead: &fakeStream{
data: expected,
},
}
var st utils.SizeTracker
sizingReader := io.TeeReader(reader, &st)
data, err := ioutil.ReadAll(sizingReader)
c.Assert(err, jc.ErrorIsNil)
stub.CheckCallNames(c, "Read") // The EOF was mixed with the data.
c.Check(string(data), gc.Equals, expected)
c.Check(st.Size(), gc.Equals, int64(len(expected)))
}
func (*sizeSuite) TestSizingWriter(c *gc.C) {
expected := "some data"
stub := &testing.Stub{}
writer, buffer := filetesting.NewStubWriter(stub)
var st utils.SizeTracker
sizingWriter := io.MultiWriter(writer, &st)
n, err := sizingWriter.Write([]byte(expected))
c.Assert(err, jc.ErrorIsNil)
stub.CheckCallNames(c, "Write")
c.Check(n, gc.Equals, len(expected))
c.Check(buffer.String(), gc.Equals, expected)
c.Check(st.Size(), gc.Equals, int64(len(expected)))
}
type fakeStream struct {
data string
pos uint64
}
func (f *fakeStream) Read(data []byte) (int, error) {
n := copy(data, f.data[f.pos:])
f.pos += uint64(n)
if f.pos >= uint64(len(f.data)) {
return n, io.EOF
}
return n, nil
}