MapComparer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * MapComparer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include <boost/test/unit_test.hpp>
  12. #include "MapComparer.h"
  13. #define VCMI_CHECK_FIELD_EQUAL_P(field) BOOST_CHECK_EQUAL(actual->field, expected->field)
  14. #define VCMI_CHECK_FIELD_EQUAL(field) BOOST_CHECK_EQUAL(actual.field, expected.field)
  15. #define VCMI_REQUIRE_FIELD_EQUAL_P(field) BOOST_REQUIRE_EQUAL(actual->field, expected->field)
  16. #define VCMI_REQUIRE_FIELD_EQUAL(field) BOOST_REQUIRE_EQUAL(actual.field, expected.field)
  17. template <class T>
  18. void checkEqual(const T & actual, const T & expected)
  19. {
  20. BOOST_CHECK_EQUAL(actual, expected) ;
  21. }
  22. void checkEqual(const std::vector<bool> & actual, const std::vector<bool> & expected)
  23. {
  24. BOOST_CHECK_EQUAL(actual.size(), expected.size());
  25. for(auto actualIt = actual.begin(), expectedIt = expected.begin(); actualIt != actual.end() && expectedIt != expected.end(); actualIt++, expectedIt++)
  26. {
  27. checkEqual(*actualIt, *expectedIt);
  28. }
  29. }
  30. template <class Element>
  31. void checkEqual(const std::vector<Element> & actual, const std::vector<Element> & expected)
  32. {
  33. BOOST_CHECK_EQUAL(actual.size(), expected.size());
  34. for(auto actualIt = actual.begin(), expectedIt = expected.begin(); actualIt != actual.end() && expectedIt != expected.end(); actualIt++, expectedIt++)
  35. {
  36. checkEqual(*actualIt, *expectedIt);
  37. }
  38. }
  39. template <class Element>
  40. void checkEqual(const std::set<Element> & actual, const std::set<Element> & expected)
  41. {
  42. BOOST_CHECK_EQUAL(actual.size(), expected.size());
  43. for(auto elem : expected)
  44. {
  45. if(!vstd::contains(actual, elem))
  46. BOOST_ERROR("Required element not found "+boost::to_string(elem));
  47. }
  48. }
  49. void checkEqual(const SHeroName & actual, const SHeroName & expected)
  50. {
  51. VCMI_CHECK_FIELD_EQUAL(heroId);
  52. VCMI_CHECK_FIELD_EQUAL(heroName);
  53. }
  54. void checkEqual(const PlayerInfo & actual, const PlayerInfo & expected)
  55. {
  56. VCMI_CHECK_FIELD_EQUAL(canHumanPlay);
  57. VCMI_CHECK_FIELD_EQUAL(canComputerPlay);
  58. VCMI_CHECK_FIELD_EQUAL(aiTactic);
  59. checkEqual(actual.allowedFactions, expected.allowedFactions);
  60. VCMI_CHECK_FIELD_EQUAL(isFactionRandom);
  61. VCMI_CHECK_FIELD_EQUAL(mainCustomHeroPortrait);
  62. VCMI_CHECK_FIELD_EQUAL(mainCustomHeroName);
  63. VCMI_CHECK_FIELD_EQUAL(mainCustomHeroId);
  64. checkEqual(actual.heroesNames, expected.heroesNames);
  65. VCMI_CHECK_FIELD_EQUAL(hasMainTown);
  66. VCMI_CHECK_FIELD_EQUAL(generateHeroAtMainTown);
  67. VCMI_CHECK_FIELD_EQUAL(posOfMainTown);
  68. VCMI_CHECK_FIELD_EQUAL(team);
  69. VCMI_CHECK_FIELD_EQUAL(hasRandomHero);
  70. }
  71. void checkEqual(const EventExpression & actual, const EventExpression & expected)
  72. {
  73. //todo: checkEventExpression
  74. }
  75. void checkEqual(const TriggeredEvent & actual, const TriggeredEvent & expected)
  76. {
  77. VCMI_CHECK_FIELD_EQUAL(identifier);
  78. VCMI_CHECK_FIELD_EQUAL(description);
  79. VCMI_CHECK_FIELD_EQUAL(onFulfill);
  80. VCMI_CHECK_FIELD_EQUAL(effect.type);
  81. VCMI_CHECK_FIELD_EQUAL(effect.toOtherMessage);
  82. checkEqual(actual.trigger, expected.trigger);
  83. }
  84. void checkEqual(const Rumor & actual, const Rumor & expected)
  85. {
  86. VCMI_CHECK_FIELD_EQUAL(name);
  87. VCMI_CHECK_FIELD_EQUAL(text);
  88. }
  89. void checkEqual(const DisposedHero & actual, const DisposedHero & expected)
  90. {
  91. VCMI_CHECK_FIELD_EQUAL(heroId);
  92. VCMI_CHECK_FIELD_EQUAL(portrait);
  93. VCMI_CHECK_FIELD_EQUAL(name);
  94. VCMI_CHECK_FIELD_EQUAL(players);
  95. }
  96. void checkEqual(const ObjectTemplate & actual, const ObjectTemplate & expected)
  97. {
  98. VCMI_CHECK_FIELD_EQUAL(id);
  99. VCMI_CHECK_FIELD_EQUAL(subid);
  100. VCMI_CHECK_FIELD_EQUAL(printPriority);
  101. VCMI_CHECK_FIELD_EQUAL(animationFile);
  102. //VCMI_CHECK_FIELD_EQUAL(stringID);
  103. }
  104. void checkEqual(const TerrainTile & actual, const TerrainTile & expected)
  105. {
  106. //fatal fail here on any error
  107. VCMI_REQUIRE_FIELD_EQUAL(terType);
  108. VCMI_REQUIRE_FIELD_EQUAL(terView);
  109. VCMI_REQUIRE_FIELD_EQUAL(riverType);
  110. VCMI_REQUIRE_FIELD_EQUAL(riverDir);
  111. VCMI_REQUIRE_FIELD_EQUAL(roadType);
  112. VCMI_REQUIRE_FIELD_EQUAL(roadDir);
  113. VCMI_REQUIRE_FIELD_EQUAL(extTileFlags);
  114. BOOST_REQUIRE_EQUAL(actual.blockingObjects.size(), expected.blockingObjects.size());
  115. BOOST_REQUIRE_EQUAL(actual.visitableObjects.size(), expected.visitableObjects.size());
  116. VCMI_REQUIRE_FIELD_EQUAL(visitable);
  117. VCMI_REQUIRE_FIELD_EQUAL(blocked);
  118. }
  119. void MapComparer::compareHeader()
  120. {
  121. //map size parameters are vital for further checks
  122. VCMI_REQUIRE_FIELD_EQUAL_P(height);
  123. VCMI_REQUIRE_FIELD_EQUAL_P(width);
  124. VCMI_REQUIRE_FIELD_EQUAL_P(twoLevel);
  125. VCMI_CHECK_FIELD_EQUAL_P(name);
  126. VCMI_CHECK_FIELD_EQUAL_P(description);
  127. VCMI_CHECK_FIELD_EQUAL_P(difficulty);
  128. VCMI_CHECK_FIELD_EQUAL_P(levelLimit);
  129. VCMI_CHECK_FIELD_EQUAL_P(victoryMessage);
  130. VCMI_CHECK_FIELD_EQUAL_P(defeatMessage);
  131. VCMI_CHECK_FIELD_EQUAL_P(victoryIconIndex);
  132. VCMI_CHECK_FIELD_EQUAL_P(defeatIconIndex);
  133. VCMI_CHECK_FIELD_EQUAL_P(howManyTeams);
  134. checkEqual(actual->players, expected->players);
  135. checkEqual(actual->allowedHeroes, expected->allowedHeroes);
  136. std::vector<TriggeredEvent> actualEvents = actual->triggeredEvents;
  137. std::vector<TriggeredEvent> expectedEvents = expected->triggeredEvents;
  138. auto sortByIdentifier = [](const TriggeredEvent & lhs, const TriggeredEvent & rhs) -> bool
  139. {
  140. return lhs.identifier < rhs.identifier;
  141. };
  142. boost::sort (actualEvents, sortByIdentifier);
  143. boost::sort (expectedEvents, sortByIdentifier);
  144. checkEqual(actualEvents, expectedEvents);
  145. }
  146. void MapComparer::compareOptions()
  147. {
  148. checkEqual(actual->rumors, expected->rumors);
  149. checkEqual(actual->disposedHeroes, expected->disposedHeroes);
  150. //todo: compareOptions predefinedHeroes
  151. checkEqual(actual->allowedAbilities, expected->allowedAbilities);
  152. checkEqual(actual->allowedArtifact, expected->allowedArtifact);
  153. checkEqual(actual->allowedSpell, expected->allowedSpell);
  154. //checkEqual(actual->allowedAbilities, expected->allowedAbilities);
  155. //todo: compareOptions events
  156. }
  157. void MapComparer::compareObject(const CGObjectInstance * actual, const CGObjectInstance * expected)
  158. {
  159. BOOST_CHECK_EQUAL(actual->instanceName, expected->instanceName);
  160. BOOST_CHECK_EQUAL(typeid(actual).name(), typeid(expected).name());//todo: remove and use just comparison
  161. std::string actualFullID = boost::to_string(boost::format("%s(%d)|%s(%d) %d") % actual->typeName % actual->ID % actual->subTypeName % actual->subID % actual->tempOwner);
  162. std::string expectedFullID = boost::to_string(boost::format("%s(%d)|%s(%d) %d") % expected->typeName % expected->ID % expected->subTypeName % expected->subID % expected->tempOwner);
  163. BOOST_CHECK_EQUAL(actualFullID, expectedFullID);
  164. VCMI_CHECK_FIELD_EQUAL_P(pos);
  165. checkEqual(actual->appearance, expected->appearance);
  166. }
  167. void MapComparer::compareObjects()
  168. {
  169. BOOST_CHECK_EQUAL(actual->objects.size(), expected->objects.size());
  170. for(size_t idx = 0; idx < expected->objects.size(); idx++)
  171. {
  172. auto expectedObject = expected->objects[idx];
  173. BOOST_REQUIRE_EQUAL(idx, expectedObject->id.getNum());
  174. {
  175. auto it = expected->instanceNames.find(expectedObject->instanceName);
  176. BOOST_REQUIRE(it != expected->instanceNames.end());
  177. }
  178. {
  179. auto it = actual->instanceNames.find(expectedObject->instanceName);
  180. BOOST_REQUIRE(it != actual->instanceNames.end());
  181. auto actualObject = it->second;
  182. compareObject(actualObject, expectedObject);
  183. }
  184. }
  185. }
  186. void MapComparer::compareTerrain()
  187. {
  188. //assume map dimensions check passed
  189. //todo: separate check for underground
  190. for(int x = 0; x < expected->width; x++)
  191. for(int y = 0; y < expected->height; y++)
  192. {
  193. int3 coord(x,y,0);
  194. BOOST_TEST_CHECKPOINT(coord);
  195. checkEqual(actual->getTile(coord), expected->getTile(coord));
  196. }
  197. }
  198. void MapComparer::compare()
  199. {
  200. BOOST_REQUIRE_NE((void *) actual, (void *) expected); //should not point to the same object
  201. BOOST_REQUIRE_MESSAGE(actual != nullptr, "Actual map is not defined");
  202. BOOST_REQUIRE_MESSAGE(expected != nullptr, "Expected map is not defined");
  203. compareHeader();
  204. compareOptions();
  205. compareObjects();
  206. compareTerrain();
  207. }
  208. void MapComparer::operator() (const std::unique_ptr<CMap>& actual, const std::unique_ptr<CMap>& expected)
  209. {
  210. this->actual = actual.get();
  211. this->expected = expected.get();
  212. compare();
  213. }