13
0
livetrax/libs/audiographer/tests/general/threader_test.cc

157 lines
5.0 KiB
C++
Raw Normal View History

#include "tests/utils.h"
#include "audiographer/general/threader.h"
using namespace AudioGrapher;
class ThreaderTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE (ThreaderTest);
CPPUNIT_TEST (testProcess);
CPPUNIT_TEST (testRemoveOutput);
CPPUNIT_TEST (testClearOutputs);
CPPUNIT_TEST (testExceptions);
CPPUNIT_TEST_SUITE_END ();
public:
void setUp()
{
samples = 128;
random_data = TestUtils::init_random_data (samples, 1.0);
2015-10-05 10:17:49 -04:00
zero_data = new float[samples];
memset (zero_data, 0, samples * sizeof(float));
2015-10-05 10:17:49 -04:00
thread_pool = new Glib::ThreadPool (3);
threader.reset (new Threader<float> (*thread_pool));
2015-10-05 10:17:49 -04:00
sink_a.reset (new VectorSink<float>());
sink_b.reset (new VectorSink<float>());
sink_c.reset (new VectorSink<float>());
sink_d.reset (new VectorSink<float>());
sink_e.reset (new VectorSink<float>());
sink_f.reset (new VectorSink<float>());
2015-10-05 10:17:49 -04:00
throwing_sink.reset (new ThrowingSink<float>());
}
void tearDown()
{
delete [] random_data;
delete [] zero_data;
2015-10-05 10:17:49 -04:00
thread_pool->shutdown();
delete thread_pool;
}
void testProcess()
{
threader->add_output (sink_a);
threader->add_output (sink_b);
threader->add_output (sink_c);
threader->add_output (sink_d);
threader->add_output (sink_e);
threader->add_output (sink_f);
2015-10-05 10:17:49 -04:00
ProcessContext<float> c (random_data, samples, 1);
threader->process (c);
2015-10-05 10:17:49 -04:00
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_a->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_b->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_c->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_d->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_e->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_f->get_array(), samples));
}
2015-10-05 10:17:49 -04:00
void testRemoveOutput()
{
threader->add_output (sink_a);
threader->add_output (sink_b);
threader->add_output (sink_c);
threader->add_output (sink_d);
threader->add_output (sink_e);
threader->add_output (sink_f);
2015-10-05 10:17:49 -04:00
ProcessContext<float> c (random_data, samples, 1);
threader->process (c);
2015-10-05 10:17:49 -04:00
// Remove a, b and f
threader->remove_output (sink_a);
threader->remove_output (sink_b);
threader->remove_output (sink_f);
2015-10-05 10:17:49 -04:00
ProcessContext<float> zc (zero_data, samples, 1);
threader->process (zc);
2015-10-05 10:17:49 -04:00
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_a->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_b->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(zero_data, sink_c->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(zero_data, sink_d->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(zero_data, sink_e->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_f->get_array(), samples));
}
2015-10-05 10:17:49 -04:00
void testClearOutputs()
{
threader->add_output (sink_a);
threader->add_output (sink_b);
threader->add_output (sink_c);
threader->add_output (sink_d);
threader->add_output (sink_e);
threader->add_output (sink_f);
2015-10-05 10:17:49 -04:00
ProcessContext<float> c (random_data, samples, 1);
threader->process (c);
2015-10-05 10:17:49 -04:00
threader->clear_outputs();
ProcessContext<float> zc (zero_data, samples, 1);
threader->process (zc);
2015-10-05 10:17:49 -04:00
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_a->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_b->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_c->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_d->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_e->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_f->get_array(), samples));
}
2015-10-05 10:17:49 -04:00
void testExceptions()
{
threader->add_output (sink_a);
threader->add_output (sink_b);
threader->add_output (sink_c);
threader->add_output (throwing_sink);
threader->add_output (sink_e);
threader->add_output (throwing_sink);
2015-10-05 10:17:49 -04:00
ProcessContext<float> c (random_data, samples, 1);
CPPUNIT_ASSERT_THROW (threader->process (c), Exception);
2015-10-05 10:17:49 -04:00
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_a->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_b->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_c->get_array(), samples));
CPPUNIT_ASSERT (TestUtils::array_equals(random_data, sink_e->get_array(), samples));
}
private:
Glib::ThreadPool * thread_pool;
2015-10-05 10:17:49 -04:00
std::shared_ptr<Threader<float> > threader;
std::shared_ptr<VectorSink<float> > sink_a;
std::shared_ptr<VectorSink<float> > sink_b;
std::shared_ptr<VectorSink<float> > sink_c;
std::shared_ptr<VectorSink<float> > sink_d;
std::shared_ptr<VectorSink<float> > sink_e;
std::shared_ptr<VectorSink<float> > sink_f;
std::shared_ptr<ThrowingSink<float> > throwing_sink;
float * random_data;
float * zero_data;
samplecnt_t samples;
};
CPPUNIT_TEST_SUITE_REGISTRATION (ThreaderTest);