Mercurial > hg > svcore
comparison data/fileio/test/CSVStreamWriterTest.h @ 1449:deabf9fd3d28 streaming-csv-writer
Add failing test case for writing a sparse model. Partially handle some of the related issues with line-breaks.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Tue, 17 Apr 2018 10:03:51 +0100 |
parents | 8305d442d1f7 |
children | 743c38b209d0 |
comparison
equal
deleted
inserted
replaced
1448:8305d442d1f7 | 1449:deabf9fd3d28 |
---|---|
22 #include <functional> | 22 #include <functional> |
23 | 23 |
24 #include "base/ProgressReporter.h" | 24 #include "base/ProgressReporter.h" |
25 #include "base/DataExportOptions.h" | 25 #include "base/DataExportOptions.h" |
26 #include "base/Selection.h" | 26 #include "base/Selection.h" |
27 #include "data/model/NoteModel.h" | |
27 #include "../CSVStreamWriter.h" | 28 #include "../CSVStreamWriter.h" |
28 #include "../../model/test/MockWaveModel.h" | 29 #include "../../model/test/MockWaveModel.h" |
29 | 30 |
30 class StubReporter : public ProgressReporter | 31 class StubReporter : public ProgressReporter |
31 { | 32 { |
35 bool isDefinite() const override { return true; } | 36 bool isDefinite() const override { return true; } |
36 void setDefinite(bool) override {} | 37 void setDefinite(bool) override {} |
37 bool wasCancelled() const override { return m_isCancelled(); } | 38 bool wasCancelled() const override { return m_isCancelled(); } |
38 void setMessage(QString) override {} | 39 void setMessage(QString) override {} |
39 void setProgress(int p) override | 40 void setProgress(int p) override |
40 { | 41 { |
41 ++m_calls; | 42 ++m_calls; |
42 m_percentageLog.push_back(p); | 43 m_percentageLog.push_back(p); |
43 } | 44 } |
44 | 45 |
45 size_t getCallCount() const { return m_calls; } | 46 size_t getCallCount() const { return m_calls; } |
253 std::ostringstream oss; | 254 std::ostringstream oss; |
254 MultiSelection regions; | 255 MultiSelection regions; |
255 regions.addSelection({0, 2}); | 256 regions.addSelection({0, 2}); |
256 regions.addSelection({4, 6}); | 257 regions.addSelection({4, 6}); |
257 regions.addSelection({16, 18}); | 258 regions.addSelection({16, 18}); |
259 qDebug("End frame: %lld", mwm.getEndFrame()); | |
258 const std::string expectedOutput { | 260 const std::string expectedOutput { |
259 "0,0,0\n" | 261 "0,0,0\n" |
260 "1,0,0\n" | 262 "1,0,0\n" |
261 "4,1,1\n" | 263 "4,1,1\n" |
262 "5,1,1\n" | 264 "5,1,1\n" |
277 const std::vector<int> expectedCallLog { 33, 66, 100 }; | 279 const std::vector<int> expectedCallLog { 33, 66, 100 }; |
278 QVERIFY( reporter.getPercentageLog() == expectedCallLog ); | 280 QVERIFY( reporter.getPercentageLog() == expectedCallLog ); |
279 qDebug("%s", oss.str().c_str()); | 281 qDebug("%s", oss.str().c_str()); |
280 QVERIFY( oss.str() == expectedOutput ); | 282 QVERIFY( oss.str() == expectedOutput ); |
281 } | 283 } |
284 | |
285 void writeSparseModel() | |
286 { | |
287 const auto pentatonicFromRoot = [](float midiPitch) { | |
288 return std::vector<float> { | |
289 0 + midiPitch, | |
290 2 + midiPitch, | |
291 4 + midiPitch, | |
292 7 + midiPitch, | |
293 9 + midiPitch | |
294 }; | |
295 }; | |
296 const auto cMajorPentatonic = pentatonicFromRoot(60.0); | |
297 NoteModel notes(8 /* sampleRate */, 4 /* resolution */); | |
298 sv_frame_t startFrame = 0; | |
299 for (const auto& note : cMajorPentatonic) { | |
300 notes.addPoint({startFrame, note, 4, 1.f, ""}); | |
301 startFrame += 8; | |
302 } | |
303 qDebug("Create Expected Output\n"); | |
304 | |
305 // NB. removed end line break | |
306 const auto expectedOutput = notes.toDelimitedDataString(",").trimmed(); | |
307 | |
308 StubReporter reporter { []() -> bool { return false; } }; | |
309 std::ostringstream oss; | |
310 qDebug("End frame: %lld", notes.getEndFrame()); | |
311 qDebug("Write streaming\n"); | |
312 const auto wroteSparseModel = CSVStreamWriter::writeInChunks( | |
313 oss, | |
314 notes, | |
315 &reporter, | |
316 ",", | |
317 DataExportDefaults, | |
318 2 | |
319 ); | |
320 | |
321 qDebug("\n%s\n", expectedOutput.toLocal8Bit().data()); | |
322 qDebug("\n%s\n", oss.str().c_str()); | |
323 QVERIFY( wroteSparseModel == true ); | |
324 QVERIFY( oss.str() == expectedOutput.toStdString() ); | |
325 } | |
282 }; | 326 }; |
283 | 327 |
284 #endif | 328 #endif |