Cleanup: test tidying.
[dcpomatic.git] / test / shuffler_test.cc
1 /*
2     Copyright (C) 2020-2021 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
22 #include "lib/content_video.h"
23 #include "lib/piece.h"
24 #include "lib/shuffler.h"
25 #include <boost/test/unit_test.hpp>
26
27
28 using std::list;
29 using std::make_shared;
30 using std::shared_ptr;
31 using std::weak_ptr;
32 using boost::optional;
33 #if BOOST_VERSION >= 106100
34 using namespace boost::placeholders;
35 #endif
36
37
38 static void
39 push (Shuffler& s, int frame, Eyes eyes)
40 {
41         auto piece = make_shared<Piece>(shared_ptr<Content>(), shared_ptr<Decoder>(), FrameRateChange(24, 24));
42         ContentVideo cv;
43         cv.frame = frame;
44         cv.eyes = eyes;
45         s.video (piece, cv);
46 }
47
48 list<ContentVideo> pending_cv;
49
50 static void
51 receive (weak_ptr<Piece>, ContentVideo cv)
52 {
53         pending_cv.push_back (cv);
54 }
55
56 static void
57 check (int frame, Eyes eyes, int line)
58 {
59         BOOST_REQUIRE_MESSAGE (!pending_cv.empty(), "Check at " << line << " failed.");
60         BOOST_CHECK_MESSAGE (pending_cv.front().frame == frame, "Check at " << line << " failed.");
61         BOOST_CHECK_MESSAGE (pending_cv.front().eyes == eyes, "Check at " << line << " failed.");
62         pending_cv.pop_front();
63 }
64
65 /** A perfect sequence */
66 BOOST_AUTO_TEST_CASE (shuffler_test1)
67 {
68         Shuffler s;
69         s.Video.connect (boost::bind (&receive, _1, _2));
70
71         for (int i = 0; i < 10; ++i) {
72                 push (s, i, Eyes::LEFT);
73                 push (s, i, Eyes::RIGHT);
74                 check (i, Eyes::LEFT, __LINE__);
75                 check (i, Eyes::RIGHT, __LINE__);
76         }
77 }
78
79 /** Everything present but some simple shuffling needed */
80 BOOST_AUTO_TEST_CASE (shuffler_test2)
81 {
82         Shuffler s;
83         s.Video.connect (boost::bind (&receive, _1, _2));
84
85         for (int i = 0; i < 10; i += 2) {
86                 push (s, i, Eyes::LEFT);
87                 push (s, i + 1, Eyes::LEFT);
88                 push (s, i, Eyes::RIGHT);
89                 push (s, i + 1, Eyes::RIGHT);
90                 check (i, Eyes::LEFT, __LINE__);
91                 check (i, Eyes::RIGHT, __LINE__);
92                 check (i + 1, Eyes::LEFT, __LINE__);
93                 check (i + 1, Eyes::RIGHT, __LINE__);
94         }
95 }
96
97 /** One missing left eye image */
98 BOOST_AUTO_TEST_CASE (shuffler_test3)
99 {
100         Shuffler s;
101         s.Video.connect (boost::bind (&receive, _1, _2));
102
103         push (s, 0, Eyes::LEFT);
104         check (0, Eyes::LEFT, __LINE__);
105         push (s, 0, Eyes::RIGHT);
106         check (0, Eyes::RIGHT, __LINE__);
107         push (s, 1, Eyes::LEFT);
108         check (1, Eyes::LEFT, __LINE__);
109         push (s, 1, Eyes::RIGHT);
110         check (1, Eyes::RIGHT, __LINE__);
111         push (s, 2, Eyes::RIGHT);
112         push (s, 3, Eyes::LEFT);
113         push (s, 3, Eyes::RIGHT);
114         push (s, 4, Eyes::LEFT);
115         push (s, 4, Eyes::RIGHT);
116         s.flush ();
117         check (2, Eyes::RIGHT, __LINE__);
118         check (3, Eyes::LEFT, __LINE__);
119         check (3, Eyes::RIGHT, __LINE__);
120         check (4, Eyes::LEFT, __LINE__);
121         check (4, Eyes::RIGHT, __LINE__);
122 }
123
124 /** One missing right eye image */
125 BOOST_AUTO_TEST_CASE (shuffler_test4)
126 {
127         Shuffler s;
128         s.Video.connect (boost::bind (&receive, _1, _2));
129
130         push (s, 0, Eyes::LEFT);
131         check (0, Eyes::LEFT, __LINE__);
132         push (s, 0, Eyes::RIGHT);
133         check (0, Eyes::RIGHT, __LINE__);
134         push (s, 1, Eyes::LEFT);
135         check (1, Eyes::LEFT, __LINE__);
136         push (s, 1, Eyes::RIGHT);
137         check (1, Eyes::RIGHT, __LINE__);
138         push (s, 2, Eyes::LEFT);
139         push (s, 3, Eyes::LEFT);
140         push (s, 3, Eyes::RIGHT);
141         push (s, 4, Eyes::LEFT);
142         push (s, 4, Eyes::RIGHT);
143         s.flush ();
144         check (2, Eyes::LEFT, __LINE__);
145         check (3, Eyes::LEFT, __LINE__);
146         check (3, Eyes::RIGHT, __LINE__);
147         check (4, Eyes::LEFT, __LINE__);
148         check (4, Eyes::RIGHT, __LINE__);
149 }
150
151 /** Only one eye */
152 BOOST_AUTO_TEST_CASE (shuffler_test5)
153 {
154         Shuffler s;
155         s.Video.connect (boost::bind (&receive, _1, _2));
156
157         /* One left should come out straight away */
158         push (s, 0, Eyes::LEFT);
159         check (0, Eyes::LEFT, __LINE__);
160
161         /* More lefts should be kept in the shuffler in the hope that some rights arrive */
162         for (int i = 0; i < s._max_size; ++i) {
163                 push (s, i + 1, Eyes::LEFT);
164         }
165         BOOST_CHECK (pending_cv.empty ());
166
167         /* If enough lefts come the shuffler should conclude that there's no rights and start
168            giving out the lefts.
169         */
170         push (s, s._max_size + 1, Eyes::LEFT);
171         check (1, Eyes::LEFT, __LINE__);
172 }
173
174 /** One complete frame (L+R) missing.
175     Shuffler should carry on, skipping this frame, as the player will cope with it.
176 */
177 BOOST_AUTO_TEST_CASE (shuffler_test6)
178 {
179         Shuffler s;
180         s.Video.connect (boost::bind (&receive, _1, _2));
181
182         push (s, 0, Eyes::LEFT);
183         check (0, Eyes::LEFT, __LINE__);
184         push (s, 0, Eyes::RIGHT);
185         check (0, Eyes::RIGHT, __LINE__);
186
187         push (s, 2, Eyes::LEFT);
188         push (s, 2, Eyes::RIGHT);
189         check (2, Eyes::LEFT, __LINE__);
190         check (2, Eyes::RIGHT, __LINE__);
191
192         push (s, 3, Eyes::LEFT);
193         check (3, Eyes::LEFT, __LINE__);
194         push (s, 3, Eyes::RIGHT);
195         check (3, Eyes::RIGHT, __LINE__);
196 }