Mercurial > hg > svcore
comparison 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 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 1523:c1b2eab6ac51 | 1524:64ef24ebb19c | 
|---|---|
| 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 | |
| 130 #endif | 
