comparison 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
comparison
equal deleted inserted replaced
1433:c0f69bddea12 1434:0684c6698e3f
2 2
3 /* 3 /*
4 Sonic Visualiser 4 Sonic Visualiser
5 An audio file viewer and annotation editor. 5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London. 6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2013 Chris Cannam. 7 This file copyright 2017 Lucas Thompson.
8 8
9 This program is free software; you can redistribute it and/or 9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as 10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the 11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
14 */ 14 */
15 15
16 #ifndef TEST_CSV_STREAM_H 16 #ifndef TEST_CSV_STREAM_H
17 #define TEST_CSV_STREAM_H 17 #define TEST_CSV_STREAM_H
18 18
19 #include <QtTest>
19 #include <QObject> 20 #include <QObject>
21 #include <sstream>
22 #include <functional>
23
24 #include "base/ProgressReporter.h"
25 #include "base/DataExportOptions.h"
20 #include "../CSVStreamWriter.h" 26 #include "../CSVStreamWriter.h"
27 #include "../../model/test/MockWaveModel.h"
28
29 namespace
30 {
31 class StubReporter : public ProgressReporter
32 {
33 public:
34 StubReporter( std::function<bool()> isCancelled )
35 : m_isCancelled(isCancelled) {}
36 bool isDefinite() const override { return true; }
37 void setDefinite(bool) override {}
38 bool wasCancelled() const override { return m_isCancelled(); }
39 void setMessage(QString) override {}
40 void setProgress(int) override { ++m_calls; }
41 size_t getCallCount() const { return m_calls; }
42 void reset() { m_calls = 0; }
43 private:
44 size_t m_calls = 0;
45 std::function<bool()> m_isCancelled;
46 };
47 } // namespace
21 48
22 class CSVStreamWriterTest : public QObject 49 class CSVStreamWriterTest : public QObject
23 { 50 {
24 Q_OBJECT 51 Q_OBJECT
25 public: 52 public:
53 std::string getExpectedString()
54 {
55 return
56 {
57 "0,0,0\n"
58 "1,0,0\n"
59 "2,0,0\n"
60 "3,0,0\n"
61 "4,1,1\n"
62 "5,1,1\n"
63 "6,1,1\n"
64 "7,1,1\n"
65 "8,1,1\n"
66 "9,1,1\n"
67 "10,1,1\n"
68 "11,1,1\n"
69 "12,1,1\n"
70 "13,1,1\n"
71 "14,1,1\n"
72 "15,1,1\n"
73 "16,1,1\n"
74 "17,1,1\n"
75 "18,1,1\n"
76 "19,1,1\n"
77 "20,0,0\n"
78 "21,0,0\n"
79 "22,0,0\n"
80 "23,0,0"
81 };
82 }
26 83
27 private slots: 84 private slots:
28 void toDo() 85 void simpleValidOutput()
29 { 86 {
87 MockWaveModel mwm({ DC, DC }, 16, 4);
30 88
89 std::ostringstream oss;
90 const auto result = CSV::writeToStreamInChunks(oss, mwm);
91 QVERIFY( oss.str() == getExpectedString() );
92 QVERIFY( result );
93 }
94
95 void callsReporterCorrectTimes()
96 {
97 MockWaveModel mwm({ DC, DC }, 16, 4);
98 StubReporter reporter { []() -> bool { return false; } };
99 const auto expected = getExpectedString();
100
101 std::ostringstream oss;
102 const auto writeStreamWithBlockSize = [&](int blockSize) {
103 return CSV::writeToStreamInChunks(
104 oss,
105 mwm,
106 &reporter,
107 ",",
108 DataExportDefaults,
109 blockSize
110 );
111 };
112
113 const auto reset = [&]() {
114 oss.str({});
115 reporter.reset();
116 };
117
118 const auto nonIntegerMultipleResult = writeStreamWithBlockSize(5);
119 QVERIFY( nonIntegerMultipleResult );
120 QVERIFY( reporter.getCallCount() == 5 /* 4.8 rounded up */ );
121 QVERIFY( oss.str() == expected );
122 reset();
123
124 const auto integerMultiple = writeStreamWithBlockSize(2);
125 QVERIFY( integerMultiple );
126 QVERIFY( reporter.getCallCount() == 12 );
127 QVERIFY( oss.str() == expected );
128 reset();
129
130 const auto largerThanNumberOfSamples = writeStreamWithBlockSize(100);
131 QVERIFY( largerThanNumberOfSamples );
132 QVERIFY( reporter.getCallCount() == 1 );
133 QVERIFY( oss.str() == expected );
134 reset();
135
136 const auto zero = writeStreamWithBlockSize(0);
137 QVERIFY( zero == false );
138 QVERIFY( reporter.getCallCount() == 0 );
139 }
140
141 void isCancellable()
142 {
143 MockWaveModel mwm({ DC, DC }, 16, 4);
144 StubReporter reporter { []() -> bool { return true; } };
145
146 std::ostringstream oss;
147 const auto cancelImmediately = CSV::writeToStreamInChunks(
148 oss,
149 mwm,
150 &reporter,
151 ",",
152 DataExportDefaults,
153 4
154 );
155 QVERIFY( cancelImmediately == false );
156 QVERIFY( reporter.getCallCount() == 0 );
157
158 StubReporter cancelMidway {
159 [&]() { return cancelMidway.getCallCount() == 3; }
160 };
161 const auto cancelledMidway = CSV::writeToStreamInChunks(
162 oss,
163 mwm,
164 &cancelMidway,
165 ",",
166 DataExportDefaults,
167 4
168 );
169 QVERIFY( cancelMidway.getCallCount() == 3 );
170 QVERIFY( cancelledMidway == false );
31 } 171 }
32 }; 172 };
33 173
34 #endif 174 #endif