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);
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END ();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
frames = 128;
|
|
|
|
random_data = TestUtils::init_random_data(frames);
|
|
|
|
sink.reset (new VectorSink<float>());
|
|
|
|
chunker.reset (new Chunker<float>(frames * 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
delete [] random_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSynchronousProcess()
|
|
|
|
{
|
|
|
|
chunker->add_output (sink);
|
2010-12-03 17:26:29 -05:00
|
|
|
framecnt_t frames_output = 0;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
ProcessContext<float> const context (random_data, frames, 1);
|
|
|
|
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) 0, frames_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * frames, frames_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), frames));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[frames], frames));
|
|
|
|
|
|
|
|
sink->reset();
|
|
|
|
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) 0, frames_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * frames, frames_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), frames));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[frames], frames));
|
|
|
|
}
|
|
|
|
|
|
|
|
void testAsynchronousProcess()
|
|
|
|
{
|
|
|
|
assert (frames % 2 == 0);
|
|
|
|
|
|
|
|
chunker->add_output (sink);
|
2010-12-03 17:26:29 -05:00
|
|
|
framecnt_t frames_output = 0;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
ProcessContext<float> const half_context (random_data, frames / 2, 1);
|
|
|
|
ProcessContext<float> const context (random_data, frames, 1);
|
|
|
|
|
|
|
|
// 0.5
|
|
|
|
chunker->process (half_context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) 0, frames_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
// 1.5
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) 0, frames_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
// 2.5
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * frames, frames_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), frames / 2));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[frames / 2], frames));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[ 3 * frames / 2], frames / 2));
|
|
|
|
|
|
|
|
sink->reset();
|
|
|
|
|
|
|
|
// 3.5
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) 0, frames_output);
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
// 4.0
|
|
|
|
chunker->process (half_context);
|
|
|
|
frames_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2 * frames, frames_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (&random_data[frames / 2], sink->get_array(), frames / 2));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[frames / 2], frames));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[ 3 * frames / 2], frames / 2));
|
|
|
|
}
|
2010-03-15 15:11:48 -04:00
|
|
|
|
|
|
|
void testChoppingProcess()
|
|
|
|
{
|
|
|
|
sink.reset (new AppendingVectorSink<float>());
|
|
|
|
|
|
|
|
assert (frames % 2 == 0);
|
|
|
|
chunker.reset (new Chunker<float>(frames / 4));
|
|
|
|
|
|
|
|
chunker->add_output (sink);
|
2010-12-03 17:26:29 -05:00
|
|
|
framecnt_t frames_output = 0;
|
2010-03-15 15:11:48 -04:00
|
|
|
|
|
|
|
ProcessContext<float> const half_context (random_data, frames / 2, 1);
|
|
|
|
ProcessContext<float> const context (random_data, frames, 1);
|
|
|
|
|
|
|
|
// 0.5
|
|
|
|
chunker->process (half_context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) frames / 2, frames_output);
|
2010-03-15 15:11:48 -04:00
|
|
|
|
|
|
|
// 1.5
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
2010-12-03 17:26:29 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL ((framecnt_t) frames / 2 * 3, frames_output);
|
2010-03-15 15:11:48 -04:00
|
|
|
|
|
|
|
// 2.5
|
|
|
|
chunker->process (context);
|
|
|
|
frames_output = sink->get_data().size();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (frames / 2 * 5, frames_output);
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, sink->get_array(), frames / 2));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[frames / 2], frames));
|
|
|
|
CPPUNIT_ASSERT (TestUtils::array_equals (random_data, &sink->get_array()[ 3 * frames / 2], frames / 2));
|
|
|
|
}
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
boost::shared_ptr<Chunker<float> > chunker;
|
|
|
|
boost::shared_ptr<VectorSink<float> > sink;
|
|
|
|
|
|
|
|
float * random_data;
|
2010-12-03 17:26:29 -05:00
|
|
|
framecnt_t frames;
|
2009-12-27 09:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (ChunkerTest);
|
|
|
|
|