File indexing completed on 2024-05-05 11:56:00

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004     SPDX-FileCopyrightText: 2023 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #include "testsage.h"
0008 
0009 #include "session.h"
0010 #include "backend.h"
0011 #include "expression.h"
0012 #include "result.h"
0013 #include "imageresult.h"
0014 #include "textresult.h"
0015 
0016 #include <QDebug>
0017 
0018 QString TestSage::backendName()
0019 {
0020     return QLatin1String("sage");
0021 }
0022 
0023 void TestSage::initTestCase() {
0024     if (QStandardPaths::findExecutable(QLatin1String("sage")).isEmpty())
0025         QSKIP("Sage executable not found");
0026     BackendTest::initTestCase();
0027 }
0028 
0029 void TestSage::testSimpleCommand()
0030 {
0031     auto* e = evalExp( QLatin1String("2+2") );
0032 
0033     QVERIFY(e != nullptr);
0034     QVERIFY(e->result() != nullptr);
0035     QCOMPARE(e->result()->data().toString(), QLatin1String(("4")));
0036 }
0037 
0038 void TestSage::testCommandQueue()
0039 {
0040     //only wait for the last Expression to return, so the queue gets
0041     //actually filled
0042 
0043     auto* e1 = evalExp(QLatin1String("0+1"));
0044     auto* e2 = evalExp(QLatin1String("1+1"));
0045     auto* e3 = evalExp(QLatin1String("1+2"));
0046 
0047     QVERIFY(e1!=nullptr);
0048     QVERIFY(e2!=nullptr);
0049     QVERIFY(e3!=nullptr);
0050 
0051     QVERIFY(e1->result());
0052     QVERIFY(e2->result());
0053     QVERIFY(e3->result());
0054 
0055     QCOMPARE(cleanOutput(e1->result()->data().toString()), QLatin1String("1"));
0056     QCOMPARE(cleanOutput(e2->result()->data().toString()), QLatin1String("2"));
0057     QCOMPARE(cleanOutput(e3->result()->data().toString()), QLatin1String("3"));
0058 }
0059 
0060 void TestSage::testMultilineCommand()
0061 {
0062     auto* e = evalExp( QLatin1String("2+2 \n simplify(1 - x + x)") );
0063 
0064     QVERIFY(e != nullptr);
0065     QVERIFY(e->result() != nullptr);
0066     QCOMPARE(e->result()->data().toString(), QLatin1String("4\n1") );
0067 }
0068 
0069 void TestSage::testDefineFunction()
0070 {
0071     const char* cmd="def func1(param) : \n" \
0072                     "    return param*param\n\n";
0073 
0074     auto* e1 = evalExp(QLatin1String(cmd));
0075     QVERIFY(e1 != nullptr);
0076 
0077     auto* e2 = evalExp( QLatin1String("func1(2)"));
0078     QVERIFY(e2 != nullptr);
0079     QVERIFY(e2->result() != nullptr );
0080     QCOMPARE(e2->result()->data().toString(), QLatin1String("4"));
0081 }
0082 
0083 void TestSage::testPlot()
0084 {
0085     auto* e = evalExp( QLatin1String("plot(sin(x))") );
0086 
0087     QVERIFY(e != nullptr);
0088     QCOMPARE(e->results().size(), 1);
0089 
0090     QVERIFY(e->results().at(0)->type() == Cantor::ImageResult::Type);
0091     QVERIFY(!e->results().at(0)->data().isNull());
0092 }
0093 
0094 void TestSage::testInvalidSyntax()
0095 {
0096     auto* e = evalExp( QLatin1String("2+2*(") );
0097 
0098     QVERIFY(e != nullptr);
0099     QVERIFY(e->errorMessage()== QLatin1String("Syntax Error"));
0100 }
0101 
0102 void TestSage::testNoOutput()
0103 {
0104     auto* e = evalExp(  QLatin1String("f(x)=x^2+3*x+2\nf(0)"));
0105 
0106     QVERIFY(e != nullptr);
0107 
0108    if(session()->status()==Cantor::Session::Running)
0109         waitForSignal(session(), SIGNAL(statusChanged(Cantor::Session::Status)));
0110 
0111     QVERIFY(e->result() != nullptr);
0112     QCOMPARE(e->result()->data().toString(), QLatin1String("2"));
0113 }
0114 
0115 void TestSage::testLoginLogout()
0116 {
0117     // Logout from session twice and all must works fine
0118     session()->logout();
0119     session()->logout();
0120 
0121     // Login in session twice and all must works fine
0122     session()->login();
0123     session()->login();
0124 }
0125 
0126 void TestSage::testRestartWhileRunning()
0127 {
0128     auto* e1 = evalExp(QLatin1String("import time; time.sleep(5)"));
0129 
0130     session()->logout();
0131     QCOMPARE(e1->status(), Cantor::Expression::Interrupted);
0132     session()->login();
0133 
0134     auto* e2 = evalExp(QLatin1String("2+2"));
0135 
0136     QVERIFY(e2 != nullptr);
0137     QVERIFY(e2->result() != nullptr);
0138     QCOMPARE(e2->result()->data().toString(), QLatin1String("4"));
0139 }
0140 
0141 QTEST_MAIN( TestSage )