File indexing completed on 2024-10-13 06:30:11

0001 /*
0002     File                 : MatioFilterTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for the Matio I/O-filter.
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2021 Stefan Gerlach <stefan.gerlach@uni.kn>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "MatioFilterTest.h"
0011 #include "backend/datasources/filters/MatioFilter.h"
0012 #include "backend/matrix/Matrix.h"
0013 #include "backend/spreadsheet/Spreadsheet.h"
0014 
0015 #include <KLocalizedString>
0016 
0017 void MatioFilterTest::testImportDouble() {
0018     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0019     MatioFilter filter;
0020 
0021     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testdouble_4.2c_SOL2.mat"));
0022     filter.setCurrentVarName(QLatin1String("testdouble"));
0023     const auto mode = AbstractFileFilter::ImportMode::Replace;
0024     filter.readDataFromFile(fileName, &spreadsheet, mode);
0025 
0026     QCOMPARE(spreadsheet.columnCount(), 1);
0027     QCOMPARE(spreadsheet.rowCount(), 9);
0028     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0029     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0030     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0031 
0032     // for (int i = 0; i < spreadsheet.rowCount(); i++)
0033     //  DEBUG(std::setprecision(15) << spreadsheet.column(0)->valueAt(i))
0034 
0035     QCOMPARE(spreadsheet.column(0)->valueAt(0), 0);
0036     QCOMPARE(spreadsheet.column(0)->valueAt(1), M_PI / 4.);
0037     QCOMPARE(spreadsheet.column(0)->valueAt(2), M_PI / 2.);
0038     QCOMPARE(spreadsheet.column(0)->valueAt(3), 3. * M_PI / 4.);
0039     QCOMPARE(spreadsheet.column(0)->valueAt(4), M_PI);
0040     QCOMPARE(spreadsheet.column(0)->valueAt(5), 5. * M_PI / 4.);
0041     QCOMPARE(spreadsheet.column(0)->valueAt(6), 3. * M_PI / 2.);
0042     QCOMPARE(spreadsheet.column(0)->valueAt(7), 7. * M_PI / 4.);
0043     QCOMPARE(spreadsheet.column(0)->valueAt(8), 2. * M_PI);
0044 
0045     // DEBUG(Q_FUNC_INFO << ", value = " << spreadsheet.column(0)->valueAt(0))
0046 }
0047 
0048 void MatioFilterTest::testImportSpreadsheet() {
0049     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0050     MatioFilter filter;
0051 
0052     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testmatrix_7.4_GLNX86.mat"));
0053     filter.setCurrentVarName(QLatin1String("testmatrix"));
0054     const auto mode = AbstractFileFilter::ImportMode::Replace;
0055     filter.readDataFromFile(fileName, &spreadsheet, mode);
0056 
0057     QCOMPARE(spreadsheet.columnCount(), 5);
0058     QCOMPARE(spreadsheet.rowCount(), 3);
0059     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0060     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0061     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0062     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0063     QCOMPARE(spreadsheet.column(4)->columnMode(), AbstractColumn::ColumnMode::Double);
0064 
0065     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0066     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0067     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0068     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0069     QCOMPARE(spreadsheet.column(4)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0070 
0071     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0072     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0073     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("Column 3"));
0074     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("Column 4"));
0075     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("Column 5"));
0076 
0077     QCOMPARE(spreadsheet.column(0)->valueAt(0), 1);
0078     QCOMPARE(spreadsheet.column(0)->valueAt(1), 2);
0079     QCOMPARE(spreadsheet.column(0)->valueAt(2), 3);
0080     QCOMPARE(spreadsheet.column(1)->valueAt(0), 2);
0081     QCOMPARE(spreadsheet.column(1)->valueAt(1), 0);
0082     QCOMPARE(spreadsheet.column(1)->valueAt(2), 0);
0083     QCOMPARE(spreadsheet.column(2)->valueAt(0), 3);
0084     QCOMPARE(spreadsheet.column(2)->valueAt(1), 0);
0085     QCOMPARE(spreadsheet.column(2)->valueAt(2), 0);
0086     QCOMPARE(spreadsheet.column(3)->valueAt(0), 4);
0087     QCOMPARE(spreadsheet.column(3)->valueAt(1), 0);
0088     QCOMPARE(spreadsheet.column(3)->valueAt(2), 0);
0089     QCOMPARE(spreadsheet.column(4)->valueAt(0), 5);
0090     QCOMPARE(spreadsheet.column(4)->valueAt(1), 0);
0091     QCOMPARE(spreadsheet.column(4)->valueAt(2), 0);
0092 
0093     // DEBUG(Q_FUNC_INFO << ", value = " << matrix.column(0)->valueAt(0))
0094 }
0095 
0096 // same test but with start/end row/col
0097 void MatioFilterTest::testImportSpreadsheetPortion() {
0098     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0099     MatioFilter filter;
0100 
0101     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testmatrix_7.4_GLNX86.mat"));
0102     filter.setCurrentVarName(QLatin1String("testmatrix"));
0103     const auto mode = AbstractFileFilter::ImportMode::Replace;
0104     // set start/end row/col
0105     filter.setStartRow(2);
0106     filter.setEndRow(3);
0107     filter.setStartColumn(2);
0108     filter.setEndColumn(3);
0109     filter.readDataFromFile(fileName, &spreadsheet, mode);
0110 
0111     QCOMPARE(spreadsheet.columnCount(), 2);
0112     QCOMPARE(spreadsheet.rowCount(), 2);
0113     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0114     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0115 
0116     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0117     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0118 
0119     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0120     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0121 
0122     QCOMPARE(spreadsheet.column(0)->valueAt(0), 0);
0123     QCOMPARE(spreadsheet.column(0)->valueAt(1), 0);
0124     QCOMPARE(spreadsheet.column(1)->valueAt(0), 0);
0125     QCOMPARE(spreadsheet.column(1)->valueAt(1), 0);
0126 
0127     // DEBUG(Q_FUNC_INFO << ", value = " << matrix.column(0)->valueAt(0))
0128 }
0129 
0130 void MatioFilterTest::testImportMatrix() {
0131     Matrix matrix(QStringLiteral("test"), false);
0132     MatioFilter filter;
0133 
0134     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testmatrix_7.4_GLNX86.mat"));
0135     filter.setCurrentVarName(QLatin1String("testmatrix"));
0136     const auto mode = AbstractFileFilter::ImportMode::Replace;
0137     filter.readDataFromFile(fileName, &matrix, mode);
0138 
0139     QCOMPARE(matrix.columnCount(), 5);
0140     QCOMPARE(matrix.rowCount(), 3);
0141     // QCOMPARE(matrix.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0142     // QCOMPARE(matrix.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0143     // QCOMPARE(matrix.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0144 
0145     // QCOMPARE(matrix.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0146     // QCOMPARE(matrix.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0147     // QCOMPARE(matrix.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0148 
0149     QCOMPARE(matrix.cell<double>(0, 0), 1);
0150     QCOMPARE(matrix.cell<double>(1, 0), 2);
0151     QCOMPARE(matrix.cell<double>(2, 0), 3);
0152     QCOMPARE(matrix.cell<double>(0, 1), 2);
0153     QCOMPARE(matrix.cell<double>(1, 1), 0);
0154     QCOMPARE(matrix.cell<double>(2, 1), 0);
0155     QCOMPARE(matrix.cell<double>(0, 2), 3);
0156     QCOMPARE(matrix.cell<double>(1, 2), 0);
0157     QCOMPARE(matrix.cell<double>(2, 2), 0);
0158     QCOMPARE(matrix.cell<double>(0, 3), 4);
0159     QCOMPARE(matrix.cell<double>(1, 3), 0);
0160     QCOMPARE(matrix.cell<double>(2, 3), 0);
0161     QCOMPARE(matrix.cell<double>(0, 4), 5);
0162     QCOMPARE(matrix.cell<double>(1, 4), 0);
0163     QCOMPARE(matrix.cell<double>(2, 4), 0);
0164 }
0165 
0166 void MatioFilterTest::testImportSparse() {
0167     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0168     MatioFilter filter;
0169 
0170     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testsparse_6.5.1_GLNX86.mat"));
0171     filter.setCurrentVarName(QLatin1String("testsparse"));
0172     const auto mode = AbstractFileFilter::ImportMode::Replace;
0173     filter.readDataFromFile(fileName, &spreadsheet, mode);
0174 
0175     QCOMPARE(spreadsheet.columnCount(), 5);
0176     QCOMPARE(spreadsheet.rowCount(), 3);
0177     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0178     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0179     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0180     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0181     QCOMPARE(spreadsheet.column(4)->columnMode(), AbstractColumn::ColumnMode::Double);
0182 
0183     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0184     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0185     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0186     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0187     QCOMPARE(spreadsheet.column(4)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0188 
0189     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0190     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0191     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("Column 3"));
0192     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("Column 4"));
0193     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("Column 5"));
0194 
0195     QCOMPARE(spreadsheet.column(0)->valueAt(0), 1);
0196     QCOMPARE(spreadsheet.column(0)->valueAt(1), 2);
0197     QCOMPARE(spreadsheet.column(0)->valueAt(2), 3);
0198     QCOMPARE(spreadsheet.column(1)->valueAt(0), 2);
0199     QCOMPARE(spreadsheet.column(1)->valueAt(1), 0);
0200     QCOMPARE(spreadsheet.column(1)->valueAt(2), 0);
0201     QCOMPARE(spreadsheet.column(2)->valueAt(0), 3);
0202     QCOMPARE(spreadsheet.column(2)->valueAt(1), 0);
0203     QCOMPARE(spreadsheet.column(2)->valueAt(2), 0);
0204     QCOMPARE(spreadsheet.column(3)->valueAt(0), 4);
0205     QCOMPARE(spreadsheet.column(3)->valueAt(1), 0);
0206     QCOMPARE(spreadsheet.column(3)->valueAt(2), 0);
0207     QCOMPARE(spreadsheet.column(4)->valueAt(0), 5);
0208     QCOMPARE(spreadsheet.column(4)->valueAt(1), 0);
0209     QCOMPARE(spreadsheet.column(4)->valueAt(2), 0);
0210 
0211     // DEBUG(Q_FUNC_INFO << ", value = " << matrix.column(0)->valueAt(0))
0212 }
0213 
0214 void MatioFilterTest::testImportLogicalSparse() {
0215     Matrix matrix(QStringLiteral("test"), false);
0216     MatioFilter filter;
0217 
0218     const QString& fileName = QFINDTESTDATA(QLatin1String("data/logical_sparse.mat"));
0219     filter.setCurrentVarName(QLatin1String("sp_log_5_4"));
0220     const auto mode = AbstractFileFilter::ImportMode::Replace;
0221     filter.readDataFromFile(fileName, &matrix, mode);
0222 
0223     QCOMPARE(matrix.columnCount(), 4);
0224     QCOMPARE(matrix.rowCount(), 5);
0225 
0226     QCOMPARE(matrix.cell<int>(0, 0), 1);
0227     QCOMPARE(matrix.cell<int>(1, 0), 0);
0228     QCOMPARE(matrix.cell<int>(2, 0), 0);
0229     QCOMPARE(matrix.cell<int>(3, 0), 0);
0230     QCOMPARE(matrix.cell<int>(4, 0), 0);
0231     QCOMPARE(matrix.cell<int>(0, 1), 1);
0232     QCOMPARE(matrix.cell<int>(1, 1), 0);
0233     QCOMPARE(matrix.cell<int>(2, 1), 0);
0234     QCOMPARE(matrix.cell<int>(3, 1), 0);
0235     QCOMPARE(matrix.cell<int>(4, 1), 0);
0236     QCOMPARE(matrix.cell<int>(0, 2), 1);
0237     QCOMPARE(matrix.cell<int>(1, 2), 1);
0238     QCOMPARE(matrix.cell<int>(2, 2), 1);
0239     QCOMPARE(matrix.cell<int>(3, 2), 0);
0240     QCOMPARE(matrix.cell<int>(4, 2), 0);
0241     QCOMPARE(matrix.cell<int>(0, 3), 0);
0242     QCOMPARE(matrix.cell<int>(1, 3), 0);
0243     QCOMPARE(matrix.cell<int>(2, 3), 0);
0244     QCOMPARE(matrix.cell<int>(3, 3), 0);
0245     QCOMPARE(matrix.cell<int>(4, 3), 0);
0246 }
0247 
0248 void MatioFilterTest::testImportLogicalSparsePortion() {
0249     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0250     MatioFilter filter;
0251 
0252     const QString& fileName = QFINDTESTDATA(QLatin1String("data/logical_sparse.mat"));
0253     filter.setCurrentVarName(QLatin1String("sp_log_5_4"));
0254     const auto mode = AbstractFileFilter::ImportMode::Replace;
0255     // set start/end row/col
0256     filter.setStartRow(2);
0257     filter.setEndRow(3);
0258     filter.setStartColumn(2);
0259     filter.setEndColumn(3);
0260     filter.readDataFromFile(fileName, &spreadsheet, mode);
0261 
0262     QCOMPARE(spreadsheet.columnCount(), 2);
0263     QCOMPARE(spreadsheet.rowCount(), 2);
0264     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Integer);
0265     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Integer);
0266 
0267     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0268     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0269 
0270     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0271     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0272 
0273     QCOMPARE(spreadsheet.column(0)->valueAt(0), 0);
0274     QCOMPARE(spreadsheet.column(0)->valueAt(1), 0);
0275     QCOMPARE(spreadsheet.column(1)->valueAt(0), 1);
0276     QCOMPARE(spreadsheet.column(1)->valueAt(1), 1);
0277 }
0278 
0279 void MatioFilterTest::testImportSparseComplex() {
0280     Matrix matrix(QStringLiteral("test"), false);
0281     MatioFilter filter;
0282 
0283     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testsparsecomplex_7.4_GLNX86.mat"));
0284     filter.setCurrentVarName(QLatin1String("testsparsecomplex"));
0285     const auto mode = AbstractFileFilter::ImportMode::Replace;
0286     filter.readDataFromFile(fileName, &matrix, mode);
0287 
0288     QCOMPARE(matrix.columnCount(), 10);
0289     QCOMPARE(matrix.rowCount(), 3);
0290 
0291     /*DEBUG(Q_FUNC_INFO << ", value = " << matrix.cell<double>(1, 1)) */
0292     QCOMPARE(matrix.cell<double>(0, 0), 1.);
0293     QCOMPARE(matrix.cell<double>(1, 0), 2.);
0294     QCOMPARE(matrix.cell<double>(2, 0), 3.);
0295     QCOMPARE(matrix.cell<double>(0, 1), 1.);
0296     QCOMPARE(matrix.cell<double>(1, 1), 0.);
0297     QCOMPARE(matrix.cell<double>(2, 1), 0.);
0298     QCOMPARE(matrix.cell<double>(0, 2), 2.);
0299     QCOMPARE(matrix.cell<double>(0, 3), 0.);
0300     QCOMPARE(matrix.cell<double>(0, 4), 3.);
0301     QCOMPARE(matrix.cell<double>(0, 5), 0.);
0302     QCOMPARE(matrix.cell<double>(0, 6), 4.);
0303     QCOMPARE(matrix.cell<double>(0, 7), 0.);
0304     QCOMPARE(matrix.cell<double>(0, 8), 5.);
0305     QCOMPARE(matrix.cell<double>(0, 9), 0.);
0306 }
0307 
0308 void MatioFilterTest::testImportStruct() {
0309     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0310     MatioFilter filter;
0311 
0312     const QString& fileName = QFINDTESTDATA(QLatin1String("data/teststruct_7.4_GLNX86.mat"));
0313     filter.setCurrentVarName(QLatin1String("teststruct"));
0314     const auto mode = AbstractFileFilter::ImportMode::Replace;
0315     filter.readDataFromFile(fileName, &spreadsheet, mode);
0316 
0317     QCOMPARE(spreadsheet.columnCount(), 4);
0318     QCOMPARE(spreadsheet.rowCount(), 3);
0319     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Text);
0320     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0321     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0322     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0323 
0324     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0325     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0326     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0327     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0328 
0329     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("stringfield"));
0330     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("doublefield"));
0331     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("complexfield - Re"));
0332     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("complexfield - Im"));
0333 
0334     QCOMPARE(spreadsheet.column(0)->textAt(0), QLatin1String("Rats live on no evil star."));
0335     for (int i = 1; i < 4; i++) {
0336         QCOMPARE(spreadsheet.column(i)->valueAt(0), M_SQRT2);
0337         QCOMPARE(spreadsheet.column(i)->valueAt(1), M_E);
0338         QCOMPARE(spreadsheet.column(i)->valueAt(2), M_PI);
0339     }
0340 }
0341 
0342 void MatioFilterTest::testImportStructPortion() {
0343     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0344     MatioFilter filter;
0345 
0346     const QString& fileName = QFINDTESTDATA(QLatin1String("data/teststruct_7.4_GLNX86.mat"));
0347     filter.setCurrentVarName(QLatin1String("teststruct"));
0348     const auto mode = AbstractFileFilter::ImportMode::Replace;
0349     // set start/end row/col
0350     filter.setStartRow(2);
0351     filter.setEndRow(3);
0352     filter.setStartColumn(2);
0353     filter.setEndColumn(3);
0354     filter.readDataFromFile(fileName, &spreadsheet, mode);
0355 
0356     QCOMPARE(spreadsheet.columnCount(), 3);
0357     QCOMPARE(spreadsheet.rowCount(), 2);
0358     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0359     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0360     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0361 
0362     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0363     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0364     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0365 
0366     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("doublefield"));
0367     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("complexfield - Re"));
0368     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("complexfield - Im"));
0369 
0370     for (int i = 0; i < 2; i++) {
0371         QCOMPARE(spreadsheet.column(i)->valueAt(0), M_E);
0372         QCOMPARE(spreadsheet.column(i)->valueAt(1), M_PI);
0373     }
0374 }
0375 
0376 void MatioFilterTest::testImportCell() {
0377     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0378     MatioFilter filter;
0379 
0380     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testcell_6.1_SOL2.mat"));
0381     filter.setCurrentVarName(QLatin1String("testcell"));
0382     const auto mode = AbstractFileFilter::ImportMode::Replace;
0383     filter.readDataFromFile(fileName, &spreadsheet, mode);
0384 
0385     QCOMPARE(spreadsheet.columnCount(), 4);
0386     QCOMPARE(spreadsheet.rowCount(), 3);
0387     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Text);
0388     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0389     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0390     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0391 
0392     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0393     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0394     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0395     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0396 
0397     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0398     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0399     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("Column 3"));
0400     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("Column 4"));
0401 
0402     QCOMPARE(spreadsheet.column(0)->textAt(1), QLatin1String());
0403     QCOMPARE(spreadsheet.column(0)->textAt(2), QLatin1String());
0404     QCOMPARE(spreadsheet.column(0)->textAt(3), QLatin1String());
0405     QCOMPARE(spreadsheet.column(1)->valueAt(0), 1.);
0406     QCOMPARE(spreadsheet.column(1)->valueAt(1), NAN);
0407     QCOMPARE(spreadsheet.column(1)->valueAt(2), NAN);
0408     QCOMPARE(spreadsheet.column(2)->valueAt(0), 1.);
0409     QCOMPARE(spreadsheet.column(2)->valueAt(1), 2.);
0410     QCOMPARE(spreadsheet.column(2)->valueAt(2), NAN);
0411     QCOMPARE(spreadsheet.column(3)->valueAt(0), 1.);
0412     QCOMPARE(spreadsheet.column(3)->valueAt(1), 2.);
0413     QCOMPARE(spreadsheet.column(3)->valueAt(2), 3.);
0414 }
0415 
0416 void MatioFilterTest::testImportCellPortion() {
0417     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0418     MatioFilter filter;
0419 
0420     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testcell_6.1_SOL2.mat"));
0421     filter.setCurrentVarName(QLatin1String("testcell"));
0422     const auto mode = AbstractFileFilter::ImportMode::Replace;
0423     // set start/end row/col
0424     filter.setStartRow(1);
0425     filter.setEndRow(3);
0426     filter.setStartColumn(2);
0427     filter.setEndColumn(3);
0428     filter.readDataFromFile(fileName, &spreadsheet, mode);
0429 
0430     QCOMPARE(spreadsheet.columnCount(), 2);
0431     QCOMPARE(spreadsheet.rowCount(), 2);
0432     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0433     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0434 
0435     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0436     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0437 
0438     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0439     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0440 
0441     QCOMPARE(spreadsheet.column(0)->valueAt(0), 1.);
0442     QCOMPARE(spreadsheet.column(0)->valueAt(1), NAN);
0443     QCOMPARE(spreadsheet.column(1)->valueAt(0), 1.);
0444     QCOMPARE(spreadsheet.column(1)->valueAt(1), 2.);
0445 }
0446 
0447 void MatioFilterTest::testImportEmptyCell() {
0448     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0449     MatioFilter filter;
0450 
0451     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testemptycell_7.4_GLNX86.mat"));
0452     filter.setCurrentVarName(QLatin1String("testemptycell"));
0453     const auto mode = AbstractFileFilter::ImportMode::Replace;
0454     filter.readDataFromFile(fileName, &spreadsheet, mode);
0455 
0456     QCOMPARE(spreadsheet.columnCount(), 5);
0457     QCOMPARE(spreadsheet.rowCount(), 1);
0458     QCOMPARE(spreadsheet.column(0)->columnMode(), AbstractColumn::ColumnMode::Double);
0459     QCOMPARE(spreadsheet.column(1)->columnMode(), AbstractColumn::ColumnMode::Double);
0460     QCOMPARE(spreadsheet.column(2)->columnMode(), AbstractColumn::ColumnMode::Double);
0461     QCOMPARE(spreadsheet.column(3)->columnMode(), AbstractColumn::ColumnMode::Double);
0462     QCOMPARE(spreadsheet.column(4)->columnMode(), AbstractColumn::ColumnMode::Double);
0463 
0464     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0465     QCOMPARE(spreadsheet.column(1)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0466     QCOMPARE(spreadsheet.column(2)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0467     QCOMPARE(spreadsheet.column(3)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0468     QCOMPARE(spreadsheet.column(4)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0469 
0470     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0471     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0472     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("Column 3"));
0473     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("Column 4"));
0474     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("Column 5"));
0475 
0476     QCOMPARE(spreadsheet.column(0)->valueAt(0), 1);
0477     QCOMPARE(spreadsheet.column(1)->valueAt(0), 2);
0478     QCOMPARE(spreadsheet.column(2)->valueAt(0), NAN);
0479     QCOMPARE(spreadsheet.column(3)->valueAt(0), NAN);
0480     QCOMPARE(spreadsheet.column(4)->valueAt(0), 3);
0481 }
0482 
0483 void MatioFilterTest::testImportMultipleVars() {
0484     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0485     MatioFilter filter;
0486 
0487     const QString& fileName = QFINDTESTDATA(QLatin1String("data/testmulti_7.4_GLNX86.mat"));
0488     filter.setSelectedVarNames(QStringList() << QLatin1String("a") << QLatin1String("theta"));
0489     const auto mode = AbstractFileFilter::ImportMode::Replace;
0490     filter.readDataFromFile(fileName, &spreadsheet, mode);
0491 
0492     QCOMPARE(spreadsheet.columnCount(), 6);
0493     QCOMPARE(spreadsheet.rowCount(), 9);
0494 
0495     for (int i = 0; i < 6; i++)
0496         QCOMPARE(spreadsheet.column(i)->columnMode(), AbstractColumn::ColumnMode::Double);
0497 
0498     QCOMPARE(spreadsheet.column(0)->plotDesignation(), AbstractColumn::PlotDesignation::X);
0499     for (int i = 1; i < 6; i++)
0500         QCOMPARE(spreadsheet.column(i)->plotDesignation(), AbstractColumn::PlotDesignation::Y);
0501 
0502     QCOMPARE(spreadsheet.column(0)->name(), QLatin1String("Column 1"));
0503     QCOMPARE(spreadsheet.column(1)->name(), QLatin1String("Column 2"));
0504     QCOMPARE(spreadsheet.column(2)->name(), QLatin1String("Column 3"));
0505     QCOMPARE(spreadsheet.column(3)->name(), QLatin1String("Column 4"));
0506     QCOMPARE(spreadsheet.column(4)->name(), QLatin1String("Column 5"));
0507     QCOMPARE(spreadsheet.column(5)->name(), QLatin1String("Column 6"));
0508 
0509     QCOMPARE(spreadsheet.column(0)->valueAt(0), 1);
0510     QCOMPARE(spreadsheet.column(1)->valueAt(0), 2);
0511     QCOMPARE(spreadsheet.column(2)->valueAt(0), 3);
0512     QCOMPARE(spreadsheet.column(3)->valueAt(0), 4);
0513     QCOMPARE(spreadsheet.column(4)->valueAt(0), 5);
0514     QCOMPARE(spreadsheet.column(5)->valueAt(0), 0);
0515     QCOMPARE(spreadsheet.column(5)->valueAt(2), M_PI / 2.);
0516     QCOMPARE(spreadsheet.column(5)->valueAt(4), M_PI);
0517     QCOMPARE(spreadsheet.column(5)->valueAt(6), 3. / 2. * M_PI);
0518     QCOMPARE(spreadsheet.column(5)->valueAt(8), 2. * M_PI);
0519 }
0520 
0521 QTEST_MAIN(MatioFilterTest)