-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfifo_test.go
114 lines (109 loc) · 2.56 KB
/
fifo_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
package tta
import (
"testing"
)
func TestReadByte(t *testing.T) {
t.Parallel()
fifo := fifo{}
for i := 0; i < fifoBufferSize; i++ {
fifo.buffer[i] = byte(i)
}
fifo.pos = 0
fifo.end = fifoBufferSize
for i := 0; i < fifoBufferSize; i++ {
if fifo.readByte() != byte(i&0xFF) {
t.Errorf("readByte fail @ %d\n", i)
}
if fifo.count != uint32(i+1) {
t.Errorf("readByte fail @ count - %d\n", i)
}
if fifo.pos != int32(i+1) {
t.Errorf("readByte fail @ pos - %d\n", i)
}
}
if fifo.count != fifoBufferSize {
t.Error("readByte fail @ count")
}
if fifo.pos != fifoBufferSize {
t.Error("readByte fail @ pos")
}
}
func TestReadUint16(t *testing.T) {
t.Parallel()
fifo := fifo{}
for i := 0; i < fifoBufferSize; i++ {
fifo.buffer[i] = byte(i)
}
fifo.pos = 0
fifo.end = fifoBufferSize
var v uint16
for i := 0; i < fifoBufferSize/2; i++ {
v = uint16((i*2+1)<<8) | (uint16(i*2) & 0xFF)
if fifo.readUint16() != v {
t.Errorf("readUint16 fail @ %d\n", i)
}
if fifo.count != uint32(i*2+2) {
t.Errorf("readUint16 fail @ count - %d\n", i)
}
if fifo.pos != int32(i*2+2) {
t.Errorf("readUint16 fail @ pos - %d\n", i)
}
}
if fifo.count != fifoBufferSize {
t.Error("readUint16 fail @ count")
}
if fifo.pos != fifoBufferSize {
t.Error("readUint16 fail @ pos")
}
}
func TestReadUint32(t *testing.T) {
t.Parallel()
fifo := fifo{}
for i := 0; i < fifoBufferSize; i++ {
fifo.buffer[i] = byte(i)
}
fifo.pos = 0
fifo.end = fifoBufferSize
var v uint32
for i := 0; i < fifoBufferSize/4; i++ {
if fifo.count != uint32(i*4) {
t.Errorf("readUint32 fail @ count - %d\n", i)
}
if fifo.pos != int32(i*4) {
t.Errorf("readUint32 fail @ pos - %d\n", i)
}
v = uint32((i*4+3)<<24&0xFF000000) | uint32((i*4+2)<<16&0xFF0000) | uint32((i*4+1)<<8&0xFF00) | (uint32(i*4) & 0xFF)
if fifo.readUint32() != v {
t.Errorf("readUint32 fail @ %d\n", i)
}
}
if fifo.count != fifoBufferSize {
t.Error("readUint32 fail @ count")
}
if fifo.pos != fifoBufferSize {
t.Error("readUint32 fail @ pos")
}
}
func TestWriteByte(t *testing.T) {
t.Parallel()
fifo := fifo{}
fifo.pos = 0
fifo.end = fifoBufferSize
for i := 0; i < fifoBufferSize; i++ {
if err := fifo.writeByte(byte(i)); err != nil {
t.Errorf("writeByte fail @ %d, %v\n", i, err)
}
if fifo.count != uint32(i+1) {
t.Errorf("writeByte fail @ count - %d\n", i)
}
if fifo.pos != int32(i+1) {
t.Errorf("writeByte fail @ pos - %d\n", i)
}
}
if fifo.count != fifoBufferSize {
t.Error("writeByte fail @ count")
}
if fifo.pos != fifoBufferSize {
t.Error("writeByte fail @ pos")
}
}