CMapFormatTest.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_MIDDLE);
  28. opt.setWidth(CMapHeader::MAP_SIZE_MIDDLE);
  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->info("Test map has been saved to:");
  64. logGlobal->info(path.string());
  65. }
  66. BOOST_TEST_CHECKPOINT("CMapFormatVCMI_Simple saved");
  67. #endif // 1
  68. serializeBuffer.seek(0);
  69. {
  70. CMapLoaderJson loader(&serializeBuffer);
  71. std::unique_ptr<CMap> serialized = loader.loadMap();
  72. MapComparer c;
  73. c(serialized, initialMap);
  74. }
  75. logGlobal->info("CMapFormatVCMI_Simple finish");
  76. }