2010-03-15 15:11:48 -04:00
|
|
|
#include "tests/utils.h"
|
|
|
|
|
|
|
|
#include "audiographer/general/chunker.h"
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
using namespace AudioGrapher;
|
|
|
|
|
|
|
|
class ChunkerTest : public CppUnit::TestFixture
|
|
|
|
{
|
2010-03-15 15:11:48 -04:00
|
|
|
// TODO: Test EndOfInput handling
|
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_TEST_SUITE (ChunkerTest);
|
|
|
|
CPPUNIT_TEST (testSynchronousProcess);
|
|
|
|
CPPUNIT_TEST (testAsynchronousProcess);
|
2010-03-15 15:11:48 -04:00
|
|
|
CPPUNIT_TEST (testChoppingProcess);
|
2012-10-30 16:15:11 -04:00
|
|
|
CPPUNIT_TEST (testEndOfInputFlagHandling);
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END ();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
samples = 128;
|
|
|
|
random_data = TestUtils::init_random_data(samples);
|
2009-12-27 09:46:23 -05:00
|
|
|
sink.reset (new VectorSink<float>());
|
2017-09-18 12:39:17 -04:00
|
|
|
chunker.reset (new Chunker<float>(samples * 2));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
delete [] random_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSynchronousProcess()
|
|
|
|
{
|
|
|
|
chunker->add_output (sink);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> const context (random_data, samples, 1);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) 0, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[samples], samples));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
sink->reset();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) 0, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[samples], samples));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
void testAsynchronousProcess()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
chunker->add_output (sink);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> const half_context (random_data, samples / 2, 1);
|
|
|
|
ProcessContext<float> const context (random_data, samples, 1);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
// 0.5
|
|
|
|
chunker->process (half_context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) 0, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
// 1.5
|
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) 0, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
// 2.5
|
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), samples / 2));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[samples / 2], samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[ 3 * samples / 2], samples / 2));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
sink->reset();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
// 3.5
|
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) 0, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
// 4.0
|
|
|
|
chunker->process (half_context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * samples, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&random_data[samples / 2], sink->get_array(), samples / 2));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[samples / 2], samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[ 3 * samples / 2], samples / 2));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-03-15 15:11:48 -04:00
|
|
|
void testChoppingProcess()
|
|
|
|
{
|
|
|
|
sink.reset (new AppendingVectorSink<float>());
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
|
|
|
chunker.reset (new Chunker<float>(samples / 4));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-03-15 15:11:48 -04:00
|
|
|
chunker->add_output (sink);
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples_output = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> const half_context (random_data, samples / 2, 1);
|
|
|
|
ProcessContext<float> const context (random_data, samples, 1);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-03-15 15:11:48 -04:00
|
|
|
// 0.5
|
|
|
|
chunker->process (half_context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) samples / 2, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-03-15 15:11:48 -04:00
|
|
|
// 1.5
|
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL ((samplecnt_t) samples / 2 * 3, samples_output);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-03-15 15:11:48 -04:00
|
|
|
// 2.5
|
|
|
|
chunker->process (context);
|
2017-09-18 12:39:17 -04:00
|
|
|
samples_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (samples / 2 * 5, samples_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), samples / 2));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[samples / 2], samples));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[ 3 * samples / 2], samples / 2));
|
2010-03-15 15:11:48 -04:00
|
|
|
}
|
2009-12-27 09:46:23 -05:00
|
|
|
|
2012-10-30 16:15:11 -04:00
|
|
|
void testEndOfInputFlagHandling()
|
|
|
|
{
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<ProcessContextGrabber<float> > grabber(new ProcessContextGrabber<float>());
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
assert (samples % 2 == 0);
|
|
|
|
chunker.reset (new Chunker<float>(samples));
|
2012-10-30 16:15:11 -04:00
|
|
|
chunker->add_output (grabber);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> const half_context (random_data, samples / 2, 1);
|
|
|
|
ProcessContext<float> const context (random_data, samples, 1);
|
2012-10-30 16:15:11 -04:00
|
|
|
context.set_flag(ProcessContext<>::EndOfInput);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2012-10-30 16:15:11 -04:00
|
|
|
// Process 0.5 then 1.0
|
|
|
|
chunker->process (half_context);
|
|
|
|
chunker->process (context);
|
|
|
|
|
|
|
|
// Should output two contexts
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int)grabber->contexts.size(), 2);
|
|
|
|
ProcessContextGrabber<float>::ContextList::iterator it = grabber->contexts.begin();
|
|
|
|
|
|
|
|
// first 1.0 not end of input
|
2017-09-18 12:39:17 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL(it->samples(), samples);
|
2012-10-30 16:15:11 -04:00
|
|
|
CPPUNIT_ASSERT(!it->has_flag(ProcessContext<>::EndOfInput));
|
|
|
|
|
|
|
|
// Then 0.5 with end of input
|
|
|
|
++it;
|
2017-09-18 12:39:17 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL(it->samples(), samples / 2);
|
2012-10-30 16:15:11 -04:00
|
|
|
CPPUNIT_ASSERT(it->has_flag(ProcessContext<>::EndOfInput));
|
|
|
|
}
|
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
private:
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<Chunker<float> > chunker;
|
|
|
|
std::shared_ptr<VectorSink<float> > sink;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
float * random_data;
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples;
|
2009-12-27 09:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (ChunkerTest);
|
|
|
|
|