File indexing completed on 2024-05-05 03:41:18

0001 /*************************************************************************************
0002  *  Copyright (C) 2012 by Aleix Pol Gonzalez <aleixpol@kde.org>                      *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "plotsmodeltest.h"
0020 
0021 #include "analitzaplot/planecurve.h"
0022 #include "analitzaplot/plotsmodel.h"
0023 #include <plotsfactory.h>
0024 #include <surface.h>
0025 
0026 #include "analitza/expression.h"
0027 #include "analitza/variables.h"
0028 #include <analitza/analyzer.h>
0029 #include <analitza/value.h>
0030 #include <QTest>
0031 #include <cmath>
0032 
0033 using namespace std;
0034 using namespace Analitza;
0035 
0036 QTEST_MAIN( PlotsModelTest )
0037 
0038 PlotsModelTest::PlotsModelTest(QObject *parent)
0039     : QObject(parent)
0040 {
0041     m_model = new PlotsModel(this);
0042 }
0043 
0044 PlotsModelTest::~PlotsModelTest()
0045 {}
0046 
0047 void PlotsModelTest::testAppend_data()
0048 {
0049     QTest::addColumn<QString>("input");
0050 
0051     QTest::newRow("x->flat") << "x->1";
0052     QTest::newRow("x->x") << "x->x";
0053     QTest::newRow("x->and") << "x->piecewise { and(x>-1, x<1) ? 1, ?0 }";
0054     QTest::newRow("x->abs") << "x->abs(x)";
0055     QTest::newRow("x->addition") << "x->2+x";
0056     QTest::newRow("x->minus") << "x->x-2";
0057     QTest::newRow("x->log") << "x->log x";
0058     QTest::newRow("x->tan") << "x->tan x";
0059     QTest::newRow("x->sqrt") << "x->root(x, 2)";
0060     QTest::newRow("x->factorof") << "x->factorof(x,x)";
0061     QTest::newRow("x->sum") << "x->sum(t : t=0..3)";
0062     QTest::newRow("x->piece") << "x->piecewise { gt(x,0) ? selector(1, vector{x, 1/x}),"
0063                               "? selector(2, vector{x, 1/x} ) }";
0064     QTest::newRow("x->diff1") << "x->(diff(x:x))(x)";
0065     QTest::newRow("x->diffx") << "x->(diff(x^2:x))(x)";
0066     QTest::newRow("x->absdiv") << "abs(4-2x)/(x-1)";
0067     QTest::newRow("y->flat") << "y->1";
0068     QTest::newRow("y->trigonometric") << "y->sin y";
0069     QTest::newRow("polar->scalar") << "q->2";
0070     QTest::newRow("polar->function") << "q->sin q";
0071     QTest::newRow("polar->hard") << "q->ceiling(q/(2*pi))";
0072     QTest::newRow("polar->strange") << "q->q/q";
0073 
0074     QTest::newRow("parametric") << "t->vector{t,t**2}";
0075     QTest::newRow("parametric1") << "t->vector{16*sin(t)^3, abs(t)^0.3*root(t,2)}";
0076     QTest::newRow("implicit") << "x+y=9";
0077 }
0078 
0079 void PlotsModelTest::testAppend()
0080 {
0081     QFETCH(QString, input);
0082 
0083     Expression exp(input);
0084     PlotBuilder plot = PlotsFactory::self()->requestPlot(exp, Dim2D);
0085     QVERIFY(plot.canDraw());
0086     
0087     PlaneCurve* item = dynamic_cast<PlaneCurve*>(plot.create(Qt::red, QStringLiteral("hola")));
0088     m_model->addPlot(item);
0089     if(!item->isCorrect())
0090         qDebug() << "errors:" << item->errors();
0091     QVERIFY(item->isCorrect());
0092     
0093     item->update(QRect(QPoint(-5, 7), QPoint(5, -7)));
0094     item->update(QRectF(-5,-5,10,10)); // the viewport is in world coordinates (not screen coordinates)
0095     if(!item->isCorrect())
0096         qDebug() << "error" << item->errors();
0097     QVERIFY(item->points().count()>=2);
0098     
0099     item->tangent(QPointF(1,1));
0100 }
0101 
0102 
0103 void PlotsModelTest::testDelete()
0104 {
0105     Expression exp(QStringLiteral("x*x+y*y+z*z=9"));
0106     PlotBuilder plot = PlotsFactory::self()->requestPlot(exp, Dim3D);
0107     m_model->addPlot(plot.create(Qt::red, QStringLiteral("item to be deleted")));
0108 
0109     int size = m_model->rowCount();
0110 
0111     m_model->removeRow(0);
0112 
0113     QCOMPARE(m_model->rowCount(), size - 1);
0114 }
0115 
0116 void PlotsModelTest::testExamples2D()
0117 {
0118     QStringList examples = PlotsFactory::self()->examples(Dim2D);
0119     foreach(const QString& example, examples) {
0120         PlotBuilder plot = PlotsFactory::self()->requestPlot(Analitza::Expression(example), Dim2D);
0121         QVERIFY(plot.canDraw());
0122         
0123         PlaneCurve* curve = dynamic_cast<PlaneCurve*>(plot.create(Qt::black, QStringLiteral("lalala")));
0124         QVERIFY(curve);
0125         curve->update(QRectF(-5,-5,10,10)); // the viewport is in world coordinates (not screen coordinates)
0126         QVERIFY(curve->isCorrect());
0127         if(curve->points().count()<2)
0128             qDebug() << "pointless plot: " << example;
0129         QVERIFY(curve->points().count()>=2);
0130     }
0131 }
0132 
0133 #include "moc_plotsmodeltest.cpp"