File indexing completed on 2024-07-21 03:47:10

0001 #include "enginetest.h"
0002 
0003 #include "renderer.h"
0004 
0005 #include <KGlobal>
0006 #include <KStandardDirs>
0007 
0008 
0009 Killbots::EngineTest::EngineTest(QObject *parent)
0010     : QObject(parent)
0011 {
0012     KGlobal::dirs()->addResourceDir("ruleset", KDESRCDIR "/../rulesets/");
0013     KGlobal::dirs()->addResourceDir("appdata", KDESRCDIR "/../");
0014 
0015     m_coordinator = new Coordinator(this);
0016     m_coordinator->setAnimationSpeed(0);
0017 
0018     m_engine = new Engine(m_coordinator, this);
0019     m_engine->setRuleset(Ruleset::load("default.desktop"));
0020     m_coordinator->setEngine(m_engine);
0021 
0022     m_scene = new Scene(this);
0023     m_coordinator->setScene(m_scene);
0024 }
0025 
0026 Killbots::EngineTest::~EngineTest()
0027 {
0028     Killbots::Renderer::cleanup();
0029 }
0030 
0031 void Killbots::EngineTest::testValidCells_data()
0032 {
0033     QTest::addColumn<int>("row");
0034     QTest::addColumn<int>("column");
0035     QTest::addColumn<bool>("valid");
0036 
0037     int rowCount = m_engine->ruleset()->rows();
0038     int columnCount = m_engine->ruleset()->columns();
0039 
0040     for (int r = 0; r < rowCount; ++r) {
0041         QTest::newRow("Cell to left of grid") << r << -1 << false;
0042         for (int c = 0; c < columnCount; ++c) {
0043             const QString description = QString("Row %1. column %2").arg(r).arg(c);
0044             QTest::newRow("Cell inside grid") << r << c << true;
0045         }
0046         QTest::newRow("Cell to right of grid") << r << columnCount << false;
0047     }
0048 
0049     for (int c = -1; c <= columnCount; ++c) {
0050         QTest::newRow("Cell above grid") << -1 << c << false;
0051         QTest::newRow("Cell below grid") << rowCount << c << false;
0052     }
0053 }
0054 
0055 void Killbots::EngineTest::testValidCells()
0056 {
0057     QFETCH(int, row);
0058     QFETCH(int, column);
0059 
0060     QTEST(m_engine->cellIsValid(QPoint(column, row)), "valid");
0061 }
0062 
0063 void Killbots::EngineTest::testRandomEmptyCell_data()
0064 {
0065     m_engine->startNewGame();
0066 }
0067 
0068 void Killbots::EngineTest::testRandomEmptyCell()
0069 {
0070     for (int i = 0; i < 100; ++i) {
0071         QPoint point = m_engine->randomEmptyCell();
0072         QVERIFY(m_engine->cellIsValid(point));
0073         QVERIFY(m_engine->spriteTypeAt(point) == NoSprite);
0074     }
0075 }
0076 
0077 void Killbots::EngineTest::testMoveIsValid_data()
0078 {
0079     //0123456789012345
0080     QString layout = "  j             \n" // 0
0081                      " r      j     jj\n" // 1
0082                      "  f    j        \n" // 2
0083                      "rjjj            \n" // 3
0084                      "                \n" // 4
0085                      "                \n" // 5
0086                      "                \n" // 6
0087                      "                \n" // 7
0088                      "                \n" // 8
0089                      "                \n" // 9
0090                      "                \n" //10
0091                      "                \n" //11
0092                      "                \n" //12
0093                      "                \n" //13
0094                      "                \n" //14
0095                      "                ";  //15
0096     m_engine->startNewRound(false, layout);
0097     const Ruleset *rules = m_engine->ruleset();
0098 
0099     QTest::addColumn<int>("row");
0100     QTest::addColumn<int>("column");
0101     QTest::addColumn<int>("action");
0102     QTest::addColumn<bool>("valid");
0103 
0104     QTest::newRow("Leaving the grid by the corner") << -1 <<  -1 << int(UpRight)  << false;
0105     QTest::newRow("Leaving the grid by the edge")   << -1 <<   8 << int(Up)       << false;
0106     QTest::newRow("Leaving the grid by teleport")   << 25 << -12 << int(Teleport) << false;
0107 
0108     QTest::newRow("Moving onto a robot")   << 1 << 1 << int(Right) << false;
0109     QTest::newRow("Moving onto a fastbot") << 2 << 2 << int(Down)  << false;
0110 
0111     QTest::newRow("Pushing a junkheap")                    << 0 <<  2 << int(Left)     << (rules->pushableJunkheaps() != Ruleset::None);
0112     QTest::newRow("Pushing two junkheaps")                 << 1 <<  8 << int(DownLeft) << (rules->pushableJunkheaps() == Ruleset::Many);
0113     QTest::newRow("Pushing a junkheap out of the grid")    << 0 <<  2 << int(Up)       << false;
0114     QTest::newRow("Pushing two junkheaps out of the grid") << 1 << 14 << int(Right)    << false;
0115     QTest::newRow("Pushing a junkheap onto a fastbot")     << 3 <<  2 << int(Up)       << (rules->pushableJunkheaps() != Ruleset::None && rules->squaskKillsEnabled());
0116     QTest::newRow("Pushing three junkheaps onto a robot")  << 3 <<  3 << int(Left)     << (rules->pushableJunkheaps() == Ruleset::Many && rules->squaskKillsEnabled());
0117 }
0118 
0119 void Killbots::EngineTest::testMoveIsValid()
0120 {
0121     QFETCH(int, row);
0122     QFETCH(int, column);
0123     QFETCH(int, action);
0124 
0125     QTEST(m_engine->moveIsValid(QPoint(column, row), HeroAction(action)), "valid");
0126 }
0127 
0128 void Killbots::EngineTest::testMoveIsSafe_data()
0129 {
0130     //0123456789012345
0131     QString layout = "----- -------   \n" // 0
0132                      "-!!!- -!!!!!-   \n" // 1
0133                      "-!r!- -!!!!!-   \n" // 2
0134                      "-!!!- -!!f!!-   \n" // 3
0135                      "----- -!!!!!-   \n" // 4
0136                      "      -!!!!!-   \n" // 5
0137                      "      -------   \n" // 6
0138                      "frj    j        \n" // 7
0139                      "      r         \n" // 8
0140                      "     f     j    \n" // 9
0141                      "                \n" //10
0142                      " f f  ff  f     \n" //11
0143                      "r   r  jjj      \n" //12
0144                      "  - f fj-j      \n" //13
0145                      "f   r  jjjf     \n" //14
0146                      " rf   f   f     ";  //15
0147     m_engine->startNewRound(false, layout);
0148 
0149     QTest::addColumn<int>("row");
0150     QTest::addColumn<int>("column");
0151     QTest::addColumn<int>("action");
0152     QTest::addColumn<bool>("safe");
0153 
0154     // Proximity of a robot
0155     QTest::newRow("Cell above above right right of robot") << 0 << 0 << int(Hold) << true;
0156     QTest::newRow("Cell above above right of robot")       << 0 << 1 << int(Hold) << true;
0157     QTest::newRow("Cell above above robot")                << 0 << 2 << int(Hold) << true;
0158     QTest::newRow("Cell above above left of robot")        << 0 << 3 << int(Hold) << true;
0159     QTest::newRow("Cell above above left left of robot")   << 0 << 4 << int(Hold) << true;
0160     QTest::newRow("Cell above right right of robot")       << 1 << 0 << int(Hold) << true;
0161     QTest::newRow("Cell above right of robot")             << 1 << 1 << int(Hold) << false;
0162     QTest::newRow("Cell above robot")                      << 1 << 2 << int(Hold) << false;
0163     QTest::newRow("Cell above left of robot")              << 1 << 3 << int(Hold) << false;
0164     QTest::newRow("Cell above left left of robot")         << 1 << 4 << int(Hold) << true;
0165     QTest::newRow("Cell right right of robot")             << 2 << 0 << int(Hold) << true;
0166     QTest::newRow("Cell right of robot")                   << 2 << 1 << int(Hold) << false;
0167     QTest::newRow("Cell left of robot")                    << 2 << 3 << int(Hold) << false;
0168     QTest::newRow("Cell left left robot")                  << 2 << 4 << int(Hold) << true;
0169     QTest::newRow("Cell below right right of robot")       << 3 << 0 << int(Hold) << true;
0170     QTest::newRow("Cell below right of robot")             << 3 << 1 << int(Hold) << false;
0171     QTest::newRow("Cell below robot")                      << 3 << 2 << int(Hold) << false;
0172     QTest::newRow("Cell below left of robot")              << 3 << 3 << int(Hold) << false;
0173     QTest::newRow("Cell below left left of robot")         << 3 << 4 << int(Hold) << true;
0174     QTest::newRow("Cell below below right right of robot") << 4 << 0 << int(Hold) << true;
0175     QTest::newRow("Cell below below right of robot")       << 4 << 1 << int(Hold) << true;
0176     QTest::newRow("Cell below below robot")                << 4 << 2 << int(Hold) << true;
0177     QTest::newRow("Cell below below left of robot")        << 4 << 3 << int(Hold) << true;
0178     QTest::newRow("Cell below below left left of robot")   << 4 << 4 << int(Hold) << true;
0179 
0180     // Proximity of a fastbot
0181     QTest::newRow("Cell above above above right right right of fastbot") << 0 <<  6 << int(Hold) << true;
0182     QTest::newRow("Cell above above above right right of fastbot")       << 0 <<  7 << int(Hold) << true;
0183     QTest::newRow("Cell above above above right of fastbot")             << 0 <<  8 << int(Hold) << true;
0184     QTest::newRow("Cell above above above fastbot")                      << 0 <<  9 << int(Hold) << true;
0185     QTest::newRow("Cell above above above left of fastbot")              << 0 << 10 << int(Hold) << true;
0186     QTest::newRow("Cell above above above left left of fastbot")         << 0 << 11 << int(Hold) << true;
0187     QTest::newRow("Cell above above above left left left of fastbot")    << 0 << 12 << int(Hold) << true;
0188     QTest::newRow("Cell above above right right right of fastbot")       << 1 <<  6 << int(Hold) << true;
0189     QTest::newRow("Cell above above right right of fastbot")             << 1 <<  7 << int(Hold) << false;
0190     QTest::newRow("Cell above above right of fastbot")                   << 1 <<  8 << int(Hold) << false;
0191     QTest::newRow("Cell above above fastbot")                            << 1 <<  9 << int(Hold) << false;
0192     QTest::newRow("Cell above above left of fastbot")                    << 1 << 10 << int(Hold) << false;
0193     QTest::newRow("Cell above above left left of fastbot")               << 1 << 11 << int(Hold) << false;
0194     QTest::newRow("Cell above above left left left of fastbot")          << 1 << 12 << int(Hold) << true;
0195     QTest::newRow("Cell above right right right of fastbot")             << 2 <<  6 << int(Hold) << true;
0196     QTest::newRow("Cell above right right of fastbot")                   << 2 <<  7 << int(Hold) << false;
0197     QTest::newRow("Cell above right of fastbot")                         << 2 <<  8 << int(Hold) << false;
0198     QTest::newRow("Cell above fastbot")                                  << 2 <<  9 << int(Hold) << false;
0199     QTest::newRow("Cell above left of fastbot")                          << 2 << 10 << int(Hold) << false;
0200     QTest::newRow("Cell above left left of fastbot")                     << 2 << 11 << int(Hold) << false;
0201     QTest::newRow("Cell above left left left of fastbot")                << 2 << 12 << int(Hold) << true;
0202     QTest::newRow("Cell right right right of fastbot")                   << 3 <<  6 << int(Hold) << true;
0203     QTest::newRow("Cell right right of fastbot")                         << 3 <<  7 << int(Hold) << false;
0204     QTest::newRow("Cell right of fastbot")                               << 3 <<  8 << int(Hold) << false;
0205     QTest::newRow("Cell left of fastbot")                                << 3 << 10 << int(Hold) << false;
0206     QTest::newRow("Cell left left of fastbot")                           << 3 << 11 << int(Hold) << false;
0207     QTest::newRow("Cell left left left of fastbot")                      << 3 << 12 << int(Hold) << true;
0208     QTest::newRow("Cell below right right right of fastbot")             << 4 <<  6 << int(Hold) << true;
0209     QTest::newRow("Cell below right right of fastbot")                   << 4 <<  7 << int(Hold) << false;
0210     QTest::newRow("Cell below right of fastbot")                         << 4 <<  8 << int(Hold) << false;
0211     QTest::newRow("Cell below fastbot")                                  << 4 <<  9 << int(Hold) << false;
0212     QTest::newRow("Cell below left of fastbot")                          << 4 << 10 << int(Hold) << false;
0213     QTest::newRow("Cell below left left of fastbot")                     << 4 << 11 << int(Hold) << false;
0214     QTest::newRow("Cell below left left left of fastbot")                << 4 << 12 << int(Hold) << true;
0215     QTest::newRow("Cell below below right right right of fastbot")       << 5 <<  6 << int(Hold) << true;
0216     QTest::newRow("Cell below below right right of fastbot")             << 5 <<  7 << int(Hold) << false;
0217     QTest::newRow("Cell below below right of fastbot")                   << 5 <<  8 << int(Hold) << false;
0218     QTest::newRow("Cell below below fastbot")                            << 5 <<  9 << int(Hold) << false;
0219     QTest::newRow("Cell below below left of fastbot")                    << 5 << 10 << int(Hold) << false;
0220     QTest::newRow("Cell below below left left of fastbot")               << 5 << 11 << int(Hold) << false;
0221     QTest::newRow("Cell below below left left left of fastbot")          << 5 << 12 << int(Hold) << true;
0222     QTest::newRow("Cell below below below right right right of fastbot") << 6 <<  6 << int(Hold) << true;
0223     QTest::newRow("Cell below below below right right of fastbot")       << 6 <<  7 << int(Hold) << true;
0224     QTest::newRow("Cell below below below right of fastbot")             << 6 <<  8 << int(Hold) << true;
0225     QTest::newRow("Cell below below below fastbot")                      << 6 <<  9 << int(Hold) << true;
0226     QTest::newRow("Cell below below below left of fastbot")              << 6 << 10 << int(Hold) << true;
0227     QTest::newRow("Cell below below below left left of fastbot")         << 6 << 11 << int(Hold) << true;
0228     QTest::newRow("Cell below below below left left left of fastbot")    << 6 << 12 << int(Hold) << true;
0229 
0230     QTest::newRow("Cell with fastbots blocked by junkheaps") << 13 << 2 << int(Hold) << true;
0231 
0232     QTest::newRow("Cell with fastbots that will collide with other bots") << 13 << 8 << int(Hold) << true;
0233 
0234     QTest::newRow("Pushing junkheap horizontally as a blocker") << 7 <<  2 << int(Left)     << true;
0235     QTest::newRow("Pushing junkheap diagonally as a blocker")   << 7 <<  7 << int(DownLeft) << true;
0236     QTest::newRow("Pushing junkheap vertically as a blocker")   << 9 << 11 << int(Down)     << true;
0237 
0238     QTest::newRow("Pushing junkheap diagonally without blocking")   << 7 <<  2 << int(UpLeft)   << false;
0239     QTest::newRow("Pushing junkheap diagonally without blocking")   << 9 << 11 << int(DownLeft) << false;
0240     QTest::newRow("Pushing junkheap horizontally without blocking") << 7 <<  7 << int(Left)     << false;
0241     QTest::newRow("Pushing junkheap vertically without blocking")   << 7 <<  7 << int(Down)     << false;
0242 }
0243 
0244 void Killbots::EngineTest::testMoveIsSafe()
0245 {
0246     QFETCH(int, row);
0247     QFETCH(int, column);
0248     QFETCH(int, action);
0249 
0250     QTEST(m_engine->moveIsSafe(QPoint(column, row), HeroAction(action)), "safe");
0251 }
0252 
0253 QTEST_MAIN(Killbots::EngineTest)
0254 
0255 #include "enginetest.moc"
0256 #include "moc_enginetest.cpp"