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