-
Notifications
You must be signed in to change notification settings - Fork 52
/
size_test.go
74 lines (58 loc) · 1.71 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
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package id3v2
import (
"bytes"
"testing"
)
var (
synchSafeSizeUint uint = 15351
synchSafeSizeBytes = []byte{0, 0, 119, 119}
synchUnsafeSizeUint uint = 65535
synchUnsafeSizeBytes = []byte{0, 0, 255, 255}
)
func testWriteSize(sizeUint uint, sizeBytes []byte, synchSafe bool, t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
bw := newBufWriter(buf)
bw.WriteBytesSize(sizeUint, synchSafe)
if err := bw.Flush(); err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf.Bytes(), sizeBytes) {
t.Errorf("Expected: %v, got: %v", sizeBytes, buf.Bytes())
}
}
func testParseSize(sizeUint uint, sizeBytes []byte, synchSafe bool, t *testing.T) {
t.Parallel()
size, err := parseSize(sizeBytes, synchSafe)
if err != nil {
t.Error(err)
}
if size != int64(sizeUint) {
t.Errorf("Expected: %v, got: %v", sizeUint, size)
}
}
func TestWriteSynchSafeSize(t *testing.T) {
testWriteSize(synchSafeSizeUint, synchSafeSizeBytes, true, t)
}
func TestParseSynchSafeSize(t *testing.T) {
testParseSize(synchSafeSizeUint, synchSafeSizeBytes, true, t)
}
func TestWriteSynchUnsafeSize(t *testing.T) {
testWriteSize(synchUnsafeSizeUint, synchUnsafeSizeBytes, false, t)
}
func TestParseSynchUnsafeSize(t *testing.T) {
testParseSize(synchUnsafeSizeUint, synchUnsafeSizeBytes, false, t)
}
func TestParseSynchUnsafeSizeUsingSynchSafeFlag(t *testing.T) {
t.Parallel()
_, err := parseSize(synchUnsafeSizeBytes, true)
if err == nil {
t.Fatal("Expected error, got nil")
}
if err != ErrInvalidSizeFormat {
t.Fatalf("Expected ErrInvalidSizeFormat, got %v", err)
}
}