forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
108 lines (90 loc) · 2.43 KB
/
reader_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
// 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"
"io"
"testing"
)
var (
bs = []byte{0, 11, 22, 33, 44, 55, 77, 88, 55, 55, 66, 77, 88}
)
func TestReadTillDelim(t *testing.T) {
t.Parallel()
bsReader := newReader(bytes.NewReader(bs))
firstIndexOf55 := bytes.Index(bs, []byte{55})
if firstIndexOf55 < 0 {
t.Fatal("Can't find 55 in bs")
}
expected := bs[:firstIndexOf55]
read, err := bsReader.ReadTillDelim(55)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expected, read) {
t.Errorf("Expected: %v, got: %v", expected, read)
}
if len(bs)-len(expected) != bsReader.buf.Buffered() {
t.Errorf("Expected buffered: %v, got: %v", len(bs)-len(expected), bsReader.buf.Buffered())
}
}
func TestReadTillZero(t *testing.T) {
t.Parallel()
bsReader := newReader(bytes.NewReader(bs))
read, err := bsReader.ReadTillDelim(0)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal([]byte{}, read) {
t.Errorf("Expected empty buffer, got %v", read)
}
if len(bs) != bsReader.buf.Buffered() {
t.Errorf("Expected buffered: %v, got: %v", len(bs), bsReader.buf.Buffered())
}
}
func TestNext(t *testing.T) {
t.Parallel()
bsReader := newReader(bytes.NewReader(bs))
n := 5 // Read 5 elements
read, err := bsReader.Next(n)
if err != nil {
t.Fatal(err)
}
if n != len(read) {
t.Errorf("Expected: %v, got: %v", n, read)
}
if !bytes.Equal(bs[:n], read) {
t.Errorf("Expected: %v, got: %v", bs[:n], len(read))
}
if len(bs)-n != bsReader.buf.Buffered() {
t.Errorf("Expected buffered: %v, got: %v", len(bs)-n, bsReader.buf.Buffered())
}
}
func TestReadTillDelimEOF(t *testing.T) {
t.Parallel()
bsReader := newReader(bytes.NewReader(bs))
_, err := bsReader.ReadTillDelim(234)
if err != io.EOF {
t.Errorf("Expected io.EOF, got %v", err)
}
}
func TestReadTillDelims(t *testing.T) {
t.Parallel()
bsReader := newReader(bytes.NewReader(bs))
delims := []byte{55, 66}
expectedLen := 9
read, err := bsReader.ReadTillDelims(delims)
if err != nil {
t.Fatal(err)
}
if expectedLen != len(read) {
t.Errorf("Expected: %v, got: %v", expectedLen, len(read))
}
if !bytes.Equal(bs[:expectedLen], read) {
t.Errorf("Expected: %v, got: %v", bs[:expectedLen], read)
}
if len(bs)-len(read) != bsReader.buf.Buffered() {
t.Errorf("Expected buffered: %v, got: %v", len(bs)-len(read), bsReader.buf.Buffered())
}
}