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
|
#include <gmock/gmock.h>
#include <QtDebug>
#include "test/mixxxtest.h"
#include "util/circularsamplebuffer.h"
class CircularSampleBufferTest: public MixxxTest {
public:
CircularSampleBufferTest()
: m_writeValue(CSAMPLE_ZERO),
m_readValue(CSAMPLE_ZERO) {
}
protected:
static const SINT kCapacity;
SINT writeTail(CircularSampleBuffer* pSampleBuffer, SINT size) {
const SampleBuffer::WritableChunk writableChunk(
pSampleBuffer->writeToTail(size));
for (SINT i = 0; i < writableChunk.size(); ++i) {
writableChunk[i] = m_writeValue;
m_writeValue += CSAMPLE_ONE;
}
return writableChunk.size();
}
SINT readHeadAndVerify(CircularSampleBuffer* pSampleBuffer, SINT size) {
const SampleBuffer::ReadableChunk readableChunk(
pSampleBuffer->readFromHead(size));
for (SINT i = 0; i < readableChunk.size(); ++i) {
EXPECT_EQ(readableChunk[i], m_readValue);
m_readValue += CSAMPLE_ONE;
}
return readableChunk.size();
}
SINT readTailAndVerify(CircularSampleBuffer* pSampleBuffer, SINT size) {
const SampleBuffer::ReadableChunk readableChunk(
pSampleBuffer->readFromTail(size));
for (SINT i = readableChunk.size(); i-- > 0; ) {
m_writeValue -= CSAMPLE_ONE;
EXPECT_EQ(readableChunk[i], m_writeValue);
}
return readableChunk.size();
}
void reset(CircularSampleBuffer* pSampleBuffer) {
pSampleBuffer->reset();
m_writeValue = CSAMPLE_ZERO;
m_readValue = CSAMPLE_ZERO;
}
private:
CSAMPLE m_writeValue;
CSAMPLE m_readValue;
};
const SINT CircularSampleBufferTest::kCapacity = 100;
TEST_F(CircularSampleBufferTest, emptyWithoutCapacity) {
CircularSampleBuffer sampleBuffer;
EXPECT_TRUE(sampleBuffer.isEmpty());
sampleBuffer.trim();
EXPECT_TRUE(sampleBuffer.isEmpty());
sampleBuffer.reset();
EXPECT_TRUE(sampleBuffer.isEmpty());
const SampleBuffer::WritableChunk writableChunk(
sampleBuffer.writeToTail(10));
EXPECT_EQ(writableChunk.data(), static_cast<CSAMPLE*>(NULL));
EXPECT_EQ(writableChunk.size(), 0);
EXPECT_TRUE(sampleBuffer.isEmpty());
const SampleBuffer::ReadableChunk readableChunk(
sampleBuffer.readFromHead(10));
EXPECT_EQ(readableChunk.data(), static_cast<const CSAMPLE*>(NULL));
EXPECT_EQ(readableChunk.size(), 0);
EXPECT_TRUE(sampleBuffer.isEmpty());
}
TEST_F(CircularSampleBufferTest, emptyWithCapacity) {
CircularSampleBuffer sampleBuffer(kCapacity);
EXPECT_TRUE(sampleBuffer.isEmpty());
sampleBuffer.trim();
EXPECT_TRUE(sampleBuffer.isEmpty());
sampleBuffer.reset();
EXPECT_TRUE(sampleBuffer.isEmpty());
}
TEST_F(CircularSampleBufferTest, readWriteTrim) {
CircularSampleBuffer sampleBuffer(kCapacity);
SINT writeCount1 = writeTail(&sampleBuffer, kCapacity + 10);
EXPECT_EQ(writeCount1, kCapacity); // buffer is full
EXPECT_FALSE(sampleBuffer.isEmpty());
EXPECT_EQ(sampleBuffer.getTailCapacity(), 0);
SINT readCount1 = readHeadAndVerify(&sampleBuffer, kCapacity - 10);
EXPECT_EQ(readCount1, kCapacity - 10);
EXPECT_FALSE(sampleBuffer.isEmpty());
EXPECT_EQ(sampleBuffer.getTailCapacity(), 0);
SINT writeCount2 = writeTail(&sampleBuffer, kCapacity);
EXPECT_EQ(writeCount2, 0); // no tail capacity left
EXPECT_FALSE(sampleBuffer.isEmpty());
sampleBuffer.trim();
EXPECT_EQ(sampleBuffer.getTailCapacity(), readCount1);
SINT writeCount3 = writeTail(&sampleBuffer, kCapacity);
EXPECT_EQ(writeCount3, readCount1); // buffer has been refilled
EXPECT_FALSE(sampleBuffer.isEmpty());
SINT readCount2 = readHeadAndVerify(&sampleBuffer, kCapacity - 10);
EXPECT_EQ(readCount2, kCapacity - 10);
EXPECT_FALSE(sampleBuffer.isEmpty());
EXPECT_EQ(sampleBuffer.getTailCapacity(), 0);
sampleBuffer.trim();
EXPECT_EQ(sampleBuffer.getTailCapacity(), readCount2);
SINT writeCount4 = writeTail(&sampleBuffer, kCapacity);
EXPECT_EQ(writeCount4, readCount2); // buffer has been refilled
EXPECT_FALSE(sampleBuffer.isEmpty());
SINT readCount3 = readHeadAndVerify(&sampleBuffer, kCapacity + 10);
EXPECT_EQ(readCount3, kCapacity); // whole buffer has been read
EXPECT_TRUE(sampleBuffer.isEmpty());
}
TEST_F(CircularSampleBufferTest, shrink) {
CircularSampleBuffer sampleBuffer(kCapacity);
SINT writeCount1 = writeTail(&sampleBuffer, kCapacity - 10);
EXPECT_EQ(writeCount1, kCapacity - 10);
EXPECT_FALSE(sampleBuffer.isEmpty());
SINT shrinkCount1 = readTailAndVerify(&sampleBuffer, 10);
EXPECT_EQ(shrinkCount1, 10);
SINT readCount1 = readHeadAndVerify(&sampleBuffer, 10);
EXPECT_EQ(readCount1, 10);
EXPECT_FALSE(sampleBuffer.isEmpty());
SINT readCount2 = readHeadAndVerify(&sampleBuffer, kCapacity - 40);
EXPECT_EQ(readCount2, kCapacity - 40);
EXPECT_FALSE(sampleBuffer.isEmpty());
SINT readCount3 = readHeadAndVerify(&sampleBuffer, 20);
EXPECT_EQ(readCount3, 10);
EXPECT_TRUE(sampleBuffer.isEmpty());
SINT writeCount2 = writeTail(&sampleBuffer, 20);
EXPECT_EQ(writeCount2, 20);
EXPECT_FALSE(sampleBuffer.isEmpty());
SINT shrinkCount2 = readTailAndVerify(&sampleBuffer, 21);
EXPECT_EQ(shrinkCount2, 20);
EXPECT_TRUE(sampleBuffer.isEmpty());
}
TEST_F(CircularSampleBufferTest, reset) {
CircularSampleBuffer sampleBuffer(kCapacity);
SINT writeCount = writeTail(&sampleBuffer, 10);
EXPECT_EQ(writeCount, 10);
EXPECT_FALSE(sampleBuffer.isEmpty());
reset(&sampleBuffer);
EXPECT_TRUE(sampleBuffer.isEmpty());
SINT readCount = readHeadAndVerify(&sampleBuffer, 10);
EXPECT_EQ(readCount, 0);
EXPECT_TRUE(sampleBuffer.isEmpty());
}
|