Mercurial > hg > svcore
comparison data/fileio/test/CSVFormatTest.h @ 1527:710e6250a401 zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:14 +0100 |
parents | a92e94215863 |
children | 9570ef94eaa3 |
comparison
equal
deleted
inserted
replaced
1324:d4a28d1479a8 | 1527:710e6250a401 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 | |
8 This program is free software; you can redistribute it and/or | |
9 modify it under the terms of the GNU General Public License as | |
10 published by the Free Software Foundation; either version 2 of the | |
11 License, or (at your option) any later version. See the file | |
12 COPYING included with this distribution for more information. | |
13 */ | |
14 | |
15 #ifndef TEST_CSV_FORMAT_H | |
16 #define TEST_CSV_FORMAT_H | |
17 | |
18 // Tests for the code that guesses the most likely format for parsing a CSV file | |
19 | |
20 #include "../CSVFormat.h" | |
21 | |
22 #include "base/Debug.h" | |
23 | |
24 #include <cmath> | |
25 | |
26 #include <QObject> | |
27 #include <QtTest> | |
28 #include <QDir> | |
29 | |
30 #include <iostream> | |
31 | |
32 using namespace std; | |
33 | |
34 class CSVFormatTest : public QObject | |
35 { | |
36 Q_OBJECT | |
37 | |
38 private: | |
39 QDir csvDir; | |
40 | |
41 public: | |
42 CSVFormatTest(QString base) { | |
43 if (base == "") { | |
44 base = "svcore/data/fileio/test"; | |
45 } | |
46 csvDir = QDir(base + "/csv"); | |
47 } | |
48 | |
49 private slots: | |
50 void init() { | |
51 if (!csvDir.exists()) { | |
52 SVCERR << "ERROR: CSV test file directory \"" << csvDir.absolutePath() << "\" does not exist" << endl; | |
53 QVERIFY2(csvDir.exists(), "CSV test file directory not found"); | |
54 } | |
55 } | |
56 | |
57 void separatorComma() { | |
58 CSVFormat f; | |
59 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-comma.csv"))); | |
60 QCOMPARE(f.getSeparator(), QChar(',')); | |
61 QCOMPARE(f.getColumnCount(), 3); | |
62 } | |
63 | |
64 void separatorTab() { | |
65 CSVFormat f; | |
66 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-tab.csv"))); | |
67 QCOMPARE(f.getSeparator(), QChar('\t')); | |
68 QCOMPARE(f.getColumnCount(), 3); | |
69 } | |
70 | |
71 void separatorPipe() { | |
72 CSVFormat f; | |
73 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-pipe.csv"))); | |
74 QCOMPARE(f.getSeparator(), QChar('|')); | |
75 // differs from the others | |
76 QCOMPARE(f.getColumnCount(), 4); | |
77 } | |
78 | |
79 void separatorSpace() { | |
80 CSVFormat f; | |
81 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-space.csv"))); | |
82 QCOMPARE(f.getSeparator(), QChar(' ')); | |
83 // NB fields are separated by 1 or more spaces, not necessarily exactly 1 | |
84 QCOMPARE(f.getColumnCount(), 3); | |
85 } | |
86 | |
87 void separatorColon() { | |
88 CSVFormat f; | |
89 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-colon.csv"))); | |
90 QCOMPARE(f.getSeparator(), QChar(':')); | |
91 QCOMPARE(f.getColumnCount(), 3); | |
92 } | |
93 | |
94 void comment() { | |
95 CSVFormat f; | |
96 QVERIFY(f.guessFormatFor(csvDir.filePath("comment.csv"))); | |
97 QCOMPARE(f.getSeparator(), QChar(',')); | |
98 QCOMPARE(f.getColumnCount(), 4); | |
99 } | |
100 | |
101 void qualities() { | |
102 CSVFormat f; | |
103 QVERIFY(f.guessFormatFor(csvDir.filePath("column-qualities.csv"))); | |
104 QCOMPARE(f.getSeparator(), QChar(',')); | |
105 QCOMPARE(f.getColumnCount(), 7); | |
106 QList<CSVFormat::ColumnQualities> q = f.getColumnQualities(); | |
107 QList<CSVFormat::ColumnQualities> expected; | |
108 expected << 0; | |
109 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | | |
110 CSVFormat::ColumnIntegral | | |
111 CSVFormat::ColumnIncreasing); | |
112 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | | |
113 CSVFormat::ColumnIntegral | | |
114 CSVFormat::ColumnIncreasing | | |
115 CSVFormat::ColumnLarge); | |
116 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric); | |
117 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | | |
118 CSVFormat::ColumnIncreasing); | |
119 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | | |
120 CSVFormat::ColumnSmall | | |
121 CSVFormat::ColumnSigned); | |
122 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | | |
123 CSVFormat::ColumnIntegral | | |
124 CSVFormat::ColumnIncreasing | | |
125 CSVFormat::ColumnNearEmpty); | |
126 QCOMPARE(q, expected); | |
127 } | |
128 | |
129 void modelType1DSamples() { | |
130 CSVFormat f; | |
131 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-1d-samples.csv"))); | |
132 QCOMPARE(f.getColumnCount(), 1); | |
133 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
134 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
135 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeAudioFrames); | |
136 QCOMPARE(f.getModelType(), CSVFormat::OneDimensionalModel); | |
137 } | |
138 | |
139 void modelType1DSeconds() { | |
140 CSVFormat f; | |
141 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-1d-seconds.csv"))); | |
142 QCOMPARE(f.getColumnCount(), 2); | |
143 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
144 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnLabel); | |
145 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
146 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeSeconds); | |
147 QCOMPARE(f.getModelType(), CSVFormat::OneDimensionalModel); | |
148 } | |
149 | |
150 void modelType2DSamples() { | |
151 CSVFormat f; | |
152 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-samples.csv"))); | |
153 QCOMPARE(f.getColumnCount(), 2); | |
154 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
155 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnValue); | |
156 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
157 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeAudioFrames); | |
158 QCOMPARE(f.getModelType(), CSVFormat::TwoDimensionalModel); | |
159 } | |
160 | |
161 void modelType2DSeconds() { | |
162 CSVFormat f; | |
163 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-seconds.csv"))); | |
164 QCOMPARE(f.getColumnCount(), 2); | |
165 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
166 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnValue); | |
167 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
168 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeSeconds); | |
169 QCOMPARE(f.getModelType(), CSVFormat::TwoDimensionalModel); | |
170 } | |
171 | |
172 void modelType2DImplicit() { | |
173 CSVFormat f; | |
174 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-implicit.csv"))); | |
175 QCOMPARE(f.getColumnCount(), 1); | |
176 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnValue); | |
177 QCOMPARE(f.getTimingType(), CSVFormat::ImplicitTiming); | |
178 } | |
179 | |
180 void modelType2DEndTimeSamples() { | |
181 CSVFormat f; | |
182 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-endtime-samples.csv"))); | |
183 QCOMPARE(f.getColumnCount(), 3); | |
184 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
185 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnEndTime); | |
186 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
187 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
188 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeAudioFrames); | |
189 QCOMPARE(f.getModelType(), CSVFormat::TwoDimensionalModelWithDuration); | |
190 } | |
191 | |
192 void modelType2DEndTimeSeconds() { | |
193 CSVFormat f; | |
194 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-endtime-seconds.csv"))); | |
195 QCOMPARE(f.getColumnCount(), 3); | |
196 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
197 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnEndTime); | |
198 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
199 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
200 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeSeconds); | |
201 QCOMPARE(f.getModelType(), CSVFormat::TwoDimensionalModelWithDuration); | |
202 } | |
203 | |
204 void modelType2DDurationSamples() { | |
205 CSVFormat f; | |
206 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-duration-samples.csv"))); | |
207 QCOMPARE(f.getColumnCount(), 3); | |
208 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
209 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnDuration); | |
210 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
211 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
212 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeAudioFrames); | |
213 QCOMPARE(f.getModelType(), CSVFormat::TwoDimensionalModelWithDuration); | |
214 } | |
215 | |
216 void modelType2DDurationSeconds() { | |
217 CSVFormat f; | |
218 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-2d-duration-seconds.csv"))); | |
219 QCOMPARE(f.getColumnCount(), 3); | |
220 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
221 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnDuration); | |
222 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
223 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
224 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeSeconds); | |
225 QCOMPARE(f.getModelType(), CSVFormat::TwoDimensionalModelWithDuration); | |
226 } | |
227 | |
228 void modelType3DSamples() { | |
229 CSVFormat f; | |
230 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-3d-samples.csv"))); | |
231 QCOMPARE(f.getColumnCount(), 7); | |
232 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
233 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnValue); | |
234 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
235 QCOMPARE(f.getColumnPurpose(3), CSVFormat::ColumnValue); | |
236 QCOMPARE(f.getColumnPurpose(4), CSVFormat::ColumnValue); | |
237 QCOMPARE(f.getColumnPurpose(5), CSVFormat::ColumnValue); | |
238 QCOMPARE(f.getColumnPurpose(6), CSVFormat::ColumnValue); | |
239 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
240 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeAudioFrames); | |
241 QCOMPARE(f.getModelType(), CSVFormat::ThreeDimensionalModel); | |
242 } | |
243 | |
244 void modelType3DSeconds() { | |
245 CSVFormat f; | |
246 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-3d-seconds.csv"))); | |
247 QCOMPARE(f.getColumnCount(), 7); | |
248 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnStartTime); | |
249 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnValue); | |
250 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
251 QCOMPARE(f.getColumnPurpose(3), CSVFormat::ColumnValue); | |
252 QCOMPARE(f.getColumnPurpose(4), CSVFormat::ColumnValue); | |
253 QCOMPARE(f.getColumnPurpose(5), CSVFormat::ColumnValue); | |
254 QCOMPARE(f.getColumnPurpose(6), CSVFormat::ColumnValue); | |
255 QCOMPARE(f.getTimingType(), CSVFormat::ExplicitTiming); | |
256 QCOMPARE(f.getTimeUnits(), CSVFormat::TimeSeconds); | |
257 QCOMPARE(f.getModelType(), CSVFormat::ThreeDimensionalModel); | |
258 } | |
259 | |
260 void modelType3DImplicit() { | |
261 CSVFormat f; | |
262 QVERIFY(f.guessFormatFor(csvDir.filePath("model-type-3d-implicit.csv"))); | |
263 QCOMPARE(f.getColumnCount(), 6); | |
264 QCOMPARE(f.getColumnPurpose(0), CSVFormat::ColumnValue); | |
265 QCOMPARE(f.getColumnPurpose(1), CSVFormat::ColumnValue); | |
266 QCOMPARE(f.getColumnPurpose(2), CSVFormat::ColumnValue); | |
267 QCOMPARE(f.getColumnPurpose(3), CSVFormat::ColumnValue); | |
268 QCOMPARE(f.getColumnPurpose(4), CSVFormat::ColumnValue); | |
269 QCOMPARE(f.getColumnPurpose(5), CSVFormat::ColumnValue); | |
270 QCOMPARE(f.getTimingType(), CSVFormat::ImplicitTiming); | |
271 QCOMPARE(f.getModelType(), CSVFormat::ThreeDimensionalModel); | |
272 } | |
273 | |
274 }; | |
275 | |
276 #endif |