2015-08-08 18:16:33 +02:00
|
|
|
/*
|
|
|
|
* MapComparer.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "MapComparer.h"
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
#include "../lib/mapping/CMap.h"
|
2023-06-02 21:00:44 +02:00
|
|
|
#include "../lib/mapObjects/ObjectTemplate.h"
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
#define VCMI_CHECK_FIELD_EQUAL_P(field) EXPECT_EQ(actual->field, expected->field)
|
2015-08-08 20:04:07 +02:00
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
#define VCMI_CHECK_FIELD_EQUAL(field) EXPECT_EQ(actual.field, expected.field)
|
2015-08-09 13:04:26 +02:00
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
#define VCMI_REQUIRE_FIELD_EQUAL_P(field) ASSERT_EQ(actual->field, expected->field)
|
|
|
|
#define VCMI_REQUIRE_FIELD_EQUAL(field) ASSERT_EQ(actual.field, expected.field)
|
2015-08-09 13:04:26 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
template <class T>
|
|
|
|
void checkEqual(const T & actual, const T & expected)
|
2015-08-08 20:04:07 +02:00
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actual, expected) ;
|
2015-08-08 20:04:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:58:09 +02:00
|
|
|
void checkEqual(const std::vector<bool> & actual, const std::vector<bool> & expected)
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actual.size(), expected.size());
|
2016-02-21 19:58:09 +02:00
|
|
|
|
|
|
|
for(auto actualIt = actual.begin(), expectedIt = expected.begin(); actualIt != actual.end() && expectedIt != expected.end(); actualIt++, expectedIt++)
|
|
|
|
{
|
|
|
|
checkEqual(*actualIt, *expectedIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
template <class Element>
|
|
|
|
void checkEqual(const std::vector<Element> & actual, const std::vector<Element> & expected)
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actual.size(), expected.size());
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
for(auto actualIt = actual.begin(), expectedIt = expected.begin(); actualIt != actual.end() && expectedIt != expected.end(); actualIt++, expectedIt++)
|
|
|
|
{
|
|
|
|
checkEqual(*actualIt, *expectedIt);
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-15 19:25:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Element>
|
|
|
|
void checkEqual(const std::set<Element> & actual, const std::set<Element> & expected)
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actual.size(), expected.size());
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
for(auto elem : expected)
|
|
|
|
{
|
|
|
|
if(!vstd::contains(actual, elem))
|
2017-07-04 13:29:50 +02:00
|
|
|
FAIL() << "Required element not found "+boost::to_string(elem);
|
2015-08-18 01:21:35 +02:00
|
|
|
}
|
2015-08-15 19:25:40 +02:00
|
|
|
}
|
2015-08-09 13:04:26 +02:00
|
|
|
|
2016-02-21 21:17:18 +02:00
|
|
|
void checkEqual(const SHeroName & actual, const SHeroName & expected)
|
|
|
|
{
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(heroId);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(heroName);
|
|
|
|
}
|
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
void checkEqual(const PlayerInfo & actual, const PlayerInfo & expected)
|
2015-08-08 20:04:07 +02:00
|
|
|
{
|
2015-08-09 13:04:26 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(canHumanPlay);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(canComputerPlay);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(aiTactic);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
checkEqual(actual.allowedFactions, expected.allowedFactions);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(isFactionRandom);
|
2015-08-18 01:21:35 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(mainCustomHeroPortrait);
|
2015-08-15 19:25:40 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(mainCustomHeroName);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(mainCustomHeroId);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-21 21:17:18 +02:00
|
|
|
checkEqual(actual.heroesNames, expected.heroesNames);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-15 19:25:40 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(hasMainTown);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(generateHeroAtMainTown);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(posOfMainTown);
|
2023-03-01 16:12:51 +02:00
|
|
|
//VCMI_CHECK_FIELD_EQUAL(team); //TODO: smart team checking, for now team checking is useless, because random team is assigned for single-member teams
|
2015-08-15 19:25:40 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(hasRandomHero);
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
void checkEqual(const EventExpression & actual, const EventExpression & expected)
|
2015-08-15 19:25:40 +02:00
|
|
|
{
|
|
|
|
//todo: checkEventExpression
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
void checkEqual(const TriggeredEvent & actual, const TriggeredEvent & expected)
|
2015-08-15 19:25:40 +02:00
|
|
|
{
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(identifier);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(description);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(onFulfill);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(effect.type);
|
2015-08-18 01:21:35 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL(effect.toOtherMessage);
|
2015-08-15 19:25:40 +02:00
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
checkEqual(actual.trigger, expected.trigger);
|
2015-08-08 20:04:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:58:09 +02:00
|
|
|
void checkEqual(const Rumor & actual, const Rumor & expected)
|
|
|
|
{
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(name);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkEqual(const DisposedHero & actual, const DisposedHero & expected)
|
|
|
|
{
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(heroId);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(portrait);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(name);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(players);
|
2016-02-26 01:03:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkEqual(const ObjectTemplate & actual, const ObjectTemplate & expected)
|
|
|
|
{
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(id);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(subid);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(printPriority);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL(animationFile);
|
|
|
|
//VCMI_CHECK_FIELD_EQUAL(stringID);
|
2016-02-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 00:29:21 +02:00
|
|
|
void checkEqual(const TerrainTile & actual, const TerrainTile & expected)
|
|
|
|
{
|
|
|
|
//fatal fail here on any error
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(terType);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(terView);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(riverType);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(riverDir);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(roadType);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(roadDir);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(extTileFlags);
|
2016-02-23 22:46:33 +02:00
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
ASSERT_EQ(actual.blockingObjects.size(), expected.blockingObjects.size());
|
|
|
|
ASSERT_EQ(actual.visitableObjects.size(), expected.visitableObjects.size());
|
2016-02-23 22:46:33 +02:00
|
|
|
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(visitable);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL(blocked);
|
2015-08-16 00:29:21 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
//MapComparer
|
2015-08-08 20:04:07 +02:00
|
|
|
void MapComparer::compareHeader()
|
|
|
|
{
|
2015-08-18 01:21:35 +02:00
|
|
|
//map size parameters are vital for further checks
|
2015-08-16 00:29:21 +02:00
|
|
|
VCMI_REQUIRE_FIELD_EQUAL_P(height);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL_P(width);
|
|
|
|
VCMI_REQUIRE_FIELD_EQUAL_P(twoLevel);
|
2015-08-14 04:22:24 +02:00
|
|
|
|
2015-08-09 13:04:26 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(name);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(description);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(difficulty);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(levelLimit);
|
|
|
|
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(victoryMessage);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(defeatMessage);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(victoryIconIndex);
|
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(defeatIconIndex);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-02-21 21:17:18 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(howManyTeams);
|
|
|
|
|
2015-08-18 01:21:35 +02:00
|
|
|
checkEqual(actual->players, expected->players);
|
|
|
|
|
2016-02-21 21:13:20 +02:00
|
|
|
checkEqual(actual->allowedHeroes, expected->allowedHeroes);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-16 00:46:58 +02:00
|
|
|
std::vector<TriggeredEvent> actualEvents = actual->triggeredEvents;
|
|
|
|
std::vector<TriggeredEvent> expectedEvents = expected->triggeredEvents;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-16 00:46:58 +02:00
|
|
|
auto sortByIdentifier = [](const TriggeredEvent & lhs, const TriggeredEvent & rhs) -> bool
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
return lhs.identifier < rhs.identifier;
|
2015-08-16 00:46:58 +02:00
|
|
|
};
|
|
|
|
boost::sort (actualEvents, sortByIdentifier);
|
|
|
|
boost::sort (expectedEvents, sortByIdentifier);
|
2015-08-18 01:21:35 +02:00
|
|
|
|
|
|
|
checkEqual(actualEvents, expectedEvents);
|
2015-08-08 20:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapComparer::compareOptions()
|
|
|
|
{
|
2016-02-21 19:58:09 +02:00
|
|
|
checkEqual(actual->rumors, expected->rumors);
|
|
|
|
checkEqual(actual->disposedHeroes, expected->disposedHeroes);
|
|
|
|
//todo: compareOptions predefinedHeroes
|
|
|
|
|
|
|
|
checkEqual(actual->allowedAbilities, expected->allowedAbilities);
|
|
|
|
checkEqual(actual->allowedArtifact, expected->allowedArtifact);
|
|
|
|
checkEqual(actual->allowedSpell, expected->allowedSpell);
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
//todo: compareOptions events
|
2015-08-08 20:04:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-13 16:47:47 +02:00
|
|
|
void MapComparer::compareObject(const CGObjectInstance * actual, const CGObjectInstance * expected)
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actual->instanceName, expected->instanceName);
|
|
|
|
EXPECT_EQ(typeid(actual).name(), typeid(expected).name());//todo: remove and use just comparison
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2016-02-14 11:13:30 +02:00
|
|
|
std::string actualFullID = boost::to_string(boost::format("%s(%d)|%s(%d) %d") % actual->typeName % actual->ID % actual->subTypeName % actual->subID % actual->tempOwner);
|
|
|
|
std::string expectedFullID = boost::to_string(boost::format("%s(%d)|%s(%d) %d") % expected->typeName % expected->ID % expected->subTypeName % expected->subID % expected->tempOwner);
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actualFullID, expectedFullID);
|
2016-02-22 18:26:42 +02:00
|
|
|
|
2016-02-26 01:03:14 +02:00
|
|
|
VCMI_CHECK_FIELD_EQUAL_P(pos);
|
2023-03-01 16:12:31 +02:00
|
|
|
checkEqual(*actual->appearance, *expected->appearance);
|
2015-11-13 16:47:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 20:04:07 +02:00
|
|
|
void MapComparer::compareObjects()
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
EXPECT_EQ(actual->objects.size(), expected->objects.size());
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-02-22 18:26:42 +02:00
|
|
|
for(size_t idx = 0; idx < expected->objects.size(); idx++)
|
2015-11-13 16:47:47 +02:00
|
|
|
{
|
2016-02-22 18:26:42 +02:00
|
|
|
auto expectedObject = expected->objects[idx];
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
ASSERT_EQ(idx, expectedObject->id.getNum());
|
2016-02-22 18:26:42 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
auto it = expected->instanceNames.find(expectedObject->instanceName);
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
ASSERT_NE(it, expected->instanceNames.end());
|
2016-02-22 18:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto it = actual->instanceNames.find(expectedObject->instanceName);
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
ASSERT_NE(it, actual->instanceNames.end());
|
2016-02-22 18:26:42 +02:00
|
|
|
|
|
|
|
auto actualObject = it->second;
|
|
|
|
|
|
|
|
compareObject(actualObject, expectedObject);
|
|
|
|
}
|
2015-11-13 16:47:47 +02:00
|
|
|
}
|
2015-08-08 20:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapComparer::compareTerrain()
|
|
|
|
{
|
2015-08-16 00:29:21 +02:00
|
|
|
//assume map dimensions check passed
|
|
|
|
//todo: separate check for underground
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-16 00:29:21 +02:00
|
|
|
for(int x = 0; x < expected->width; x++)
|
|
|
|
for(int y = 0; y < expected->height; y++)
|
|
|
|
{
|
|
|
|
int3 coord(x,y,0);
|
2017-08-11 19:03:05 +02:00
|
|
|
SCOPED_TRACE(coord.toString());
|
2015-08-16 00:29:21 +02:00
|
|
|
checkEqual(actual->getTile(coord), expected->getTile(coord));
|
|
|
|
}
|
2015-08-08 20:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapComparer::compare()
|
|
|
|
{
|
2017-07-04 13:29:50 +02:00
|
|
|
ASSERT_NE((void *) actual, (void *) expected); //should not point to the same object
|
|
|
|
ASSERT_TRUE(actual != nullptr) << "Actual map is not defined";
|
|
|
|
ASSERT_TRUE(expected != nullptr) << "Expected map is not defined";
|
2015-08-08 20:04:07 +02:00
|
|
|
|
|
|
|
compareHeader();
|
|
|
|
compareOptions();
|
2015-08-09 13:04:26 +02:00
|
|
|
compareObjects();
|
2015-08-08 20:04:07 +02:00
|
|
|
compareTerrain();
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:29:50 +02:00
|
|
|
void MapComparer::operator() (const std::unique_ptr<CMap>& actual, const std::unique_ptr<CMap>& expected)
|
2015-08-08 18:16:33 +02:00
|
|
|
{
|
2015-08-08 20:04:07 +02:00
|
|
|
this->actual = actual.get();
|
|
|
|
this->expected = expected.get();
|
|
|
|
compare();
|
2015-08-08 18:16:33 +02:00
|
|
|
}
|