Move things round a bit.
[dcpomatic.git] / test / long.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <fstream>
21 #include <iostream>
22 #include <boost/filesystem.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include "format.h"
25 #include "film.h"
26 #include "filter.h"
27 #include "job_manager.h"
28 #include "util.h"
29 #include "exceptions.h"
30 #define BOOST_TEST_DYN_LINK
31 #define BOOST_TEST_MODULE dvdomatic_test
32 #include <boost/test/unit_test.hpp>
33
34 using namespace std;
35 using namespace boost;
36
37 bool
38 compare (string ref, string test, list<string> exclude)
39 {
40         ifstream r (ref.c_str ());
41         ifstream t (test.c_str ());
42
43         while (r.good ()) {
44                 string rl;
45                 getline (r, rl);
46                 string tl;
47                 getline (t, tl);
48
49                 bool ex = false;
50                 for (list<string>::iterator i = exclude.begin(); i != exclude.end(); ++i) {
51                         if (rl.find (*i) != string::npos && tl.find (*i) != string::npos) {
52                                 ex = true;
53                         }
54                 }
55
56                 if (!ex && rl != tl) {
57                         cerr << "Fail:\n" << rl << "\n" << tl << "\n";
58                         return true;
59                 }
60         }
61
62         return false;
63 }
64
65
66 BOOST_AUTO_TEST_CASE (make_dcp_test)
67 {
68         dvdomatic_setup ();
69         
70         string const dcp_name = "FOO-BAR-BAZ";
71         
72         string const ref_film = "test/film";
73         string const ref_dcp = ref_film + "/" + dcp_name;
74         string const ref_pkl = ref_dcp + "/bdb4ae0a-0d09-4554-8557-0b4260f4c359_pkl.xml";
75         string const ref_cpl = ref_dcp + "/08dd6e45-83b5-41dc-9179-d7c59f597a12_cpl.xml";
76         string const test_film = "build/test/film";
77         string const test_dcp = test_film + "/" + dcp_name;
78         
79         if (boost::filesystem::exists (test_film)) {
80                 boost::filesystem::remove_all (test_film);
81         }
82
83         Film f (test_film, false);
84         f.write_metadata ();
85         boost::filesystem::copy_file ("test/zombie.mpeg", "build/test/film/zombie.mpeg");
86         f.set_content ("zombie.mpeg");
87         f.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
88
89         BOOST_CHECK_EQUAL (f.audio_channels(), 2);
90         BOOST_CHECK_EQUAL (f.audio_sample_rate(), 48000);
91         BOOST_CHECK_EQUAL (audio_sample_format_to_string (f.audio_sample_format()), "S16");
92         
93         f.set_format (Format::from_nickname ("Flat"));
94
95         f.make_dcp (true, 5);
96
97         while (JobManager::instance()->work_to_do ()) {
98                 sleep (1);
99         }
100
101         {
102                 stringstream s;
103                 s << "diff -ur test/film/j2c " << test_film << "/j2c";
104                 int const r = ::system (s.str().c_str ());
105                 BOOST_CHECK_EQUAL (r, 0);
106         }
107
108         {
109                 stringstream s;
110                 s << "diff -ur test/film/wavs " << test_film << "/wavs";
111                 int const r = ::system (s.str().c_str ());
112                 BOOST_CHECK_EQUAL (r, 0);
113         }
114
115         {
116                 stringstream s;
117                 s << "diff -u test/film/metadata " << test_film << "/metadata";
118                 int const r = ::system (s.str().c_str ());
119                 BOOST_CHECK_EQUAL (r, 0);
120         }
121
122         /* Find the test pkl and cpl */
123         string test_pkl;
124         string test_cpl;
125
126         for (filesystem::directory_iterator i = filesystem::directory_iterator (test_dcp); i != filesystem::directory_iterator(); ++i) {
127 #if BOOST_FILESYSTEM_VERSION == 3               
128                 string const t = filesystem::path(*i).generic_string ();
129 #else
130                 string const t = i->string ();
131 #endif
132                 if (algorithm::ends_with (t, "cpl.xml")) {
133                         test_cpl = t;
134                 } else if (algorithm::ends_with (t, "pkl.xml")) {
135                         test_pkl = t;
136                 }
137         }
138
139         {
140                 list<string> exclude;
141                 exclude.push_back ("urn:uuid");
142                 exclude.push_back ("urn:uri");
143                 exclude.push_back ("<IssueDate>");
144                 exclude.push_back ("<LabelText>");
145                 exclude.push_back ("<Hash>");
146                 BOOST_CHECK_EQUAL (compare (ref_cpl, test_cpl, exclude), false);
147         }
148
149         {
150                 list<string> exclude;
151                 exclude.push_back ("urn:uuid");
152                 exclude.push_back ("<IssueDate>");
153                 exclude.push_back ("<Hash>");
154                 BOOST_CHECK_EQUAL (compare (ref_pkl, test_pkl, exclude), false);
155         }
156 }