Chris@1345: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1345: Chris@1345: /* Chris@1345: Sonic Visualiser Chris@1345: An audio file viewer and annotation editor. Chris@1345: Centre for Digital Music, Queen Mary, University of London. Chris@1345: Chris@1345: This program is free software; you can redistribute it and/or Chris@1345: modify it under the terms of the GNU General Public License as Chris@1345: published by the Free Software Foundation; either version 2 of the Chris@1345: License, or (at your option) any later version. See the file Chris@1345: COPYING included with this distribution for more information. Chris@1345: */ Chris@1345: Chris@1524: #ifndef TEST_CSV_FORMAT_H Chris@1524: #define TEST_CSV_FORMAT_H Chris@1345: Chris@1524: // Tests for the code that guesses the most likely format for parsing a CSV file Chris@1345: Chris@1524: #include "../CSVFormat.h" Chris@1524: Chris@1524: #include "base/Debug.h" Chris@1345: Chris@1345: #include Chris@1345: Chris@1345: #include Chris@1345: #include Chris@1345: #include Chris@1345: Chris@1345: #include Chris@1345: Chris@1345: using namespace std; Chris@1345: Chris@1524: class CSVFormatTest : public QObject Chris@1345: { Chris@1345: Q_OBJECT Chris@1345: Chris@1346: private: Chris@1524: QDir csvDir; Chris@1346: Chris@1346: public: Chris@1524: CSVFormatTest(QString base) { Chris@1346: if (base == "") { Chris@1346: base = "svcore/data/fileio/test"; Chris@1346: } Chris@1524: csvDir = QDir(base + "/csv"); Chris@1346: } Chris@1346: Chris@1524: private slots: Chris@1524: void init() { Chris@1524: if (!csvDir.exists()) { Chris@1524: SVCERR << "ERROR: CSV test file directory \"" << csvDir.absolutePath() << "\" does not exist" << endl; Chris@1524: QVERIFY2(csvDir.exists(), "CSV test file directory not found"); Chris@1359: } Chris@1359: } Chris@1359: Chris@1524: void separatorComma() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("separator-comma.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar(',')); Chris@1524: QCOMPARE(f.getColumnCount(), 3); Chris@1524: } Chris@1524: Chris@1524: void separatorTab() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("separator-tab.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar('\t')); Chris@1524: QCOMPARE(f.getColumnCount(), 3); Chris@1524: } Chris@1524: Chris@1524: void separatorPipe() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("separator-pipe.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar('|')); Chris@1524: // differs from the others Chris@1524: QCOMPARE(f.getColumnCount(), 4); Chris@1524: } Chris@1524: Chris@1524: void separatorSpace() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("separator-space.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar(' ')); Chris@1524: // NB fields are separated by 1 or more spaces, not necessarily exactly 1 Chris@1524: QCOMPARE(f.getColumnCount(), 3); Chris@1524: } Chris@1524: Chris@1524: void separatorColon() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("separator-colon.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar(':')); Chris@1524: QCOMPARE(f.getColumnCount(), 3); Chris@1524: } Chris@1524: Chris@1524: void comment() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("comment.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar(',')); Chris@1524: QCOMPARE(f.getColumnCount(), 4); Chris@1345: } Chris@1345: Chris@1524: void qualities() { Chris@1524: CSVFormat f; Chris@1524: QVERIFY(f.guessFormatFor(csvDir.filePath("column-qualities.csv"))); Chris@1524: QCOMPARE(f.getSeparator(), QChar(',')); Chris@1524: QCOMPARE(f.getColumnCount(), 7); Chris@1524: QList q = f.getColumnQualities(); Chris@1524: QList expected; Chris@1524: expected << 0; Chris@1524: expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | Chris@1524: CSVFormat::ColumnIntegral | Chris@1524: CSVFormat::ColumnIncreasing); Chris@1524: expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | Chris@1524: CSVFormat::ColumnIntegral | Chris@1524: CSVFormat::ColumnIncreasing | Chris@1524: CSVFormat::ColumnLarge); Chris@1524: expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric); Chris@1524: expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | Chris@1524: CSVFormat::ColumnIncreasing); Chris@1524: expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | Chris@1524: CSVFormat::ColumnSmall | Chris@1524: CSVFormat::ColumnSigned); Chris@1524: expected << CSVFormat::ColumnQualities(CSVFormat::ColumnNumeric | Chris@1524: CSVFormat::ColumnIntegral | Chris@1524: CSVFormat::ColumnIncreasing | Chris@1524: CSVFormat::ColumnNearEmpty); Chris@1524: QCOMPARE(q, expected); Chris@1359: } Chris@1345: }; Chris@1345: Chris@1345: #endif