X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=test%2Ftest.cc;h=241032099b8922ebf7935c971978dcba6ac898f0;hb=1f8b45c7fd49714628009f5ed2161fbaa2b4d729;hp=5addc2d75966d4a09edb912f5b3a0c8f3b43347b;hpb=d7fe5fa4178af87b5f1e5a571a78313fa00c3327;p=dcpomatic.git diff --git a/test/test.cc b/test/test.cc index 5addc2d75..241032099 100644 --- a/test/test.cc +++ b/test/test.cc @@ -17,485 +17,228 @@ */ -#include -#include -#include -#include -#include "format.h" -#include "film.h" -#include "filter.h" -#include "job_manager.h" -#include "util.h" -#include "exceptions.h" -#include "delay_line.h" -#include "image.h" -#include "log.h" -#include "dcp_video_frame.h" -#include "config.h" -#include "server.h" -#include "cross.h" -#include "job.h" -#include "subtitle.h" -#include "scaler.h" +#include +#include +#include +#include +#include +#include "lib/config.h" +#include "lib/util.h" +#include "lib/ui_signaller.h" +#include "lib/film.h" +#include "lib/job_manager.h" +#include "lib/job.h" +#include "lib/cross.h" +#include "lib/server_finder.h" +#include "lib/image.h" #define BOOST_TEST_DYN_LINK -#define BOOST_TEST_MODULE dvdomatic_test +#define BOOST_TEST_MODULE dcpomatic_test #include using std::string; -using std::list; -using std::stringstream; using std::vector; +using std::min; +using std::cout; +using std::cerr; +using std::list; using boost::shared_ptr; -using boost::thread; -void -setup_test_config () +class TestUISignaller : public UISignaller { - Config::instance()->set_num_local_encoding_threads (1); - Config::instance()->set_colour_lut_index (0); - Config::instance()->set_j2k_bandwidth (200000000); - Config::instance()->set_servers (vector ()); - Config::instance()->set_server_port (61920); -} +public: + /* No wakes in tests: we call ui_idle ourselves */ + void wake_ui () + { -shared_ptr -new_test_film (string name) -{ - string const d = String::compose ("build/test/%1", name); - if (boost::filesystem::exists (d)) { - boost::filesystem::remove_all (d); } - return shared_ptr (new Film (d, false)); -} +}; -BOOST_AUTO_TEST_CASE (film_metadata_test) +struct TestConfig { - dvdomatic_setup (); - setup_test_config (); + TestConfig () + { + dcpomatic_setup (); - string const test_film = "build/test/film_metadata_test"; - - if (boost::filesystem::exists (test_film)) { - boost::filesystem::remove_all (test_film); - } + Config::instance()->set_num_local_encoding_threads (1); + Config::instance()->set_server_port_base (61920); + Config::instance()->set_default_dci_metadata (DCIMetadata ()); + Config::instance()->set_default_container (static_cast (0)); + Config::instance()->set_default_dcp_content_type (static_cast (0)); + Config::instance()->set_default_audio_delay (0); - BOOST_CHECK_THROW (new Film (test_film, true), OpenFileError); - - shared_ptr f (new Film (test_film, false)); - BOOST_CHECK (f->format() == 0); - BOOST_CHECK (f->dcp_content_type() == 0); - BOOST_CHECK (f->filters ().empty()); - - f->set_name ("fred"); - BOOST_CHECK_THROW (f->set_content ("jim"), OpenFileError); - f->set_dcp_content_type (DCPContentType::from_pretty_name ("Short")); - f->set_format (Format::from_nickname ("Flat")); - f->set_left_crop (1); - f->set_right_crop (2); - f->set_top_crop (3); - f->set_bottom_crop (4); - vector f_filters; - f_filters.push_back (Filter::from_id ("pphb")); - f_filters.push_back (Filter::from_id ("unsharp")); - f->set_filters (f_filters); - f->set_dcp_trim_start (42); - f->set_dcp_trim_end (99); - f->set_dcp_ab (true); - f->write_metadata (); + ServerFinder::instance()->disable (); - stringstream s; - s << "diff -u test/metadata.ref " << test_film << "/metadata"; - BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0); - - shared_ptr g (new Film (test_film, true)); - - BOOST_CHECK_EQUAL (g->name(), "fred"); - BOOST_CHECK_EQUAL (g->dcp_content_type(), DCPContentType::from_pretty_name ("Short")); - BOOST_CHECK_EQUAL (g->format(), Format::from_nickname ("Flat")); - BOOST_CHECK_EQUAL (g->crop().left, 1); - BOOST_CHECK_EQUAL (g->crop().right, 2); - BOOST_CHECK_EQUAL (g->crop().top, 3); - BOOST_CHECK_EQUAL (g->crop().bottom, 4); - vector g_filters = g->filters (); - BOOST_CHECK_EQUAL (g_filters.size(), 2); - BOOST_CHECK_EQUAL (g_filters.front(), Filter::from_id ("pphb")); - BOOST_CHECK_EQUAL (g_filters.back(), Filter::from_id ("unsharp")); - BOOST_CHECK_EQUAL (g->dcp_trim_start(), 42); - BOOST_CHECK_EQUAL (g->dcp_trim_end(), 99); - BOOST_CHECK_EQUAL (g->dcp_ab(), true); - - g->write_metadata (); - BOOST_CHECK_EQUAL (::system (s.str().c_str ()), 0); -} + ui_signaller = new TestUISignaller (); + } +}; -BOOST_AUTO_TEST_CASE (format_test) -{ - Format::setup_formats (); - - Format const * f = Format::from_nickname ("Flat"); - BOOST_CHECK (f); - BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr ()), 185); - - f = Format::from_nickname ("Scope"); - BOOST_CHECK (f); - BOOST_CHECK_EQUAL (f->ratio_as_integer(shared_ptr ()), 239); -} +BOOST_GLOBAL_FIXTURE (TestConfig); -BOOST_AUTO_TEST_CASE (util_test) +boost::filesystem::path +test_film_dir (string name) { - string t = "Hello this is a string \"with quotes\" and indeed without them"; - vector b = split_at_spaces_considering_quotes (t); - vector::iterator i = b.begin (); - BOOST_CHECK_EQUAL (*i++, "Hello"); - BOOST_CHECK_EQUAL (*i++, "this"); - BOOST_CHECK_EQUAL (*i++, "is"); - BOOST_CHECK_EQUAL (*i++, "a"); - BOOST_CHECK_EQUAL (*i++, "string"); - BOOST_CHECK_EQUAL (*i++, "with quotes"); - BOOST_CHECK_EQUAL (*i++, "and"); - BOOST_CHECK_EQUAL (*i++, "indeed"); - BOOST_CHECK_EQUAL (*i++, "without"); - BOOST_CHECK_EQUAL (*i++, "them"); + boost::filesystem::path p; + p /= "build"; + p /= "test"; + p /= name; + return p; } -void -do_positive_delay_line_test (int delay_length, int data_length) +shared_ptr +new_test_film (string name) { - DelayLine d (6, delay_length); - shared_ptr data (new AudioBuffers (6, data_length)); - - int in = 0; - int out = 0; - int returned = 0; - int zeros = 0; - - for (int i = 0; i < 64; ++i) { - for (int j = 0; j < data_length; ++j) { - for (int c = 0; c < 6; ++c ) { - data->data(c)[j] = in; - ++in; - } - } - - d.feed (data); - returned += data->frames (); - - for (int j = 0; j < data->frames(); ++j) { - if (zeros < delay_length) { - for (int c = 0; c < 6; ++c) { - BOOST_CHECK_EQUAL (data->data(c)[j], 0); - } - ++zeros; - } else { - for (int c = 0; c < 6; ++c) { - BOOST_CHECK_EQUAL (data->data(c)[j], out); - ++out; - } - } - } + boost::filesystem::path p = test_film_dir (name); + if (boost::filesystem::exists (p)) { + boost::filesystem::remove_all (p); } - - BOOST_CHECK_EQUAL (returned, 64 * data_length); + + shared_ptr f = shared_ptr (new Film (p.string())); + f->write_metadata (); + return f; } void -do_negative_delay_line_test (int delay_length, int data_length) -{ - DelayLine d (6, delay_length); - shared_ptr data (new AudioBuffers (6, data_length)); - - int in = 0; - int out = -delay_length * 6; - int returned = 0; +check_file (boost::filesystem::path ref, boost::filesystem::path check) +{ + uintmax_t N = boost::filesystem::file_size (ref); + BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check)); + FILE* ref_file = fopen (ref.c_str(), "rb"); + BOOST_CHECK (ref_file); + FILE* check_file = fopen_boost (check, "rb"); + BOOST_CHECK (check_file); - for (int i = 0; i < 256; ++i) { - data->set_frames (data_length); - for (int j = 0; j < data_length; ++j) { - for (int c = 0; c < 6; ++c) { - data->data(c)[j] = in; - ++in; - } - } + int const buffer_size = 65536; + uint8_t* ref_buffer = new uint8_t[buffer_size]; + uint8_t* check_buffer = new uint8_t[buffer_size]; - d.feed (data); - returned += data->frames (); + while (N) { + uintmax_t this_time = min (uintmax_t (buffer_size), N); + size_t r = fread (ref_buffer, 1, this_time, ref_file); + BOOST_CHECK_EQUAL (r, this_time); + r = fread (check_buffer, 1, this_time, check_file); + BOOST_CHECK_EQUAL (r, this_time); - for (int j = 0; j < data->frames(); ++j) { - for (int c = 0; c < 6; ++c) { - BOOST_CHECK_EQUAL (data->data(c)[j], out); - ++out; - } - } + BOOST_CHECK_EQUAL (memcmp (ref_buffer, check_buffer, this_time), 0); + N -= this_time; } - returned += -delay_length; - BOOST_CHECK_EQUAL (returned, 256 * data_length); -} - -BOOST_AUTO_TEST_CASE (delay_line_test) -{ - do_positive_delay_line_test (64, 128); - do_positive_delay_line_test (128, 64); - do_positive_delay_line_test (3, 512); - do_positive_delay_line_test (512, 3); - - do_positive_delay_line_test (0, 64); - - do_negative_delay_line_test (-64, 128); - do_negative_delay_line_test (-128, 64); - do_negative_delay_line_test (-3, 512); - do_negative_delay_line_test (-512, 3); -} - -BOOST_AUTO_TEST_CASE (md5_digest_test) -{ - string const t = md5_digest ("test/md5.test"); - BOOST_CHECK_EQUAL (t, "15058685ba99decdc4398c7634796eb0"); + delete[] ref_buffer; + delete[] check_buffer; - BOOST_CHECK_THROW (md5_digest ("foobar"), OpenFileError); + fclose (ref_file); + fclose (check_file); } -BOOST_AUTO_TEST_CASE (paths_test) +static void +note (dcp::NoteType t, string n) { - shared_ptr f = new_test_film ("paths_test"); - f->set_directory ("build/test/a/b/c/d/e"); - vector thumbs; - thumbs.push_back (42); - f->set_thumbs (thumbs); - BOOST_CHECK_EQUAL (f->thumb_file (0), "build/test/a/b/c/d/e/thumbs/00000042.png"); - - f->_content = "/foo/bar/baz"; - BOOST_CHECK_EQUAL (f->content_path(), "/foo/bar/baz"); - f->_content = "foo/bar/baz"; - BOOST_CHECK_EQUAL (f->content_path(), "build/test/a/b/c/d/e/foo/bar/baz"); + if (t == dcp::ERROR) { + cerr << n << "\n"; + } } void -do_remote_encode (shared_ptr frame, ServerDescription* description, shared_ptr locally_encoded, int N) -{ - shared_ptr remotely_encoded; - BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description)); - BOOST_CHECK (remotely_encoded); +check_dcp (boost::filesystem::path ref, boost::filesystem::path check) +{ + dcp::DCP ref_dcp (ref); + ref_dcp.read (); + dcp::DCP check_dcp (check); + check_dcp.read (); + + dcp::EqualityOptions options; + options.max_mean_pixel_error = 5; + options.max_std_dev_pixel_error = 5; + options.max_audio_sample_error = 255; + options.cpl_annotation_texts_can_differ = true; + options.mxf_names_can_differ = true; - BOOST_CHECK_EQUAL (locally_encoded->size(), remotely_encoded->size()); - BOOST_CHECK (memcmp (locally_encoded->data(), remotely_encoded->data(), locally_encoded->size()) == 0); + BOOST_CHECK (ref_dcp.equals (check_dcp, options, boost::bind (note, _1, _2))); } -BOOST_AUTO_TEST_CASE (client_server_test) -{ - shared_ptr image (new CompactImage (PIX_FMT_RGB24, Size (1998, 1080))); - uint8_t* p = image->data()[0]; - - for (int y = 0; y < 1080; ++y) { - for (int x = 0; x < 1998; ++x) { - *p++ = x % 256; - *p++ = y % 256; - *p++ = (x + y) % 256; +void +check_xml (xmlpp::Element* ref, xmlpp::Element* test, list ignore) +{ + BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ()); + BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ()); + + if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) { + return; + } + + xmlpp::Element::NodeList ref_children = ref->get_children (); + xmlpp::Element::NodeList test_children = test->get_children (); + BOOST_CHECK_EQUAL (ref_children.size (), test_children.size ()); + + xmlpp::Element::NodeList::iterator k = ref_children.begin (); + xmlpp::Element::NodeList::iterator l = test_children.begin (); + while (k != ref_children.end ()) { + + /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */ + + xmlpp::Element* ref_el = dynamic_cast (*k); + xmlpp::Element* test_el = dynamic_cast (*l); + BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el)); + if (ref_el && test_el) { + check_xml (ref_el, test_el, ignore); } - } - shared_ptr sub_image (new CompactImage (PIX_FMT_RGBA, Size (100, 200))); - p = sub_image->data()[0]; - for (int y = 0; y < 200; ++y) { - for (int x = 0; x < 100; ++x) { - *p++ = y % 256; - *p++ = x % 256; - *p++ = (x + y) % 256; - *p++ = 1; + xmlpp::ContentNode* ref_cn = dynamic_cast (*k); + xmlpp::ContentNode* test_cn = dynamic_cast (*l); + BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn)); + if (ref_cn && test_cn) { + BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ()); } - } - - shared_ptr subtitle (new Subtitle (Position (50, 60), sub_image)); - - FileLog log ("build/test/client_server_test.log"); - - shared_ptr frame ( - new DCPVideoFrame ( - image, - subtitle, - Size (1998, 1080), - 0, - 0, - 1, - Scaler::from_id ("bicubic"), - 0, - 24, - "", - 0, - 200000000, - &log - ) - ); - - shared_ptr locally_encoded = frame->encode_locally (); - BOOST_ASSERT (locally_encoded); - - Server* server = new Server (&log); - - new thread (boost::bind (&Server::run, server, 2)); - - /* Let the server get itself ready */ - dvdomatic_sleep (1); - ServerDescription description ("localhost", 2); - - list threads; - for (int i = 0; i < 8; ++i) { - threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded, i))); - } - - for (list::iterator i = threads.begin(); i != threads.end(); ++i) { - (*i)->join (); - } - - for (list::iterator i = threads.begin(); i != threads.end(); ++i) { - delete *i; - } -} - -BOOST_AUTO_TEST_CASE (make_dcp_test) -{ - shared_ptr film = new_test_film ("make_dcp_test"); - film->set_name ("test_film2"); - film->set_content ("../../../test/test.mp4"); - film->set_format (Format::from_nickname ("Flat")); - film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test")); - film->make_dcp (true); - - while (JobManager::instance()->work_to_do ()) { - dvdomatic_sleep (1); - } - - BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false); -} + xmlpp::Attribute* ref_at = dynamic_cast (*k); + xmlpp::Attribute* test_at = dynamic_cast (*l); + BOOST_CHECK ((ref_at && test_at) || (!ref_at && !test_at)); + if (ref_at && test_at) { + BOOST_CHECK_EQUAL (ref_at->get_name(), test_at->get_name ()); + BOOST_CHECK_EQUAL (ref_at->get_value(), test_at->get_value ()); + } -BOOST_AUTO_TEST_CASE (make_dcp_with_range_test) -{ - shared_ptr film = new_test_film ("make_dcp_with_range_test"); - film->set_name ("test_film3"); - film->set_content ("../../../test/test.mp4"); - film->examine_content (); - film->set_format (Format::from_nickname ("Flat")); - film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test")); - film->set_dcp_trim_end (42); - film->make_dcp (true); - - while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) { - dvdomatic_sleep (1); + ++k; + ++l; } - - BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false); } -BOOST_AUTO_TEST_CASE (audio_sampling_rate_test) +void +check_xml (boost::filesystem::path ref, boost::filesystem::path test, list ignore) { - shared_ptr f = new_test_film ("audio_sampling_rate_test"); - f->set_frames_per_second (24); - - f->set_audio_sample_rate (48000); - BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000); - - f->set_audio_sample_rate (44100); - BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 48000); + xmlpp::DomParser* ref_parser = new xmlpp::DomParser (ref.string ()); + xmlpp::Element* ref_root = ref_parser->get_document()->get_root_node (); + xmlpp::DomParser* test_parser = new xmlpp::DomParser (test.string ()); + xmlpp::Element* test_root = test_parser->get_document()->get_root_node (); - f->set_audio_sample_rate (80000); - BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 96000); - - f->set_frames_per_second (23.976); - f->set_audio_sample_rate (48000); - BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952); - - f->set_frames_per_second (29.97); - f->set_audio_sample_rate (48000); - BOOST_CHECK_EQUAL (f->target_audio_sample_rate(), 47952); + check_xml (ref_root, test_root, ignore); } -class TestJob : public Job +void +wait_for_jobs () { -public: - TestJob (shared_ptr f, shared_ptr req) - : Job (f, req) - { - + JobManager* jm = JobManager::instance (); + while (jm->work_to_do ()) { + ui_signaller->ui_idle (); } - - void set_finished_ok () { - set_state (FINISHED_OK); - } - - void set_finished_error () { - set_state (FINISHED_ERROR); - } - - void run () - { - while (1) { - if (finished ()) { - return; + if (jm->errors ()) { + for (list >::iterator i = jm->_jobs.begin(); i != jm->_jobs.end(); ++i) { + if ((*i)->finished_in_error ()) { + cerr << (*i)->error_summary () << "\n" + << (*i)->error_details () << "\n"; } } } + + BOOST_CHECK (!jm->errors()); - string name () const { - return ""; - } -}; - -BOOST_AUTO_TEST_CASE (job_manager_test) -{ - shared_ptr f; - - /* Single job, no dependency */ - shared_ptr a (new TestJob (f, shared_ptr ())); - - JobManager::instance()->add (a); - dvdomatic_sleep (1); - BOOST_CHECK_EQUAL (a->running (), true); - a->set_finished_ok (); - dvdomatic_sleep (2); - BOOST_CHECK_EQUAL (a->finished_ok(), true); - - /* Two jobs, dependency */ - a.reset (new TestJob (f, shared_ptr ())); - shared_ptr b (new TestJob (f, a)); - - JobManager::instance()->add (a); - JobManager::instance()->add (b); - dvdomatic_sleep (2); - BOOST_CHECK_EQUAL (a->running(), true); - BOOST_CHECK_EQUAL (b->running(), false); - a->set_finished_ok (); - dvdomatic_sleep (2); - BOOST_CHECK_EQUAL (a->finished_ok(), true); - BOOST_CHECK_EQUAL (b->running(), true); - b->set_finished_ok (); - dvdomatic_sleep (2); - BOOST_CHECK_EQUAL (b->finished_ok(), true); - - /* Two jobs, dependency, first fails */ - a.reset (new TestJob (f, shared_ptr ())); - b.reset (new TestJob (f, a)); - - JobManager::instance()->add (a); - JobManager::instance()->add (b); - dvdomatic_sleep (2); - BOOST_CHECK_EQUAL (a->running(), true); - BOOST_CHECK_EQUAL (b->running(), false); - a->set_finished_error (); - dvdomatic_sleep (2); - BOOST_CHECK_EQUAL (a->finished_in_error(), true); - BOOST_CHECK_EQUAL (b->running(), false); + ui_signaller->ui_idle (); } -BOOST_AUTO_TEST_CASE (stream_test) +void +write_image (shared_ptr image, boost::filesystem::path file) { - AudioStream a ("4 9 hello there world"); - BOOST_CHECK_EQUAL (a.id(), 4); - BOOST_CHECK_EQUAL (a.channels(), 9); - BOOST_CHECK_EQUAL (a.name(), "hello there world"); - BOOST_CHECK_EQUAL (a.to_string(), "4 9 hello there world"); - - SubtitleStream s ("5 a b c"); - BOOST_CHECK_EQUAL (s.id(), 5); - BOOST_CHECK_EQUAL (s.name(), "a b c"); + using namespace MagickCore; + + Magick::Image m (image->size().width, image->size().height, "ARGB", CharPixel, (void *) image->data()[0]); + m.write (file.string ()); }