File indexing completed on 2024-04-21 11:14:04

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2020 Shubham <aryan100jangid@gmail.com>
0004 */
0005 
0006 #include "backend.h"
0007 #include "expression.h"
0008 #include "imageresult.h"
0009 #include "result.h"
0010 #include "session.h"
0011 #include "testscilab.h"
0012 
0013 #include <QtTest>
0014 
0015 QString TestScilab::backendName()
0016 {
0017     return QLatin1String("scilab");
0018 }
0019 
0020 void TestScilab::initTestCase() {
0021     if (QStandardPaths::findExecutable(QLatin1String("scilab")).isEmpty())
0022         QSKIP("Scilab executable not found");
0023     BackendTest::initTestCase();
0024 }
0025 
0026 void TestScilab::testSimpleCommand()
0027 {
0028     Cantor::Expression* e = evalExp(QLatin1String("printf(\"Testing\")\n"));
0029 
0030     QVERIFY(e != nullptr);
0031     QVERIFY(e->result() != nullptr);
0032 
0033     QCOMPARE(cleanOutput(e->result()->data().toString()), QLatin1String("Testing"));
0034 }
0035 
0036 void TestScilab::testVariableDefinition()
0037 {
0038     Cantor::Expression* e = evalExp(QLatin1String("test = 100 disp(test)"));
0039 
0040     QVERIFY(e != nullptr);
0041     QVERIFY(e->result() != nullptr);
0042 
0043     QCOMPARE(cleanOutput(e->result()->data().toString()), QLatin1String("100"));
0044 }
0045 
0046 void TestScilab::testInvalidSyntax()
0047 {
0048     Cantor::Expression* e = evalExp(QLatin1String("^invalidvar = 100")); // invalid variable name, since it starts with ^
0049 
0050     QVERIFY(e != nullptr);
0051     QCOMPARE(e->status(), Cantor::Expression::Error);
0052 }
0053 
0054 void TestScilab::testLoginLogout()
0055 {
0056     // Logout from session twice and all must works fine
0057     session()->logout();
0058     session()->logout();
0059 
0060     // Login in session twice and all must works fine
0061     session()->login();
0062     session()->login();
0063 }
0064 
0065 void TestScilab::testPlot()
0066 {
0067     Cantor::Expression* e = evalExp(QLatin1String("x = [0:0.1:2*%pi] plot(x, sin(x))"));
0068 
0069     int cnt = 0;
0070     //give some time to create the image
0071     while(e->result() == nullptr || e->result()->type() != (int)Cantor::ImageResult::Type)
0072     {
0073         QTest::qWait(250);
0074         cnt += 250;
0075         if(cnt > 5000)
0076             break;
0077     }
0078 
0079     QVERIFY(e != nullptr);
0080     QVERIFY(e->result() != nullptr);
0081 
0082     QCOMPARE(e->result()->type(), (int)Cantor::ImageResult::Type);
0083 
0084     QVERIFY(!e->result()->data().isNull());
0085     QVERIFY(e->errorMessage().isNull());
0086 }
0087 
0088 QTEST_MAIN(TestScilab)
0089