2010-03-15 15:11:48 -04:00
|
|
|
#include "tests/utils.h"
|
|
|
|
|
|
|
|
#include "audiographer/general/threader.h"
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
samples = 128;
|
|
|
|
random_data = TestUtils::init_random_data (samples, 1.0);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
zero_data = new float[samples];
|
|
|
|
memset (zero_data, 0, samples * sizeof(float));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
thread_pool = new Glib::ThreadPool (3);
|
|
|
|
threader.reset (new Threader<float> (*thread_pool));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05: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
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
throwing_sink.reset (new ThrowingSink<float>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
delete [] random_data;
|
|
|
|
delete [] zero_data;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05: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
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
threader->process (c);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -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));
|
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 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
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
threader->process (c);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05: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
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> zc (zero_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
threader->process (zc);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -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));
|
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 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
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
threader->process (c);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2009-12-27 09:46:23 -05:00
|
|
|
threader->clear_outputs();
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> zc (zero_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
threader->process (zc);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -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));
|
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 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
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
ProcessContext<float> c (random_data, samples, 1);
|
2009-12-27 09:46:23 -05:00
|
|
|
CPPUNIT_ASSERT_THROW (threader->process (c), Exception);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2017-09-18 12:39:17 -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));
|
2009-12-27 09:46:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Glib::ThreadPool * thread_pool;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-02-16 18:33:28 -05: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;
|
2009-12-27 09:46:23 -05:00
|
|
|
|
|
|
|
float * random_data;
|
|
|
|
float * zero_data;
|
2017-09-18 12:39:17 -04:00
|
|
|
samplecnt_t samples;
|
2009-12-27 09:46:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (ThreaderTest);
|
|
|
|
|