Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnalyzerWaveformTest refactoring as a foundation for more tests #11488

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 38 additions & 49 deletions src/test/analyserwaveformtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,68 @@
#include "library/dao/analysisdao.h"
#include "track/track.h"

#define BIGBUF_SIZE (1024 * 1024) //Megabyte
#define CANARY_SIZE (1024 * 4)
#define MAGIC_FLOAT 1234.567890f
#define CANARY_FLOAT 0.0f

namespace {

constexpr std::size_t kBigBufSize = 1024 * 1024; // Megabyte
constexpr std::size_t kCanarySize = 1024 * 4;
constexpr float kMagicFloat = 1234.567890f;
constexpr float kCanaryFloat = 0.0f;

class AnalyzerWaveformTest : public MixxxTest {
protected:
AnalyzerWaveformTest()
: aw(config(), QSqlDatabase()),
bigbuf(nullptr),
canaryBigBuf(nullptr) {
: m_aw(config(), QSqlDatabase()),
m_canaryBigBuf(nullptr) {
}

void SetUp() override {
tio = Track::newTemporary();
tio->setAudioProperties(
m_pTrack = Track::newTemporary();
m_pTrack->setAudioProperties(
mixxx::audio::ChannelCount(2),
mixxx::audio::SampleRate(44100),
mixxx::audio::Bitrate(),
mixxx::Duration::fromMillis(1000));

bigbuf = new CSAMPLE[BIGBUF_SIZE];
for (int i = 0; i < BIGBUF_SIZE; i++)
bigbuf[i] = MAGIC_FLOAT;
// Memory layout for m_canaryBigBuf looks like
// [ canary | big buf | canary ]

//Memory layout for canaryBigBuf looks like
// [ canary | big buf | canary ]

canaryBigBuf = new CSAMPLE[BIGBUF_SIZE + 2 * CANARY_SIZE];
for (int i = 0; i < CANARY_SIZE; i++)
canaryBigBuf[i] = CANARY_FLOAT;
for (int i = CANARY_SIZE; i < CANARY_SIZE + BIGBUF_SIZE; i++)
canaryBigBuf[i] = MAGIC_FLOAT;
for (int i = CANARY_SIZE + BIGBUF_SIZE; i < 2 * CANARY_SIZE + BIGBUF_SIZE; i++)
canaryBigBuf[i] = CANARY_FLOAT;
m_canaryBigBuf = new CSAMPLE[kBigBufSize + 2 * kCanarySize];
for (std::size_t i = 0; i < kCanarySize; i++) {
m_canaryBigBuf[i] = kCanaryFloat;
}
for (std::size_t i = kCanarySize; i < kCanarySize + kBigBufSize; i++) {
m_canaryBigBuf[i] = kMagicFloat;
}
for (std::size_t i = kCanarySize + kBigBufSize; i < 2 * kCanarySize + kBigBufSize; i++) {
m_canaryBigBuf[i] = kCanaryFloat;
}
Comment on lines +37 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't these be memsets?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memset() works with char arrays only. We can do a lot more refactoring here, but I like to stop at this point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I forgot about that.

}

void TearDown() override {
delete[] bigbuf;
delete[] canaryBigBuf;
delete[] m_canaryBigBuf;
}

protected:
AnalyzerWaveform aw;
TrackPointer tio;
CSAMPLE* bigbuf;
CSAMPLE* canaryBigBuf;
AnalyzerWaveform m_aw;
TrackPointer m_pTrack;
CSAMPLE* m_canaryBigBuf;
};

//Test to make sure we don't modify the source buffer.
TEST_F(AnalyzerWaveformTest, simpleAnalyze) {
aw.initialize(tio, tio->getSampleRate(), BIGBUF_SIZE);
aw.processSamples(bigbuf, BIGBUF_SIZE);
aw.storeResults(tio);
aw.cleanup();
for (int i = 0; i < BIGBUF_SIZE; i++) {
EXPECT_FLOAT_EQ(bigbuf[i], MAGIC_FLOAT);
}
}

//Basic test to make sure we don't step out of bounds.
// Basic test to make sure we don't alter the input buffer and don't step out of bounds.
TEST_F(AnalyzerWaveformTest, canary) {
aw.initialize(tio, tio->getSampleRate(), BIGBUF_SIZE);
aw.processSamples(&canaryBigBuf[CANARY_SIZE], BIGBUF_SIZE);
aw.storeResults(tio);
aw.cleanup();
for (int i = 0; i < CANARY_SIZE; i++) {
EXPECT_FLOAT_EQ(canaryBigBuf[i], CANARY_FLOAT);
m_aw.initialize(m_pTrack, m_pTrack->getSampleRate(), kBigBufSize);
m_aw.processSamples(&m_canaryBigBuf[kCanarySize], kBigBufSize);
m_aw.storeResults(m_pTrack);
m_aw.cleanup();
std::size_t i = 0;
for (; i < kCanarySize; i++) {
EXPECT_FLOAT_EQ(m_canaryBigBuf[i], kCanaryFloat);
}
for (; i < kCanarySize + kBigBufSize; i++) {
EXPECT_FLOAT_EQ(m_canaryBigBuf[i], kMagicFloat);
}
for (int i = CANARY_SIZE + BIGBUF_SIZE; i < 2 * CANARY_SIZE + BIGBUF_SIZE; i++) {
EXPECT_FLOAT_EQ(canaryBigBuf[i], CANARY_FLOAT);
for (; i < 2 * kCanarySize + kBigBufSize; i++) {
EXPECT_FLOAT_EQ(m_canaryBigBuf[i], kCanaryFloat);
}
}

Expand Down