forked from open-ephys/plugin-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataThreadTests.cpp
More file actions
125 lines (100 loc) · 3.3 KB
/
DataThreadTests.cpp
File metadata and controls
125 lines (100 loc) · 3.3 KB
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
#include "gtest/gtest.h"
#include <DataThreadHeaders.h>
#include <TestFixtures.h>
class FakeDataThread : public DataThread
{
public:
FakeDataThread(SourceNode* sn) :
DataThread(sn)
{}
~FakeDataThread() noexcept override = default;
bool updateBuffer() override
{
return false;
}
bool foundInputSource() override
{
return false;
}
bool startAcquisition() override
{
return true;
}
bool stopAcquisition() override
{
return true;
}
void updateSettings(
OwnedArray<ContinuousChannel>* continuousChannels,
OwnedArray<EventChannel>* eventChannels,
OwnedArray<SpikeChannel>* spikeChannels,
OwnedArray<DataStream>* sourceStreams,
OwnedArray<DeviceInfo>* devices,
OwnedArray<ConfigurationObject>* configurationObjects) override
{}
};
class DataThreadTests : public testing::Test
{
protected:
void SetUp() override
{
tester = std::make_unique<DataThreadTester>(TestSourceNodeBuilder(FakeSourceNodeParams{}));
processor = tester->createDataThread<FakeDataThread>();
}
AudioBuffer<float> createBuffer(float startingValue, float step, int numChannels, int numSamples)
{
AudioBuffer<float> inputBuffer(numChannels, numSamples);
// in microvolts
float curValue = startingValue;
for (int chidx = 0; chidx < numChannels; chidx++)
{
for (int sample_idx = 0; sample_idx < numSamples; sample_idx++)
{
inputBuffer.setSample(chidx, sample_idx, curValue);
curValue += step;
}
}
return inputBuffer;
}
void writeBlock(AudioBuffer<float>& buffer)
{
auto audioProcessor = (AudioProcessor*)tester->getSourceNode();
auto dataStreams = tester->getSourceNode()->getDataStreams();
ASSERT_EQ(dataStreams.size(), 1);
auto streamId = dataStreams[0]->getStreamId();
HeapBlock<char> data;
size_t dataSize = SystemEvent::fillTimestampAndSamplesData(
data,
tester->getSourceNode(),
streamId,
currentSampleIndex,
0,
buffer.getNumSamples(),
0);
MidiBuffer eventBuffer;
eventBuffer.addEvent(data, dataSize, 0);
auto originalBuffer = buffer;
audioProcessor->processBlock(buffer, eventBuffer);
// Assert the buffer hasn't changed after process()
ASSERT_EQ(buffer.getNumSamples(), originalBuffer.getNumSamples());
ASSERT_EQ(buffer.getNumChannels(), originalBuffer.getNumChannels());
for (int chidx = 0; chidx < buffer.getNumChannels(); chidx++)
{
for (int sampleIdx = 0; sampleIdx < buffer.getNumSamples(); ++sampleIdx)
ASSERT_EQ(buffer.getSample(chidx, sampleIdx), originalBuffer.getSample(chidx, sampleIdx));
}
currentSampleIndex += buffer.getNumSamples();
}
protected:
std::unique_ptr<DataThreadTester> tester;
FakeDataThread* processor;
int64_t currentSampleIndex = 0;
};
TEST_F(DataThreadTests, DataIntegrity)
{
processor->startAcquisition();
int numSamples = 100;
auto inputBuffer = createBuffer(1000.0, 20.0, 5, numSamples);
writeBlock(inputBuffer);
processor->stopAcquisition();
}