File indexing completed on 2024-05-12 03:41:59
0001 /************************************************************************************* 0002 * Copyright (C) 2014 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> * 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 "commandstest.h" 0020 0021 #include <QTest> 0022 0023 #include "analyzer.h" 0024 #include <config-analitza.h> 0025 0026 using Analitza::Expression; 0027 0028 QTEST_MAIN( CommandsTest ) 0029 0030 CommandsTest::CommandsTest(QObject *parent) 0031 : QObject(parent) 0032 {} 0033 0034 CommandsTest::~CommandsTest() 0035 {} 0036 0037 void CommandsTest::initTestCase() 0038 { 0039 a=new Analitza::Analyzer; 0040 } 0041 0042 void CommandsTest::cleanupTestCase() 0043 { 0044 delete a; 0045 } 0046 0047 void CommandsTest::testCorrect_data() 0048 { 0049 QTest::addColumn<QStringList>("expression"); 0050 QTest::addColumn<QString>("result"); 0051 0052 QStringList script; 0053 0054 script.clear(); 0055 script << QStringLiteral("range(10)"); 0056 QTest::newRow("simple range") << script << "list { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }"; 0057 0058 script.clear(); 0059 script << QStringLiteral("range(-1.5, 2)"); 0060 QTest::newRow("range(-1.5, 2)") << script << "list { -1.5, -0.5, 0.5, 1.5 }"; 0061 0062 script.clear(); 0063 script << QStringLiteral("range(0, 1, 0.2)"); 0064 QTest::newRow("range(0, 1, 0.2)") << script << "list { 0, 0.2, 0.4, 0.6, 0.8, 1 }"; 0065 0066 script.clear(); 0067 script << QStringLiteral("vector(3, -2.3)"); 0068 QTest::newRow("simple fill vector") << script << "vector { -2.3, -2.3, -2.3 }"; 0069 0070 script.clear(); 0071 script << QStringLiteral("7*vector(34, 14.2)[23]"); 0072 QTest::newRow("select fill vector") << script << "99.4"; 0073 0074 script.clear(); 0075 script << QStringLiteral("seq := range(14, 20, 2)"); 0076 script << QStringLiteral("vector(seq)"); 0077 QTest::newRow("vector by range/seq") << script << "vector { 14, 16, 18, 20 }"; 0078 0079 script.clear(); 0080 script << QStringLiteral("matrix(3, 2, -15.7)"); 0081 QTest::newRow("simple fill matrix") << script << "matrix { matrixrow { -15.7, -15.7 }, matrixrow { -15.7, -15.7 }, matrixrow { -15.7, -15.7 } }"; 0082 0083 script.clear(); 0084 script << QStringLiteral("matrix(2)"); 0085 QTest::newRow("simple fill square matrix") << script << "matrix { matrixrow { 0, 0 }, matrixrow { 0, 0 } }"; 0086 0087 script.clear(); 0088 script << QStringLiteral("matrix(3, 2)"); 0089 QTest::newRow("simple matrix") << script << "matrix { matrixrow { 0, 0 }, matrixrow { 0, 0 }, matrixrow { 0, 0 } }"; 0090 0091 script.clear(); 0092 script << QStringLiteral("matrix(3, 2, -9.8)[2][1]"); 0093 QTest::newRow("select simple matrix") << script << "-9.8"; 0094 0095 script.clear(); 0096 script << QStringLiteral("matrix(vector{2,3}, vector{7, 4})"); 0097 QTest::newRow("matrix by vectors/columns") << script << "matrix { matrixrow { 2, 7 }, matrixrow { 3, 4 } }"; 0098 0099 script.clear(); 0100 script << QStringLiteral("matrix(matrixrow{2,3}, matrixrow{7, 4})"); 0101 QTest::newRow("matrix by rows") << script << "matrix { matrixrow { 2, 3 }, matrixrow { 7, 4 } }"; 0102 0103 script.clear(); 0104 script << QStringLiteral("A := matrix{matrixrow{13,6}, matrixrow{-2,5}}"); 0105 script << QStringLiteral("matrix(vector{2,3}, vector{7, 4}) + A"); 0106 QTest::newRow("matrix by vectors/columns + A") << script << "matrix { matrixrow { 15, 13 }, matrixrow { 1, 9 } }"; 0107 0108 script.clear(); 0109 script << QStringLiteral("A := matrix{matrixrow{2, 3, 6}, matrixrow{-5, 0, 2.3}}"); 0110 script << QStringLiteral("matrix(2, 3, -9) + A"); 0111 QTest::newRow("fill + A") << script << "matrix { matrixrow { -7, -6, -3 }, matrixrow { -14, -9, -6.7 } }"; 0112 0113 script.clear(); 0114 script << QStringLiteral("A := matrix{matrixrow{2, 3}, matrixrow{-5, 1}}"); 0115 script << QStringLiteral("B := matrix{matrixrow{12, 13}, matrixrow{-15, 11}}"); 0116 script << QStringLiteral("blockmatrix(matrixrow{A, B})"); 0117 QTest::newRow("simple block matrix") << script << "matrix { matrixrow { 2, 3, 12, 13 }, matrixrow { -5, 1, -15, 11 } }"; 0118 0119 const QString resultblockmatrix = QStringLiteral("matrix { matrixrow { 1, 8, 7, 6 }, matrixrow { 3, 5, 0, 2 }, matrixrow { 1, 4, 9, 3 } }"); 0120 0121 script.clear(); 0122 script << QStringLiteral("A := matrix(vector{1,3}, vector{8,5})"); 0123 script << QStringLiteral("B := matrix{matrixrow{7,6}, matrixrow{0,2}}"); 0124 script << QStringLiteral("C := matrix(matrixrow{1,4})"); 0125 script << QStringLiteral("D := matrix(vector{9}, vector{3})"); 0126 script << QStringLiteral("blockmatrix(matrixrow{A, B}, matrixrow{C, D})"); 0127 QTest::newRow("block matrix 4 blocks conf 1") << script << resultblockmatrix; 0128 0129 script.clear(); 0130 script << QStringLiteral("A := matrix{matrixrow{1,8,7}, matrixrow{3,5,0}}"); 0131 script << QStringLiteral("B := matrix(vector{6,2})"); 0132 script << QStringLiteral("C := matrix(matrixrow{1,4,9})"); 0133 script << QStringLiteral("D := matrix(1,1,3)"); 0134 script << QStringLiteral("blockmatrix(matrixrow{A, B}, matrixrow{C, D})"); 0135 QTest::newRow("block matrix by rows, conf 2") << script << resultblockmatrix; 0136 0137 script.clear(); 0138 script << QStringLiteral("A := matrix(matrixrow{1,8})"); 0139 script << QStringLiteral("B := transpose(matrix(diag(diag(7,6))))"); 0140 script << QStringLiteral("C := matrix{matrixrow{3,5}}"); 0141 script << QStringLiteral("D := matrix(matrixrow{0,2})"); 0142 script << QStringLiteral("E := matrix(vector{1},vector{4})"); 0143 script << QStringLiteral("F := matrix{matrixrow{9,3}}"); 0144 script << QStringLiteral("blockmatrix(matrixrow{A, B}, matrixrow{C, D}, matrixrow{E, F})"); 0145 QTest::newRow("block matrix by rows, conf 3") << script << resultblockmatrix; 0146 0147 script.clear(); 0148 script << QStringLiteral("A := matrix(vector{1,3}, vector{8,5})"); 0149 script << QStringLiteral("B := matrix(matrixrow{1,4})"); 0150 script << QStringLiteral("C := matrix{matrixrow{7,6}, matrixrow{0,2}}"); 0151 script << QStringLiteral("D := matrix(vector{9}, vector{3})"); 0152 script << QStringLiteral("blockmatrix(vector{A, B}, vector{C, D})"); 0153 QTest::newRow("block matrix by cols, conf 1") << script << resultblockmatrix; 0154 0155 script.clear(); 0156 script << QStringLiteral("A := matrix{matrixrow{1,8}}"); 0157 script << QStringLiteral("B := matrix(matrixrow{3,5})"); 0158 script << QStringLiteral("C := matrix(matrixrow{1,4})"); 0159 script << QStringLiteral("D := matrix{matrixrow{7,6}}"); 0160 script << QStringLiteral("E := matrix(matrixrow{0,2})"); 0161 script << QStringLiteral("F := matrix(matrixrow{9,3})"); 0162 script << QStringLiteral("blockmatrix(vector{A, B, C}, vector{D, E, F})"); 0163 QTest::newRow("block matrix by cols, conf 2") << script << resultblockmatrix; 0164 0165 script.clear(); 0166 script << QStringLiteral("matrix(2,5)"); 0167 QTest::newRow("simple 0") << script << "matrix { matrixrow { 0, 0, 0, 0, 0 }, matrixrow { 0, 0, 0, 0, 0 } }"; 0168 0169 script.clear(); 0170 script << QStringLiteral("identitymatrix(3)"); 0171 QTest::newRow("simple I") << script << "matrix { matrixrow { 1, 0, 0 }, matrixrow { 0, 1, 0 }, matrixrow { 0, 0, 1 } }"; 0172 0173 script.clear(); 0174 script << QStringLiteral("identitymatrix(3)-identitymatrix(3)"); 0175 QTest::newRow("I - I") << script << "matrix { matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 } }"; 0176 0177 script.clear(); 0178 script << QStringLiteral("0*identitymatrix(3)"); 0179 QTest::newRow("0*I") << script << "matrix { matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 } }"; 0180 0181 script.clear(); 0182 script << QStringLiteral("matrix(3,3) + identitymatrix(3)"); 0183 QTest::newRow("0 + I") << script << "matrix { matrixrow { 1, 0, 0 }, matrixrow { 0, 1, 0 }, matrixrow { 0, 0, 1 } }"; 0184 0185 script.clear(); 0186 script << QStringLiteral("diag(5, 3.2, 6, -9)"); 0187 QTest::newRow("simple diag") << script << "matrix { matrixrow { 5, 0, 0, 0 }, matrixrow { 0, 3.2, 0, 0 }, matrixrow { 0, 0, 6, 0 }, matrixrow { 0, 0, 0, -9 } }"; 0188 0189 script.clear(); 0190 script << QStringLiteral("diag(vector{5, 3.2, 6, -9})"); 0191 QTest::newRow("simple diag by vector") << script << "matrix { matrixrow { 5, 0, 0, 0 }, matrixrow { 0, 3.2, 0, 0 }, matrixrow { 0, 0, 6, 0 }, matrixrow { 0, 0, 0, -9 } }"; 0192 0193 script.clear(); 0194 script << QStringLiteral("diag(1, 1, 1) - identitymatrix(3)"); 0195 QTest::newRow("zeromatrix: diag - I") << script << "matrix { matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 } }"; 0196 0197 script.clear(); 0198 script << QStringLiteral("diag(vector{1, 1, 1}) - identitymatrix(3)"); 0199 QTest::newRow("zeromatrix: diag by vector - I") << script << "matrix { matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 }, matrixrow { 0, 0, 0 } }"; 0200 0201 script.clear(); 0202 script << QStringLiteral("diag(5, 7.8, -0.6, 3.5)[3][3] + diag(5, 7.8, -0.6, 3.5)[2][3]"); 0203 QTest::newRow("selector diag") << script << "-0.6"; 0204 0205 script.clear(); 0206 script << QStringLiteral("v := vector{5, 7.8, -0.6, 3.5}"); 0207 script << QStringLiteral("diag(v)[3][3] + diag(v)[2][3]"); 0208 QTest::newRow("selector diag by vector") << script << "-0.6"; 0209 0210 script.clear(); 0211 script << QStringLiteral("blockdiag(matrix{matrixrow{1, 3}, matrixrow{-6, 8}}, matrix{matrixrow{5, 6}, matrixrow{14, -1.2}})"); 0212 QTest::newRow("simple block diagonal") << script << "matrix { matrixrow { 1, 3, 0, 0 }, matrixrow { -6, 8, 0, 0 }, matrixrow { 0, 0, 5, 6 }, matrixrow { 0, 0, 14, -1.2 } }"; 0213 0214 script.clear(); 0215 script << QStringLiteral("I := identitymatrix(3)"); 0216 script << QStringLiteral("A := matrix(2,3, -6)"); 0217 script << QStringLiteral("B := 3*I"); 0218 script << QStringLiteral("blockdiag(I, A, B)"); 0219 QTest::newRow("block diagonal") << script << "matrix { matrixrow { 1, 0, 0, 0, 0, 0, 0, 0, 0 }, matrixrow { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, matrixrow { 0, 0, 1, 0, 0, 0, 0, 0, 0 }, matrixrow { 0, 0, 0, -6, -6, -6, 0, 0, 0 }, matrixrow { 0, 0, 0, -6, -6, -6, 0, 0, 0 }, matrixrow { 0, 0, 0, 0, 0, 0, 3, 0, 0 }, matrixrow { 0, 0, 0, 0, 0, 0, 0, 3, 0 }, matrixrow { 0, 0, 0, 0, 0, 0, 0, 0, 3 } }"; 0220 0221 script.clear(); 0222 script << QStringLiteral("tridiag(-2.1, 3.6, 48, 5)"); 0223 QTest::newRow("simple tridiag") << script << "matrix { matrixrow { 3.6, 48, 0, 0, 0 }, matrixrow { -2.1, 3.6, 48, 0, 0 }, matrixrow { 0, -2.1, 3.6, 48, 0 }, matrixrow { 0, 0, -2.1, 3.6, 48 }, matrixrow { 0, 0, 0, -2.1, 3.6 } }"; 0224 0225 script.clear(); 0226 script << QStringLiteral("A := matrix{matrixrow{-7.8, 2.3, 5}, matrixrow{0, -1.2, cos(pi)}, matrixrow{-45, 9.6, -2.3}}"); 0227 script << QStringLiteral("tridiag(-8, 10, 7.2, 3) - identitymatrix(3) + A"); 0228 QTest::newRow("tridiag - I + A") << script << "matrix { matrixrow { 1.2, 9.5, 5 }, matrixrow { -8, 7.8, 6.2 }, matrixrow { -45, 1.6, 6.7 } }"; 0229 0230 script.clear(); 0231 script << QStringLiteral("A := matrix{matrixrow{-7.8, 2.3, 5}, matrixrow{0, -12, 1}, matrixrow{-45, 9.6, cos(pi)}}"); 0232 script << QStringLiteral("diag(A)"); 0233 QTest::newRow("simple diag(A)") << script << "vector { -7.8, -12, -1 }"; 0234 0235 script.clear(); 0236 script << QStringLiteral("A := matrix{matrixrow{8, 2.3, 5}, matrixrow{-45, -cos(pi), 12}}"); 0237 script << QStringLiteral("diag(A)"); 0238 QTest::newRow("getdiag fat") << script << "vector { 8, 1 }"; 0239 0240 script.clear(); 0241 script << QStringLiteral("A := matrix{matrixrow{8, 2.3, 1}, matrixrow{3, 32, 2}, matrixrow{-45, 12, 3}, matrixrow{1, 0, 3}, matrixrow{-5, 1, 0}}"); 0242 script << QStringLiteral("diag(A)"); 0243 QTest::newRow("getdiag skinny") << script << "vector { 8, 32, 3 }"; 0244 0245 script.clear(); 0246 script << QStringLiteral("A := matrix{matrixrow{-7.8, 2.3, 5}, matrixrow{0, -12, 1}, matrixrow{-45, 9.6, cos(pi)}}"); 0247 script << QStringLiteral("diag(A)[2]"); 0248 QTest::newRow("selector getdiag") << script << "-12"; 0249 0250 script.clear(); 0251 script << QStringLiteral("v := vector{5,5,0}"); 0252 script << QStringLiteral("A := matrix{matrixrow{0, -1, 2}, matrixrow{4, 0, -3.2}, matrixrow{5.8, -15, 0}}"); 0253 script << QStringLiteral("B := matrix(3,3, 4.5)"); 0254 script << QStringLiteral("D := diag(v)"); 0255 script << QStringLiteral("I := identitymatrix(3)"); 0256 script << QStringLiteral("O := matrix(3,3)"); 0257 script << QStringLiteral("T := tridiag(2,1,8,3)"); 0258 script << QStringLiteral("A + B + D - cos(pi)*I + O + T"); 0259 QTest::newRow("complex exp") << script << "matrix { matrixrow { 11.5, 11.5, 6.5 }, matrixrow { 10.5, 11.5, 9.3 }, matrixrow { 10.3, -8.5, 6.5 } }"; 0260 0261 // 3.6 48 0 9 80 0262 // 2.1 3.2 49 20 100 0263 // 1 -2.1 3.6 47 90 0264 // 0 -7 -2.1 3.3 42 0265 // 5 4 3 -2.1 3.5 0266 const QString square = QStringLiteral("A := matrix{matrixrow{3.6, 48, 0, 9, 80}, matrixrow {2.1, 3.2, 49, 20, 100}, matrixrow{1, -2.1, 3.6, 47, 90}, matrixrow{0, -7, -2.1, 3.3, 42}, matrixrow{5, 4, 3, -2.1, 3.5}}"); 0267 0268 script.clear(); 0269 script << square; 0270 script << QStringLiteral("diag(A,2)"); 0271 QTest::newRow("getndiag square +2") << script << "vector { 0, 20, 90 }"; 0272 0273 script.clear(); 0274 script << square; 0275 script << QStringLiteral("diag(A,-2)"); 0276 QTest::newRow("getndiag square -2") << script << "vector { 1, -7, 3 }"; 0277 0278 script.clear(); 0279 script << square; 0280 script << QStringLiteral("diag(A,-3)"); 0281 QTest::newRow("getndiag square -3") << script << "vector { 0, 4 }"; 0282 0283 script.clear(); 0284 script << square; 0285 script << QStringLiteral("diag(A,3)"); 0286 QTest::newRow("getndiag square +3") << script << "vector { 9, 100 }"; 0287 0288 script.clear(); 0289 script << square; 0290 script << QStringLiteral("diag(A,-3)"); 0291 QTest::newRow("getndiag square -3") << script << "vector { 0, 4 }"; 0292 0293 script.clear(); 0294 script << square; 0295 script << QStringLiteral("diag(A,4)[1]"); 0296 QTest::newRow("selector getndiag square corner +") << script << "80"; 0297 0298 script.clear(); 0299 script << square; 0300 script << QStringLiteral("diag(A,-4)[1]"); 0301 QTest::newRow("selector getndiag square corner -") << script << "5"; 0302 0303 // 3.6 42 0 9 80 4 15 0304 // -4.1 7.8 45 0 100 6 7 0305 // 1 2.2 3.6 47 0 8 9 0306 // 0 0 -2.3 5.6 48 2 1 0307 // 2 4 3 -2.1 3.6 3 5 0308 const QString fat = QStringLiteral("A := matrix{matrixrow{3.6, 42, 0, 9, 80, 4, 15}, matrixrow{-4.1, 7.8, 45, 0, 100, 6,7}, matrixrow{1, 2.2, 5.6, 47, 0 , 8, 9}, matrixrow{0, 0, -2.3, 3.6, 48, 2,1}, matrixrow{2, 4, 3, -2.1, 3.6, 3,5}}"); 0309 0310 script.clear(); 0311 script << fat; 0312 script << QStringLiteral("diag(A,5)"); 0313 QTest::newRow("getndiag fat +5") << script << "vector { 4, 7 }"; 0314 0315 script.clear(); 0316 script << fat; 0317 script << QStringLiteral("diag(A,1)"); 0318 QTest::newRow("getndiag fat +1") << script << "vector { 42, 45, 47, 48, 3 }"; 0319 0320 script.clear(); 0321 script << fat; 0322 script << QStringLiteral("diag(A,-1)"); 0323 QTest::newRow("getndiag fat -1") << script << "vector { -4.1, 2.2, -2.3, -2.1 }"; 0324 0325 script.clear(); 0326 script << fat; 0327 script << QStringLiteral("diag(A,6)[1]"); 0328 QTest::newRow("selector getndiag fat corner +") << script << "15"; 0329 0330 script.clear(); 0331 script << fat; 0332 script << QStringLiteral("diag(A,-4)[1]"); 0333 QTest::newRow("selector getndiag fat corner -") << script << "2"; 0334 0335 // 3.6 42 0336 // -4.1 7.8 0337 // 1 2.2 0338 // 0 1 0339 // 2 4 0340 const QString skinny = QStringLiteral("A := matrix{matrixrow{3.6, 42}, matrixrow{-4.1, 7.8}, matrixrow{1, 2.2}, matrixrow{0, 1}, matrixrow{2, 4}}"); 0341 0342 script.clear(); 0343 script << fat; 0344 script << QStringLiteral("diag(A,1)[1]"); 0345 QTest::newRow("selector getndiag skinny corner +") << script << "42"; 0346 0347 script.clear(); 0348 script << skinny; 0349 script << QStringLiteral("diag(A,-1)"); 0350 QTest::newRow("getndiag skinny -1") << script << "vector { -4.1, 2.2 }"; 0351 0352 script.clear(); 0353 script << skinny; 0354 script << QStringLiteral("diag(A,-3)"); 0355 QTest::newRow("getndiag skinny -3") << script << "vector { 0, 4 }"; 0356 0357 script.clear(); 0358 script << skinny; 0359 script << QStringLiteral("diag(A,-4)"); 0360 QTest::newRow("getndiag skinny -4") << script << "vector { 2 }"; 0361 0362 script.clear(); 0363 script << skinny; 0364 script << QStringLiteral("isdiag(A)"); 0365 QTest::newRow("is not diag") << script << "false"; 0366 0367 script.clear(); 0368 script << QStringLiteral("iszeromatrix(matrix(8,5))"); 0369 QTest::newRow("is zero matrix") << script << "true"; 0370 0371 script.clear(); 0372 script << QStringLiteral("isidentitymatrix(identitymatrix(5))"); 0373 QTest::newRow("Id is identity matrix") << script << "true"; 0374 0375 script.clear(); 0376 script << QStringLiteral("isidentitymatrix(blockdiag(identitymatrix(3), matrix{matrixrow{1}}, identitymatrix(2)))"); 0377 QTest::newRow("block of Id is Id matrix") << script << "true"; 0378 0379 script.clear(); 0380 script << QStringLiteral("isdiag(identitymatrix(4))"); 0381 QTest::newRow("Id is diag matrix") << script << "true"; 0382 0383 #ifdef HAVE_EIGEN3 0384 script.clear(); 0385 script << QStringLiteral("eigenvalues(identitymatrix(4))"); 0386 QTest::newRow("eigenvalues: from Id") << script << "list { 1, 1, 1, 1 }"; 0387 0388 script.clear(); 0389 script << QStringLiteral("eigenvalues(matrix(vector{3,4}, vector{-2, -1}))"); 0390 QTest::newRow("eigenvalues: full complex") << script << "list { 1+2*i, 1-2*i }"; 0391 0392 script.clear(); 0393 script << QStringLiteral("eigenvectors(identitymatrix(4))"); 0394 QTest::newRow("eigenvectors: from Id") << script << "list { vector { 1, 0, 0, 0 }, vector { 0, 1, 0, 0 }, vector { 0, 0, 1, 0 }, vector { 0, 0, 0, 1 } }"; 0395 0396 script.clear(); 0397 script << QStringLiteral("eigenvectors(matrix(vector{3,4}, vector{-2, -1}))"); 0398 QTest::newRow("complex eigenvectors") << script << "list { vector { -0.408248290464+0.408248290464*i, 0.816496580928*i }, vector { -0.408248290464-0.408248290464*i, -0.816496580928*i } }"; 0399 #endif 0400 } 0401 0402 void CommandsTest::testCorrect() 0403 { 0404 QFETCH(QStringList, expression); 0405 QFETCH(QString, result); 0406 0407 Expression last; 0408 Analitza::Analyzer b1; 0409 foreach(const QString &exp, expression) { 0410 Expression e(exp, false); 0411 if(!e.isCorrect()) qDebug() << "error:" << e.error(); 0412 QVERIFY(e.isCorrect()); 0413 0414 b1.setExpression(e); 0415 0416 if(!b1.isCorrect()) qDebug() << "errors: " << b1.errors(); 0417 QVERIFY(b1.isCorrect()); 0418 last = b1.calculate(); 0419 if(!b1.isCorrect()) qDebug() << "errors:" << e.toString() << b1.errors(); 0420 QVERIFY(b1.isCorrect()); 0421 } 0422 QCOMPARE(last.toString(), result); 0423 0424 QString script = expression.join(QStringLiteral("\n")); 0425 script+=QLatin1String("\n\n\n"); 0426 QTextStream stream(&script); 0427 a->importScript(&stream); 0428 QVERIFY(a->isCorrect()); 0429 } 0430 0431 void CommandsTest::testIncorrect_data() 0432 { 0433 QTest::addColumn<QString>("expression"); 0434 0435 QTest::newRow("range: bad input") << "range(list{3,4}-9)"; 0436 QTest::newRow("range: bad arg type") << "range(list{3}, 31, true)"; 0437 QTest::newRow("range: bad arg type2") << "range(list{3}, 31)"; 0438 QTest::newRow("range: bad arg count") << "range(2,3,vector{3}, list{2}, 4)"; 0439 QTest::newRow("range: bad args1") << "range(2,3,9, list{2}, 4)"; 0440 QTest::newRow("range: bad args2") << "range(2,3,9, 9, vector{4})"; 0441 QTest::newRow("range: bad args3") << "range(2,3,9, 9, 3,4,5,5)"; 0442 0443 QTest::newRow("vector: bad arg type") << "vector(list{23},4)"; 0444 QTest::newRow("vector: bad number of args") << "vector(4, list{23}, 44)"; 0445 0446 QTest::newRow("matrix: 0 args") << "matrix()"; 0447 QTest::newRow("matrix: bad args") << "matrix(list{3}, 31)"; 0448 QTest::newRow("matrix: invalid parameter") << "matrix(range(list{matrix{2}}))"; 0449 QTest::newRow("matrix: empty matrix result") << "matrix(0, 0)"; 0450 QTest::newRow("matrix: fill empty matrix result") << "matrix(0, 0, sin(1))"; 0451 QTest::newRow("matrix: not all vectors") << "matrix(vector{1}, 3)"; 0452 QTest::newRow("matrix: not all matrixrow elements") << "matrix(matrixrow{1}, list{2})"; 0453 QTest::newRow("matrix: not all matrixrow elements 2") << "matrix(matrixrow{1}, vector{2})"; 0454 QTest::newRow("matrix: neg square") << "matrix(-9)"; 0455 0456 QTest::newRow("zero matrix: empty matrix result") << "matrix(0, 0)"; 0457 QTest::newRow("zero matrix: bad number of args") << "matrix()"; 0458 QTest::newRow("zero matrix: bad number of args") << "matrix(3,list{3},4,6)"; 0459 QTest::newRow("zero matrix: bad dim") << "matrix(23, -3.5)"; 0460 QTest::newRow("zero matrix: bad dim2") << "matrix(-23, 5)"; 0461 0462 QTest::newRow("identity matrix: bad arg") << "identitymatrix(-98)"; 0463 QTest::newRow("identity matrix: empty matrix result") << "identitymatrix(0)"; 0464 0465 QTest::newRow("diag: 0 args") << "diag()"; 0466 QTest::newRow("diag: bad arg, one empty matrix") << "diag(identitymatrix(0))"; 0467 QTest::newRow("diag: bad arg, one empty matrix2") << "diag(matrix{matrixrow{}})"; 0468 QTest::newRow("diag: bad diag index") << "diag(matrix(4,6,3.2), -98)"; 0469 QTest::newRow("diag: bad diag index type") << "diag(matrix(4,6,3.2), list{-98})"; 0470 QTest::newRow("diag: invalid parameter") << "diag(matrix(-8,5))"; 0471 0472 QTest::newRow("tridiag: empty matrix result") << "tridiag(1,2,3,0)"; 0473 QTest::newRow("tridiag: bad number of args") << "tridiag(1,2,2)"; 0474 QTest::newRow("tridiag: bad number of args2") << "tridiag(1,2,2, 4, list{2})"; 0475 0476 QTest::newRow("iszeromatrix: bad number of args") << "iszeromatrix()"; 0477 QTest::newRow("iszeromatrix: empty matrix") << "iszeromatrix(matrix{})"; 0478 QTest::newRow("iszeromatrix: invalid parameter") << "iszeromatrix(matrix(-8,5))"; 0479 0480 QTest::newRow("isidentitymatrix: bad number of args") << "isidentitymatrix(matrix{matrixrow{1}}, 2)"; 0481 QTest::newRow("isidentitymatrix: bad number of args2") << "isidentitymatrix()"; 0482 QTest::newRow("isidentitymatrix: empty matrix") << "isidentitymatrix(matrix{})"; 0483 QTest::newRow("isidentitymatrix: empty matrix2") << "isidentitymatrix(matrix{matrixrow{}})"; 0484 QTest::newRow("isidentitymatrix: invalid parameter") << "isidentitymatrix(matrix(-8,5))"; 0485 QTest::newRow("isidentitymatrix: invalid parameter2") << "isidentitymatrix(matrix(list{},5))"; 0486 0487 QTest::newRow("isdiag: bad number of args") << "isdiag(matrix{matrixrow{1}}, 2)"; 0488 QTest::newRow("isdiag: bad number of args2") << "isdiag()"; 0489 QTest::newRow("isdiag: empty matrix") << "isdiag(matrix{})"; 0490 QTest::newRow("isdiag: empty matrix2") << "isdiag(matrix{matrixrow{}})"; 0491 QTest::newRow("isdiag: invalid parameter") << "isdiag(matrix(-8,5))"; 0492 0493 QTest::newRow("blockmatrix: bad block matrix size") << "blockmatrix(vector{matrix(1,2), matrix(32,13)})"; 0494 QTest::newRow("blockmatrix: empty matrix1") << "blockmatrix(vector{matrix{}}, vector{matrix{matrixrow{}}})"; 0495 QTest::newRow("blockmatrix: empty matrix2") << "blockmatrix(vector{identitymatrix(0)})"; 0496 QTest::newRow("blockmatrix: bad arg type") << "blockmatrix(vector{-5})"; 0497 QTest::newRow("blockmatrix: err arg") << "blockmatrix(vector{matrix{-98}})"; 0498 QTest::newRow("blockmatrix: bad block matrix args size") << "blockmatrix(vector{matrix(1,2), matrix(32,13)}, vector{matrix(7,13)})"; 0499 QTest::newRow("blockmatrix: bad block matrix args type 1") << "blockmatrix(vector{matrix(32,13), list{23}}, vector{matrix(7,13), matrix(32,1)})"; 0500 QTest::newRow("blockmatrix: bad block matrix args type 2") << "matrix(vector{list{23}, zeromatrix(32,13)}, vector{zeromatrix(7,13), zeromatrix(32,1)})"; 0501 0502 QTest::newRow("blockdiag: bad block diag, empty matrix 1") << "blockdiag(matrix(0,0), matrix(2,2,1))"; 0503 QTest::newRow("blockdiag: empty matrix1") << "blockdiag(matrix{})"; 0504 QTest::newRow("blockdiag: empty matrix2") << "blockdiag(identitymatrix(0))"; 0505 QTest::newRow("blockdiag: bad arg type") << "blockdiag(vector{-5})"; 0506 QTest::newRow("blockdiag: bad block diag, empty matrix 2") << "blockdiag(matrix{matrixrow{1}}, tridiag(1,2,3,0))"; 0507 QTest::newRow("blockdiag: invalid parameter") << "blockdiag(matrix(-8,5))"; 0508 0509 QTest::newRow("bad dimensions:2x2identitymatrix and 2x1zeromatrix") << "2*(identitymatrix(2) + matrix(2,1))"; 0510 QTest::newRow("bad dimensions:2x2identitymatrix and -2x2matrix") << "2*(identitymatrix(2) + matrix(-2, 2,1))"; 0511 0512 #ifdef HAVE_EIGEN3 0513 QTest::newRow("eigenvalues: bad args type1") << "eigenvalues(list{23},4)"; 0514 QTest::newRow("eigenvalues: bad args type2") << "eigenvalues(list{23})"; 0515 QTest::newRow("eigenvalues: empty matrix2") << "eigenvalues(matrix{})"; 0516 QTest::newRow("eigenvalues: empty invalid matrix2") << "eigenvalues(matrix{1}, matrix{})"; 0517 QTest::newRow("eigenvalues: bad numbe of args") << "eigenvalues(identitymatrix(3), zeromatrix(4,5))"; 0518 QTest::newRow("eigenvalues: empty matrix") << "eigenvalues(matrix{matrixrow{1,2}}, matrix{})"; 0519 QTest::newRow("eigenvalues: invalid arg types1") << "eigenvalues(matrix{matrixrow{1,2}}, matrix{matrixrow{list{5},6}})"; 0520 QTest::newRow("eigenvalues: invalid arg types2") << "eigenvalues(matrix{matrixrow{1,list{2}}})"; 0521 QTest::newRow("eigenvalues: non square matrix") << "eigenvalues(matrix{matrixrow{1,2,3}})"; 0522 //TODO nan and inf cases 0523 #endif 0524 } 0525 0526 void CommandsTest::testIncorrect() 0527 { 0528 QFETCH(QString, expression); 0529 0530 Expression exp(expression, false); 0531 0532 if (exp.isCorrect()) { 0533 Analitza::Analyzer a; 0534 a.setExpression(exp); 0535 0536 if (a.isCorrect()) { 0537 Expression calc = a.calculate(); 0538 QVERIFY(!a.isCorrect()); 0539 QCOMPARE(calc.toString(), QString()); 0540 qDebug() << "calc errors:" << a.errors(); 0541 0542 Expression eval = a.evaluate(); 0543 QVERIFY(!a.isCorrect()); 0544 QCOMPARE(eval.toString(), QString()); 0545 qDebug() << "eval errors:" << a.errors(); 0546 } else { 0547 QVERIFY(!a.isCorrect()); 0548 qDebug() << "set expression errors:" << a.errors(); 0549 } 0550 } else { 0551 qDebug() << "expression errors:" << exp.error(); 0552 } 0553 } 0554 0555 0556 0557 #include "moc_commandstest.cpp"