2 Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "lib/audio_ring_buffers.h"
22 #include <boost/test/unit_test.hpp>
26 using boost::shared_ptr;
30 /** Basic tests fetching the same number of channels as went in */
31 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test1)
35 /* Should start off empty */
36 BOOST_CHECK_EQUAL (rb.size(), 0);
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);
45 BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
47 /* clear() should give the same result */
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);
55 BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
57 /* Put some data in */
58 shared_ptr<AudioBuffers> data (new AudioBuffers (6, 91));
60 for (int i = 0; i < 91; ++i) {
61 for (int j = 0; j < 6; ++j) {
62 data->data(j)[i] = value++;
66 BOOST_CHECK_EQUAL (rb.size(), 91);
68 /* Get part of it out */
69 buffer[40 * 6] = CANARY;
70 BOOST_CHECK_EQUAL (rb.get (buffer, 6, 40), false);
72 for (int i = 0; i < 40 * 6; ++i) {
73 BOOST_REQUIRE_EQUAL (buffer[i], check++);
75 BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
76 BOOST_CHECK_EQUAL (rb.size(), 51);
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++);
84 BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
85 BOOST_CHECK_EQUAL (rb.size(), 0);
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);
93 /** Similar tests but fetching more channels than were put in */
94 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test2)
98 /* Put some data in */
99 shared_ptr<AudioBuffers> data (new AudioBuffers (2, 91));
101 for (int i = 0; i < 91; ++i) {
102 for (int j = 0; j < 2; ++j) {
103 data->data(j)[i] = value++;
107 BOOST_CHECK_EQUAL (rb.size(), 91);
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);
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++);
118 for (int j = 2; j < 6; ++j) {
119 BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
122 BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
123 BOOST_CHECK_EQUAL (rb.size(), 51);
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++);
132 for (int j = 2; j < 6; ++j) {
133 BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
136 BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
137 BOOST_CHECK_EQUAL (rb.size(), 0);
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);
145 /** Similar tests but fetching fewer channels than were put in */
146 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test3)
150 /* Put some data in */
151 shared_ptr<AudioBuffers> data (new AudioBuffers (6, 91));
153 for (int i = 0; i < 91; ++i) {
154 for (int j = 0; j < 6; ++j) {
155 data->data(j)[i] = value++;
159 BOOST_CHECK_EQUAL (rb.size(), 91);
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);
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++);
172 BOOST_CHECK_EQUAL (buffer[40 * 2], CANARY);
173 BOOST_CHECK_EQUAL (rb.size(), 51);
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++);
184 BOOST_CHECK_EQUAL (buffer[51 * 2], CANARY);
185 BOOST_CHECK_EQUAL (rb.size(), 0);
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);