summaryrefslogtreecommitdiff
path: root/libs/audiographer/tests/interleaver_test.cc
blob: abe385699d84a3bfc7fa0cba0c338722e5dcea93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "utils.h"
#include "audiographer/interleaver.h"

using namespace AudioGrapher;

class InterleaverTest : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE (InterleaverTest);
  CPPUNIT_TEST (testUninitialized);
  CPPUNIT_TEST (testInvalidInputIndex);
  CPPUNIT_TEST (testInvalidInputSize);
  CPPUNIT_TEST (testOutputSize);
  CPPUNIT_TEST (testZeroInput);
  CPPUNIT_TEST (testChannelSync);
  CPPUNIT_TEST_SUITE_END ();

  public:
	void setUp()
	{
		channels = 3;
		frames = 128;
		random_data = TestUtils::init_random_data (frames, 1.0);

		interleaver.reset (new Interleaver<float>());
		sink.reset (new VectorSink<float>());
		
		interleaver->init (channels, frames);
	}

	void tearDown()
	{
		delete [] random_data;
	}

	void testUninitialized()
	{
		interleaver.reset (new Interleaver<float>());
		ProcessContext<float> c (random_data, frames, 1);
		CPPUNIT_ASSERT_THROW (interleaver->input(0)->process (c), Exception);
	}

	void testInvalidInputIndex()
	{
		ProcessContext<float> c (random_data, frames, 1);
		CPPUNIT_ASSERT_THROW (interleaver->input (3)->process (c), Exception);
	}

	void testInvalidInputSize()
	{
		ProcessContext<float> c (random_data, frames + 1, 1);
		CPPUNIT_ASSERT_THROW (interleaver->input (0)->process (c), Exception);
		
		c.frames() = frames;
		interleaver->input (0)->process (c);
		interleaver->input (1)->process (c);
		c.frames() = frames -1;
		CPPUNIT_ASSERT_THROW (interleaver->input (2)->process (c), Exception);

		interleaver->input (0)->process (c);
		interleaver->input (1)->process (c);
		c.frames() = frames;
		CPPUNIT_ASSERT_THROW (interleaver->input (2)->process (c), Exception);
	}

	void testOutputSize()
	{
		interleaver->add_output (sink);

		ProcessContext<float> c (random_data, frames, 1);
		interleaver->input (0)->process (c);
		interleaver->input (1)->process (c);
		interleaver->input (2)->process (c);

		nframes_t expected_frames = frames * channels;
		nframes_t generated_frames = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (expected_frames, generated_frames);

		nframes_t less_frames = frames / 2;
		c.frames() = less_frames;
		interleaver->input (0)->process (c);
		interleaver->input (1)->process (c);
		interleaver->input (2)->process (c);

		expected_frames = less_frames * channels;
		generated_frames = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (expected_frames, generated_frames);
	}

	void testZeroInput()
	{
		interleaver->add_output (sink);

		// input zero frames to all inputs
		ProcessContext<float> c (random_data, 0, 1);
		interleaver->input (0)->process (c);
		interleaver->input (1)->process (c);
		interleaver->input (2)->process (c);
		
		// NOTE zero input is allowed to be a NOP
		
		// ...now test regular input
		c.frames() = frames;
		interleaver->input (0)->process (c);
		interleaver->input (1)->process (c);
		interleaver->input (2)->process (c);

		nframes_t expected_frames = frames * channels;
		nframes_t generated_frames = sink->get_data().size();
		CPPUNIT_ASSERT_EQUAL (expected_frames, generated_frames);
	}

	void testChannelSync()
	{
		interleaver->add_output (sink);
		ProcessContext<float> c (random_data, frames, 1);
		interleaver->input (0)->process (c);
		CPPUNIT_ASSERT_THROW (interleaver->input (0)->process (c), Exception);		
	}


  private:
	boost::shared_ptr<Interleaver<float> > interleaver;

	boost::shared_ptr<VectorSink<float> > sink;

	nframes_t channels;
	float * random_data;
	nframes_t frames;
};

CPPUNIT_TEST_SUITE_REGISTRATION (InterleaverTest);