-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathfixedslicewriter_test.go
117 lines (114 loc) · 3.18 KB
/
fixedslicewriter_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
package bits_test
import (
"bytes"
"testing"
"github.com/Eyevinn/mp4ff/bits"
)
func TestFixedSliceWriter(t *testing.T) {
t.Run("Write uints and overflow", func(t *testing.T) {
sw := bits.NewFixedSliceWriter(20)
sw.WriteUint8(0xff)
sw.WriteUint16(0xffff)
sw.WriteUint24(0xffffff)
sw.WriteUint32(0xffffffff)
sw.WriteUint64(0xffffffffffffffff)
sw.WriteUint16(0)
expected := make([]byte, 20)
for i := range expected {
if i == 18 {
break
}
expected[i] = 0xff
}
if !bytes.Equal(expected, sw.Bytes()) {
t.Errorf("bytes differ: %v %v", expected, sw.Bytes())
}
sw.WriteUint24(0xffffff)
if sw.AccError() == nil {
t.Errorf("no overflow error")
}
// Write past end of slice (should not panic)
sw.WriteUint8(0xff)
sw.WriteUint16(0xffff)
sw.WriteUint32(0xffffffff)
sw.WriteUint48(0xffffffffffff)
sw.WriteUint64(0xffffffff00112233)
sw.WriteInt16(-1)
sw.WriteInt32(-1)
sw.WriteInt64(-1)
sw.WriteString("hello", true)
sw.WriteBytes([]byte{0x01, 0x02, 0x03})
sw.WriteZeroBytes(7)
sw.WriteUnityMatrix()
sw.WriteBits(0x0f, 4)
sw.FlushBits()
if sw.AccError() == nil {
t.Errorf("no overflow error")
}
})
t.Run("write signed", func(t *testing.T) {
s := make([]byte, 14)
sw := bits.NewFixedSliceWriterFromSlice(s)
sw.WriteInt16(-1)
sw.WriteInt32(-2)
sw.WriteInt64(-1)
if sw.AccError() != nil {
t.Errorf("unexpected error writing signed")
}
expected := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
if !bytes.Equal(expected, sw.Bytes()) {
t.Errorf("bytes differ: %v %v", expected, sw.Bytes())
}
})
t.Run("write other", func(t *testing.T) {
sw := bits.NewFixedSliceWriter(55)
sw.WriteUint48(0x123456789abc)
sw.WriteZeroBytes(3)
sw.WriteBytes([]byte{0x01, 0x02, 0x03})
sw.WriteString("hello", true)
sw.WriteUnityMatrix()
sw.WriteFlag(false)
sw.WriteFlag(true)
sw.FlushBits()
if sw.AccError() != nil {
t.Errorf("unexpected error writing other")
}
expected := []byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x40}
if !bytes.Equal(sw.Bytes(), expected) {
t.Errorf("bytes differ: %v %v", expected, sw.Bytes())
}
})
t.Run("write bits", func(t *testing.T) {
s := make([]byte, 4)
sw := bits.NewFixedSliceWriterFromSlice(s)
cap := sw.Capacity()
if cap != 4 {
t.Errorf("unexpected capacity %d", cap)
}
sw.WriteBits(0xf, 4)
sw.WriteBits(0x2, 4)
sw.WriteBits(0x5, 4)
sw.FlushBits()
if sw.AccError() != nil {
t.Errorf("unexpected error writing bits")
}
wantedOffset := 2
offset := sw.Offset()
if offset != wantedOffset {
t.Errorf("unexpected offset %d instead of %d", offset, wantedOffset)
}
result := sw.Bytes()
if !(result[0] == 0xf2 && result[1] == 0x50) {
t.Errorf("got %02x%02x instead of 0xf250", result[0], result[1])
}
sw.WriteUint16(0xffff)
if sw.Len() != 4 {
t.Errorf("unexpected length %d after 4 bytes", sw.Len())
}
})
}