CMapFormatTest.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * CMapFormatTest.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 "../lib/filesystem/CMemoryBuffer.h"
  13. #include "../lib/mapping/CMap.h"
  14. #include "../lib/rmg/CMapGenOptions.h"
  15. #include "../lib/rmg/CMapGenerator.h"
  16. #include "../lib/mapping/MapFormatJson.h"
  17. #include "../lib/VCMIDirs.h"
  18. #include "MapComparer.h"
  19. static const int TEST_RANDOM_SEED = 1337;
  20. static std::unique_ptr<CMap> initialMap;
  21. class CMapTestFixture
  22. {
  23. public:
  24. CMapTestFixture()
  25. {
  26. CMapGenOptions opt;
  27. opt.setHeight(CMapHeader::MAP_SIZE_LARGE);
  28. opt.setWidth(CMapHeader::MAP_SIZE_LARGE);
  29. opt.setHasTwoLevels(true);
  30. opt.setPlayerCount(4);
  31. opt.setPlayerTypeForStandardPlayer(PlayerColor(0), EPlayerType::HUMAN);
  32. opt.setPlayerTypeForStandardPlayer(PlayerColor(1), EPlayerType::AI);
  33. opt.setPlayerTypeForStandardPlayer(PlayerColor(2), EPlayerType::AI);
  34. opt.setPlayerTypeForStandardPlayer(PlayerColor(3), EPlayerType::AI);
  35. CMapGenerator gen;
  36. initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
  37. initialMap->name = "Test";
  38. };
  39. ~CMapTestFixture()
  40. {
  41. initialMap.reset();
  42. };
  43. };
  44. BOOST_GLOBAL_FIXTURE(CMapTestFixture);
  45. BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple)
  46. {
  47. logGlobal->info("CMapFormatVCMI_Simple start");
  48. BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple start");
  49. CMemoryBuffer serializeBuffer;
  50. {
  51. CMapSaverJson saver(&serializeBuffer);
  52. saver.saveMap(initialMap);
  53. }
  54. BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple serialized");
  55. #if 1
  56. {
  57. auto path = VCMIDirs::get().userDataPath()/"test.vmap";
  58. boost::filesystem::remove(path);
  59. boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
  60. tmp.write((const char *)serializeBuffer.getBuffer().data(),serializeBuffer.getSize());
  61. tmp.flush();
  62. tmp.close();
  63. logGlobal->infoStream() << "Test map has been saved to " << path;
  64. }
  65. BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple saved");
  66. #endif // 1
  67. serializeBuffer.seek(0);
  68. {
  69. CMapLoaderJson loader(&serializeBuffer);
  70. std::unique_ptr<CMap> serialized = loader.loadMap();
  71. MapComparer c;
  72. c(serialized, initialMap);
  73. }
  74. logGlobal->info("CMapFormatVCMI_Simple finish");
  75. }