-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitrate_encoding_test.go
354 lines (328 loc) · 8.93 KB
/
bitrate_encoding_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// 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 (
"bytes"
"encoding/hex"
"encoding/json"
"math"
"strings"
"testing"
"github.com/tunabay/go-infounit"
"gopkg.in/yaml.v2"
)
//
func TestBitRate_MarshalBinary_1(t *testing.T) {
t.Parallel()
tc := []struct {
b float64
hex string
}{
{0.0, "0000000000000000"},
{1.0, "3ff0000000000000"},
{-1.0, "bff0000000000000"},
{123456.78, "40fe240c7ae147ae"},
{math.NaN(), "7ff8000000000001"},
{math.Inf(+1), "7ff0000000000000"},
{math.Inf(-1), "fff0000000000000"},
{math.MaxFloat64, "7fefffffffffffff"},
{math.SmallestNonzeroFloat64, "0000000000000001"},
}
for _, c := range tc {
br := infounit.BitRate(c.b)
bin, err := br.MarshalBinary()
if err != nil {
t.Error(err)
}
exbin, err := hex.DecodeString(c.hex)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(bin, exbin) {
t.Errorf(`%v: want: %x, got: %x`, br, exbin, bin)
}
// t.Logf(`%v: %x`, br, bin)
}
}
//
func TestBitRate_UnmarshalBinary_1(t *testing.T) {
t.Parallel()
tc := []struct {
b float64
hex string
}{
{0.0, "0000000000000000"},
{1.0, "3ff0000000000000"},
{-1.0, "bff0000000000000"},
{123456.78, "40fe240c7ae147ae"},
{math.NaN(), "7ff8000000000001"},
{math.Inf(+1), "7ff0000000000000"},
{math.Inf(-1), "fff0000000000000"},
{math.MaxFloat64, "7fefffffffffffff"},
{math.SmallestNonzeroFloat64, "0000000000000001"},
}
for _, c := range tc {
var br infounit.BitRate
bin, err := hex.DecodeString(c.hex)
if err != nil {
t.Fatal(err)
}
if err := br.UnmarshalBinary(bin); err != nil {
t.Error(err)
}
var ok bool
var exbr infounit.BitRate
switch {
case math.IsNaN(c.b):
exbr = infounit.BitRate(math.NaN())
ok = math.IsNaN(float64(br))
case math.IsInf(c.b, +1):
exbr = infounit.BitRate(math.Inf(+1))
ok = math.IsInf(float64(br), +1)
case math.IsInf(c.b, -1):
exbr = infounit.BitRate(math.Inf(-1))
ok = math.IsInf(float64(br), -1)
default:
exbr = infounit.BitRate(c.b)
ok = br == exbr
}
if !ok {
t.Errorf(`%x: want: %v, got: %v`, c.hex, exbr, br)
}
// t.Logf(`%s: %v`, c.hex, br)
}
}
//
func TestBitRate_MarshalText_1(t *testing.T) {
t.Parallel()
tc := []struct {
b float64
txt string
}{
{0, "0 bit/s"},
{1, "1 bit/s"},
{987654.321, "987654.321 bit/s"},
{0.00987654321, "0.00987654321 bit/s"},
{99999999999.9999, "99999999999.9999 bit/s"},
}
for _, c := range tc {
br := infounit.BitRate(c.b)
txtb, err := br.MarshalText()
if err != nil {
t.Error(err)
}
txt := string(txtb)
if txt != c.txt {
t.Errorf(`%f: want: "%s", got: "%s"`, br, c.txt, txt)
}
}
}
//
func TestBitRate_UnmarshalText_1(t *testing.T) {
t.Parallel()
tc := []struct {
b float64
txt string
}{
{0, "0 bit/s"},
{1, "1 bit/s"},
{987654.321, "987654.321 bit/s"},
{0.00987654321, "0.00987654321 bit/s"},
{99999999999.9999, "99999999999.9999 bit/s"},
}
for _, c := range tc {
var br infounit.BitRate
exbr := infounit.BitRate(c.b)
if err := br.UnmarshalText(([]byte)(c.txt)); err != nil {
t.Errorf(`%s: want: %s, got: %s`, c.txt, exbr, err)
}
if br != exbr {
t.Errorf(`%s: want: %s, got: %s`, c.txt, exbr, br)
}
}
}
func TestBitRate_MarshalYAML(t *testing.T) {
var (
x = infounit.BitRate(999.99999)
y = infounit.BitRate(888.88888)
z = infounit.BitRate(777.77777)
)
v := &struct {
Val infounit.BitRate
Ptr *infounit.BitRate
PtrNil *infounit.BitRate
ValSlice []infounit.BitRate
PtrSlice []*infounit.BitRate
Renamed infounit.BitRate `yaml:"xyzBC"`
ZeroVal infounit.BitRate `yaml:"zeroVal,omitempty"`
ZeroPtr *infounit.BitRate `yaml:",omitempty"`
}{
Val: infounit.BitRate(666.666),
Ptr: &x,
ValSlice: []infounit.BitRate{555.111, 555.222, 555.333},
PtrSlice: []*infounit.BitRate{&y, &z},
Renamed: 444.4444,
ZeroVal: infounit.BitRate(0),
ZeroPtr: nil,
}
expected := strings.Join([]string{
"val: 666.666",
"ptr: 999.99999",
"ptrnil: null",
"valslice:",
"- 555.111",
"- 555.222",
"- 555.333",
"ptrslice:",
"- 888.88888",
"- 777.77777",
"xyzBC: 444.4444",
"",
}, "\n")
yamlBytes, err := yaml.Marshal(v)
if err != nil {
t.Errorf("yaml.Marshal() failed: %v", err)
}
yaml := string(yamlBytes)
if yaml != expected {
t.Errorf("yaml.Marshal() unexpected result: %q", yaml)
}
}
func TestBitRate_UnmarshalYAML(t *testing.T) {
v := struct {
Val infounit.BitRate
Ptr *infounit.BitRate
PtrNil *infounit.BitRate
ValSlice []infounit.BitRate
PtrSlice []*infounit.BitRate
Renamed infounit.BitRate `yaml:"xyzBC"`
VarExprs []infounit.BitRate
}{}
yamlSrc := strings.Join([]string{
"val: 9999.99999",
"ptr: 8888.88888",
"ptrnil: null",
"valslice:",
"- 777.111",
"- 777.222",
"- 777.333",
"ptrslice:",
"- 66666.2222",
"- 66666.3333",
"xyzBC: 5555555.555",
"varexprs:",
`- "12345.678 kilobits per second"`,
`- "345 Mbit/s"`,
`- "67.8Gbit/s"`,
"",
}, "\n")
if err := yaml.UnmarshalStrict(([]byte)(yamlSrc), &v); err != nil {
t.Errorf("yaml.Unmarshal() failed: %v", err)
}
if v.Val != 9999.99999 {
t.Errorf("Val: unexpected value: got: %v, want: 9999.99999 bit/s", v.Val)
}
switch {
case v.Ptr == nil:
t.Errorf("Ptr: unexpected value: got: <nil>, want: %v", 8888.88888)
case *v.Ptr != 8888.88888:
t.Errorf("Ptr: unexpected value: got: %v, want: %v", *v.Ptr, 8888.88888)
}
if v.PtrNil != nil {
t.Errorf("PtrNil: unexpected value: got: %v, want: <nil>", *v.PtrNil)
}
switch {
case len(v.ValSlice) != 3:
t.Errorf("ValSlice: unexpected length: got: %d, want: 3", len(v.ValSlice))
case v.ValSlice[0] != 777.111:
t.Errorf("ValSlice[0]: unexpected value: got: %v, want: 777.111", v.ValSlice[0])
case v.ValSlice[1] != 777.222:
t.Errorf("ValSlice[1]: unexpected value: got: %v, want: 777.222", v.ValSlice[1])
case v.ValSlice[2] != 777.333:
t.Errorf("ValSlice[2]: unexpected value: got: %d, want: 777.333", v.ValSlice[2])
}
switch {
case len(v.PtrSlice) != 2:
t.Errorf("PtrSlice: unexpected length: got: %d, want: 2", len(v.PtrSlice))
case *v.PtrSlice[0] != 66666.2222:
t.Errorf("PtrSlice[0]: unexpected value: got: %v, want: 66666.2222", *v.PtrSlice[0])
case *v.PtrSlice[1] != 66666.3333:
t.Errorf("PtrSlice[1]: unexpected value: got: %v, want: 66666.3333", *v.PtrSlice[1])
}
if v.Renamed != 5555555.555 {
t.Errorf("Renamed: unexpected value: got: %v, want: 5555555.555", v.Renamed)
}
switch {
case len(v.VarExprs) != 3:
t.Errorf("VarExprs: unexpected length: got: %v, want: 3", len(v.VarExprs))
case v.VarExprs[0] != infounit.BitRate(12345678):
t.Errorf("VarExprs[0]: unexpected value: got: %v, want: %v", v.VarExprs[0], infounit.BitRate(12345678))
case v.VarExprs[1] != infounit.MegabitPerSecond*345:
t.Errorf("VarExprs[1]: unexpected value: got: %v, want: %v", v.VarExprs[1], infounit.MegabitPerSecond*345)
case v.VarExprs[2] != infounit.GigabitPerSecond/10*678:
t.Errorf("VarExprs[2]: unexpected value: got: %v, want: %v", v.VarExprs[2], infounit.GigabitPerSecond/10*678)
}
}
func TestBitRate_MarshalJSON(t *testing.T) {
var x, y, z infounit.BitRate = 777.777, 888.888, 999.999
tc := []struct {
j interface{}
s string
}{
{j: infounit.BitRate(123.456), s: `123.456`},
{j: []infounit.BitRate{0.123, 45.6, 789}, s: `[0.123,45.6,789]`},
{j: []*infounit.BitRate{&x, &y, &z, nil}, s: `[777.777,888.888,999.999,null]`},
{j: &struct {
BR infounit.BitRate `json:"xx"`
}{BR: 987.654}, s: `{"xx":987.654}`},
}
for _, c := range tc {
b, err := json.Marshal(c.j)
if err != nil {
t.Errorf("%v: json.Marshal() failed: %v", c.j, err)
continue
}
if s := string(b); s != c.s {
t.Errorf("unexpected JSON output: want: %q, got: %q", c.s, s)
}
}
}
func TestBitRate_UnmarshalJSON(t *testing.T) {
tc := []struct {
s string
v infounit.BitRate
n bool
}{
{s: `12345`, v: 12345},
{s: `0.12345`, v: 0.12345},
{s: `123.45678`, v: 123.45678},
{s: `456789000`, v: 456789000},
{s: `"123 kbit/s"`, v: infounit.KilobitPerSecond * 123},
{s: `"456Mbit/s"`, v: infounit.MegabitPerSecond * 456},
{s: `"789 gigabits per second"`, v: infounit.GigabitPerSecond * 789},
{s: `"123.456 kbps"`, v: infounit.KilobitPerSecond / 1000 * 123456},
{s: `"456.789Mbps"`, v: infounit.MegabitPerSecond / 1000 * 456789},
{s: `null`, n: true},
}
for _, c := range tc {
js := "[" + c.s + "]"
var bca []*infounit.BitRate
if err := json.Unmarshal([]byte(js), &bca); err != nil {
t.Errorf("%v: json.Unmarshal() failed: %v", c.s, err)
continue
}
if len(bca) != 1 {
t.Errorf("unexpected JSON output len: want: 1, got: %d", len(bca))
}
bcp := bca[0]
switch {
case !c.n && bcp == nil:
t.Errorf("%q: unexpected output: want: %v, got: <nil>", c.s, c.v)
case c.n && bcp != nil:
t.Errorf("%q: unexpected output: want: <nil>, got: %v", c.s, *bcp)
case !c.n && *bcp != c.v:
t.Errorf("%q: unexpected output: want: %v, got: %v", c.s, c.v, *bcp)
}
}
}