File indexing completed on 2024-05-12 03:42:00

0001 /*************************************************************************************
0002  *  Copyright (C) 2008 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 "mathmlpresentationtest.h"
0020 #include "mathmlpresentationlexer.h"
0021 #include "expressionparser.h"
0022 #include "expression.h"
0023 #include <QTest>
0024 
0025 using Analitza::Expression;
0026 
0027 QTEST_MAIN(MathMLPresentationTest)
0028 Q_DECLARE_METATYPE(AbstractLexer::TOKEN*)
0029 
0030 MathMLPresentationTest::MathMLPresentationTest(QObject *parent)
0031  : QObject(parent)
0032 {}
0033 
0034 MathMLPresentationTest::~MathMLPresentationTest()
0035 {}
0036 
0037 void MathMLPresentationTest::initTestCase() {}
0038 void MathMLPresentationTest::cleanupTestCase() {}
0039 
0040 void MathMLPresentationTest::testSimple_data()
0041 {
0042     QTest::addColumn<QString>("input");
0043     QTest::addColumn<AbstractLexer::TOKEN*>("output");
0044     
0045     QTest::newRow("1 value") << "<mn>123</mn>" << new AbstractLexer::TOKEN(ExpressionTable::tVal, 0, QStringLiteral("<cn>123</cn>"));
0046     QTest::newRow("1 variable") << "<mi>x</mi>" << new AbstractLexer::TOKEN(ExpressionTable::tId, 0, QStringLiteral("x"));
0047 //     QTest::newRow("1 variable pi") << "<mi>&pi;</mi>" << new AbstractLexer::TOKEN(ExpressionTable::tVal, 0, "<ci>&pi;</ci>");
0048     
0049     QTest::newRow("1 operator") << "<mo>+</mo>" << new AbstractLexer::TOKEN(ExpressionTable::tAdd, 0, QString());
0050 }
0051 
0052 void MathMLPresentationTest::testSimple()
0053 {
0054     QFETCH(QString, input);
0055     QFETCH(AbstractLexer::TOKEN*, output);
0056     
0057     MathMLPresentationLexer lex(input);
0058     
0059     int t=lex.lex();
0060     
0061     if(!lex.error().isEmpty())
0062         qDebug() << "error!" << lex.error();
0063     QVERIFY(lex.error().isEmpty());
0064     QCOMPARE(lex.current.type, output->type);
0065     QCOMPARE(t, output->type);
0066     QCOMPARE(lex.current.val, output->val);
0067     
0068     delete output;
0069 }
0070 
0071 void MathMLPresentationTest::testConversion_data()
0072 {
0073     QTest::addColumn<QString>("mml_pr");
0074     QTest::addColumn<QString>("expression");
0075     
0076     QTest::newRow("square per square") <<
0077     "<math><mrow>"
0078         "<msup>"
0079             "<mi>x</mi>"
0080             "<mn>2</mn>"
0081         "</msup>"
0082         "<mo>*</mo>"
0083         "<msup>"
0084             "<mi>y</mi>"
0085             "<mn>2</mn>"
0086         "</msup>"
0087     "</mrow></math>" << "x^2*y^2";
0088     
0089     QTest::newRow("division") <<
0090     "<math>"
0091     "<mrow>"
0092         "<mfrac>"
0093             "<mrow>"
0094                 "<mi>x</mi>"
0095                 "<mo>+</mo>"
0096                 "<msup>"
0097                     "<mi>y</mi>"
0098                     "<mn>2</mn>"
0099                 "</msup>"
0100             "</mrow>"
0101             "<mrow>"
0102                 "<mi>k</mi>"
0103                 "<mo>+</mo>"
0104                 "<mn>1</mn>"
0105             "</mrow>"
0106         "</mfrac>"
0107     "</mrow>"
0108     "</math>" << "(x+y^2)/(k+1)";
0109     
0110     QTest::newRow("powers") <<
0111     "<math>"
0112         "<mrow>"
0113             "<msup>"
0114                 "<mn>2</mn>"
0115                 "<msup>"
0116                     "<mn>2</mn>"
0117                     "<msup>"
0118                         "<mn>2</mn>"
0119                         "<mi>x</mi>"
0120                     "</msup>"
0121                 "</msup>"
0122             "</msup>"
0123         "</mrow>"
0124     "</math>" << "2^(2^(2^x))";
0125     
0126     QTest::newRow("x+y^smth") <<
0127     "<math>"
0128         "<mrow>"
0129             "<mi>x</mi>"
0130             "<mo>+</mo>"
0131             "<msup>"
0132                 "<mi>y</mi>"
0133                 "<mfrac>"
0134                     "<mn>2</mn>"
0135                     "<mrow>"
0136                         "<mi>k</mi>"
0137                         "<mo>+</mo>"
0138                         "<mn>1</mn>"
0139                     "</mrow>"
0140                 "</mfrac>"
0141             "</msup>"
0142         "</mrow>"
0143     "</math>" << "x+y^(2/(k+1))";
0144     
0145     QTest::newRow("a/(b/2)") <<
0146     "<math>"
0147         "<mrow>"
0148             "<mfrac>"
0149                 "<mi>a</mi>"
0150                 "<mrow>"
0151                     "<mi>b</mi>"
0152                     "<mo>/</mo>"
0153                     "<mn>2</mn>"
0154                 "</mrow>"
0155             "</mfrac>"
0156         "</mrow>"
0157     "</math>" << "a/(b/2)";
0158     
0159     QTest::newRow("13th-test") <<
0160     "<math>"
0161     "<mrow>"
0162         "<msqrt>"
0163             "<mn>1</mn>"
0164             "<mo>+</mo>"
0165             "<msqrt>"
0166                 "<mn>1</mn>"
0167                 "<mo>+</mo>"
0168                 "<msqrt>"
0169                     "<mn>1</mn>"
0170                     "<mo>+</mo>"
0171                     "<msqrt>"
0172                         "<mn>1</mn>"
0173                         "<mo>+</mo>"
0174                         "<msqrt>"
0175                             "<mn>1</mn>"
0176                             "<mo>+</mo>"
0177                             "<msqrt>"
0178                                 "<mn>1</mn>"
0179                                 "<mo>+</mo>"
0180                                 "<msqrt>"
0181                                     "<mn>1</mn>"
0182                                     "<mo>+</mo>"
0183                                     "<mi>x</mi>"
0184                                 "</msqrt>"
0185                             "</msqrt>"
0186                         "</msqrt>"
0187                     "</msqrt>"
0188                 "</msqrt>"
0189             "</msqrt>"
0190         "</msqrt>"
0191     "</mrow>"
0192     "</math>" << "root(1+root(1+root(1+root(1+root(1+root(1+root(1+x, 2), 2), 2), 2), 2), 2), 2)";
0193 }
0194 
0195 void MathMLPresentationTest::testConversion()
0196 {
0197     QFETCH(QString, mml_pr);
0198     QFETCH(QString, expression);
0199     
0200     MathMLPresentationLexer lex(mml_pr);
0201     ExpressionParser parser;
0202     bool corr=parser.parse(&lex);
0203     QString mathML=parser.mathML();
0204     
0205     if(!corr)
0206         qDebug() << "error!" << parser.error();
0207     QVERIFY(corr);
0208     QVERIFY(!mathML.isEmpty());
0209     
0210     Expression e(mathML, true);
0211     QVERIFY(e.isCorrect());
0212 //     qDebug() << "fuuu" << mathML;
0213     QCOMPARE(expression, e.toString());
0214 }
0215 
0216 void MathMLPresentationTest::testToPresentation_data()
0217 {
0218     QTest::addColumn<QString>("mml_pr");
0219     QTest::addColumn<QString>("expression");
0220     
0221     QTest::newRow("square per square") <<
0222     "<math><mrow>"
0223         "<msup>"
0224             "<mi>x</mi>"
0225             "<mn>2</mn>"
0226         "</msup>"
0227         "<mo>*</mo>"
0228         "<msup>"
0229             "<mi>y</mi>"
0230             "<mn>2</mn>"
0231         "</msup>"
0232     "</mrow></math>" << "x^2*y^2";
0233     
0234     QTest::newRow("division") <<
0235     "<math>"
0236     "<mrow>"
0237         "<mfrac>"
0238             "<mrow>"
0239                 "<mi>x</mi>"
0240                 "<mo>+</mo>"
0241                 "<msup>"
0242                     "<mi>y</mi>"
0243                     "<mn>2</mn>"
0244                 "</msup>"
0245             "</mrow>"
0246             "<mrow>"
0247                 "<mi>k</mi>"
0248                 "<mo>+</mo>"
0249                 "<mn>1</mn>"
0250             "</mrow>"
0251         "</mfrac>"
0252     "</mrow>"
0253     "</math>" << "(x+y^2)/(k+1)";
0254     
0255     QTest::newRow("powers") <<
0256     "<math>"
0257         "<mrow>"
0258             "<msup>"
0259                 "<mn>2</mn>"
0260                 "<msup>"
0261                     "<mn>2</mn>"
0262                     "<msup>"
0263                         "<mn>2</mn>"
0264                         "<mi>x</mi>"
0265                     "</msup>"
0266                 "</msup>"
0267             "</msup>"
0268         "</mrow>"
0269     "</math>" << "2^(2^(2^x))";
0270     
0271     QTest::newRow("x+y^smth") <<
0272     "<math>"
0273         "<mrow>"
0274             "<mi>x</mi>"
0275             "<mo>+</mo>"
0276             "<msup>"
0277                 "<mi>y</mi>"
0278                 "<mfrac>"
0279                     "<mrow><mn>2</mn></mrow>"
0280                     "<mrow>"
0281                         "<mi>k</mi>"
0282                         "<mo>+</mo>"
0283                         "<mn>1</mn>"
0284                     "</mrow>"
0285                 "</mfrac>"
0286             "</msup>"
0287         "</mrow>"
0288     "</math>" << "x+y^(2/(k+1))";
0289     
0290     QTest::newRow("13th-test") <<
0291     "<math>"
0292     "<mrow>"
0293         "<msqrt>"
0294             "<mn>1</mn>"
0295             "<mo>+</mo>"
0296             "<msqrt>"
0297                 "<mn>1</mn>"
0298                 "<mo>+</mo>"
0299                 "<msqrt>"
0300                     "<mn>1</mn>"
0301                     "<mo>+</mo>"
0302                     "<msqrt>"
0303                         "<mn>1</mn>"
0304                         "<mo>+</mo>"
0305                         "<msqrt>"
0306                             "<mn>1</mn>"
0307                             "<mo>+</mo>"
0308                             "<msqrt>"
0309                                 "<mn>1</mn>"
0310                                 "<mo>+</mo>"
0311                                 "<msqrt>"
0312                                     "<mn>1</mn>"
0313                                     "<mo>+</mo>"
0314                                     "<mi>x</mi>"
0315                                 "</msqrt>"
0316                             "</msqrt>"
0317                         "</msqrt>"
0318                     "</msqrt>"
0319                 "</msqrt>"
0320             "</msqrt>"
0321         "</msqrt>"
0322     "</mrow>"
0323     "</math>" << "root(1+root(1+root(1+root(1+root(1+root(1+root(1+x, 2), 2), 2), 2), 2), 2), 2)";
0324     
0325     QTest::newRow("normal function") <<
0326     "<math><mrow><mi>sin</mi><mo> &ApplyFunction; </mo><mfenced><mi>x</mi></mfenced></mrow></math>" << "sin(x)";
0327     
0328     QTest::newRow("piecewise") <<
0329     "<math><mrow><mrow><mo stretchy='true'> { </mo><mtable columnalign='left left'><mtr><mtd><mi>x</mi></mtd><mtd><mtext>if </mtext><mi>x</mi><mo>=</mo><mn>3</mn></mtd></mtr><mtr><mtd><mn>33</mn></mtd><mtd><mtext>otherwise</mtext></mtd></mtr></mtable></mrow></mrow></math>"
0330         << "piecewise { x=3 ? x, ? 33 }";
0331 }
0332 
0333 void MathMLPresentationTest::testToPresentation()
0334 {
0335     if(QLatin1String(QTest::currentDataTag())==QLatin1String("piecewise") || QLatin1String(QTest::currentDataTag())==QLatin1String("normal function"))
0336         QSKIP("need to find time to fix piecewise and normal function");
0337     QFETCH(QString, mml_pr);
0338     QFETCH(QString, expression);
0339     
0340     Expression e(expression, false);
0341     if(!e.isCorrect())
0342         qDebug() << "error:" << e.error() << expression;
0343     QString mmlcnt=e.toMathML();
0344     
0345     QVERIFY(e.isCorrect());
0346     QCOMPARE(e.toString(), expression);
0347     QCOMPARE(e.toMathMLPresentation(), mml_pr);
0348     
0349     MathMLPresentationLexer lex(mml_pr);
0350     ExpressionParser parser;
0351     bool corr=parser.parse(&lex);
0352     if(!corr)
0353         qDebug() << parser.error();
0354     QVERIFY(corr);
0355     QCOMPARE(mmlcnt, parser.mathML());
0356 }
0357 
0358 
0359 
0360 #include "moc_mathmlpresentationtest.cpp"