2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include "exceptions.h"
22 #include <boost/test/unit_test.hpp>
25 /* Check time construction */
26 BOOST_AUTO_TEST_CASE (time_construction_test)
29 sub::Time t = sub::Time::from_hms (3, 5, 7, 40);
30 BOOST_CHECK_EQUAL (t.hours(), 3);
31 BOOST_CHECK_EQUAL (t.minutes(), 5);
32 BOOST_CHECK_EQUAL (t.seconds(), 7);
33 BOOST_CHECK_EQUAL (t.milliseconds(), 40);
37 sub::Time t = sub::Time::from_hms (591353, 1, 2, 3);
38 BOOST_CHECK_EQUAL (t.hours(), 591353);
39 BOOST_CHECK_EQUAL (t.minutes(), 1);
40 BOOST_CHECK_EQUAL (t.seconds(), 2);
41 BOOST_CHECK_EQUAL (t.milliseconds(), 3);
45 /* Check time conversions */
46 BOOST_AUTO_TEST_CASE (time_conversion_test)
50 /* 40ms = 1 frame at 25fps */
51 p = sub::Time::from_hms (3, 5, 7, 40);
52 BOOST_CHECK_EQUAL (p.frames_at (sub::Rational (25, 1)), 1);
53 p = sub::Time::from_hmsf (3, 5, 7, 1, sub::Rational (25, 1));
54 BOOST_CHECK_EQUAL (p.milliseconds (), 40);
56 /* 120ms = 3 frames at 25fps */
57 p = sub::Time::from_hms (3, 5, 7, 120);
58 BOOST_CHECK_EQUAL (p.frames_at (sub::Rational (25, 1)), 3);
59 p = sub::Time::from_hmsf (3, 5, 7, 3, sub::Rational (25, 1));
60 BOOST_CHECK_EQUAL (p.milliseconds (), 120);
63 /* Check some operators */
64 BOOST_AUTO_TEST_CASE (time_operator_test)
66 BOOST_CHECK_EQUAL (sub::Time::from_hms (0, 0, 5, 198 * 4), sub::Time::from_hms (0, 0, 5, 198 * 4));
67 BOOST_CHECK (sub::Time::from_hms (0, 0, 55, 332) != sub::Time::from_hms (0, 0, 58, 332));
70 /* Check some other bits of Time */
71 BOOST_AUTO_TEST_CASE (time_other_test)
73 BOOST_CHECK_THROW (sub::Time::from_hmsf (2, 1, 58, 4).all_as_seconds(), sub::UnknownFrameRateError);
74 BOOST_CHECK_CLOSE (sub::Time::from_hmsf (2, 1, 58, 4, sub::Rational (24, 1)).all_as_seconds(), 7318.1667, 0.001);
77 /* Check an addition case that gave an odd result */
78 BOOST_AUTO_TEST_CASE (time_add_test)
80 sub::Time t = sub::Time::from_hmsf (0, 4, 8, 208, sub::Rational(1000, 1));
81 t.add (sub::Time::from_frames(54641, sub::Rational(24, 1)));
82 BOOST_CHECK_EQUAL (t, sub::Time::from_hmsf (0, 42, 4, 916, sub::Rational(1000, 1)));
85 BOOST_AUTO_TEST_CASE (time_scale_test1)
87 sub::Time t = sub::Time::from_hmsf (0, 0, 1, 0, sub::Rational(1000, 1));
89 BOOST_CHECK_EQUAL (t.seconds(), 0);
90 BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 960);
93 BOOST_AUTO_TEST_CASE (time_scale_test2)
95 sub::Time t = sub::Time::from_hmsf (0, 0, 2, 0, sub::Rational(1000, 1));
97 BOOST_CHECK_EQUAL (t.seconds(), 1);
98 BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 920);