annotate data/fileio/test/CSVStreamWriterTest.h @ 1434:0684c6698e3f streaming-csv-writer

Added utility function for splitting a model selection into chunks and writing to a stream.
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:03:49 +0100
parents b5283878cca2
children 09c2ba31a711
rev   line source
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@1434 7 This file copyright 2017 Lucas Thompson.
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@1430 26 #include "../CSVStreamWriter.h"
dev@1434 27 #include "../../model/test/MockWaveModel.h"
dev@1434 28
dev@1434 29 namespace
dev@1434 30 {
dev@1434 31 class StubReporter : public ProgressReporter
dev@1434 32 {
dev@1434 33 public:
dev@1434 34 StubReporter( std::function<bool()> isCancelled )
dev@1434 35 : m_isCancelled(isCancelled) {}
dev@1434 36 bool isDefinite() const override { return true; }
dev@1434 37 void setDefinite(bool) override {}
dev@1434 38 bool wasCancelled() const override { return m_isCancelled(); }
dev@1434 39 void setMessage(QString) override {}
dev@1434 40 void setProgress(int) override { ++m_calls; }
dev@1434 41 size_t getCallCount() const { return m_calls; }
dev@1434 42 void reset() { m_calls = 0; }
dev@1434 43 private:
dev@1434 44 size_t m_calls = 0;
dev@1434 45 std::function<bool()> m_isCancelled;
dev@1434 46 };
dev@1434 47 } // namespace
dev@1430 48
dev@1430 49 class CSVStreamWriterTest : public QObject
dev@1430 50 {
dev@1430 51 Q_OBJECT
dev@1430 52 public:
dev@1434 53 std::string getExpectedString()
dev@1434 54 {
dev@1434 55 return
dev@1434 56 {
dev@1434 57 "0,0,0\n"
dev@1434 58 "1,0,0\n"
dev@1434 59 "2,0,0\n"
dev@1434 60 "3,0,0\n"
dev@1434 61 "4,1,1\n"
dev@1434 62 "5,1,1\n"
dev@1434 63 "6,1,1\n"
dev@1434 64 "7,1,1\n"
dev@1434 65 "8,1,1\n"
dev@1434 66 "9,1,1\n"
dev@1434 67 "10,1,1\n"
dev@1434 68 "11,1,1\n"
dev@1434 69 "12,1,1\n"
dev@1434 70 "13,1,1\n"
dev@1434 71 "14,1,1\n"
dev@1434 72 "15,1,1\n"
dev@1434 73 "16,1,1\n"
dev@1434 74 "17,1,1\n"
dev@1434 75 "18,1,1\n"
dev@1434 76 "19,1,1\n"
dev@1434 77 "20,0,0\n"
dev@1434 78 "21,0,0\n"
dev@1434 79 "22,0,0\n"
dev@1434 80 "23,0,0"
dev@1434 81 };
dev@1434 82 }
dev@1430 83
dev@1430 84 private slots:
dev@1434 85 void simpleValidOutput()
dev@1430 86 {
dev@1434 87 MockWaveModel mwm({ DC, DC }, 16, 4);
dev@1430 88
dev@1434 89 std::ostringstream oss;
dev@1434 90 const auto result = CSV::writeToStreamInChunks(oss, mwm);
dev@1434 91 QVERIFY( oss.str() == getExpectedString() );
dev@1434 92 QVERIFY( result );
dev@1434 93 }
dev@1434 94
dev@1434 95 void callsReporterCorrectTimes()
dev@1434 96 {
dev@1434 97 MockWaveModel mwm({ DC, DC }, 16, 4);
dev@1434 98 StubReporter reporter { []() -> bool { return false; } };
dev@1434 99 const auto expected = getExpectedString();
dev@1434 100
dev@1434 101 std::ostringstream oss;
dev@1434 102 const auto writeStreamWithBlockSize = [&](int blockSize) {
dev@1434 103 return CSV::writeToStreamInChunks(
dev@1434 104 oss,
dev@1434 105 mwm,
dev@1434 106 &reporter,
dev@1434 107 ",",
dev@1434 108 DataExportDefaults,
dev@1434 109 blockSize
dev@1434 110 );
dev@1434 111 };
dev@1434 112
dev@1434 113 const auto reset = [&]() {
dev@1434 114 oss.str({});
dev@1434 115 reporter.reset();
dev@1434 116 };
dev@1434 117
dev@1434 118 const auto nonIntegerMultipleResult = writeStreamWithBlockSize(5);
dev@1434 119 QVERIFY( nonIntegerMultipleResult );
dev@1434 120 QVERIFY( reporter.getCallCount() == 5 /* 4.8 rounded up */ );
dev@1434 121 QVERIFY( oss.str() == expected );
dev@1434 122 reset();
dev@1434 123
dev@1434 124 const auto integerMultiple = writeStreamWithBlockSize(2);
dev@1434 125 QVERIFY( integerMultiple );
dev@1434 126 QVERIFY( reporter.getCallCount() == 12 );
dev@1434 127 QVERIFY( oss.str() == expected );
dev@1434 128 reset();
dev@1434 129
dev@1434 130 const auto largerThanNumberOfSamples = writeStreamWithBlockSize(100);
dev@1434 131 QVERIFY( largerThanNumberOfSamples );
dev@1434 132 QVERIFY( reporter.getCallCount() == 1 );
dev@1434 133 QVERIFY( oss.str() == expected );
dev@1434 134 reset();
dev@1434 135
dev@1434 136 const auto zero = writeStreamWithBlockSize(0);
dev@1434 137 QVERIFY( zero == false );
dev@1434 138 QVERIFY( reporter.getCallCount() == 0 );
dev@1434 139 }
dev@1434 140
dev@1434 141 void isCancellable()
dev@1434 142 {
dev@1434 143 MockWaveModel mwm({ DC, DC }, 16, 4);
dev@1434 144 StubReporter reporter { []() -> bool { return true; } };
dev@1434 145
dev@1434 146 std::ostringstream oss;
dev@1434 147 const auto cancelImmediately = CSV::writeToStreamInChunks(
dev@1434 148 oss,
dev@1434 149 mwm,
dev@1434 150 &reporter,
dev@1434 151 ",",
dev@1434 152 DataExportDefaults,
dev@1434 153 4
dev@1434 154 );
dev@1434 155 QVERIFY( cancelImmediately == false );
dev@1434 156 QVERIFY( reporter.getCallCount() == 0 );
dev@1434 157
dev@1434 158 StubReporter cancelMidway {
dev@1434 159 [&]() { return cancelMidway.getCallCount() == 3; }
dev@1434 160 };
dev@1434 161 const auto cancelledMidway = CSV::writeToStreamInChunks(
dev@1434 162 oss,
dev@1434 163 mwm,
dev@1434 164 &cancelMidway,
dev@1434 165 ",",
dev@1434 166 DataExportDefaults,
dev@1434 167 4
dev@1434 168 );
dev@1434 169 QVERIFY( cancelMidway.getCallCount() == 3 );
dev@1434 170 QVERIFY( cancelledMidway == false );
dev@1430 171 }
dev@1430 172 };
dev@1430 173
dev@1430 174 #endif