334811cb7a9db4caa0219513d32b7ec007c8e129
[dcpomatic.git] / test / util_test.cc
1 /*
2     Copyright (C) 2012-2016 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/util_test.cc
22  *  @brief Test various utility methods.
23  */
24
25 #include "lib/util.h"
26 #include "lib/exceptions.h"
27 #include <boost/test/unit_test.hpp>
28
29 using std::string;
30 using std::vector;
31 using boost::shared_ptr;
32
33 BOOST_AUTO_TEST_CASE (digest_head_tail_test)
34 {
35         vector<boost::filesystem::path> p;
36         p.push_back ("test/data/digest.test");
37         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "57497ef84a0487f2bb0939a1f5703912");
38
39         p.push_back ("test/data/digest.test2");
40         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "5a3a89857b931755ae728a518224a05c");
41
42         p.clear ();
43         p.push_back ("test/data/digest.test3");
44         p.push_back ("test/data/digest.test");
45         p.push_back ("test/data/digest.test2");
46         p.push_back ("test/data/digest.test4");
47         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "52ccf111e4e72b58bb7b2aaa6bd45ea5");
48
49         p.clear ();
50         p.push_back ("foobar");
51         BOOST_CHECK_THROW (digest_head_tail (p, 1024), OpenFileError);
52 }
53
54 /* Straightforward test of DCPTime::ceil */
55 BOOST_AUTO_TEST_CASE (dcptime_ceil_test)
56 {
57         BOOST_CHECK_EQUAL (DCPTime(0).ceil(DCPTime::HZ / 2).get(), 0);
58         BOOST_CHECK_EQUAL (DCPTime(1).ceil(DCPTime::HZ / 2).get(), 2);
59         BOOST_CHECK_EQUAL (DCPTime(2).ceil(DCPTime::HZ / 2).get(), 2);
60         BOOST_CHECK_EQUAL (DCPTime(3).ceil(DCPTime::HZ / 2).get(), 4);
61
62         BOOST_CHECK_EQUAL (DCPTime(0).ceil(DCPTime::HZ / 42).get(), 0);
63         BOOST_CHECK_EQUAL (DCPTime(1).ceil(DCPTime::HZ / 42).get(), 42);
64         BOOST_CHECK_EQUAL (DCPTime(42).ceil(DCPTime::HZ / 42).get(), 42);
65         BOOST_CHECK_EQUAL (DCPTime(43).ceil(DCPTime::HZ / 42).get(), 84);
66
67         /* Check that rounding up to non-integer frame rates works */
68         BOOST_CHECK_EQUAL (DCPTime(45312).ceil(29.976).get(), 48038);
69 }
70
71 /* Straightforward test of DCPTime::floor */
72 BOOST_AUTO_TEST_CASE (dcptime_floor_test)
73 {
74         BOOST_CHECK_EQUAL (DCPTime(0).floor(DCPTime::HZ / 2).get(), 0);
75         BOOST_CHECK_EQUAL (DCPTime(1).floor(DCPTime::HZ / 2).get(), 0);
76         BOOST_CHECK_EQUAL (DCPTime(2).floor(DCPTime::HZ / 2).get(), 2);
77         BOOST_CHECK_EQUAL (DCPTime(3).floor(DCPTime::HZ / 2).get(), 2);
78
79         BOOST_CHECK_EQUAL (DCPTime(0).floor(DCPTime::HZ / 42).get(), 0);
80         BOOST_CHECK_EQUAL (DCPTime(1).floor(DCPTime::HZ / 42).get(), 0);
81         BOOST_CHECK_EQUAL (DCPTime(42).floor(DCPTime::HZ / 42.0).get(), 42);
82         BOOST_CHECK_EQUAL (DCPTime(43).floor(DCPTime::HZ / 42.0).get(), 42);
83
84         /* Check that rounding down to non-integer frame rates works */
85         BOOST_CHECK_EQUAL (DCPTime(45312).floor(29.976).get(), 44836);
86 }
87
88 BOOST_AUTO_TEST_CASE (timecode_test)
89 {
90         DCPTime t = DCPTime::from_seconds (2 * 60 * 60 + 4 * 60 + 31) + DCPTime::from_frames (19, 24);
91         BOOST_CHECK_EQUAL (t.timecode (24), "02:04:31:19");
92 }
93
94 BOOST_AUTO_TEST_CASE (seconds_to_approximate_hms_test)
95 {
96         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1), "1s");
97         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2), "2s");
98         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (60), "1m");
99         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1.5 * 60), "1m 30s");
100         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 60), "2m");
101         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (17 * 60 + 20), "17m");
102         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1 * 3600), "1h");
103         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (3600 + 40 * 60), "1h 40m");
104         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (13 * 3600 + 40 * 60), "14h");
105 }
106
107 BOOST_AUTO_TEST_CASE (tidy_for_filename_test)
108 {
109         BOOST_CHECK_EQUAL (tidy_for_filename ("fish\\chips"), "fish_chips");
110         BOOST_CHECK_EQUAL (tidy_for_filename ("fish:chips\\"), "fish_chips_");
111         BOOST_CHECK_EQUAL (tidy_for_filename ("fish/chips\\"), "fish_chips_");
112         BOOST_CHECK_EQUAL (tidy_for_filename ("abcdefghï"), "abcdefghï");
113 }