Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / empty_test.cc
1 /*
2     Copyright (C) 2017-2021 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
22 /** @file  test/empty_test.cc
23  *  @brief Test the creation of Empty objects.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/dcp_content_type.h"
29 #include "lib/decoder.h"
30 #include "lib/empty.h"
31 #include "lib/film.h"
32 #include "lib/image_content.h"
33 #include "lib/player.h"
34 #include "lib/ratio.h"
35 #include "lib/video_content.h"
36 #include "test.h"
37 #include <boost/test/unit_test.hpp>
38
39
40 using std::list;
41 using std::make_shared;
42 using std::shared_ptr;
43 #if BOOST_VERSION >= 106100
44 using namespace boost::placeholders;
45 #endif
46 using namespace dcpomatic;
47
48
49 bool
50 has_video (shared_ptr<const Content> content)
51 {
52         return static_cast<bool>(content->video);
53 }
54
55
56 BOOST_AUTO_TEST_CASE (empty_test1)
57 {
58         auto film = new_test_film2 ("empty_test1");
59         film->set_sequence (false);
60         auto contentA = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
61         auto contentB = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
62
63         film->examine_and_add_content (contentA);
64         film->examine_and_add_content (contentB);
65         BOOST_REQUIRE (!wait_for_jobs());
66
67         int const vfr = film->video_frame_rate ();
68
69         /* 0 1 2 3 4 5 6 7
70          *     A A A     B
71          */
72         contentA->video->set_length (3);
73         contentA->set_position (film, DCPTime::from_frames(2, vfr));
74         contentB->video->set_length (1);
75         contentB->set_position (film, DCPTime::from_frames(7, vfr));
76
77         Empty black (film, film->playlist(), bind(&has_video, _1), film->playlist()->length(film));
78         BOOST_REQUIRE_EQUAL (black._periods.size(), 2U);
79         auto i = black._periods.begin();
80         BOOST_CHECK (i->from == DCPTime::from_frames(0, vfr));
81         BOOST_CHECK (i->to ==   DCPTime::from_frames(2, vfr));
82         ++i;
83         BOOST_CHECK (i->from == DCPTime::from_frames(5, vfr));
84         BOOST_CHECK (i->to ==   DCPTime::from_frames(7, vfr));
85 }
86
87
88 /** Some tests where the first empty period is not at time 0 */
89 BOOST_AUTO_TEST_CASE (empty_test2)
90 {
91         auto film = new_test_film2 ("empty_test2");
92         film->set_sequence (false);
93         auto contentA = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
94         auto contentB = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
95
96         film->examine_and_add_content (contentA);
97         film->examine_and_add_content (contentB);
98         BOOST_REQUIRE (!wait_for_jobs());
99
100         int const vfr = film->video_frame_rate ();
101
102         /* 0 1 2 3 4 5 6 7
103          * A A A         B
104          */
105         contentA->video->set_length (3);
106         contentA->set_position (film, DCPTime(0));
107         contentB->video->set_length (1);
108         contentB->set_position (film, DCPTime::from_frames(7, vfr));
109
110         Empty black (film, film->playlist(), bind(&has_video, _1), film->playlist()->length(film));
111         BOOST_REQUIRE_EQUAL (black._periods.size(), 1U);
112         BOOST_CHECK (black._periods.front().from == DCPTime::from_frames(3, vfr));
113         BOOST_CHECK (black._periods.front().to == DCPTime::from_frames(7, vfr));
114
115         /* position should initially be the start of the first empty period */
116         BOOST_CHECK (black.position() == DCPTime::from_frames(3, vfr));
117
118         /* check that done() works */
119         BOOST_CHECK (!black.done ());
120         black.set_position (DCPTime::from_frames (4, vfr));
121         BOOST_CHECK (!black.done ());
122         black.set_position (DCPTime::from_frames (7, vfr));
123         BOOST_CHECK (black.done ());
124 }
125
126
127 /** Test for when the film's playlist is not the same as the one passed into Empty */
128 BOOST_AUTO_TEST_CASE (empty_test3)
129 {
130         auto film = new_test_film2 ("empty_test3");
131         film->set_sequence (false);
132         auto contentA = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
133         auto contentB = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
134
135         film->examine_and_add_content (contentA);
136         film->examine_and_add_content (contentB);
137         BOOST_REQUIRE (!wait_for_jobs());
138
139         int const vfr = film->video_frame_rate ();
140
141         /* 0 1 2 3 4 5 6 7
142          * A A A         B
143          */
144         contentA->video->set_length (3);
145         contentA->set_position (film, DCPTime(0));
146         contentB->video->set_length (1);
147         contentB->set_position (film, DCPTime::from_frames(7, vfr));
148
149         auto playlist = make_shared<Playlist>();
150         playlist->add (film, contentB);
151         Empty black (film, playlist, bind(&has_video, _1), playlist->length(film));
152         BOOST_REQUIRE_EQUAL (black._periods.size(), 1U);
153         BOOST_CHECK (black._periods.front().from == DCPTime::from_frames(0, vfr));
154         BOOST_CHECK (black._periods.front().to == DCPTime::from_frames(7, vfr));
155
156         /* position should initially be the start of the first empty period */
157         BOOST_CHECK (black.position() == DCPTime::from_frames(0, vfr));
158 }
159
160
161 BOOST_AUTO_TEST_CASE (empty_test_with_overlapping_content)
162 {
163         auto film = new_test_film2 ("empty_test_with_overlapping_content");
164         film->set_sequence (false);
165         auto contentA = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
166         auto contentB = make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
167
168         film->examine_and_add_content (contentA);
169         film->examine_and_add_content (contentB);
170         BOOST_REQUIRE (!wait_for_jobs());
171
172         int const vfr = film->video_frame_rate ();
173
174         contentA->video->set_length (vfr * 3);
175         contentA->set_position (film, DCPTime());
176         contentB->video->set_length (vfr * 1);
177         contentB->set_position (film, DCPTime::from_seconds(1));
178
179         Empty black(film, film->playlist(), bind(&has_video, _1), film->playlist()->length(film));
180
181         BOOST_REQUIRE (black._periods.empty());
182 }
183