dev@1430
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
dev@1430
|
2
|
dev@1430
|
3 /*
|
dev@1430
|
4 Sonic Visualiser
|
dev@1430
|
5 An audio file viewer and annotation editor.
|
dev@1430
|
6 Centre for Digital Music, Queen Mary, University of London.
|
dev@1438
|
7 This file copyright 2017 Queen Mary, University of London.
|
dev@1430
|
8
|
dev@1430
|
9 This program is free software; you can redistribute it and/or
|
dev@1430
|
10 modify it under the terms of the GNU General Public License as
|
dev@1430
|
11 published by the Free Software Foundation; either version 2 of the
|
dev@1430
|
12 License, or (at your option) any later version. See the file
|
dev@1430
|
13 COPYING included with this distribution for more information.
|
dev@1430
|
14 */
|
dev@1430
|
15
|
dev@1430
|
16 #ifndef TEST_CSV_STREAM_H
|
dev@1430
|
17 #define TEST_CSV_STREAM_H
|
dev@1430
|
18
|
dev@1434
|
19 #include <QtTest>
|
dev@1430
|
20 #include <QObject>
|
dev@1434
|
21 #include <sstream>
|
dev@1434
|
22 #include <functional>
|
dev@1434
|
23
|
dev@1434
|
24 #include "base/ProgressReporter.h"
|
dev@1434
|
25 #include "base/DataExportOptions.h"
|
dev@1446
|
26 #include "base/Selection.h"
|
dev@1430
|
27 #include "../CSVStreamWriter.h"
|
dev@1434
|
28 #include "../../model/test/MockWaveModel.h"
|
dev@1434
|
29
|
dev@1434
|
30 namespace
|
dev@1434
|
31 {
|
dev@1434
|
32 class StubReporter : public ProgressReporter
|
dev@1434
|
33 {
|
dev@1434
|
34 public:
|
dev@1434
|
35 StubReporter( std::function<bool()> isCancelled )
|
dev@1434
|
36 : m_isCancelled(isCancelled) {}
|
dev@1434
|
37 bool isDefinite() const override { return true; }
|
dev@1434
|
38 void setDefinite(bool) override {}
|
dev@1434
|
39 bool wasCancelled() const override { return m_isCancelled(); }
|
dev@1434
|
40 void setMessage(QString) override {}
|
dev@1440
|
41 void setProgress(int p) override
|
dev@1440
|
42 {
|
dev@1440
|
43 ++m_calls;
|
dev@1440
|
44 m_percentageLog.push_back(p);
|
dev@1440
|
45 }
|
dev@1440
|
46
|
dev@1434
|
47 size_t getCallCount() const { return m_calls; }
|
dev@1440
|
48 std::vector<int> getPercentageLog() const { return m_percentageLog; }
|
dev@1434
|
49 void reset() { m_calls = 0; }
|
dev@1434
|
50 private:
|
dev@1434
|
51 size_t m_calls = 0;
|
dev@1434
|
52 std::function<bool()> m_isCancelled;
|
dev@1440
|
53 std::vector<int> m_percentageLog;
|
dev@1434
|
54 };
|
dev@1434
|
55 } // namespace
|
dev@1430
|
56
|
dev@1430
|
57 class CSVStreamWriterTest : public QObject
|
dev@1430
|
58 {
|
dev@1430
|
59 Q_OBJECT
|
dev@1430
|
60 public:
|
dev@1434
|
61 std::string getExpectedString()
|
dev@1434
|
62 {
|
dev@1434
|
63 return
|
dev@1434
|
64 {
|
dev@1434
|
65 "0,0,0\n"
|
dev@1434
|
66 "1,0,0\n"
|
dev@1434
|
67 "2,0,0\n"
|
dev@1434
|
68 "3,0,0\n"
|
dev@1434
|
69 "4,1,1\n"
|
dev@1434
|
70 "5,1,1\n"
|
dev@1434
|
71 "6,1,1\n"
|
dev@1434
|
72 "7,1,1\n"
|
dev@1434
|
73 "8,1,1\n"
|
dev@1434
|
74 "9,1,1\n"
|
dev@1434
|
75 "10,1,1\n"
|
dev@1434
|
76 "11,1,1\n"
|
dev@1434
|
77 "12,1,1\n"
|
dev@1434
|
78 "13,1,1\n"
|
dev@1434
|
79 "14,1,1\n"
|
dev@1434
|
80 "15,1,1\n"
|
dev@1434
|
81 "16,1,1\n"
|
dev@1434
|
82 "17,1,1\n"
|
dev@1434
|
83 "18,1,1\n"
|
dev@1434
|
84 "19,1,1\n"
|
dev@1434
|
85 "20,0,0\n"
|
dev@1434
|
86 "21,0,0\n"
|
dev@1434
|
87 "22,0,0\n"
|
dev@1434
|
88 "23,0,0"
|
dev@1434
|
89 };
|
dev@1434
|
90 }
|
dev@1430
|
91
|
dev@1430
|
92 private slots:
|
dev@1434
|
93 void simpleValidOutput()
|
dev@1430
|
94 {
|
dev@1434
|
95 MockWaveModel mwm({ DC, DC }, 16, 4);
|
dev@1430
|
96
|
dev@1434
|
97 std::ostringstream oss;
|
dev@1438
|
98 const auto result = CSVStreamWriter::writeInChunks(oss, mwm);
|
dev@1434
|
99 QVERIFY( oss.str() == getExpectedString() );
|
dev@1434
|
100 QVERIFY( result );
|
dev@1434
|
101 }
|
dev@1434
|
102
|
dev@1434
|
103 void callsReporterCorrectTimes()
|
dev@1434
|
104 {
|
dev@1434
|
105 MockWaveModel mwm({ DC, DC }, 16, 4);
|
dev@1434
|
106 StubReporter reporter { []() -> bool { return false; } };
|
dev@1434
|
107 const auto expected = getExpectedString();
|
dev@1434
|
108
|
dev@1434
|
109 std::ostringstream oss;
|
dev@1434
|
110 const auto writeStreamWithBlockSize = [&](int blockSize) {
|
dev@1438
|
111 return CSVStreamWriter::writeInChunks(
|
dev@1434
|
112 oss,
|
dev@1434
|
113 mwm,
|
dev@1434
|
114 &reporter,
|
dev@1434
|
115 ",",
|
dev@1434
|
116 DataExportDefaults,
|
dev@1434
|
117 blockSize
|
dev@1434
|
118 );
|
dev@1434
|
119 };
|
dev@1434
|
120
|
dev@1434
|
121 const auto reset = [&]() {
|
dev@1434
|
122 oss.str({});
|
dev@1434
|
123 reporter.reset();
|
dev@1434
|
124 };
|
dev@1434
|
125
|
dev@1434
|
126 const auto nonIntegerMultipleResult = writeStreamWithBlockSize(5);
|
dev@1434
|
127 QVERIFY( nonIntegerMultipleResult );
|
dev@1434
|
128 QVERIFY( reporter.getCallCount() == 5 /* 4.8 rounded up */ );
|
dev@1434
|
129 QVERIFY( oss.str() == expected );
|
dev@1434
|
130 reset();
|
dev@1434
|
131
|
dev@1434
|
132 const auto integerMultiple = writeStreamWithBlockSize(2);
|
dev@1434
|
133 QVERIFY( integerMultiple );
|
dev@1434
|
134 QVERIFY( reporter.getCallCount() == 12 );
|
dev@1434
|
135 QVERIFY( oss.str() == expected );
|
dev@1434
|
136 reset();
|
dev@1434
|
137
|
dev@1434
|
138 const auto largerThanNumberOfSamples = writeStreamWithBlockSize(100);
|
dev@1434
|
139 QVERIFY( largerThanNumberOfSamples );
|
dev@1434
|
140 QVERIFY( reporter.getCallCount() == 1 );
|
dev@1434
|
141 QVERIFY( oss.str() == expected );
|
dev@1434
|
142 reset();
|
dev@1434
|
143
|
dev@1434
|
144 const auto zero = writeStreamWithBlockSize(0);
|
dev@1434
|
145 QVERIFY( zero == false );
|
dev@1434
|
146 QVERIFY( reporter.getCallCount() == 0 );
|
dev@1434
|
147 }
|
dev@1434
|
148
|
dev@1434
|
149 void isCancellable()
|
dev@1434
|
150 {
|
dev@1434
|
151 MockWaveModel mwm({ DC, DC }, 16, 4);
|
dev@1434
|
152 StubReporter reporter { []() -> bool { return true; } };
|
dev@1434
|
153
|
dev@1434
|
154 std::ostringstream oss;
|
dev@1438
|
155 const auto cancelImmediately = CSVStreamWriter::writeInChunks(
|
dev@1434
|
156 oss,
|
dev@1434
|
157 mwm,
|
dev@1434
|
158 &reporter,
|
dev@1434
|
159 ",",
|
dev@1434
|
160 DataExportDefaults,
|
dev@1434
|
161 4
|
dev@1434
|
162 );
|
dev@1434
|
163 QVERIFY( cancelImmediately == false );
|
dev@1434
|
164 QVERIFY( reporter.getCallCount() == 0 );
|
dev@1434
|
165
|
dev@1434
|
166 StubReporter cancelMidway {
|
dev@1434
|
167 [&]() { return cancelMidway.getCallCount() == 3; }
|
dev@1434
|
168 };
|
dev@1438
|
169 const auto cancelledMidway = CSVStreamWriter::writeInChunks(
|
dev@1434
|
170 oss,
|
dev@1434
|
171 mwm,
|
dev@1434
|
172 &cancelMidway,
|
dev@1434
|
173 ",",
|
dev@1434
|
174 DataExportDefaults,
|
dev@1434
|
175 4
|
dev@1434
|
176 );
|
dev@1434
|
177 QVERIFY( cancelMidway.getCallCount() == 3 );
|
dev@1434
|
178 QVERIFY( cancelledMidway == false );
|
dev@1430
|
179 }
|
dev@1440
|
180
|
dev@1440
|
181 void zeroStartTimeReportsPercentageCorrectly()
|
dev@1440
|
182 {
|
dev@1440
|
183 MockWaveModel mwm({ DC, DC }, 16, 4);
|
dev@1440
|
184 StubReporter reporter { []() -> bool { return false; } };
|
dev@1440
|
185 std::ostringstream oss;
|
dev@1440
|
186 const auto succeeded = CSVStreamWriter::writeInChunks(
|
dev@1440
|
187 oss,
|
dev@1440
|
188 mwm,
|
dev@1440
|
189 &reporter,
|
dev@1440
|
190 ",",
|
dev@1440
|
191 DataExportDefaults,
|
dev@1440
|
192 4
|
dev@1440
|
193 );
|
dev@1440
|
194 QVERIFY( succeeded == true );
|
dev@1440
|
195 QVERIFY( reporter.getCallCount() == 6 );
|
dev@1440
|
196 const std::vector<int> expectedCallLog {
|
dev@1440
|
197 16,
|
dev@1440
|
198 33,
|
dev@1440
|
199 50,
|
dev@1440
|
200 66,
|
dev@1440
|
201 83,
|
dev@1440
|
202 100
|
dev@1440
|
203 };
|
dev@1440
|
204 QVERIFY( reporter.getPercentageLog() == expectedCallLog );
|
dev@1440
|
205 QVERIFY( oss.str() == getExpectedString() );
|
dev@1440
|
206 }
|
dev@1440
|
207
|
dev@1440
|
208 void nonZeroStartTimeReportsPercentageCorrectly()
|
dev@1440
|
209 {
|
dev@1440
|
210 MockWaveModel mwm({ DC, DC }, 16, 4);
|
dev@1440
|
211 StubReporter reporter { []() -> bool { return false; } };
|
dev@1440
|
212 std::ostringstream oss;
|
dev@1440
|
213 const auto writeSubSection = CSVStreamWriter::writeInChunks(
|
dev@1440
|
214 oss,
|
dev@1440
|
215 mwm,
|
dev@1440
|
216 {4, 20},
|
dev@1440
|
217 &reporter,
|
dev@1440
|
218 ",",
|
dev@1440
|
219 DataExportDefaults,
|
dev@1440
|
220 4
|
dev@1440
|
221 );
|
dev@1440
|
222 QVERIFY( reporter.getCallCount() == 4 );
|
dev@1440
|
223 const std::vector<int> expectedCallLog {
|
dev@1440
|
224 25,
|
dev@1440
|
225 50,
|
dev@1440
|
226 75,
|
dev@1440
|
227 100
|
dev@1440
|
228 };
|
dev@1440
|
229 QVERIFY( reporter.getPercentageLog() == expectedCallLog );
|
dev@1440
|
230 QVERIFY( writeSubSection == true );
|
dev@1440
|
231 const std::string expectedOutput {
|
dev@1440
|
232 "4,1,1\n"
|
dev@1440
|
233 "5,1,1\n"
|
dev@1440
|
234 "6,1,1\n"
|
dev@1440
|
235 "7,1,1\n"
|
dev@1440
|
236 "8,1,1\n"
|
dev@1440
|
237 "9,1,1\n"
|
dev@1440
|
238 "10,1,1\n"
|
dev@1440
|
239 "11,1,1\n"
|
dev@1440
|
240 "12,1,1\n"
|
dev@1440
|
241 "13,1,1\n"
|
dev@1440
|
242 "14,1,1\n"
|
dev@1440
|
243 "15,1,1\n"
|
dev@1440
|
244 "16,1,1\n"
|
dev@1440
|
245 "17,1,1\n"
|
dev@1440
|
246 "18,1,1\n"
|
dev@1440
|
247 "19,1,1"
|
dev@1440
|
248 };
|
dev@1440
|
249 QVERIFY( oss.str() == expectedOutput );
|
dev@1440
|
250 }
|
dev@1446
|
251
|
dev@1446
|
252 void multipleSelectionOutput()
|
dev@1446
|
253 {
|
dev@1446
|
254 MockWaveModel mwm({ DC, DC }, 16, 4);
|
dev@1446
|
255 StubReporter reporter { []() -> bool { return false; } };
|
dev@1446
|
256 std::ostringstream oss;
|
dev@1446
|
257 MultiSelection regions;
|
dev@1446
|
258 regions.addSelection({0, 2});
|
dev@1446
|
259 regions.addSelection({4, 6});
|
dev@1446
|
260 regions.addSelection({16, 18});
|
dev@1446
|
261 const std::string expectedOutput {
|
dev@1446
|
262 "0,0,0\n"
|
dev@1446
|
263 "1,0,0\n"
|
dev@1446
|
264 "4,1,1\n"
|
dev@1446
|
265 "5,1,1\n"
|
dev@1446
|
266 "16,1,1\n"
|
dev@1446
|
267 "17,1,1"
|
dev@1446
|
268 };
|
dev@1446
|
269 const auto wroteMultiSection = CSVStreamWriter::writeInChunks(
|
dev@1446
|
270 oss,
|
dev@1446
|
271 mwm,
|
dev@1446
|
272 regions,
|
dev@1446
|
273 &reporter,
|
dev@1446
|
274 ",",
|
dev@1446
|
275 DataExportDefaults,
|
dev@1446
|
276 2
|
dev@1446
|
277 );
|
dev@1446
|
278 QVERIFY( wroteMultiSection == true );
|
dev@1446
|
279 QVERIFY( reporter.getCallCount() == 3 );
|
dev@1446
|
280 const std::vector<int> expectedCallLog { 33, 66, 100 };
|
dev@1446
|
281 QVERIFY( reporter.getPercentageLog() == expectedCallLog );
|
dev@1446
|
282 qDebug("%s", oss.str().c_str());
|
dev@1446
|
283 QVERIFY( oss.str() == expectedOutput );
|
dev@1446
|
284 }
|
dev@1430
|
285 };
|
dev@1430
|
286
|
dev@1430
|
287 #endif |