finalize PROGRAM_NAME change for ardour3
[ardour.git] / libs / audiographer / tests / sample_format_converter_test.cc
1 #include "utils.h"
2 #include "audiographer/sample_format_converter.h"
3
4 using namespace AudioGrapher;
5
6 class SampleFormatConverterTest : public CppUnit::TestFixture
7 {
8   CPPUNIT_TEST_SUITE (SampleFormatConverterTest);
9   CPPUNIT_TEST (testInit);
10   CPPUNIT_TEST (testFrameCount);
11   CPPUNIT_TEST (testFloat);
12   CPPUNIT_TEST (testInt32);
13   CPPUNIT_TEST (testInt24);
14   CPPUNIT_TEST (testInt16);
15   CPPUNIT_TEST (testUint8);
16   CPPUNIT_TEST (testChannelCount);
17   CPPUNIT_TEST_SUITE_END ();
18
19   public:
20         void setUp()
21         {
22                 frames = 128;
23                 random_data = TestUtils::init_random_data(frames, 1.0);
24         }
25
26         void tearDown()
27         {
28                 delete [] random_data;
29         }
30
31         void testInit()
32         {
33                 boost::shared_ptr<SampleFormatConverter<float> > f_converter (new SampleFormatConverter<float>(1));
34                 f_converter->init (frames, D_Tri, 32); // Doesn't throw
35                 CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 24), Exception);
36                 CPPUNIT_ASSERT_THROW (f_converter->init (frames, D_Tri, 48), Exception);
37                 
38                 boost::shared_ptr<SampleFormatConverter<int32_t> > i_converter (new SampleFormatConverter<int32_t>(1));
39                 i_converter->init (frames, D_Tri, 32); // Doesn't throw
40                 i_converter->init (frames, D_Tri, 24); // Doesn't throw
41                 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 8), Exception);
42                 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 16), Exception);
43                 CPPUNIT_ASSERT_THROW (i_converter->init (frames, D_Tri, 48), Exception);
44                 
45                 boost::shared_ptr<SampleFormatConverter<int16_t> > i16_converter (new SampleFormatConverter<int16_t>(1));
46                 i16_converter->init (frames, D_Tri, 16); // Doesn't throw
47                 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 8), Exception);
48                 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 32), Exception);
49                 CPPUNIT_ASSERT_THROW (i16_converter->init (frames, D_Tri, 48), Exception);
50                 
51                 boost::shared_ptr<SampleFormatConverter<uint8_t> > ui_converter (new SampleFormatConverter<uint8_t>(1));
52                 ui_converter->init (frames, D_Tri, 8); // Doesn't throw
53                 CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 4), Exception);
54                 CPPUNIT_ASSERT_THROW (ui_converter->init (frames, D_Tri, 16), Exception);
55         }
56
57         void testFrameCount()
58         {
59                 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
60                 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
61                 
62                 converter->init (frames, D_Tri, 32);
63                 converter->add_output (sink);
64                 nframes_t frames_output = 0;
65                 
66                 {
67                 ProcessContext<float> pc(random_data, frames / 2, 1);
68                 converter->process (pc);
69                 frames_output = sink->get_data().size();
70                 CPPUNIT_ASSERT_EQUAL (frames / 2, frames_output);
71                 }
72                 
73                 {
74                 ProcessContext<float> pc(random_data, frames, 1);
75                 converter->process (pc);
76                 frames_output = sink->get_data().size();
77                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
78                 }
79                 
80                 {
81                 ProcessContext<float> pc(random_data, frames + 1, 1);
82                 CPPUNIT_ASSERT_THROW(converter->process (pc), Exception);
83                 }
84         }
85
86         void testFloat()
87         {
88                 boost::shared_ptr<SampleFormatConverter<float> > converter (new SampleFormatConverter<float>(1));
89                 boost::shared_ptr<VectorSink<float> > sink (new VectorSink<float>());
90                 nframes_t frames_output = 0;
91                 
92                 converter->init(frames, D_Tri, 32);
93                 converter->add_output (sink);
94                 
95                 converter->set_clip_floats (false);
96                 ProcessContext<float> const pc(random_data, frames, 1);
97                 converter->process (pc);
98                 frames_output = sink->get_data().size();
99                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
100                 CPPUNIT_ASSERT (TestUtils::array_equals(sink->get_array(), random_data, frames));
101                 
102                 // Make sure a few samples are < -1.0 and > 1.0
103                 random_data[10] = -1.5;
104                 random_data[20] = 1.5;
105                 
106                 converter->set_clip_floats (true);
107                 converter->process (pc);
108                 frames_output = sink->get_data().size();
109                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
110                 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
111                 
112                 for (nframes_t i = 0; i < frames; ++i) {
113                         // fp comparison needs a bit of tolerance, 1.01 << 1.5
114                         CPPUNIT_ASSERT(sink->get_data()[i] < 1.01);
115                         CPPUNIT_ASSERT(sink->get_data()[i] > -1.01);
116                 }
117         }
118
119         void testInt32()
120         {
121                 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
122                 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
123                 nframes_t frames_output = 0;
124                 
125                 converter->init(frames, D_Tri, 32);
126                 converter->add_output (sink);
127                 
128                 ProcessContext<float> pc(random_data, frames, 1);
129                 converter->process (pc);
130                 frames_output = sink->get_data().size();
131                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
132                 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
133         }
134         
135         void testInt24()
136         {
137                 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(1));
138                 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
139                 nframes_t frames_output = 0;
140                 
141                 converter->init(frames, D_Tri, 24);
142                 converter->add_output (sink);
143                 
144                 ProcessContext<float> pc(random_data, frames, 1);
145                 converter->process (pc);
146                 frames_output = sink->get_data().size();
147                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
148                 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
149         }
150         
151         void testInt16()
152         {
153                 boost::shared_ptr<SampleFormatConverter<int16_t> > converter (new SampleFormatConverter<int16_t>(1));
154                 boost::shared_ptr<VectorSink<int16_t> > sink (new VectorSink<int16_t>());
155                 nframes_t frames_output = 0;
156                 
157                 converter->init(frames, D_Tri, 16);
158                 converter->add_output (sink);
159                 
160                 ProcessContext<float> pc(random_data, frames, 1);
161                 converter->process (pc);
162                 frames_output = sink->get_data().size();
163                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
164                 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
165         }
166         
167         void testUint8()
168         {
169                 boost::shared_ptr<SampleFormatConverter<uint8_t> > converter (new SampleFormatConverter<uint8_t>(1));
170                 boost::shared_ptr<VectorSink<uint8_t> > sink (new VectorSink<uint8_t>());
171                 nframes_t frames_output = 0;
172                 
173                 converter->init(frames, D_Tri, 8);
174                 converter->add_output (sink);
175                 
176                 ProcessContext<float> pc(random_data, frames, 1);
177                 converter->process (pc);
178                 frames_output = sink->get_data().size();
179                 CPPUNIT_ASSERT_EQUAL (frames, frames_output);
180                 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), frames));
181         }
182         
183         void testChannelCount()
184         {
185                 boost::shared_ptr<SampleFormatConverter<int32_t> > converter (new SampleFormatConverter<int32_t>(3));
186                 boost::shared_ptr<VectorSink<int32_t> > sink (new VectorSink<int32_t>());
187                 nframes_t frames_output = 0;
188                 
189                 converter->init(frames, D_Tri, 32);
190                 converter->add_output (sink);
191                 
192                 ProcessContext<float> pc(random_data, 4, 1);
193                 CPPUNIT_ASSERT_THROW (converter->process (pc), Exception);
194                 
195                 pc.frames() = frames - (frames % 3);
196                 converter->process (pc);
197                 frames_output = sink->get_data().size();
198                 CPPUNIT_ASSERT_EQUAL (pc.frames(), frames_output);
199                 CPPUNIT_ASSERT (TestUtils::array_filled(sink->get_array(), pc.frames()));
200         }
201
202   private:
203
204         float * random_data;
205         nframes_t frames;
206 };
207
208 CPPUNIT_TEST_SUITE_REGISTRATION (SampleFormatConverterTest);
209