File indexing completed on 2024-05-19 05:41:31
0001 /*************************************************************************** 0002 * Copyright (C) 2011 by Renaud Guezennec * 0003 * http://renaudguezennec.homelinux.org/accueil,3.html * 0004 * * 0005 * Rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include <QtCore/QCoreApplication> 0022 #include <QtCore/QString> 0023 #include <QtTest/QtTest> 0024 0025 #include "dicealias.h" 0026 #include "diceparser.h" 0027 #include "die.h" 0028 0029 // node 0030 #include "node/bind.h" 0031 #include "node/countexecutenode.h" 0032 #include "node/explodedicenode.h" 0033 #include "node/filternode.h" 0034 #include "node/groupnode.h" 0035 #include "node/ifnode.h" 0036 #include "node/jumpbackwardnode.h" 0037 #include "node/keepdiceexecnode.h" 0038 #include "node/numbernode.h" 0039 #include "node/occurencecountnode.h" 0040 #include "node/rerolldicenode.h" 0041 #include "node/sortresult.h" 0042 #include "node/stringnode.h" 0043 #include "node/testnode.h" 0044 #include "result/stringresult.h" 0045 0046 class TestDice : public QObject 0047 { 0048 Q_OBJECT 0049 0050 public: 0051 TestDice(); 0052 0053 private slots: 0054 void initTestCase(); 0055 void getAndSetTest(); 0056 void diceRollD10Test(); 0057 void diceRollD20Test(); 0058 void commandEndlessLoop(); 0059 0060 void mathPriority(); 0061 void mathPriority_data(); 0062 0063 void commandsTest(); 0064 void commandsTest_data(); 0065 0066 void dangerousCommandsTest(); 0067 void dangerousCommandsTest_data(); 0068 0069 void wrongCommandsTest(); 0070 void wrongCommandsTest_data(); 0071 0072 void wrongCommandsExecutionTimeTest(); 0073 void scopeDF(); 0074 void scopeDF_data(); 0075 0076 void severalInstruction(); 0077 void testAlias(); 0078 void cleanupTestCase(); 0079 0080 void keepTest(); 0081 void keepTest_data(); 0082 0083 void sortTest(); 0084 void sortTest_data(); 0085 0086 void countTest(); 0087 void countTest_data(); 0088 0089 void rerollTest(); 0090 void rerollTest_data(); 0091 0092 void explodeTest(); 0093 void explodeTest_data(); 0094 0095 void rerollUntilTest(); 0096 void rerollUntilTest_data(); 0097 0098 void rerollAddTest(); 0099 void rerollAddTest_data(); 0100 0101 void mergeTest(); 0102 void mergeTest_data(); 0103 0104 void ifTest(); 0105 void ifTest_data(); 0106 0107 void paintTest(); 0108 void paintTest_data(); 0109 0110 void filterTest(); 0111 void filterTest_data(); 0112 0113 void splitTest(); 0114 void splitTest_data(); 0115 0116 void uniqueTest(); 0117 void uniqueTest_data(); 0118 0119 void groupTest(); 0120 void groupTest_data(); 0121 0122 void bindTest(); 0123 void bindTest_data(); 0124 0125 void occurenceTest(); 0126 void occurenceTest_data(); 0127 0128 private: 0129 std::unique_ptr<Die> m_die; 0130 std::unique_ptr<DiceParser> m_diceParser; 0131 }; 0132 0133 TestDice::TestDice() {} 0134 0135 void TestDice::initTestCase() 0136 { 0137 m_die.reset(new Die()); 0138 m_diceParser.reset(new DiceParser()); 0139 } 0140 0141 void TestDice::getAndSetTest() 0142 { 0143 for(unsigned int i= 0; i < 2000; i++) 0144 { 0145 m_die->setMaxValue(i); 0146 QVERIFY(m_die->getMaxValue() == i); 0147 } 0148 0149 m_die->setSelected(true); 0150 QVERIFY(m_die->isSelected() == true); 0151 0152 m_die->setSelected(false); 0153 QVERIFY(m_die->isSelected() == false); 0154 } 0155 0156 void TestDice::diceRollD10Test() 0157 { 0158 m_die->setMaxValue(10); 0159 for(int i= 0; i < 2000; i++) 0160 { 0161 m_die->roll(false); 0162 QVERIFY(m_die->getValue() > 0); 0163 QVERIFY(m_die->getValue() < 11); 0164 } 0165 } 0166 void TestDice::diceRollD20Test() 0167 { 0168 m_die->setMaxValue(20); 0169 for(int i= 0; i < 2000; i++) 0170 { 0171 m_die->roll(false); 0172 QVERIFY(m_die->getValue() > 0); 0173 QVERIFY(m_die->getValue() < 21); 0174 } 0175 } 0176 void TestDice::commandEndlessLoop() 0177 { 0178 bool a= m_diceParser->parseLine("1D10e[>0]"); 0179 QVERIFY(!a); 0180 } 0181 0182 void TestDice::commandsTest() 0183 { 0184 QFETCH(QString, cmd); 0185 0186 bool a= m_diceParser->parseLine(cmd); 0187 QVERIFY2(a, "parsing"); 0188 0189 m_diceParser->start(); 0190 QVERIFY2(m_diceParser->humanReadableError().isEmpty(), "no error"); 0191 0192 QVERIFY2(m_diceParser->humanReadableWarning().isEmpty(), "no warning"); 0193 } 0194 0195 void TestDice::commandsTest_data() 0196 { 0197 QTest::addColumn<QString>("cmd"); 0198 0199 QTest::addRow("cmd1") << "1L[cheminée,chocolat,épée,arc,chute de pierre]"; 0200 QTest::addRow("cmd2") << "10d10c[>=6]-@c[=1]"; 0201 QTest::addRow("cmd3") << "10d10c[>=6]-@c[=1]-@c[=1]"; 0202 QTest::addRow("cmd4") << "10d10c[>6]+@c[=10]"; 0203 QTest::addRow("cmd5") << "1+1D10"; 0204 QTest::addRow("cmd6") << "3d10c[>=5]"; 0205 QTest::addRow("cmd7") << "1+(4*3)D10"; 0206 QTest::addRow("cmd8") << "2+4/4"; 0207 QTest::addRow("cmd9") << "2D10*2D20*8"; 0208 QTest::addRow("cmd10") << "1+(4*3)D10"; 0209 QTest::addRow("cmd11") << "(4D6)D10"; 0210 QTest::addRow("cmd12") << "1D100a[>=95]a[>=96]a[>=97]a[>=98]a[>=99]e[>=100]"; 0211 QTest::addRow("cmd13") << "3D100"; 0212 QTest::addRow("cmd14") << "4k3"; 0213 QTest::addRow("cmd15") << "10D10e[>=6]sc[>=6]"; 0214 QTest::addRow("cmd16") << "10D10e10s"; 0215 QTest::addRow("cmd17") << "10D10s"; 0216 QTest::addRow("cmd18") << "15D10e10c[8-10]"; 0217 QTest::addRow("cmd19") << "10d10e10"; 0218 QTest::addRow("cmd30") << "(4+4)^4"; 0219 QTest::addRow("cmd31") << "(1d20+20)*7/10"; 0220 QTest::addRow("cmd32") << "20*7/10"; 0221 QTest::addRow("cmd33") << "1D8+2D6+7"; 0222 QTest::addRow("cmd34") << "D25"; 0223 QTest::addRow("cmd35") << "1L[tete[10],ventre[50],jambe[40]]"; 0224 QTest::addRow("cmd36") << "2d6c[%2=0]"; 0225 QTest::addRow("cmd37") << "D25+D10"; 0226 QTest::addRow("cmd38") << "D25;D10"; 0227 QTest::addRow("cmd39") << "8+8+8"; 0228 QTest::addRow("cmd40") << "1D20-88"; 0229 QTest::addRow("cmd41") << "100*1D20*2D6"; 0230 QTest::addRow("cmd42") << "2D6 # two 6sided dice"; 0231 QTest::addRow("cmd43") << "100/28*3"; 0232 QTest::addRow("cmd44") << "100/8"; 0233 QTest::addRow("cmd45") << "100*3*8"; 0234 QTest::addRow("cmd46") << "help"; 0235 QTest::addRow("cmd47") << "la"; 0236 QTest::addRow("cmd48") << "10D10c[<2|>7]"; 0237 QTest::addRow("cmd49") << "10D6c[=2|=4|=6]"; 0238 QTest::addRow("cmd50") << "10D10e[=1|=10]k4"; 0239 QTest::addRow("cmd51") << "1L[tete,bras droit,bras gauche,jambe droite,jambe gauche,ventre[6-7],buste[8-10]]"; 0240 QTest::addRow("cmd52") << "10+10s"; 0241 QTest::addRow("cmd53") << "1d6e6;1d4e4mk1"; 0242 QTest::addRow("cmd54") << "1d6e6;1d4e4mk1"; 0243 QTest::addRow("cmd55") << "400D20/400000"; 0244 QTest::addRow("cmd56") << "1d100e[>=95]i[<5]{-1d100e95}"; 0245 QTest::addRow("cmd57") << "100*3*8"; 0246 QTest::addRow("cmd58") << "1d100i[<70]{1d10i[=10]{1d100i[<70]{1d10e10}}}"; 0247 QTest::addRow("cmd59") << "10d6c[<2|>5]"; 0248 QTest::addRow("cmd60") << "5-5*5+5"; 0249 QTest::addRow("cmd61") << "((3+4)*2)d6"; 0250 QTest::addRow("cmd62") << "4d6i[=6]{+1d6}"; 0251 QTest::addRow("cmd63") << "10d[-8--1]"; 0252 QTest::addRow("cmd64") << "4d6e6i[=4]{-4}+2"; 0253 QTest::addRow("cmd65") << "4d6e6f[!=4]+2"; 0254 QTest::addRow("cmd66") << "5d10g10"; 0255 QTest::addRow("cmd67") << "4d6p[4:blue]c[>=4];1d6p[1:#FFFFFF]c6-@c1;1d6p[1:#FF0000]c[>=4]+@c6-@c1"; 0256 QTest::addRow("cmd68") << "10d[0-9]"; 0257 QTest::addRow("cmd69") << "1d8e8;1d6e6mk1+2"; 0258 QTest::addRow("cmd70") << "3d100g50"; 0259 QTest::addRow("cmd71") << "3d100g33"; 0260 QTest::addRow("cmd72") << "3d100g5"; 0261 QTest::addRow("cmd73") << "3d100g40"; 0262 QTest::addRow("cmd74") << "2d10k1+2d10k1+2d10k1"; 0263 } 0264 0265 void TestDice::wrongCommandsTest() 0266 { 0267 // << "pajaejlbnmàw"; 0268 QFETCH(QString, cmd); 0269 QFETCH(bool, valid); 0270 bool a= m_diceParser->parseLine(cmd); 0271 if(a) 0272 { 0273 m_diceParser->start(); 0274 auto map= m_diceParser->getErrorMap(); 0275 a= map.isEmpty(); 0276 } 0277 QCOMPARE(a, valid); 0278 } 0279 0280 void TestDice::wrongCommandsTest_data() 0281 { 0282 QTest::addColumn<QString>("cmd"); 0283 QTest::addColumn<bool>("valid"); 0284 0285 QTest::newRow("test1") << "1L[cheminée,chocolat,épée,arc,chute de pierre" << false; 0286 QTest::newRow("test2") << "10d10c" << false; 0287 QTest::newRow("test3") << "10d10a" << false; 0288 QTest::newRow("test4") << "10d0a[>7]" << false; 0289 QTest::newRow("test5") << "aiteanetauearuteurn" << false; 0290 QTest::newRow("test6") << "meregue" << false; 0291 QTest::newRow("test7") << "p i follow rivers" << false; 0292 QTest::newRow("test8") << "manga violet evergarden" << false; 0293 QTest::newRow("test9") << "((1d8e8+2);(1d6e6+2))" << true; 0294 } 0295 0296 void TestDice::wrongCommandsExecutionTimeTest() 0297 { 0298 QStringList commands; 0299 0300 //<< "8D20+10*@c[=20]" 0301 commands << "1/0" 0302 << "" 0303 << "0d10" 0304 << "10d10k11" 0305 << "!!!!"; 0306 0307 for(QString cmd : commands) 0308 { 0309 bool test= m_diceParser->parseLine(cmd); 0310 m_diceParser->start(); 0311 0312 QVERIFY2(m_diceParser->getErrorMap().isEmpty() == false || !test, cmd.toStdString().c_str()); 0313 } 0314 } 0315 void TestDice::scopeDF() 0316 { 0317 QFETCH(QString, cmd); 0318 QFETCH(int, min); 0319 QFETCH(int, max); 0320 QFETCH(bool, valid); 0321 0322 bool test= m_diceParser->parseLine(cmd); 0323 QVERIFY2(test == valid, cmd.toStdString().c_str()); 0324 m_diceParser->start(); 0325 auto results= m_diceParser->getLastIntegerResults(); 0326 0327 for(auto result : results) 0328 QVERIFY(result >= min && result <= max); 0329 } 0330 0331 void TestDice::scopeDF_data() 0332 { 0333 QTest::addColumn<QString>("cmd"); 0334 QTest::addColumn<int>("min"); 0335 QTest::addColumn<int>("max"); 0336 QTest::addColumn<bool>("valid"); 0337 0338 QTest::newRow("test1") << "1D[-1-1]" << -1 << 1 << true; 0339 QTest::newRow("test2") << "1D[-10--5]" << -10 << -5 << true; 0340 QTest::newRow("test3") << "1D[-100-100]" << -100 << 100 << true; 0341 QTest::newRow("test4") << "1D[-1-0]" << -1 << 0 << true; 0342 QTest::newRow("test5") << "1D[10-20]" << 10 << 20 << true; 0343 QTest::newRow("test6") << "1D[30-100]" << 30 << 100 << true; 0344 QTest::newRow("test7") << "1D[0-99]" << 0 << 99 << true; 0345 QTest::newRow("test8") << "5-5*5+5" << -15 << -15 << true; 0346 QTest::newRow("test9") << "2d20c[<=13]+@c[<=3]" << 0 << 4 << true; 0347 QTest::newRow("test10") << "6d10c[>=6]-@c1" << -6 << 6 << true; 0348 QTest::newRow("test11") << "2d6k1+2d8k1+2d10k1" << 3 << 30 << true; 0349 QTest::newRow("test12") << "1D[-2-50]" << -2 << 50 << true; 0350 } 0351 void TestDice::testAlias() 0352 { 0353 m_diceParser->insertAlias(new DiceAlias("!", "3d6c"), 0); 0354 m_diceParser->insertAlias(new DiceAlias("g", "d10k"), 1); 0355 m_diceParser->insertAlias(new DiceAlias("(.*)C(.*)", QStringLiteral("\\1d10e10c[>=\\2]"), false), 2); 0356 0357 QStringList cmds; 0358 cmds << "!2" 0359 << "${rang}g4" 0360 << "${rang}g4 # gerald" 0361 << "5C3" 0362 << "1d100i:[<101]{\"great!\"}{\"try again\"}"; 0363 0364 QStringList expected; 0365 expected << "3d6c2" 0366 << "${rang}d10k4" 0367 << "${rang}d10k4 # gerald" 0368 << "5d10e10c[>=3]" 0369 << "1d100i:[<101]{\"great!\"}{\"try again\"}"; 0370 0371 int i= 0; 0372 for(auto cmd : cmds) 0373 { 0374 auto result= m_diceParser->convertAlias(cmd); 0375 QVERIFY2(result == expected[i], result.toLatin1()); 0376 ++i; 0377 } 0378 } 0379 void TestDice::severalInstruction() 0380 { 0381 QStringList commands; 0382 0383 commands << "1d10;2d20;$1+$2"; 0384 0385 QList<int> results; 0386 results << 3; 0387 0388 int i= 0; 0389 for(auto cmd : commands) 0390 { 0391 auto test= m_diceParser->parseLine(cmd); 0392 QVERIFY2(test, cmd.toStdString().c_str()); 0393 QVERIFY2(m_diceParser->getStartNodeCount() == results[i], "Wrong number of instruction"); 0394 } 0395 } 0396 void TestDice::mathPriority() 0397 { 0398 QFETCH(QString, cmd); 0399 QFETCH(int, expected); 0400 0401 bool test= m_diceParser->parseLine(cmd); 0402 QVERIFY(test); 0403 m_diceParser->start(); 0404 auto resultList= m_diceParser->getLastIntegerResults(); 0405 QCOMPARE(resultList.size(), 1); 0406 0407 auto value= resultList.first(); 0408 QVERIFY(qFuzzyCompare(value, expected) == 1); 0409 } 0410 0411 void TestDice::mathPriority_data() 0412 { 0413 QTest::addColumn<QString>("cmd"); 0414 QTest::addColumn<int>("expected"); 0415 0416 QTest::addRow("cmd1") << "10+2" << 12; 0417 QTest::addRow("cmd2") << "2-10" << -8; 0418 QTest::addRow("cmd3") << "5+2*3" << 11; 0419 QTest::addRow("cmd4") << "5-5*5+5" << -15; 0420 QTest::addRow("cmd5") << "5-5/5+5" << 9; 0421 QTest::addRow("cmd6") << "10*(3*2)" << 60; 0422 QTest::addRow("cmd7") << "60/(3*2)" << 10; 0423 QTest::addRow("cmd8") << "5-(5*5+5)" << -25; 0424 } 0425 0426 void TestDice::dangerousCommandsTest() 0427 { 0428 QFETCH(QString, cmd); 0429 0430 for(int i= 0; i < 1000; ++i) 0431 { 0432 auto b= m_diceParser->parseLine(cmd); 0433 QVERIFY(b); 0434 m_diceParser->start(); 0435 } 0436 } 0437 void TestDice::dangerousCommandsTest_data() 0438 { 0439 QTest::addColumn<QString>("cmd"); 0440 0441 QTest::addRow("cmd1") << "10d6g10"; 0442 QTest::addRow("cmd2") << "10d2g10"; 0443 QTest::addRow("cmd3") << "10d10g10"; 0444 // QTest::addRow("cmd4") << "10d10g10"; 0445 // QTest::addRow("cmd5") << "10d10g10"; 0446 } 0447 0448 void makeResult(DiceResult& result, const QVector<int>& values) 0449 { 0450 for(int val : values) 0451 { 0452 auto die= new Die(); 0453 die->setBase(1); 0454 die->setMaxValue(10); 0455 die->insertRollValue(val); 0456 result.insertResult(die); 0457 } 0458 } 0459 0460 Validator* makeValidator(int number, BooleanCondition::LogicOperator op) 0461 { 0462 BooleanCondition* validator= new BooleanCondition(); 0463 NumberNode* node= new NumberNode(); 0464 node->setNumber(number); 0465 validator->setValueNode(node); 0466 validator->setOperator(op); 0467 return validator; 0468 } 0469 0470 void TestDice::keepTest() 0471 { 0472 QFETCH(QVector<int>, values); 0473 QFETCH(int, keep); 0474 QFETCH(int, score); 0475 QFETCH(bool, error); 0476 0477 TestNode node; 0478 KeepDiceExecNode keepN; 0479 keepN.setDiceKeepNumber(keep); 0480 0481 DiceResult result; 0482 0483 makeResult(result, values); 0484 0485 node.setResult(&result); 0486 node.setNextNode(&keepN); 0487 0488 node.run(nullptr); 0489 0490 bool isErrorEmpty= !keepN.getExecutionErrorMap().isEmpty(); 0491 0492 QCOMPARE(isErrorEmpty, error); 0493 0494 if(error) 0495 return; 0496 0497 auto resultScore= keepN.getResult()->getResult(Result::SCALAR).toInt(); 0498 0499 QCOMPARE(score, resultScore); 0500 } 0501 0502 void TestDice::keepTest_data() 0503 { 0504 QTest::addColumn<QVector<int>>("values"); 0505 QTest::addColumn<int>("keep"); 0506 QTest::addColumn<int>("score"); 0507 QTest::addColumn<bool>("error"); 0508 0509 QTest::addRow("cmd1") << QVector<int>({10, 9, 2}) << 1 << 10 << false; 0510 QTest::addRow("cmd2") << QVector<int>({10, 9, 2}) << 2 << 19 << false; 0511 QTest::addRow("cmd3") << QVector<int>({10, 9, 2}) << 3 << 21 << false; 0512 QTest::addRow("cmd4") << QVector<int>({10, 9, 2}) << 4 << 0 << true; 0513 } 0514 0515 void TestDice::sortTest() 0516 { 0517 QFETCH(QVector<int>, values); 0518 QFETCH(bool, ascending); 0519 QFETCH(QVector<int>, scores); 0520 0521 TestNode node; 0522 SortResultNode sortN; 0523 sortN.setSortAscending(ascending); 0524 0525 DiceResult result; 0526 0527 makeResult(result, values); 0528 0529 node.setResult(&result); 0530 node.setNextNode(&sortN); 0531 0532 DiceResult expectedScore; 0533 makeResult(expectedScore, scores); 0534 0535 node.run(nullptr); 0536 0537 auto list= dynamic_cast<DiceResult*>(sortN.getResult())->getResultList(); 0538 0539 int i= 0; 0540 auto expected= expectedScore.getResultList(); 0541 for(auto sortedDie : list) 0542 { 0543 QCOMPARE(expected[i]->getValue(), sortedDie->getValue()); 0544 ++i; 0545 } 0546 } 0547 0548 void TestDice::sortTest_data() 0549 { 0550 QTest::addColumn<QVector<int>>("values"); 0551 QTest::addColumn<bool>("ascending"); 0552 QTest::addColumn<QVector<int>>("scores"); 0553 0554 QTest::addRow("cmd1") << QVector<int>({10, 9, 2}) << true << QVector<int>({2, 9, 10}); 0555 QTest::addRow("cmd2") << QVector<int>({1, 2, 3}) << false << QVector<int>({3, 2, 1}); 0556 QTest::addRow("cmd3") << QVector<int>({10, 9, 2}) << false << QVector<int>({10, 9, 2}); 0557 QTest::addRow("cmd4") << QVector<int>({2, 9, 10}) << true << QVector<int>({2, 9, 10}); 0558 QTest::addRow("cmd5") << QVector<int>({1, 25, 10}) << false << QVector<int>({25, 10, 1}); 0559 QTest::addRow("cmd6") << QVector<int>({10, 2, 100}) << false << QVector<int>({100, 10, 2}); 0560 } 0561 0562 void TestDice::countTest() 0563 { 0564 QFETCH(QVector<int>, values); 0565 QFETCH(int, condition); 0566 QFETCH(int, score); 0567 0568 TestNode node; 0569 CountExecuteNode countN; 0570 0571 auto validator= makeValidator(condition, BooleanCondition::GreaterThan); 0572 0573 countN.setValidator(validator); 0574 DiceResult result; 0575 node.setResult(&result); 0576 node.setNextNode(&countN); 0577 0578 makeResult(result, values); 0579 0580 node.run(nullptr); 0581 0582 QCOMPARE(score, countN.getResult()->getResult(Result::SCALAR).toInt()); 0583 0584 countN.setValidator(nullptr); 0585 } 0586 0587 void TestDice::countTest_data() 0588 { 0589 QTest::addColumn<QVector<int>>("values"); 0590 QTest::addColumn<int>("condition"); 0591 QTest::addColumn<int>("score"); 0592 0593 QTest::addRow("cmd1") << QVector<int>({10, 9, 2}) << 3 << 2; 0594 QTest::addRow("cmd2") << QVector<int>({1, 2, 3}) << 3 << 0; 0595 } 0596 0597 void TestDice::rerollTest() 0598 { 0599 QFETCH(QVector<int>, values); 0600 QFETCH(int, condition); 0601 QFETCH(bool, different); 0602 0603 TestNode node; 0604 RerollDiceNode reroll(true, false); 0605 0606 DiceResult result; 0607 makeResult(result, values); 0608 node.setResult(&result); 0609 0610 auto validator= makeValidator(condition, BooleanCondition::GreaterThan); 0611 reroll.setValidator(validator); 0612 node.setNextNode(&reroll); 0613 0614 node.run(nullptr); 0615 0616 auto list= dynamic_cast<DiceResult*>(reroll.getResult())->getResultList(); 0617 0618 int i= 0; 0619 auto expected= result.getResultList(); 0620 bool resultDiff= false; 0621 for(auto rerolled : list) 0622 { 0623 if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) 0624 resultDiff= true; 0625 ++i; 0626 } 0627 0628 QCOMPARE(different, resultDiff); 0629 } 0630 0631 void TestDice::rerollTest_data() 0632 { 0633 QTest::addColumn<QVector<int>>("values"); 0634 QTest::addColumn<int>("condition"); 0635 QTest::addColumn<bool>("different"); 0636 0637 QTest::addRow("cmd1") << QVector<int>({8, 9, 2}) << 10 << false; 0638 QTest::addRow("cmd2") << QVector<int>({0, 0, 0}) << -1 << true; 0639 } 0640 0641 void TestDice::explodeTest() 0642 { 0643 QFETCH(QVector<int>, values); 0644 QFETCH(int, condition); 0645 QFETCH(bool, different); 0646 0647 TestNode node; 0648 ExplodeDiceNode explode; 0649 0650 DiceResult result; 0651 makeResult(result, values); 0652 node.setResult(&result); 0653 0654 auto validator= makeValidator(condition, BooleanCondition::Equal); 0655 explode.setValidator(validator); 0656 node.setNextNode(&explode); 0657 0658 node.run(nullptr); 0659 0660 auto list= dynamic_cast<DiceResult*>(explode.getResult())->getResultList(); 0661 0662 int i= 0; 0663 auto expected= result.getResultList(); 0664 bool resultDiff= false; 0665 for(auto rerolled : list) 0666 { 0667 if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) 0668 resultDiff= true; 0669 ++i; 0670 } 0671 0672 QCOMPARE(different, resultDiff); 0673 } 0674 0675 void TestDice::explodeTest_data() 0676 { 0677 QTest::addColumn<QVector<int>>("values"); 0678 QTest::addColumn<int>("condition"); 0679 QTest::addColumn<bool>("different"); 0680 0681 QTest::addRow("cmd1") << QVector<int>({8, 9, 2}) << 10 << false; 0682 QTest::addRow("cmd2") << QVector<int>({0, 0, 0}) << 0 << true; 0683 } 0684 0685 void TestDice::rerollUntilTest() 0686 { 0687 QFETCH(QVector<int>, values); 0688 QFETCH(int, condition); 0689 QFETCH(bool, different); 0690 0691 TestNode node; 0692 RerollDiceNode reroll(false, false); 0693 0694 DiceResult result; 0695 makeResult(result, values); 0696 node.setResult(&result); 0697 0698 auto validator= makeValidator(condition, BooleanCondition::Equal); 0699 reroll.setValidator(validator); 0700 node.setNextNode(&reroll); 0701 0702 node.run(nullptr); 0703 0704 auto list= dynamic_cast<DiceResult*>(reroll.getResult())->getResultList(); 0705 0706 int i= 0; 0707 auto expected= result.getResultList(); 0708 bool resultDiff= false; 0709 for(auto rerolled : list) 0710 { 0711 if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) 0712 resultDiff= true; 0713 ++i; 0714 } 0715 0716 QCOMPARE(different, resultDiff); 0717 } 0718 void TestDice::rerollUntilTest_data() 0719 { 0720 QTest::addColumn<QVector<int>>("values"); 0721 QTest::addColumn<int>("condition"); 0722 QTest::addColumn<bool>("different"); 0723 0724 QTest::addRow("cmd1") << QVector<int>({8, 9, 2}) << 10 << false; 0725 QTest::addRow("cmd2") << QVector<int>({0, 0, 0}) << 0 << true; 0726 } 0727 0728 void TestDice::rerollAddTest() 0729 { 0730 QFETCH(QVector<int>, values); 0731 QFETCH(int, condition); 0732 QFETCH(bool, different); 0733 0734 TestNode node; 0735 RerollDiceNode reroll(true, true); 0736 0737 DiceResult result; 0738 makeResult(result, values); 0739 node.setResult(&result); 0740 0741 auto validator= makeValidator(condition, BooleanCondition::Equal); 0742 reroll.setValidator(validator); 0743 node.setNextNode(&reroll); 0744 0745 node.run(nullptr); 0746 0747 auto list= dynamic_cast<DiceResult*>(reroll.getResult())->getResultList(); 0748 0749 int i= 0; 0750 auto expected= result.getResultList(); 0751 bool resultDiff= false; 0752 for(auto rerolled : list) 0753 { 0754 if(!resultDiff && rerolled->getValue() != expected[i]->getValue()) 0755 resultDiff= true; 0756 ++i; 0757 } 0758 0759 QCOMPARE(different, resultDiff); 0760 } 0761 void TestDice::rerollAddTest_data() 0762 { 0763 QTest::addColumn<QVector<int>>("values"); 0764 QTest::addColumn<int>("condition"); 0765 QTest::addColumn<bool>("different"); 0766 0767 QTest::addRow("cmd1") << QVector<int>({8, 9, 2}) << 10 << false; 0768 QTest::addRow("cmd2") << QVector<int>({0, 0, 0}) << 0 << true; 0769 } 0770 0771 void TestDice::mergeTest() {} 0772 void TestDice::mergeTest_data() {} 0773 0774 void TestDice::ifTest() 0775 { 0776 QFETCH(QVector<int>, values); 0777 QFETCH(int, condition); 0778 QFETCH(int, valCondition); 0779 QFETCH(QString, expectedResult); 0780 0781 IfNode::ConditionType conditionType= static_cast<IfNode::ConditionType>(condition); 0782 0783 TestNode node; 0784 IfNode ifNode; 0785 ifNode.setConditionType(conditionType); 0786 0787 DiceResult result; 0788 makeResult(result, values); 0789 node.setResult(&result); 0790 0791 StringNode trueNode; 0792 trueNode.setString(QStringLiteral("True")); 0793 StringNode falseNode; 0794 falseNode.setString(QStringLiteral("False")); 0795 0796 ifNode.setInstructionTrue(&trueNode); 0797 ifNode.setInstructionFalse(&falseNode); 0798 0799 auto validator= makeValidator(valCondition, BooleanCondition::Equal); 0800 ifNode.setValidator(validator); 0801 node.setNextNode(&ifNode); 0802 0803 node.run(nullptr); 0804 0805 auto text= dynamic_cast<StringResult*>(ifNode.getNextNode()->getResult())->getText(); 0806 0807 QCOMPARE(expectedResult, text); 0808 0809 ifNode.setNextNode(nullptr); 0810 } 0811 void TestDice::ifTest_data() 0812 { 0813 QTest::addColumn<QVector<int>>("values"); 0814 QTest::addColumn<int>("condition"); 0815 QTest::addColumn<int>("valCondition"); 0816 QTest::addColumn<QString>("expectedResult"); 0817 0818 int onEach= 0; 0819 int oneOfThem= 1; 0820 int allOfThem= 2; 0821 int onScalar= 3; 0822 0823 QTest::addRow("cmd1") << QVector<int>({8, 9, 2}) << onEach << 0 << "False"; 0824 QTest::addRow("cmd2") << QVector<int>({2, 2, 2}) << onEach << 2 << "True"; 0825 0826 QTest::addRow("cmd3") << QVector<int>({0, 0, 0}) << oneOfThem << 10 << "False"; 0827 QTest::addRow("cmd4") << QVector<int>({10, 9, 5}) << oneOfThem << 10 << "True"; 0828 QTest::addRow("cmd5") << QVector<int>({9, 9, 9}) << oneOfThem << 9 << "True"; 0829 0830 QTest::addRow("cmd6") << QVector<int>({8, 9, 2}) << allOfThem << 1 << "False"; 0831 QTest::addRow("cmd7") << QVector<int>({8, 9, 2}) << allOfThem << 9 << "False"; 0832 QTest::addRow("cmd8") << QVector<int>({8, 8, 8}) << allOfThem << 8 << "True"; 0833 0834 QTest::addRow("cmd9") << QVector<int>({25, 8, 14}) << onScalar << 1 << "False"; 0835 QTest::addRow("cmd10") << QVector<int>({25, 8, 14}) << onScalar << 47 << "True"; 0836 } 0837 0838 void TestDice::paintTest() {} 0839 void TestDice::paintTest_data() {} 0840 0841 void TestDice::filterTest() 0842 { 0843 QFETCH(QVector<int>, values); 0844 QFETCH(int, condition); 0845 QFETCH(bool, different); 0846 0847 TestNode node; 0848 FilterNode filter; 0849 0850 DiceResult result; 0851 makeResult(result, values); 0852 node.setResult(&result); 0853 0854 auto validator= makeValidator(condition, BooleanCondition::Different); 0855 filter.setValidator(validator); 0856 node.setNextNode(&filter); 0857 0858 node.run(nullptr); 0859 0860 auto list= dynamic_cast<DiceResult*>(filter.getResult())->getResultList(); 0861 0862 auto expected= result.getResultList(); 0863 bool resultDiff= (list.size() != expected.size()); 0864 0865 QCOMPARE(different, resultDiff); 0866 } 0867 0868 void TestDice::filterTest_data() 0869 { 0870 QTest::addColumn<QVector<int>>("values"); 0871 QTest::addColumn<int>("condition"); 0872 QTest::addColumn<bool>("different"); 0873 0874 QTest::addRow("cmd1") << QVector<int>({8, 4, 2}) << 4 << true; 0875 QTest::addRow("cmd2") << QVector<int>({0, 0, 0}) << 1 << false; 0876 } 0877 0878 void TestDice::splitTest() {} 0879 void TestDice::splitTest_data() {} 0880 0881 void TestDice::uniqueTest() {} 0882 void TestDice::uniqueTest_data() {} 0883 0884 void TestDice::groupTest() {} 0885 void TestDice::groupTest_data() {} 0886 0887 void TestDice::bindTest() {} 0888 void TestDice::bindTest_data() {} 0889 0890 void TestDice::occurenceTest() 0891 { 0892 QFETCH(QVector<int>, values); 0893 QFETCH(int, condition); 0894 QFETCH(QString, expected); 0895 0896 TestNode node; 0897 OccurenceCountNode count; 0898 0899 DiceResult result; 0900 makeResult(result, values); 0901 node.setResult(&result); 0902 0903 auto validator= makeValidator(condition, BooleanCondition::GreaterThan); 0904 count.setValidator(validator); 0905 node.setNextNode(&count); 0906 0907 node.run(nullptr); 0908 0909 auto text= dynamic_cast<StringResult*>(count.getResult())->getText(); 0910 0911 QVERIFY(text.startsWith(expected)); 0912 } 0913 void TestDice::occurenceTest_data() 0914 { 0915 QTest::addColumn<QVector<int>>("values"); 0916 QTest::addColumn<int>("condition"); 0917 QTest::addColumn<QString>("expected"); 0918 0919 QTest::addRow("cmd1") << QVector<int>({8, 8, 2}) << 7 << "2x8"; 0920 QTest::addRow("cmd2") << QVector<int>({0, 0, 0}) << 1 << "No matching result"; 0921 } 0922 0923 void TestDice::cleanupTestCase() {} 0924 0925 QTEST_MAIN(TestDice); 0926 0927 #include "tst_dice.moc"