CMapFormatTest.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_SMALL);
  28. opt.setWidth(CMapHeader::MAP_SIZE_SMALL);
  29. opt.setHasTwoLevels(true);
  30. opt.setPlayerCount(2);
  31. opt.setPlayerTypeForStandardPlayer(PlayerColor(0), EPlayerType::HUMAN);
  32. opt.setPlayerTypeForStandardPlayer(PlayerColor(1), EPlayerType::HUMAN);
  33. CMapGenerator gen;
  34. initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
  35. initialMap->name = "Test";
  36. };
  37. ~CMapTestFixture()
  38. {
  39. initialMap.reset();
  40. };
  41. };
  42. BOOST_GLOBAL_FIXTURE(CMapTestFixture);
  43. BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple)
  44. {
  45. logGlobal->info("CMapFormatVCMI_Simple start");
  46. CMemoryBuffer serializeBuffer;
  47. {
  48. CMapSaverJson saver(&serializeBuffer);
  49. saver.saveMap(initialMap);
  50. }
  51. #if 1
  52. {
  53. auto path = VCMIDirs::get().userDataPath()/"test.zip";
  54. boost::filesystem::remove(path);
  55. boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
  56. tmp.write((const char *)serializeBuffer.getBuffer().data(),serializeBuffer.getSize());
  57. tmp.flush();
  58. tmp.close();
  59. logGlobal->infoStream() << "Test map has been saved to " << path;
  60. }
  61. #endif // 1
  62. serializeBuffer.seek(0);
  63. {
  64. CMapLoaderJson loader(&serializeBuffer);
  65. std::unique_ptr<CMap> serialized = loader.loadMap();
  66. MapComparer c;
  67. c(serialized, initialMap);
  68. }
  69. logGlobal->info("CMapFormatVCMI_Simple finish");
  70. }