Fix crash with sub-sample push parts in AudioMerger.
[dcpomatic.git] / test / audio_mapping_test.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  test/audio_mapping_test.cc
22  *  @brief Test AudioMapping class.
23  *  @ingroup selfcontained
24  */
25
26 #include <boost/test/unit_test.hpp>
27 #include "lib/audio_mapping.h"
28 #include "lib/util.h"
29
30 using std::list;
31 using std::string;
32 using boost::optional;
33
34 BOOST_AUTO_TEST_CASE (audio_mapping_test)
35 {
36         AudioMapping none;
37         BOOST_CHECK_EQUAL (none.input_channels(), 0);
38
39         AudioMapping four (4, MAX_DCP_AUDIO_CHANNELS);
40         BOOST_CHECK_EQUAL (four.input_channels(), 4);
41
42         four.set (0, 1, 1);
43
44         for (int i = 0; i < 4; ++i) {
45                 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
46                         BOOST_CHECK_EQUAL (four.get (i, j), (i == 0 && j == 1) ? 1 : 0);
47                 }
48         }
49
50         list<int> mapped = four.mapped_output_channels ();
51         BOOST_CHECK_EQUAL (mapped.size(), 1);
52         BOOST_CHECK_EQUAL (mapped.front(), 1);
53
54         four.make_zero ();
55
56         for (int i = 0; i < 4; ++i) {
57                 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
58                         BOOST_CHECK_EQUAL (four.get (i, j), 0);
59                 }
60         }
61 }
62
63 static void
64 guess_check (string filename, int output_channel)
65 {
66         AudioMapping m (1, 8);
67         m.make_default (0, optional<boost::filesystem::path>(filename));
68         for (int i = 0; i < 8; ++i) {
69                 BOOST_TEST_INFO (filename);
70                 BOOST_CHECK_CLOSE (m.get(0, i), i == output_channel ? 1 : 0, 0.01);
71         }
72 }
73
74 BOOST_AUTO_TEST_CASE (audio_mapping_guess_test)
75 {
76         guess_check ("stuff_L_nonsense.wav", 0);
77         guess_check ("stuff_nonsense.wav", 2);
78         guess_check ("fred_R.wav", 1);
79         guess_check ("jim_C_sheila.aiff", 2);
80         guess_check ("things_Lfe_and.wav", 3);
81         guess_check ("weeee_Ls.aiff", 4);
82         guess_check ("try_Rs-it.wav", 5);
83
84         /* PT-style */
85         guess_check ("things_LFE.wav", 3);
86         guess_check ("ptish_Lsr_abc.wav", 6);
87         guess_check ("ptish_Rsr_abc.wav", 7);
88         guess_check ("more_Lss_s.wav", 4);
89         guess_check ("other_Rss.aiff", 5);
90 }
91
92