Improve OpenFileError so that it doesn't say "opening for read"
[dcpomatic.git] / test / reel_writer_test.cc
1 /*
2     Copyright (C) 2019 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/reel_writer_test.cc
22  *  @brief Test ReelWriter class.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/reel_writer.h"
27 #include "lib/film.h"
28 #include "lib/cross.h"
29 #include "test.h"
30 #include <boost/test/unit_test.hpp>
31
32 using std::string;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 static bool equal (dcp::FrameInfo a, ReelWriter const & writer, boost::filesystem::path file, Frame frame, Eyes eyes)
37 {
38         FILE* f = fopen_boost(file, "rb");
39         BOOST_REQUIRE (f);
40         dcp::FrameInfo b = writer.read_frame_info(f, frame, eyes);
41         bool const r = a.offset == b.offset && a.size == b.size && a.hash == b.hash;
42         fclose (f);
43         return r;
44 }
45
46 BOOST_AUTO_TEST_CASE (write_frame_info_test)
47 {
48         shared_ptr<Film> film = new_test_film2 ("write_frame_info_test");
49         dcpomatic::DCPTimePeriod const period (dcpomatic::DCPTime(0), dcpomatic::DCPTime(96000));
50         ReelWriter writer (film, period, shared_ptr<Job>(), 0, 1, optional<string>());
51
52         /* Write the first one */
53
54         boost::filesystem::path file = film->info_file (period);
55         BOOST_CHECK (!boost::filesystem::exists(file));
56         dcp::FrameInfo info1(0, 123, "12345678901234567890123456789012");
57         writer.write_frame_info (0, EYES_LEFT, info1);
58         BOOST_CHECK (boost::filesystem::exists(file));
59
60         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
61
62         /* Write some more */
63
64         dcp::FrameInfo info2(596, 14921, "123acb789f1234ae782012n456339522");
65         writer.write_frame_info (5, EYES_RIGHT, info2);
66         BOOST_CHECK (boost::filesystem::exists(file));
67
68         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
69         BOOST_CHECK (equal(info2, writer, file, 5, EYES_RIGHT));
70
71         dcp::FrameInfo info3(12494, 99157123, "xxxxyyyyabc12356ffsfdsf456339522");
72         writer.write_frame_info (10, EYES_LEFT, info3);
73         BOOST_CHECK (boost::filesystem::exists(file));
74
75         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
76         BOOST_CHECK (equal(info2, writer, file, 5, EYES_RIGHT));
77         BOOST_CHECK (equal(info3, writer, file, 10, EYES_LEFT));
78
79         /* Overwrite one */
80
81         dcp::FrameInfo info4(55512494, 123599157123, "ABCDEFGyabc12356ffsfdsf4563395ZZ");
82         writer.write_frame_info (5, EYES_RIGHT, info4);
83         BOOST_CHECK (boost::filesystem::exists(file));
84
85         BOOST_CHECK (equal(info1, writer, file, 0, EYES_LEFT));
86         BOOST_CHECK (equal(info4, writer, file, 5, EYES_RIGHT));
87         BOOST_CHECK (equal(info3, writer, file, 10, EYES_LEFT));
88 }