# HG changeset patch # User Lucas Thompson # Date 1523955831 -3600 # Node ID deabf9fd3d2877737e9dc652b400659bacb26073 # Parent 8305d442d1f7a54f183ddf9f5ef74361035a70cd Add failing test case for writing a sparse model. Partially handle some of the related issues with line-breaks. diff -r 8305d442d1f7 -r deabf9fd3d28 data/fileio/CSVStreamWriter.h --- a/data/fileio/CSVStreamWriter.h Tue Apr 17 10:03:50 2018 +0100 +++ b/data/fileio/CSVStreamWriter.h Tue Apr 17 10:03:51 2018 +0100 @@ -68,13 +68,17 @@ const auto start = readPtr; const auto end = std::min(start + blockSize, endFrame); - - oss << model.toDelimitedDataStringSubsetWithOptions( + const auto data = model.toDelimitedDataStringSubsetWithOptions( delimiter, options, start, end - ) << (end < finalFrameOfLastRegion ? "\n" : ""); + ).trimmed(); + + if ( data != "" ) { + oss << data << (end < finalFrameOfLastRegion ? "\n" : ""); + } + nFramesWritten += end - start; const auto currentProgress = 100 * nFramesWritten / nFramesToWrite; const bool hasIncreased = currentProgress > previousProgress; diff -r 8305d442d1f7 -r deabf9fd3d28 data/fileio/test/CSVStreamWriterTest.h --- a/data/fileio/test/CSVStreamWriterTest.h Tue Apr 17 10:03:50 2018 +0100 +++ b/data/fileio/test/CSVStreamWriterTest.h Tue Apr 17 10:03:51 2018 +0100 @@ -24,6 +24,7 @@ #include "base/ProgressReporter.h" #include "base/DataExportOptions.h" #include "base/Selection.h" +#include "data/model/NoteModel.h" #include "../CSVStreamWriter.h" #include "../../model/test/MockWaveModel.h" @@ -37,7 +38,7 @@ bool wasCancelled() const override { return m_isCancelled(); } void setMessage(QString) override {} void setProgress(int p) override - { + { ++m_calls; m_percentageLog.push_back(p); } @@ -255,6 +256,7 @@ regions.addSelection({0, 2}); regions.addSelection({4, 6}); regions.addSelection({16, 18}); + qDebug("End frame: %lld", mwm.getEndFrame()); const std::string expectedOutput { "0,0,0\n" "1,0,0\n" @@ -279,6 +281,48 @@ qDebug("%s", oss.str().c_str()); QVERIFY( oss.str() == expectedOutput ); } + + void writeSparseModel() + { + const auto pentatonicFromRoot = [](float midiPitch) { + return std::vector { + 0 + midiPitch, + 2 + midiPitch, + 4 + midiPitch, + 7 + midiPitch, + 9 + midiPitch + }; + }; + const auto cMajorPentatonic = pentatonicFromRoot(60.0); + NoteModel notes(8 /* sampleRate */, 4 /* resolution */); + sv_frame_t startFrame = 0; + for (const auto& note : cMajorPentatonic) { + notes.addPoint({startFrame, note, 4, 1.f, ""}); + startFrame += 8; + } + qDebug("Create Expected Output\n"); + + // NB. removed end line break + const auto expectedOutput = notes.toDelimitedDataString(",").trimmed(); + + StubReporter reporter { []() -> bool { return false; } }; + std::ostringstream oss; + qDebug("End frame: %lld", notes.getEndFrame()); + qDebug("Write streaming\n"); + const auto wroteSparseModel = CSVStreamWriter::writeInChunks( + oss, + notes, + &reporter, + ",", + DataExportDefaults, + 2 + ); + + qDebug("\n%s\n", expectedOutput.toLocal8Bit().data()); + qDebug("\n%s\n", oss.str().c_str()); + QVERIFY( wroteSparseModel == true ); + QVERIFY( oss.str() == expectedOutput.toStdString() ); + } }; #endif \ No newline at end of file