Fix binary STL output with subs that are too big for a single TTI block.
[libsub.git] / test / time_test.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "sub_time.h"
21 #include "exceptions.h"
22 #include <boost/test/unit_test.hpp>
23 #include <iostream>
24
25 /* Check time construction */
26 BOOST_AUTO_TEST_CASE (time_construction_test)
27 {
28         {
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);
34         }
35
36         {
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);
42         }
43 }
44
45 /* Check time conversions */
46 BOOST_AUTO_TEST_CASE (time_conversion_test)
47 {
48         sub::Time p;
49
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);
55
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);
61 }
62
63 /* Check some operators */
64 BOOST_AUTO_TEST_CASE (time_operator_test)
65 {
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));
68 }
69
70 /* Check some other bits of Time */
71 BOOST_AUTO_TEST_CASE (time_other_test)
72 {
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);
75 }
76
77 /* Check an addition case that gave an odd result */
78 BOOST_AUTO_TEST_CASE (time_add_test)
79 {
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)));
83 }
84
85 BOOST_AUTO_TEST_CASE (time_scale_test1)
86 {
87         sub::Time t = sub::Time::from_hmsf (0, 0, 1, 0, sub::Rational(1000, 1));
88         t.scale (0.96);
89         BOOST_CHECK_EQUAL (t.seconds(), 0);
90         BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 960);
91 }
92
93 BOOST_AUTO_TEST_CASE (time_scale_test2)
94 {
95         sub::Time t = sub::Time::from_hmsf (0, 0, 2, 0, sub::Rational(1000, 1));
96         t.scale (0.96);
97         BOOST_CHECK_EQUAL (t.seconds(), 1);
98         BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 920);
99 }