File indexing completed on 2024-04-14 03:39:20

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org>                               *
0003  *  Copyright (C) 2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com>      *
0004  *                                                                                   *
0005  *  This program is free software; you can redistribute it and/or                    *
0006  *  modify it under the terms of the GNU General Public License                      *
0007  *  as published by the Free Software Foundation; either version 2                   *
0008  *  of the License, or (at your option) any later version.                           *
0009  *                                                                                   *
0010  *  This program is distributed in the hope that it will be useful,                  *
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0013  *  GNU General Public License for more details.                                     *
0014  *                                                                                   *
0015  *  You should have received a copy of the GNU General Public License                *
0016  *  along with this program; if not, write to the Free Software                      *
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0018  *************************************************************************************/
0019 
0020 #include "planecurvetest.h"
0021 #include "analitzaplot/planecurve.h"
0022 #include <plotsfactory.h>
0023 #include "analitza/expression.h"
0024 #include "analitza/variables.h"
0025 #include <QTest>
0026 #include <cmath>
0027 
0028 using namespace std;
0029 using namespace Analitza;
0030 
0031 QTEST_MAIN( PlaneCurveTest )
0032 
0033 PlaneCurveTest::PlaneCurveTest(QObject *parent)
0034     : QObject(parent)
0035     , m_vars(new Analitza::Variables)
0036 {
0037 }
0038 
0039 PlaneCurveTest::~PlaneCurveTest()
0040 {
0041 }
0042 
0043 void PlaneCurveTest::initTestCase()
0044 {}
0045 
0046 void PlaneCurveTest::cleanupTestCase()
0047 {}
0048 
0049 void PlaneCurveTest::testCorrect_data()
0050 {
0051     QTest::addColumn<QString>("input");
0052 
0053     QTest::newRow("fx-diag-line") << "x->x";
0054     QTest::newRow("fy-diag-line") << "y->y";
0055     QTest::newRow("vector-diag-line") << "t->vector{t,t}";
0056     QTest::newRow("simple-algebraic") << "x*x+y*y=3";
0057     QTest::newRow("roots") << "y=root(x,y)";
0058     QTest::newRow("polar") << "abs(sin(2*q)/sin(q/2))";
0059 }
0060 
0061 void PlaneCurveTest::testCorrect()
0062 {
0063     QFETCH(QString, input);
0064     
0065     PlotBuilder pb = PlotsFactory::self()->requestPlot(Expression(input), Dim2D);
0066 
0067     QVERIFY(pb.canDraw());
0068     if(pb.canDraw()) {
0069         FunctionGraph* item = pb.create(Qt::green, QStringLiteral("hola"));
0070         QVERIFY(item);
0071         QVERIFY(item->isCorrect());
0072         PlaneCurve* curve = dynamic_cast<PlaneCurve*>(item);
0073         QVERIFY(curve);
0074 
0075         curve->update(QRect(QPoint(-5, 7), QPoint(5, -7)));
0076         curve->update(QRectF(-5,-5,10,10));
0077         QVERIFY(item->isCorrect() && !curve->points().isEmpty());
0078     }
0079 }
0080 
0081 void PlaneCurveTest::testIncorrect_data()
0082 {
0083     QTest::addColumn<QString>("input");
0084 
0085     //QTest::newRow("empty function") << ""; // se elimina este test porque no se podran crear graficos sin una expresion correcta o con una vacia
0086     QTest::newRow("undefined var") << "x:=w";
0087     QTest::newRow("parametric-wrongvector") << "t->vector{3}";
0088     QTest::newRow("wrong-dimension") << "vector{2,3}";
0089     QTest::newRow("wrong-dimension-y") << "y->vector{2,3}";
0090     QTest::newRow("wrong-dimension-q") << "q->vector{2,3}";
0091     QTest::newRow("wrong-parametric") << "t->v";
0092     QTest::newRow("wrong-variable") << "x->x(x)";
0093     QTest::newRow("wrong-call") << "(x+1)(x+2)";
0094     QTest::newRow("wrong-inf") << "y->y/0";
0095     QTest::newRow("wrong-nan") << "x/0 + y/0 = 89";
0096     QTest::newRow("collision") << "(x,y)->5=x*y";
0097 
0098 //     QTest::newRow("implicit.notindomain") << "(x,y)->3-sin(x)*sin(y)";
0099 
0100 //     QTest::newRow("not a function") << "t";
0101 }
0102 
0103 void PlaneCurveTest::testIncorrect()
0104 {
0105     QFETCH(QString, input);
0106 
0107     PlotBuilder rp = PlotsFactory::self()->requestPlot(Expression(input), Dim2D);
0108     if(rp.canDraw()) {
0109         FunctionGraph* f = rp.create(Qt::red, QStringLiteral("lala"));
0110         PlaneCurve* curve = dynamic_cast<PlaneCurve*>(f);
0111         QVERIFY(curve);
0112         
0113         curve->update(QRectF(-5,-5,10,10));
0114         QVERIFY(!f->isCorrect() || curve->points().isEmpty());
0115     }
0116 }
0117 
0118 void PlaneCurveTest::testJumps_data()
0119 {
0120     QTest::addColumn<QString>("input");
0121     QTest::addColumn<int>("jumps");
0122 
0123     QTest::newRow("tanx") << "x->tan x" << 6;
0124     QTest::newRow("divx") << "x->1/x" << 1;
0125 }
0126 
0127 void PlaneCurveTest::testJumps()
0128 {
0129     QFETCH(QString, input);
0130     QFETCH(int, jumps);
0131 
0132     PlotItem* plot = PlotsFactory::self()->requestPlot(Expression(input), Dim2D).create(Qt::red, QStringLiteral("hola"));
0133     PlaneCurve* f3 = dynamic_cast<PlaneCurve*>(plot);
0134     QVERIFY(f3->isCorrect());
0135     f3->update(QRect(-10, 10, 20, -20));
0136     QVERIFY(f3->isCorrect());
0137 
0138     f3->image(QPointF(1,1));
0139 
0140     QCOMPARE(f3->jumps().count(), jumps);
0141 }
0142 
0143 typedef QPair<double, double> IntervalValue;
0144 Q_DECLARE_METATYPE(IntervalValue)
0145 
0146 typedef QPair<Analitza::Expression, Analitza::Expression> IntervalExpression;
0147 Q_DECLARE_METATYPE(IntervalExpression)
0148 
0149 void PlaneCurveTest::testParamIntervals_data()
0150 {
0151     QTest::addColumn<QString>("input");
0152     QTest::addColumn<QString>("param");
0153     QTest::addColumn<IntervalValue>("interval_value");
0154     QTest::addColumn<IntervalExpression>("interval_expression");
0155 
0156     QTest::newRow("simple_interval_vals") << "x->x*x" << "x" <<
0157                                           qMakePair(-7.0, 5.0) << qMakePair(Analitza::Expression(QStringLiteral("a+b-4")), Analitza::Expression(QStringLiteral("16")));
0158 
0159     QTest::newRow("implicit_curve_1_interval_vals") << "x*x+y*y=8" << "y" <<
0160             qMakePair(-9.0+2, 15.0) << qMakePair(Analitza::Expression(QStringLiteral("-abs(a*b)")), Analitza::Expression(QStringLiteral("cos(0)*a*a")));
0161 }
0162 
0163 void PlaneCurveTest::testParamIntervals()
0164 {
0165     QFETCH(QString, input);
0166     QFETCH(QString, param);
0167     QFETCH(IntervalValue, interval_value);
0168     QFETCH(IntervalExpression, interval_expression);
0169 
0170     m_vars->modify(QStringLiteral("a"), -4.0);
0171     m_vars->modify(QStringLiteral("b"), -9.5);
0172 
0173     PlotItem* plot = PlotsFactory::self()->requestPlot(Expression(input), Dim2D, m_vars).create(Qt::red, QStringLiteral("hola"));
0174     PlaneCurve* f3 = dynamic_cast<PlaneCurve*>(plot);
0175     QVERIFY(f3->isCorrect());
0176 
0177     QVERIFY(f3->setInterval(param, interval_value.first, interval_value.second));
0178     QCOMPARE(f3->interval(param).first, -7.0);
0179 
0180     //Interval as expression
0181     QVERIFY(f3->setInterval(param, interval_expression.first, interval_expression.second));
0182     QCOMPARE(f3->interval(param, true).second.toString(), QStringLiteral("16"));
0183     
0184     delete m_vars->take(QStringLiteral("a"));
0185     delete m_vars->take(QStringLiteral("b"));
0186 }
0187 
0188 
0189 
0190 #include "moc_planecurvetest.cpp"