Warning, file /education/labplot/tests/CommonTest.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     File                 : CommonTest.h
0003     Project              : LabPlot
0004     Description          : General test class
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2019-2022 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 #ifndef COMMONTEST_H
0011 #define COMMONTEST_H
0012 
0013 #include "backend/worksheet/plots/cartesian/CartesianCoordinateSystem.h"
0014 #include <QtTest>
0015 
0016 extern "C" {
0017 #include <gsl/gsl_math.h>
0018 }
0019 
0020 ///////////////////////// macros ///////////
0021 
0022 // Comparing two values. First a direct comparsion will be done, because for std::nan nsl_math_aproximately_equal does not work
0023 #define VALUES_EQUAL(v1, ref)                                                                                                                                  \
0024     QVERIFY2(v1 == ref ? true : (std::isnan(ref) ? std::isnan(v1) : nsl_math_approximately_equal(v1, ref) == true),                                            \
0025              qPrintable(QStringLiteral("v1:%1, ref:%2").arg((double)v1, 0, 'g', 15, QLatin1Char(' ')).arg((double)ref, 0, 'g', 15, QLatin1Char(' '))))
0026 
0027 #define RANGE_CORRECT(range, start_, end_)                                                                                                                     \
0028     VALUES_EQUAL(range.start(), start_);                                                                                                                       \
0029     VALUES_EQUAL(range.end(), end_);
0030 
0031 /*!
0032  * Checks the range of dim \p dim from the coordinatesystem with index \p cSystemIndex
0033  */
0034 #define CHECK_RANGE_CSYSTEMINDEX(plot, cSystemIndex, dim, start_, end_)                                                                                        \
0035     RANGE_CORRECT(plot->range(dim, plot->coordinateSystem(cSystemIndex)->index(dim)), start_, end_)
0036 
0037 /*!
0038  * Checks the range of dim \p dim from the coordinatesystem assigned to \p aspect
0039  */
0040 #define CHECK_RANGE(plot, aspect, dim, start_, end_) CHECK_RANGE_CSYSTEMINDEX(plot, aspect->coordinateSystemIndex(), dim, start_, end_)
0041 
0042 #define CHECK_SCALE_PLOT(plot, coordinateSystemIndex, dimension, a_ref, b_ref, c_ref)                                                                          \
0043     do {                                                                                                                                                       \
0044         QVERIFY(plot);                                                                                                                                         \
0045         QVERIFY(plot->coordinateSystemCount() > coordinateSystemIndex);                                                                                        \
0046         const auto scales = plot->coordinateSystem(coordinateSystemIndex)->scales(dimension);                                                                  \
0047         QCOMPARE(scales.length(), 1);                                                                                                                          \
0048         QVERIFY(scales.at(0) != nullptr);                                                                                                                      \
0049         CHECK_SCALE(scales.at(0), a_ref, b_ref, c_ref);                                                                                                        \
0050     } while (false);
0051 
0052 #define CHECK_SCALE(scale, a_ref, b_ref, c_ref)                                                                                                                \
0053     do {                                                                                                                                                       \
0054         double a;                                                                                                                                              \
0055         double b;                                                                                                                                              \
0056         double c;                                                                                                                                              \
0057         Range<double> r;                                                                                                                                       \
0058         scale->getProperties(&r, &a, &b, &c);                                                                                                                  \
0059         QVERIFY2(nsl_math_approximately_equal(a, a_ref), qPrintable(QStringLiteral("a: v1:%1, ref:%2").arg(a).arg(a_ref)));                                    \
0060         QVERIFY2(nsl_math_approximately_equal(b, b_ref), qPrintable(QStringLiteral("b: v1:%1, ref:%2").arg(b).arg(b_ref)));                                    \
0061         QVERIFY2(nsl_math_approximately_equal(c, c_ref), qPrintable(QStringLiteral("c: v1:%1, ref:%2").arg(c).arg(c_ref)));                                    \
0062     } while (false);
0063 
0064 #define DEBUG_RANGE(plot, aspect)                                                                                                                              \
0065     {                                                                                                                                                          \
0066         int cSystem = aspect->coordinateSystemIndex();                                                                                                         \
0067         WARN(Q_FUNC_INFO << ", csystem index = " << cSystem)                                                                                                   \
0068         int xIndex = plot->coordinateSystem(cSystem)->index(Dimension::X);                                                                                     \
0069         int yIndex = plot->coordinateSystem(cSystem)->index(Dimension::Y);                                                                                     \
0070                                                                                                                                                                \
0071         auto xrange = plot->range(Dimension::X, xIndex);                                                                                                       \
0072         auto yrange = plot->range(Dimension::Y, yIndex);                                                                                                       \
0073         WARN(Q_FUNC_INFO << ", x index = " << xIndex << ", range = " << xrange.start() << " .. " << xrange.end())                                              \
0074         WARN(Q_FUNC_INFO << ", y index = " << yIndex << ", range = " << yrange.start() << " .. " << yrange.end())                                              \
0075     }
0076 
0077 #define COMPARE_DOUBLE_VECTORS(res, ref)                                                                                                                       \
0078     QCOMPARE(res.length(), ref.length());                                                                                                                      \
0079     for (int i = 0; i < res.length(); i++)                                                                                                                     \
0080         QVERIFY2(qFuzzyCompare(res.at(i), ref.at(i)),                                                                                                          \
0081                  qPrintable(QStringLiteral("i=") + QString::number(i) + QStringLiteral(", res=") + QString::number(res.at(i)) + QStringLiteral(", ref=")       \
0082                             + QString::number(ref.at(i))));
0083 
0084 #define COMPARE_STRING_VECTORS(res, ref)                                                                                                                       \
0085     QCOMPARE(res.length(), ref.length());                                                                                                                      \
0086     for (int i = 0; i < res.length(); i++)                                                                                                                     \
0087         QVERIFY2(res.at(i).compare(ref.at(i)) == 0,                                                                                                            \
0088                  qPrintable(QStringLiteral("i=") + QString::number(i) + QStringLiteral(", res=") + res.at(i) + QStringLiteral(", ref=") + ref.at(i)));
0089 
0090 #define SAVE_PROJECT(project_name)                                                                                                                             \
0091     do {                                                                                                                                                       \
0092         auto* tempFile = new QTemporaryFile(QStringLiteral("XXXXXX_") + QLatin1String(project_name), this);                                                    \
0093         QCOMPARE(tempFile->open(), true);                                                                                                                      \
0094         savePath = tempFile->fileName();                                                                                                                       \
0095         QFile file(savePath);                                                                                                                                  \
0096         QCOMPARE(file.open(QIODevice::WriteOnly), true);                                                                                                       \
0097                                                                                                                                                                \
0098         project.setFileName(tempFile->fileName());                                                                                                             \
0099         QXmlStreamWriter writer(&file);                                                                                                                        \
0100         QPixmap thumbnail;                                                                                                                                     \
0101         project.save(thumbnail, &writer);                                                                                                                      \
0102         file.close();                                                                                                                                          \
0103         DEBUG(QStringLiteral("File stored as: ").toStdString() << tempFile->fileName().toStdString());                                                         \
0104     } while (0);
0105 
0106 ///////////////////////////////////////////////////////
0107 
0108 class CommonTest : public QObject {
0109     Q_OBJECT
0110 
0111 private Q_SLOTS:
0112     void initTestCase();
0113 
0114 protected:
0115     // compare floats with given delta
0116     // delta - relative error
0117     static inline void FuzzyCompare(double actual, double expected, double delta = 1.e-12) {
0118         if (std::abs(expected) < delta)
0119             QVERIFY(std::abs(actual) < delta);
0120         else {
0121             DEBUG(std::setprecision(15) << actual - std::abs(actual) * delta << " <= " << expected << " <= " << actual + std::abs(actual) * delta);
0122             QVERIFY(!gsl_fcmp(actual, expected, delta));
0123         }
0124     }
0125 };
0126 #endif