annotate data/fileio/test/CSVFormatTest.h @ 1524:64ef24ebb19c

Some CSV format tests and minor fixes
author Chris Cannam
date Fri, 14 Sep 2018 09:25:17 +0100
parents data/fileio/test/EncodingTest.h@87ae75da6527
children a92e94215863
rev   line source
Chris@1345 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1345 2
Chris@1345 3 /*
Chris@1345 4 Sonic Visualiser
Chris@1345 5 An audio file viewer and annotation editor.
Chris@1345 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1345 7
Chris@1345 8 This program is free software; you can redistribute it and/or
Chris@1345 9 modify it under the terms of the GNU General Public License as
Chris@1345 10 published by the Free Software Foundation; either version 2 of the
Chris@1345 11 License, or (at your option) any later version. See the file
Chris@1345 12 COPYING included with this distribution for more information.
Chris@1345 13 */
Chris@1345 14
Chris@1524 15 #ifndef TEST_CSV_FORMAT_H
Chris@1524 16 #define TEST_CSV_FORMAT_H
Chris@1345 17
Chris@1524 18 // Tests for the code that guesses the most likely format for parsing a CSV file
Chris@1345 19
Chris@1524 20 #include "../CSVFormat.h"
Chris@1524 21
Chris@1524 22 #include "base/Debug.h"
Chris@1345 23
Chris@1345 24 #include <cmath>
Chris@1345 25
Chris@1345 26 #include <QObject>
Chris@1345 27 #include <QtTest>
Chris@1345 28 #include <QDir>
Chris@1345 29
Chris@1345 30 #include <iostream>
Chris@1345 31
Chris@1345 32 using namespace std;
Chris@1345 33
Chris@1524 34 class CSVFormatTest : public QObject
Chris@1345 35 {
Chris@1345 36 Q_OBJECT
Chris@1345 37
Chris@1346 38 private:
Chris@1524 39 QDir csvDir;
Chris@1346 40
Chris@1346 41 public:
Chris@1524 42 CSVFormatTest(QString base) {
Chris@1346 43 if (base == "") {
Chris@1346 44 base = "svcore/data/fileio/test";
Chris@1346 45 }
Chris@1524 46 csvDir = QDir(base + "/csv");
Chris@1346 47 }
Chris@1346 48
Chris@1524 49 private slots:
Chris@1524 50 void init() {
Chris@1524 51 if (!csvDir.exists()) {
Chris@1524 52 SVCERR << "ERROR: CSV test file directory \"" << csvDir.absolutePath() << "\" does not exist" << endl;
Chris@1524 53 QVERIFY2(csvDir.exists(), "CSV test file directory not found");
Chris@1359 54 }
Chris@1359 55 }
Chris@1359 56
Chris@1524 57 void separatorComma() {
Chris@1524 58 CSVFormat f;
Chris@1524 59 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-comma.csv")));
Chris@1524 60 QCOMPARE(f.getSeparator(), QChar(','));
Chris@1524 61 QCOMPARE(f.getColumnCount(), 3);
Chris@1524 62 }
Chris@1524 63
Chris@1524 64 void separatorTab() {
Chris@1524 65 CSVFormat f;
Chris@1524 66 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-tab.csv")));
Chris@1524 67 QCOMPARE(f.getSeparator(), QChar('\t'));
Chris@1524 68 QCOMPARE(f.getColumnCount(), 3);
Chris@1524 69 }
Chris@1524 70
Chris@1524 71 void separatorPipe() {
Chris@1524 72 CSVFormat f;
Chris@1524 73 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-pipe.csv")));
Chris@1524 74 QCOMPARE(f.getSeparator(), QChar('|'));
Chris@1524 75 // differs from the others
Chris@1524 76 QCOMPARE(f.getColumnCount(), 4);
Chris@1524 77 }
Chris@1524 78
Chris@1524 79 void separatorSpace() {
Chris@1524 80 CSVFormat f;
Chris@1524 81 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-space.csv")));
Chris@1524 82 QCOMPARE(f.getSeparator(), QChar(' '));
Chris@1524 83 // NB fields are separated by 1 or more spaces, not necessarily exactly 1
Chris@1524 84 QCOMPARE(f.getColumnCount(), 3);
Chris@1524 85 }
Chris@1524 86
Chris@1524 87 void separatorColon() {
Chris@1524 88 CSVFormat f;
Chris@1524 89 QVERIFY(f.guessFormatFor(csvDir.filePath("separator-colon.csv")));
Chris@1524 90 QCOMPARE(f.getSeparator(), QChar(':'));
Chris@1524 91 QCOMPARE(f.getColumnCount(), 3);
Chris@1524 92 }
Chris@1524 93
Chris@1524 94 void comment() {
Chris@1524 95 CSVFormat f;
Chris@1524 96 QVERIFY(f.guessFormatFor(csvDir.filePath("comment.csv")));
Chris@1524 97 QCOMPARE(f.getSeparator(), QChar(','));
Chris@1524 98 QCOMPARE(f.getColumnCount(), 4);
Chris@1345 99 }
Chris@1345 100
Chris@1524 101 void qualities() {
Chris@1524 102 CSVFormat f;
Chris@1524 103 QVERIFY(f.guessFormatFor(csvDir.filePath("column-qualities.csv")));
Chris@1524 104 QCOMPARE(f.getSeparator(), QChar(','));
Chris@1524 105 QCOMPARE(f.getColumnCount(), 7);
Chris@1524 106 QList<CSVFormat::ColumnQualities> q = f.getColumnQualities();
Chris@1524 107 QList<CSVFormat::ColumnQualities> expected;
Chris@1524 108 expected << 0;
Chris@1524 109 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric |
Chris@1524 110 CSVFormat::ColumnIntegral |
Chris@1524 111 CSVFormat::ColumnIncreasing);
Chris@1524 112 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric |
Chris@1524 113 CSVFormat::ColumnIntegral |
Chris@1524 114 CSVFormat::ColumnIncreasing |
Chris@1524 115 CSVFormat::ColumnLarge);
Chris@1524 116 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric);
Chris@1524 117 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric |
Chris@1524 118 CSVFormat::ColumnIncreasing);
Chris@1524 119 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric |
Chris@1524 120 CSVFormat::ColumnSmall |
Chris@1524 121 CSVFormat::ColumnSigned);
Chris@1524 122 expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric |
Chris@1524 123 CSVFormat::ColumnIntegral |
Chris@1524 124 CSVFormat::ColumnIncreasing |
Chris@1524 125 CSVFormat::ColumnNearEmpty);
Chris@1524 126 QCOMPARE(q, expected);
Chris@1359 127 }
Chris@1345 128 };
Chris@1345 129
Chris@1345 130 #endif