Re-integrate export-optimization branch.
[ardour.git] / libs / audiographer / tests / interleaver_test.cc
1 #include "utils.h"
2 #include "audiographer/interleaver.h"
3
4 using namespace AudioGrapher;
5
6 class InterleaverTest : public CppUnit::TestFixture
7 {
8   CPPUNIT_TEST_SUITE (InterleaverTest);
9   CPPUNIT_TEST (testUninitialized);
10   CPPUNIT_TEST (testInvalidInputIndex);
11   CPPUNIT_TEST (testInvalidInputSize);
12   CPPUNIT_TEST (testOutputSize);
13   CPPUNIT_TEST (testZeroInput);
14   CPPUNIT_TEST (testChannelSync);
15   CPPUNIT_TEST_SUITE_END ();
16
17   public:
18         void setUp()
19         {
20                 channels = 3;
21                 frames = 128;
22                 random_data = TestUtils::init_random_data (frames, 1.0);
23
24                 interleaver.reset (new Interleaver<float>());
25                 sink.reset (new VectorSink<float>());
26                 
27                 interleaver->init (channels, frames);
28         }
29
30         void tearDown()
31         {
32                 delete [] random_data;
33         }
34
35         void testUninitialized()
36         {
37                 interleaver.reset (new Interleaver<float>());
38                 ProcessContext<float> c (random_data, frames, 1);
39                 CPPUNIT_ASSERT_THROW (interleaver->input(0)->process (c), Exception);
40         }
41
42         void testInvalidInputIndex()
43         {
44                 ProcessContext<float> c (random_data, frames, 1);
45                 CPPUNIT_ASSERT_THROW (interleaver->input (3)->process (c), Exception);
46         }
47
48         void testInvalidInputSize()
49         {
50                 ProcessContext<float> c (random_data, frames + 1, 1);
51                 CPPUNIT_ASSERT_THROW (interleaver->input (0)->process (c), Exception);
52                 
53                 c.frames() = frames;
54                 interleaver->input (0)->process (c);
55                 interleaver->input (1)->process (c);
56                 c.frames() = frames -1;
57                 CPPUNIT_ASSERT_THROW (interleaver->input (2)->process (c), Exception);
58
59                 interleaver->input (0)->process (c);
60                 interleaver->input (1)->process (c);
61                 c.frames() = frames;
62                 CPPUNIT_ASSERT_THROW (interleaver->input (2)->process (c), Exception);
63         }
64
65         void testOutputSize()
66         {
67                 interleaver->add_output (sink);
68
69                 ProcessContext<float> c (random_data, frames, 1);
70                 interleaver->input (0)->process (c);
71                 interleaver->input (1)->process (c);
72                 interleaver->input (2)->process (c);
73
74                 nframes_t expected_frames = frames * channels;
75                 nframes_t generated_frames = sink->get_data().size();
76                 CPPUNIT_ASSERT_EQUAL (expected_frames, generated_frames);
77
78                 nframes_t less_frames = frames / 2;
79                 c.frames() = less_frames;
80                 interleaver->input (0)->process (c);
81                 interleaver->input (1)->process (c);
82                 interleaver->input (2)->process (c);
83
84                 expected_frames = less_frames * channels;
85                 generated_frames = sink->get_data().size();
86                 CPPUNIT_ASSERT_EQUAL (expected_frames, generated_frames);
87         }
88
89         void testZeroInput()
90         {
91                 interleaver->add_output (sink);
92
93                 // input zero frames to all inputs
94                 ProcessContext<float> c (random_data, 0, 1);
95                 interleaver->input (0)->process (c);
96                 interleaver->input (1)->process (c);
97                 interleaver->input (2)->process (c);
98                 
99                 // NOTE zero input is allowed to be a NOP
100                 
101                 // ...now test regular input
102                 c.frames() = frames;
103                 interleaver->input (0)->process (c);
104                 interleaver->input (1)->process (c);
105                 interleaver->input (2)->process (c);
106
107                 nframes_t expected_frames = frames * channels;
108                 nframes_t generated_frames = sink->get_data().size();
109                 CPPUNIT_ASSERT_EQUAL (expected_frames, generated_frames);
110         }
111
112         void testChannelSync()
113         {
114                 interleaver->add_output (sink);
115                 ProcessContext<float> c (random_data, frames, 1);
116                 interleaver->input (0)->process (c);
117                 CPPUNIT_ASSERT_THROW (interleaver->input (0)->process (c), Exception);          
118         }
119
120
121   private:
122         boost::shared_ptr<Interleaver<float> > interleaver;
123
124         boost::shared_ptr<VectorSink<float> > sink;
125
126         nframes_t channels;
127         float * random_data;
128         nframes_t frames;
129 };
130
131 CPPUNIT_TEST_SUITE_REGISTRATION (InterleaverTest);
132