/************************************************************************************* * Copyright (C) 2008 by Aleix Pol * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #include "mathmlpresentationtest.h" #include "mathmlpresentationlexer.h" #include "expressionparser.h" #include "expression.h" #include using Analitza::Expression; QTEST_KDEMAIN_CORE(MathMLPresentationTest) Q_DECLARE_METATYPE(AbstractLexer::TOKEN*) MathMLPresentationTest::MathMLPresentationTest(QObject *parent) : QObject(parent) {} MathMLPresentationTest::~MathMLPresentationTest() {} void MathMLPresentationTest::initTestCase() {} void MathMLPresentationTest::cleanupTestCase() {} void MathMLPresentationTest::testSimple_data() { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow("1 value") << "123" << new AbstractLexer::TOKEN(ExpressionTable::tVal, 0, "123"); QTest::newRow("1 variable") << "x" << new AbstractLexer::TOKEN(ExpressionTable::tId, 0, "x"); // QTest::newRow("1 variable pi") << "π" << new AbstractLexer::TOKEN(ExpressionTable::tVal, 0, "π"); QTest::newRow("1 operator") << "+" << new AbstractLexer::TOKEN(ExpressionTable::tAdd, 0, QString()); } void MathMLPresentationTest::testSimple() { QFETCH(QString, input); QFETCH(AbstractLexer::TOKEN*, output); MathMLPresentationLexer lex(input); int t=lex.lex(); if(!lex.error().isEmpty()) qDebug() << "error!" << lex.error(); QVERIFY(lex.error().isEmpty()); QCOMPARE(lex.current.type, output->type); QCOMPARE(t, output->type); QCOMPARE(lex.current.val, output->val); delete output; } void MathMLPresentationTest::testConversion_data() { QTest::addColumn("mml_pr"); QTest::addColumn("expression"); QTest::newRow("square per square") << "" "" "x" "2" "" "*" "" "y" "2" "" "" << "x^2*y^2"; QTest::newRow("division") << "" "" "" "" "x" "+" "" "y" "2" "" "" "" "k" "+" "1" "" "" "" "" << "(x+y^2)/(k+1)"; QTest::newRow("powers") << "" "" "" "2" "" "2" "" "2" "x" "" "" "" "" "" << "2^(2^(2^x))"; QTest::newRow("x+y^smth") << "" "" "x" "+" "" "y" "" "2" "" "k" "+" "1" "" "" "" "" "" << "x+y^(2/(k+1))"; QTest::newRow("a/(b/2)") << "" "" "" "a" "" "b" "/" "2" "" "" "" "" << "a/(b/2)"; QTest::newRow("13th-test") << "" "" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "x" "" "" "" "" "" "" "" "" "" << "root((1+root((1+root((1+root((1+root((1+root((1+root((1+x))))))))))))))"; } void MathMLPresentationTest::testConversion() { QFETCH(QString, mml_pr); QFETCH(QString, expression); MathMLPresentationLexer lex(mml_pr); ExpressionParser parser; bool corr=parser.parse(&lex); QString mathML=parser.mathML(); if(!corr) qDebug() << "error!" << parser.error(); QVERIFY(corr); QVERIFY(!mathML.isEmpty()); Expression e(mathML, true); QVERIFY(e.isCorrect()); } void MathMLPresentationTest::testToPresentation_data() { QTest::addColumn("mml_pr"); QTest::addColumn("expression"); QTest::newRow("square per square") << "" "" "x" "2" "" "*" "" "y" "2" "" "" << "x^2*y^2"; QTest::newRow("division") << "" "" "" "" "x" "+" "" "y" "2" "" "" "" "k" "+" "1" "" "" "" "" << "(x+y^2)/(k+1)"; QTest::newRow("powers") << "" "" "" "2" "" "2" "" "2" "x" "" "" "" "" "" << "2^(2^(2^x))"; QTest::newRow("x+y^smth") << "" "" "x" "+" "" "y" "" "2" "" "k" "+" "1" "" "" "" "" "" << "x+y^(2/(k+1))"; QTest::newRow("13th-test") << "" "" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "" "1" "+" "x" "" "" "" "" "" "" "" "" "" << "root((1+root((1+root((1+root((1+root((1+root((1+root((1+x))))))))))))))"; QTest::newRow("normal function") << "" "" "sin" "(" "x" ")" "" "" << "sin(x)"; QTest::newRow("piecewise") << " { eq(x, 3)if x33otherwise" << "piecewise { eq(x, 3) ? x, ? 33 }"; } void MathMLPresentationTest::testToPresentation() { QFETCH(QString, mml_pr); QFETCH(QString, expression); Expression e(expression, false); QString mmlcnt=e.toMathML(); if(!e.isCorrect()) qDebug() << "error:" << e.error(); QVERIFY(e.isCorrect()); QCOMPARE(e.toString(), expression); QCOMPARE(e.toMathMLPresentation(), mml_pr); MathMLPresentationLexer lex(mml_pr); ExpressionParser parser; bool corr=parser.parse(&lex); if(!corr) qDebug() << parser.error(); QVERIFY(corr); QCOMPARE(mmlcnt, parser.mathML()); } #include "mathmlpresentationtest.moc"