annotate data/fileio/test/CSVStreamWriterTest.h @ 1440:04caefd35391 streaming-csv-writer

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