Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / writer_test.cc
1 /*
2     Copyright (C) 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 #include "lib/audio_buffers.h"
23 #include "lib/content.h"
24 #include "lib/content_factory.h"
25 #include "lib/cross.h"
26 #include "lib/film.h"
27 #include "lib/job.h"
28 #include "lib/video_content.h"
29 #include "lib/writer.h"
30 #include "test.h"
31 #include <dcp/openjpeg_image.h>
32 #include <dcp/j2k_transcode.h>
33 #include <boost/test/unit_test.hpp>
34 #include <memory>
35
36
37 using std::make_shared;
38 using std::shared_ptr;
39 using std::vector;
40
41
42 BOOST_AUTO_TEST_CASE (test_write_odd_amount_of_silence)
43 {
44         auto content = content_factory("test/data/flat_red.png").front();
45         auto film = new_test_film2 ("test_write_odd_amount_of_silence", {content});
46         content->video->set_length(24);
47         auto writer = make_shared<Writer>(film, shared_ptr<Job>());
48
49         auto audio = make_shared<AudioBuffers>(6, 48000);
50         audio->make_silent ();
51         writer->write (audio, dcpomatic::DCPTime(1));
52 }
53
54
55 BOOST_AUTO_TEST_CASE (interrupt_writer)
56 {
57         Cleanup cl;
58
59         auto film = new_test_film2 ("test_interrupt_writer", {}, &cl);
60
61         auto content = content_factory("test/data/check_image0.png").front();
62         film->examine_and_add_content (content);
63         BOOST_REQUIRE (!wait_for_jobs());
64
65         /* Add some dummy content to the film so that it has a reel of the right length */
66         auto constexpr frames = 24 * 60;
67         content->video->set_length (frames);
68
69         /* Make a random J2K image */
70         auto size = dcp::Size(1998, 1080);
71         auto image = make_shared<dcp::OpenJPEGImage>(size);
72         for (int i = 0; i < 3; ++i) {
73                 for (int j = 0; j < (size.width * size.height); ++j) {
74                         image->data(i)[j] = rand();
75                 }
76         }
77
78         /* Write some data */
79         auto video = dcp::compress_j2k(image, 100000000, 24, false, false);
80         auto video_ptr = make_shared<dcp::ArrayData>(video.data(), video.size());
81         auto audio = make_shared<AudioBuffers>(6, 48000 / 24);
82
83         auto writer = make_shared<Writer>(film, shared_ptr<Job>());
84         writer->start ();
85
86         for (int i = 0; i < frames; ++i) {
87                 writer->write (video_ptr, i, Eyes::BOTH);
88                 writer->write (audio, dcpomatic::DCPTime::from_frames(i, 24));
89         }
90
91         /* Start digest calculations then abort them; there should be no crash or error */
92         boost::thread thread([film, writer]() {
93                 writer->finish(film->dir(film->dcp_name()));
94         });
95
96         dcpomatic_sleep_seconds (1);
97
98         thread.interrupt ();
99         thread.join ();
100
101         dcpomatic_sleep_seconds (1);
102         cl.run ();
103 }
104
105
106 BOOST_AUTO_TEST_CASE (writer_disambiguate_font_ids1)
107 {
108         auto film = new_test_film2("writer_disambiguate_font_ids1", {});
109         Writer writer(film, {});
110
111         auto fonts = vector<shared_ptr<dcpomatic::Font>> {
112                 make_shared<dcpomatic::Font>("a"),
113                 make_shared<dcpomatic::Font>("b"),
114                 make_shared<dcpomatic::Font>("c")
115         };
116
117         writer.write(fonts);
118
119         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[0]), "a");
120         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[1]), "b");
121         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[2]), "c");
122 }
123
124
125 BOOST_AUTO_TEST_CASE (writer_disambiguate_font_ids2)
126 {
127         auto film = new_test_film2("writer_disambiguate_font_ids2", {});
128         Writer writer(film, {});
129
130         auto fonts = vector<shared_ptr<dcpomatic::Font>> {
131                 make_shared<dcpomatic::Font>("a"),
132                 make_shared<dcpomatic::Font>("a"),
133                 make_shared<dcpomatic::Font>("a")
134         };
135
136         writer.write(fonts);
137
138         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[0]), "a");
139         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[1]), "a_0");
140         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[2]), "a_1");
141 }
142
143
144 BOOST_AUTO_TEST_CASE (writer_disambiguate_font_ids3)
145 {
146         auto film = new_test_film2("writer_disambiguate_font_ids3", {});
147         Writer writer(film, {});
148
149         auto fonts = vector<shared_ptr<dcpomatic::Font>> {
150                 make_shared<dcpomatic::Font>("a_2"),
151                 make_shared<dcpomatic::Font>("a_1"),
152                 make_shared<dcpomatic::Font>("a_1"),
153                 make_shared<dcpomatic::Font>("b")
154         };
155
156         writer.write(fonts);
157
158         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[1]), "a_1");
159         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[0]), "a_2");
160         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[2]), "a_3");
161         BOOST_CHECK_EQUAL(writer._fonts.get(fonts[3]), "b");
162 }
163