Allow repeat-frame to work with 3D.
[dcpomatic.git] / test / dcpomatic_time_test.cc
1 /*
2     Copyright (C) 2015-2017 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/dcpomatic_time_test.cc
22  *  @brief Test Time and TimePeriod classes.
23  *  @ingroup selfcontained
24  */
25
26 #include "lib/dcpomatic_time.h"
27 #include <boost/test/unit_test.hpp>
28 #include <list>
29
30 using std::list;
31
32 BOOST_AUTO_TEST_CASE (dcpomatic_time_test)
33 {
34         FrameRateChange frc (24, 48);
35         int j = 0;
36         int k = 0;
37         for (int64_t i = 0; i < 62000; i += 2000) {
38                 DCPTime d (i);
39                 ContentTime c (d, frc);
40                 BOOST_CHECK_EQUAL (c.frames_floor (24.0), j);
41                 ++k;
42                 if (k == 2) {
43                         ++j;
44                         k = 0;
45                 }
46         }
47 }
48
49 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_overlaps_test)
50 {
51         /* Taking times as the start of a sampling interval
52
53            |--|--|--|--|--|--|--|--|--|--|
54            0  1  2  3  4  5  6  7  8  9  |
55            |--|--|--|--|--|--|--|--|--|--|
56
57            <------a----><----b----->
58
59            and saying `from' is the start of the first sampling
60            interval and `to' is the start of the interval after
61            the period... a and b do not overlap.
62         */
63
64         TimePeriod<DCPTime> a (DCPTime (0), DCPTime (4));
65         TimePeriod<DCPTime> b (DCPTime (4), DCPTime (8));
66         BOOST_CHECK (!a.overlap (b));
67
68         /* Some more obvious non-overlaps */
69         a = TimePeriod<DCPTime> (DCPTime (0), DCPTime (4));
70         b = TimePeriod<DCPTime> (DCPTime (5), DCPTime (8));
71         BOOST_CHECK (!a.overlap (b));
72
73         /* Some overlaps */
74         a = TimePeriod<DCPTime> (DCPTime (0), DCPTime (4));
75         b = TimePeriod<DCPTime> (DCPTime (3), DCPTime (8));
76         BOOST_CHECK (a.overlap(b));
77         BOOST_CHECK (a.overlap(b).get() == DCPTimePeriod(DCPTime(3), DCPTime(4)));
78         a = TimePeriod<DCPTime> (DCPTime (1), DCPTime (9));
79         b = TimePeriod<DCPTime> (DCPTime (0), DCPTime (10));
80         BOOST_CHECK (a.overlap(b));
81         BOOST_CHECK (a.overlap(b).get() == DCPTimePeriod(DCPTime(1), DCPTime(9)));
82 }
83
84 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test1)
85 {
86         DCPTimePeriod A (DCPTime (0), DCPTime (106));
87         list<DCPTimePeriod> B;
88         B.push_back (DCPTimePeriod (DCPTime (0), DCPTime (42)));
89         B.push_back (DCPTimePeriod (DCPTime (52), DCPTime (91)));
90         B.push_back (DCPTimePeriod (DCPTime (94), DCPTime (106)));
91         list<DCPTimePeriod> r = subtract (A, B);
92         list<DCPTimePeriod>::const_iterator i = r.begin ();
93         BOOST_REQUIRE (i != r.end ());
94         BOOST_CHECK (i->from == DCPTime (42));
95         BOOST_CHECK (i->to == DCPTime (52));
96         ++i;
97         BOOST_REQUIRE (i != r.end ());
98         BOOST_CHECK (i->from == DCPTime (91));
99         BOOST_CHECK (i->to == DCPTime (94));
100         ++i;
101         BOOST_REQUIRE (i == r.end ());
102 }
103
104 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test2)
105 {
106         DCPTimePeriod A (DCPTime (0), DCPTime (106));
107         list<DCPTimePeriod> B;
108         B.push_back (DCPTimePeriod (DCPTime (14), DCPTime (42)));
109         B.push_back (DCPTimePeriod (DCPTime (52), DCPTime (91)));
110         B.push_back (DCPTimePeriod (DCPTime (94), DCPTime (106)));
111         list<DCPTimePeriod> r = subtract (A, B);
112         list<DCPTimePeriod>::const_iterator i = r.begin ();
113         BOOST_REQUIRE (i != r.end ());
114         BOOST_CHECK (i->from == DCPTime (0));
115         BOOST_CHECK (i->to == DCPTime (14));
116         ++i;
117         BOOST_REQUIRE (i != r.end ());
118         BOOST_CHECK (i->from == DCPTime (42));
119         BOOST_CHECK (i->to == DCPTime (52));
120         ++i;
121         BOOST_REQUIRE (i != r.end ());
122         BOOST_CHECK (i->from == DCPTime (91));
123         BOOST_CHECK (i->to == DCPTime (94));
124         ++i;
125         BOOST_REQUIRE (i == r.end ());
126 }
127
128 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test3)
129 {
130         DCPTimePeriod A (DCPTime (0), DCPTime (106));
131         list<DCPTimePeriod> B;
132         B.push_back (DCPTimePeriod (DCPTime (14), DCPTime (42)));
133         B.push_back (DCPTimePeriod (DCPTime (52), DCPTime (91)));
134         B.push_back (DCPTimePeriod (DCPTime (94), DCPTime (99)));
135         list<DCPTimePeriod> r = subtract (A, B);
136         list<DCPTimePeriod>::const_iterator i = r.begin ();
137         BOOST_REQUIRE (i != r.end ());
138         BOOST_CHECK (i->from == DCPTime (0));
139         BOOST_CHECK (i->to == DCPTime (14));
140         ++i;
141         BOOST_REQUIRE (i != r.end ());
142         BOOST_CHECK (i->from == DCPTime (42));
143         BOOST_CHECK (i->to == DCPTime (52));
144         ++i;
145         BOOST_REQUIRE (i != r.end ());
146         BOOST_CHECK (i->from == DCPTime (91));
147         BOOST_CHECK (i->to == DCPTime (94));
148         ++i;
149         BOOST_REQUIRE (i != r.end ());
150         BOOST_CHECK (i->from == DCPTime (99));
151         BOOST_CHECK (i->to == DCPTime (106));
152         ++i;
153         BOOST_REQUIRE (i == r.end ());
154 }
155
156 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test4)
157 {
158         DCPTimePeriod A (DCPTime (0), DCPTime (106));
159         list<DCPTimePeriod> B;
160         list<DCPTimePeriod> r = subtract (A, B);
161         list<DCPTimePeriod>::const_iterator i = r.begin ();
162         BOOST_REQUIRE (i != r.end ());
163         BOOST_CHECK (i->from == DCPTime (0));
164         BOOST_CHECK (i->to == DCPTime (106));
165         ++i;
166         BOOST_REQUIRE (i == r.end ());
167 }
168
169 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test5)
170 {
171         DCPTimePeriod A (DCPTime (0), DCPTime (106));
172         list<DCPTimePeriod> B;
173         B.push_back (DCPTimePeriod (DCPTime (14), DCPTime (42)));
174         B.push_back (DCPTimePeriod (DCPTime (42), DCPTime (91)));
175         B.push_back (DCPTimePeriod (DCPTime (94), DCPTime (99)));
176         list<DCPTimePeriod> r = subtract (A, B);
177         list<DCPTimePeriod>::const_iterator i = r.begin ();
178         BOOST_REQUIRE (i != r.end ());
179         BOOST_CHECK (i->from == DCPTime (0));
180         BOOST_CHECK (i->to == DCPTime (14));
181         ++i;
182         BOOST_REQUIRE (i != r.end ());
183         BOOST_CHECK (i->from ==DCPTime (91));
184         BOOST_CHECK (i->to == DCPTime (94));
185         ++i;
186         BOOST_REQUIRE (i != r.end ());
187         BOOST_CHECK (i->from == DCPTime (99));
188         BOOST_CHECK (i->to == DCPTime (106));
189         ++i;
190         BOOST_REQUIRE (i == r.end ());
191 }
192
193 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test6)
194 {
195         DCPTimePeriod A (DCPTime (0), DCPTime (106));
196         list<DCPTimePeriod> B;
197         B.push_back (DCPTimePeriod (DCPTime (0), DCPTime (42)));
198         B.push_back (DCPTimePeriod (DCPTime (42), DCPTime (91)));
199         B.push_back (DCPTimePeriod (DCPTime (91), DCPTime (106)));
200         list<DCPTimePeriod> r = subtract (A, B);
201         BOOST_CHECK (r.empty());
202 }
203
204 BOOST_AUTO_TEST_CASE (dcpomatic_time_period_subtract_test7)
205 {
206         DCPTimePeriod A (DCPTime (228), DCPTime (356));
207         list<DCPTimePeriod> B;
208         B.push_back (DCPTimePeriod (DCPTime (34), DCPTime (162)));
209         list<DCPTimePeriod> r = subtract (A, B);
210         list<DCPTimePeriod>::const_iterator i = r.begin ();
211         BOOST_REQUIRE (i != r.end ());
212         BOOST_CHECK (i->from == DCPTime (228));
213         BOOST_CHECK (i->to == DCPTime (356));
214         ++i;
215         BOOST_REQUIRE (i == r.end ());
216 }