Note and indicate servers with bad link version (#982).
[dcpomatic.git] / test / audio_ring_buffers_test.cc
1 /*
2     Copyright (C) 2016-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 #include "lib/audio_ring_buffers.h"
22 #include <boost/test/unit_test.hpp>
23 #include <iostream>
24
25 using std::cout;
26 using boost::shared_ptr;
27
28 #define CANARY 9999
29
30 /** Basic tests fetching the same number of channels as went in */
31 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test1)
32 {
33         AudioRingBuffers rb;
34
35         /* Should start off empty */
36         BOOST_CHECK_EQUAL (rb.size(), 0);
37
38         /* Getting some data should give an underrun and write zeros */
39         float buffer[256 * 6];
40         buffer[240 * 6] = CANARY;
41         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
42         for (int i = 0; i < 240 * 6; ++i) {
43                 BOOST_REQUIRE_EQUAL (buffer[i], 0);
44         }
45         BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
46
47         /* clear() should give the same result */
48         rb.clear ();
49         BOOST_CHECK_EQUAL (rb.size(), 0);
50         buffer[240 * 6] = CANARY;
51         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
52         for (int i = 0; i < 240 * 6; ++i) {
53                 BOOST_REQUIRE_EQUAL (buffer[i], 0);
54         }
55         BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
56
57         /* Put some data in */
58         shared_ptr<AudioBuffers> data (new AudioBuffers (6, 91));
59         int value = 0;
60         for (int i = 0; i < 91; ++i) {
61                 for (int j = 0; j < 6; ++j) {
62                         data->data(j)[i] = value++;
63                 }
64         }
65         rb.put (data);
66         BOOST_CHECK_EQUAL (rb.size(), 91);
67
68         /* Get part of it out */
69         buffer[40 * 6] = CANARY;
70         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 40), false);
71         int check = 0;
72         for (int i = 0; i < 40 * 6; ++i) {
73                 BOOST_REQUIRE_EQUAL (buffer[i], check++);
74         }
75         BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
76         BOOST_CHECK_EQUAL (rb.size(), 51);
77
78         /* Get the rest */
79         buffer[51 * 6] = CANARY;
80         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 51), false);
81         for (int i = 0; i < 51 * 6; ++i) {
82                 BOOST_REQUIRE_EQUAL (buffer[i], check++);
83         }
84         BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
85         BOOST_CHECK_EQUAL (rb.size(), 0);
86
87         /* Now there should be an underrun */
88         buffer[240 * 6] = CANARY;
89         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
90         BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
91 }
92
93 /** Similar tests but fetching more channels than were put in */
94 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test2)
95 {
96         AudioRingBuffers rb;
97
98         /* Put some data in */
99         shared_ptr<AudioBuffers> data (new AudioBuffers (2, 91));
100         int value = 0;
101         for (int i = 0; i < 91; ++i) {
102                 for (int j = 0; j < 2; ++j) {
103                         data->data(j)[i] = value++;
104                 }
105         }
106         rb.put (data);
107         BOOST_CHECK_EQUAL (rb.size(), 91);
108
109         /* Get part of it out */
110         float buffer[256 * 6];
111         buffer[40 * 6] = CANARY;
112         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 40), false);
113         int check = 0;
114         for (int i = 0; i < 40; ++i) {
115                 for (int j = 0; j < 2; ++j) {
116                         BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], check++);
117                 }
118                 for (int j = 2; j < 6; ++j) {
119                         BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
120                 }
121         }
122         BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
123         BOOST_CHECK_EQUAL (rb.size(), 51);
124
125         /* Get the rest */
126         buffer[51 * 6] = CANARY;
127         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 51), false);
128         for (int i = 0; i < 51; ++i) {
129                 for (int j = 0; j < 2; ++j) {
130                         BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], check++);
131                 }
132                 for (int j = 2; j < 6; ++j) {
133                         BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
134                 }
135         }
136         BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
137         BOOST_CHECK_EQUAL (rb.size(), 0);
138
139         /* Now there should be an underrun */
140         buffer[240 * 6] = CANARY;
141         BOOST_CHECK_EQUAL (rb.get (buffer, 6, 240), true);
142         BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
143 }
144
145 /** Similar tests but fetching fewer channels than were put in */
146 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test3)
147 {
148         AudioRingBuffers rb;
149
150         /* Put some data in */
151         shared_ptr<AudioBuffers> data (new AudioBuffers (6, 91));
152         int value = 0;
153         for (int i = 0; i < 91; ++i) {
154                 for (int j = 0; j < 6; ++j) {
155                         data->data(j)[i] = value++;
156                 }
157         }
158         rb.put (data);
159         BOOST_CHECK_EQUAL (rb.size(), 91);
160
161         /* Get part of it out */
162         float buffer[256 * 6];
163         buffer[40 * 2] = CANARY;
164         BOOST_CHECK_EQUAL (rb.get (buffer, 2, 40), false);
165         int check = 0;
166         for (int i = 0; i < 40; ++i) {
167                 for (int j = 0; j < 2; ++j) {
168                         BOOST_REQUIRE_EQUAL (buffer[i * 2 + j], check++);
169                 }
170                 check += 4;
171         }
172         BOOST_CHECK_EQUAL (buffer[40 * 2], CANARY);
173         BOOST_CHECK_EQUAL (rb.size(), 51);
174
175         /* Get the rest */
176         buffer[51 * 2] = CANARY;
177         BOOST_CHECK_EQUAL (rb.get (buffer, 2, 51), false);
178         for (int i = 0; i < 51; ++i) {
179                 for (int j = 0; j < 2; ++j)  {
180                         BOOST_REQUIRE_EQUAL (buffer[i * 2 + j], check++);
181                 }
182                 check += 4;
183         }
184         BOOST_CHECK_EQUAL (buffer[51 * 2], CANARY);
185         BOOST_CHECK_EQUAL (rb.size(), 0);
186
187         /* Now there should be an underrun */
188         buffer[240 * 2] = CANARY;
189         BOOST_CHECK_EQUAL (rb.get (buffer, 2, 240), true);
190         BOOST_CHECK_EQUAL (buffer[240 * 2], CANARY);
191 }