Add some add() methods to Time classes.
[libsub.git] / test / time_test.cc
1 /*
2     Copyright (C) 2014 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 <boost/test/unit_test.hpp>
21 #include "metric_time.h"
22 #include "frame_time.h"
23 #include "time_pair.h"
24
25 /* Check time construction */
26 BOOST_AUTO_TEST_CASE (time_construction_test)
27 {
28         {
29                 sub::MetricTime t (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::MetricTime t (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                 sub::FrameTime t (3 * 60 * 60 * 24 + 31 * 60 * 24 + 4 * 24 + 11, 24);
46                 BOOST_CHECK_EQUAL (t.hours(), 3);
47                 BOOST_CHECK_EQUAL (t.minutes(), 31);
48                 BOOST_CHECK_EQUAL (t.seconds(), 4);
49                 BOOST_CHECK_EQUAL (t.frames(), 11);
50         }
51 }
52
53 /* Check time conversions */
54 BOOST_AUTO_TEST_CASE (time_conversion_test)
55 {
56         sub::TimePair p;
57         
58         /* 40ms = 1 frame at 25fps */
59         p.set_metric (sub::MetricTime (3, 5, 7, 40));
60         BOOST_CHECK_EQUAL (p.frame (25), sub::FrameTime (3, 5, 7, 1));
61         p.set_frame (sub::FrameTime  (3, 5, 7, 1));
62         BOOST_CHECK_EQUAL (p.metric (25), sub::MetricTime (3, 5, 7, 40));
63
64         /* 120ms = 3 frames at 25fps */
65         p.set_metric (sub::MetricTime (3, 5, 7, 120));
66         BOOST_CHECK_EQUAL (p.frame (25), sub::FrameTime (3, 5, 7, 3));
67         p.set_frame (sub::FrameTime (3, 5, 7, 3));
68         BOOST_CHECK_EQUAL (p.metric (25), sub::MetricTime (3, 5, 7, 120));
69 }
70
71 /* Check time maths */
72 BOOST_AUTO_TEST_CASE (time_maths_test)
73 {
74         {
75                 sub::FrameTime a (1, 59, 59, 23);
76                 sub::FrameTime b (2, 31, 19, 2);
77                 a.add (b, 24);
78                 BOOST_CHECK_EQUAL (a, sub::FrameTime (4, 31, 19, 1));
79         }
80
81         {
82                 sub::MetricTime a (1, 59, 59, 999);
83                 sub::MetricTime b (2, 31, 19, 2);
84                 a.add (b);
85                 BOOST_CHECK_EQUAL (a, sub::MetricTime (4, 31, 19, 1));
86         }
87 }