Fix seconds_to_approximate_hms sometimes saying things like 1h60m (#1314).
[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  *  @ingroup selfcontained
24  */
25
26 #include "lib/util.h"
27 #include "lib/exceptions.h"
28 #include <boost/test/unit_test.hpp>
29
30 using std::string;
31 using std::vector;
32 using boost::shared_ptr;
33
34 BOOST_AUTO_TEST_CASE (digest_head_tail_test)
35 {
36         vector<boost::filesystem::path> p;
37         p.push_back ("test/data/digest.test");
38         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "57497ef84a0487f2bb0939a1f5703912");
39
40         p.push_back ("test/data/digest.test2");
41         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "5a3a89857b931755ae728a518224a05c");
42
43         p.clear ();
44         p.push_back ("test/data/digest.test3");
45         p.push_back ("test/data/digest.test");
46         p.push_back ("test/data/digest.test2");
47         p.push_back ("test/data/digest.test4");
48         BOOST_CHECK_EQUAL (digest_head_tail (p, 1024), "52ccf111e4e72b58bb7b2aaa6bd45ea5");
49
50         p.clear ();
51         p.push_back ("foobar");
52         BOOST_CHECK_THROW (digest_head_tail (p, 1024), OpenFileError);
53 }
54
55 BOOST_AUTO_TEST_CASE (timecode_test)
56 {
57         DCPTime t = DCPTime::from_seconds (2 * 60 * 60 + 4 * 60 + 31) + DCPTime::from_frames (19, 24);
58         BOOST_CHECK_EQUAL (t.timecode (24), "02:04:31:19");
59 }
60
61 BOOST_AUTO_TEST_CASE (seconds_to_approximate_hms_test)
62 {
63         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1), "1s");
64         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2), "2s");
65         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (60), "1m");
66         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1.5 * 60), "1m 30s");
67         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 60), "2m");
68         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (17 * 60 + 20), "17m");
69         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1 * 3600), "1h");
70         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (3600 + 40 * 60), "1h 40m");
71         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 3600), "2h");
72         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 3600 - 1), "2h");
73         BOOST_CHECK_EQUAL (seconds_to_approximate_hms (13 * 3600 + 40 * 60), "14h");
74 }
75
76 BOOST_AUTO_TEST_CASE (time_to_hmsf_test)
77 {
78         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_frames(12, 24), 24), "0:00:00.12");
79         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_frames(24, 24), 24), "0:00:01.0");
80         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_frames(32, 24), 24), "0:00:01.8");
81         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_seconds(92), 24), "0:01:32.0");
82         BOOST_CHECK_EQUAL (time_to_hmsf(DCPTime::from_seconds(2 * 60 * 60 + 92), 24), "2:01:32.0");
83 }
84
85 BOOST_AUTO_TEST_CASE (tidy_for_filename_test)
86 {
87         BOOST_CHECK_EQUAL (tidy_for_filename ("fish\\chips"), "fish_chips");
88         BOOST_CHECK_EQUAL (tidy_for_filename ("fish:chips\\"), "fish_chips_");
89         BOOST_CHECK_EQUAL (tidy_for_filename ("fish/chips\\"), "fish_chips_");
90         BOOST_CHECK_EQUAL (tidy_for_filename ("abcdefghï"), "abcdefghï");
91 }