File indexing completed on 2024-04-21 14:42:48

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 by Aleix Pol <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 "operatorsmodeltest.h"
0020 #include <analitza/expression.h>
0021 #include <analitza/analyzer.h>
0022 #include <analitza/variables.h>
0023 #include <QTest>
0024 #include <cmath>
0025 #include <analitza/analitzautils.h>
0026 #include <analitza/value.h>
0027 
0028 using namespace std;
0029 using Analitza::Expression;
0030 
0031 QTEST_GUILESS_MAIN( OperatorsModelTest )
0032 
0033 OperatorsModelTest::OperatorsModelTest(QObject *parent)
0034     : QObject(parent)
0035 {}
0036 
0037 OperatorsModelTest::~OperatorsModelTest() {}
0038 void OperatorsModelTest::initTestCase() {}
0039 void OperatorsModelTest::cleanupTestCase() {}
0040 
0041 void OperatorsModelTest::testExamples_data()
0042 {
0043     QTest::addColumn<int>("i");
0044     for(int i=0; i<m.rowCount(); i++) {
0045         QModelIndex idx(m.index(i, 0));
0046         QTest::newRow(qPrintable(idx.data().toString())) << i;
0047     }
0048 }
0049 
0050 void OperatorsModelTest::testExamples()
0051 {
0052     QFETCH(int, i);
0053     QModelIndex idx(m.index(i, 0));
0054     
0055     QModelIndex nameIdx, descriptionIdx, sampleIdx, exampleIdx;
0056     nameIdx = idx.sibling(idx.row(), 0);
0057     descriptionIdx = idx.sibling(idx.row(), 1);
0058     sampleIdx = idx.sibling(idx.row(), 2);
0059     exampleIdx = idx.sibling(idx.row(), 3);
0060     
0061     QString name=m.data(nameIdx).toString();
0062     QString description=m.data(descriptionIdx).toString();
0063     QString sample=m.data(sampleIdx).toString();
0064     QString example=m.data(exampleIdx).toString();
0065     
0066 //         qDebug() << "testing: " << name << example;
0067     
0068     QVERIFY(!name.isEmpty());
0069     QVERIFY(!description.isEmpty());
0070     QVERIFY(!sample.isEmpty());
0071     QVERIFY(!example.isEmpty());
0072     Expression ex(example, false);
0073     if(!ex.isCorrect())
0074         qDebug() << "error" << example << ex.error();
0075     QCOMPARE(ex.toString(), example);
0076     QVERIFY(!ex.toMathMLPresentation().isEmpty());
0077     ex = ex.lambdaBody();
0078     
0079     Analitza::Analyzer a;
0080     a.setExpression(ex);
0081     if(!a.isCorrect()) qDebug() << example << "1. error" << a.errors();// QVERIFY(a.isCorrect());
0082     
0083     a.simplify();
0084     if(!a.isCorrect()) qDebug() << example << "2. error" << a.errors();// QVERIFY(a.isCorrect());
0085     a.variables()->modify(QStringLiteral("x"), 0.1);
0086     a.setExpression(ex);
0087     if(!a.isCorrect()) qDebug() << example << "2.1 type error" << ex.toString() << a.errors();// QVERIFY(a.isCorrect());
0088     
0089     bool typecorr = a.type().canReduceTo(Analitza::ExpressionType::Value);
0090     if(!typecorr)
0091         qDebug() << example << "2.2 error" << ex.toString() << a.type().toString();
0092     QVERIFY(typecorr);
0093     
0094     Expression e = a.calculate();
0095     if(!a.isCorrect()) qDebug() << example << "3. error" << a.errors();// QVERIFY(a.isCorrect());
0096     if(!e.isCorrect()) qDebug() << example << "4. error" << e.error(); // QVERIFY(e.isCorrect());
0097     
0098     e = a.evaluate();
0099     if(!a.isCorrect()) qDebug() << example << "5. error" << a.errors();// QVERIFY(a.isCorrect());
0100     if(!e.isCorrect()) qDebug() << example << "6. error" << e.error(); // QVERIFY(e.isCorrect());
0101     QVERIFY(!a.expression().toMathMLPresentation().isEmpty());
0102 }
0103 
0104 
0105 
0106 #include "moc_operatorsmodeltest.cpp"