Merge branch '1.0' of git.carlh.net:git/libdcp into 1.0
[libdcp.git] / test / test.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define BOOST_TEST_DYN_LINK
21 #define BOOST_TEST_MODULE libdcp_test
22 #include "util.h"
23 #include "test.h"
24 #include <locked_sstream.h>
25 #include <libxml++/libxml++.h>
26 #include <boost/test/unit_test.hpp>
27 #include <cstdio>
28
29 using std::string;
30 using std::min;
31 using std::list;
32
33 boost::filesystem::path private_test;
34
35 struct TestConfig
36 {
37         TestConfig()
38         {
39                 dcp::init ();
40                 if (boost::unit_test::framework::master_test_suite().argc >= 2) {
41                         private_test = boost::unit_test::framework::master_test_suite().argv[1];
42                 }
43         }
44 };
45
46 void
47 check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
48 {
49         BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ());
50         BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ());
51
52         if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) {
53                 return;
54         }
55
56         xmlpp::Element::NodeList ref_children = ref->get_children ();
57         xmlpp::Element::NodeList test_children = test->get_children ();
58         BOOST_REQUIRE_EQUAL (ref_children.size (), test_children.size ());
59
60         xmlpp::Element::NodeList::iterator k = ref_children.begin ();
61         xmlpp::Element::NodeList::iterator l = test_children.begin ();
62         while (k != ref_children.end ()) {
63
64                 /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */
65
66                 xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k);
67                 xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l);
68                 BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el));
69                 if (ref_el && test_el) {
70                         check_xml (ref_el, test_el, ignore);
71                 }
72
73                 xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
74                 xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
75                 BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn));
76                 if (ref_cn && test_cn) {
77                         BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ());
78                 }
79
80                 ++k;
81                 ++l;
82         }
83
84         xmlpp::Element::AttributeList ref_attributes = ref->get_attributes ();
85         xmlpp::Element::AttributeList test_attributes = test->get_attributes ();
86         BOOST_CHECK_EQUAL (ref_attributes.size(), test_attributes.size ());
87
88         xmlpp::Element::AttributeList::const_iterator m = ref_attributes.begin();
89         xmlpp::Element::AttributeList::const_iterator n = test_attributes.begin();
90         while (m != ref_attributes.end ()) {
91                 BOOST_CHECK_EQUAL ((*m)->get_name(), (*n)->get_name());
92                 BOOST_CHECK_EQUAL ((*m)->get_value(), (*n)->get_value());
93
94                 ++m;
95                 ++n;
96         }
97 }
98
99 void
100 check_xml (string ref, string test, list<string> ignore)
101 {
102         xmlpp::DomParser* ref_parser = new xmlpp::DomParser ();
103         ref_parser->parse_memory (ref);
104         xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node ();
105         xmlpp::DomParser* test_parser = new xmlpp::DomParser ();
106         test_parser->parse_memory (test);
107         xmlpp::Element* test_root = test_parser->get_document()->get_root_node ();
108
109         check_xml (ref_root, test_root, ignore);
110 }
111
112 void
113 check_file (boost::filesystem::path ref, boost::filesystem::path check)
114 {
115         uintmax_t N = boost::filesystem::file_size (ref);
116         BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check));
117         FILE* ref_file = dcp::fopen_boost (ref, "rb");
118         BOOST_CHECK (ref_file);
119         FILE* check_file = dcp::fopen_boost (check, "rb");
120         BOOST_CHECK (check_file);
121
122         int const buffer_size = 65536;
123         uint8_t* ref_buffer = new uint8_t[buffer_size];
124         uint8_t* check_buffer = new uint8_t[buffer_size];
125
126         locked_stringstream error;
127         error << "File " << check.string() << " differs from reference " << ref.string();
128
129         while (N) {
130                 uintmax_t this_time = min (uintmax_t (buffer_size), N);
131                 size_t r = fread (ref_buffer, 1, this_time, ref_file);
132                 BOOST_CHECK_EQUAL (r, this_time);
133                 r = fread (check_buffer, 1, this_time, check_file);
134                 BOOST_CHECK_EQUAL (r, this_time);
135
136                 BOOST_CHECK_MESSAGE (memcmp (ref_buffer, check_buffer, this_time) == 0, error.str ());
137                 if (memcmp (ref_buffer, check_buffer, this_time)) {
138                         break;
139                 }
140
141                 N -= this_time;
142         }
143
144         delete[] ref_buffer;
145         delete[] check_buffer;
146
147         fclose (ref_file);
148         fclose (check_file);
149 }
150
151 BOOST_GLOBAL_FIXTURE (TestConfig);