-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytecount_test.go
222 lines (198 loc) · 4.8 KB
/
bytecount_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) 2020 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package infounit_test
import (
"math"
"testing"
"time"
"github.com/tunabay/go-infounit"
)
//
func TestByteCount_String(t *testing.T) {
t.Parallel()
tc := []struct {
b infounit.ByteCount
s string
}{
{infounit.Byte * 0, "0 B"},
{infounit.Byte * 1, "1 B"},
{infounit.Byte * 987654321, "987.7 MB"},
{infounit.Byte * 9876543210, "9.9 GB"},
{infounit.ByteCount(18446744073709551615), "18.4 EB"},
}
for _, c := range tc {
s := c.b.String()
// t.Logf(`%d: %s`, c.b, s)
if s != c.s {
t.Errorf(`%d: want: %s, got: %s`, c.b, c.s, s)
}
}
}
//
func TestByteCount_GoString(t *testing.T) {
t.Parallel()
tc := []struct {
b infounit.ByteCount
s string
}{
{infounit.ByteCount(0), "ByteCount(0)"},
{infounit.Byte * 987654321, "ByteCount(987654321)"},
{infounit.Byte * 9876543210, "ByteCount(9876543210)"},
{infounit.ByteCount(18446744073709551615), "ByteCount(18446744073709551615)"},
}
for _, c := range tc {
s := c.b.GoString()
// t.Logf(`%d: %s`, c.b, s)
if s != c.s {
t.Errorf(`%d: want: %s, got: %s`, c.b, c.s, s)
}
}
}
//
func TestByteCount_BitCount(t *testing.T) {
t.Parallel()
tc := []struct {
b infounit.ByteCount
bits infounit.BitCount
err error
}{
{infounit.ByteCount(0), infounit.BitCount(0), nil},
{infounit.ByteCount(1), infounit.BitCount(8), nil},
{infounit.ByteCount(1111111111), infounit.BitCount(8888888888), nil},
{
infounit.ByteCount(0x_1fff_ffff_ffff_ffff),
infounit.BitCount(0x_ffff_ffff_ffff_fff8),
nil,
},
{
infounit.ByteCount(0x_1fff_ffff_ffff_ffff + 1),
infounit.BitCount(0),
infounit.ErrOutOfRange,
},
{
infounit.ByteCount(0x_ffff_ffff_ffff_ffff),
infounit.BitCount(0),
infounit.ErrOutOfRange,
},
}
for _, c := range tc {
bits, err := c.b.BitCount()
// t.Logf(`%d: %s, %s`, c.b, bits, err)
if err != c.err {
t.Errorf(`%d: want(err): %s, got(err): %s`, c.b, c.err, err)
}
if bits != c.bits {
t.Errorf(`%d: want: %d, got: %d`, c.b, c.bits, bits)
}
}
}
//
func TestByteCount_CalcTime(t *testing.T) {
t.Parallel()
tc := []struct {
b infounit.ByteCount
r infounit.BitRate
t time.Duration
err error
}{
{0, infounit.BitPerSecond * 1, time.Second * 0, nil},
{1000, infounit.KilobitPerSecond * 1, time.Second * 8, nil},
{infounit.Megabyte, infounit.KilobitPerSecond, time.Second * 8000, nil},
{infounit.Terabyte, infounit.KilobitPerSecond, time.Second * 8000000000, nil},
{1, 0, 0, infounit.ErrDivZeroBitRate},
{infounit.Exabyte * 10, infounit.BitPerSecond, 0, infounit.ErrOutOfRange},
}
for _, c := range tc {
tm, err := c.b.CalcTime(c.r)
// t.Logf(`%v in %v: %s, %s`, c.b, c.r, tm, err)
if err != c.err {
t.Errorf(`%v in %v: want(err): %s, got(err): %s`, c.b, c.r, c.err, err)
}
if tm != c.t {
t.Errorf(`%v in %v: want: %s, got: %s`, c.b, c.r, c.t, tm)
}
}
}
//
func TestByteCount_CalcBitRate(t *testing.T) {
t.Parallel()
tc := []struct {
b infounit.ByteCount
t time.Duration
r infounit.BitRate
}{
{0, time.Second, 0},
{1000, time.Second, 8000},
{infounit.Megabyte, time.Second * 8000, infounit.KilobitPerSecond},
{infounit.Byte, time.Second * 10, infounit.BitPerSecond * 0.8},
{1000, 0, infounit.BitRate(math.Inf(+1))},
{0, 0, 0},
}
for _, c := range tc {
rate := c.b.CalcBitRate(c.t)
// t.Logf(`%v in %v: %v`, c.b, c.t, rate)
switch {
case c.r.IsInf(+1) && !rate.IsInf(+1):
t.Errorf(`%v in %v: want: %v, got: %v`, c.b, c.t, c.r, rate)
case rate != c.r:
t.Errorf(`%v in %v: want: %v, got: %v`, c.b, c.t, c.r, rate)
}
}
}
//
func TestParseByteCount(t *testing.T) {
t.Parallel()
tc := []struct {
s string
b infounit.ByteCount
e bool
}{
{"", 0, true},
{"1B", 1, false},
{"9 B", 9, false},
{"1.23 kB", 1230, false},
{"-1 kB", 0, true},
}
for _, c := range tc {
bc, err := infounit.ParseByteCount(c.s)
switch {
case c.e && err == nil:
t.Errorf(`%s: error expected, but nil error.`, c.s)
case !c.e && err != nil:
t.Errorf(`%s: unexpected error: %v`, c.s, err)
}
if bc != c.b {
t.Errorf(`%s: want: %d, got: %d`, c.s, c.b, bc)
}
}
}
//
func TestParseByteCountBinary(t *testing.T) {
t.Parallel()
tc := []struct {
s string
b infounit.ByteCount
e bool
}{
{"", 0, true},
{"1B", 1, false},
{"9 B", 9, false},
{"1 kB", 1024, false},
{"1.5 kB", 1536, false},
{"-1 kB", 0, true},
}
for _, c := range tc {
bc, err := infounit.ParseByteCountBinary(c.s)
// t.Logf(`%s: %v, %v`, c.s, bc, err)
switch {
case c.e && err == nil:
t.Errorf(`%s: error expected, but nil error.`, c.s)
case !c.e && err != nil:
t.Errorf(`%s: unexpected error: %v`, c.s, err)
}
if bc != c.b {
t.Errorf(`%s: want: %d, got: %d`, c.s, c.b, bc)
}
}
}