1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/test/map/CMapFormatTest.cpp

226 lines
7.1 KiB
C++
Raw Normal View History

2015-07-27 07:33:47 +02:00
/*
* CMapFormatTest.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"
2015-08-08 18:16:33 +02:00
#include "../../lib/JsonDetail.h"
2017-08-11 15:27:42 +02:00
#include "../../lib/filesystem/CMemoryBuffer.h"
#include "../../lib/filesystem/Filesystem.h"
2015-08-08 18:16:33 +02:00
#include "../../lib/mapping/CMap.h"
#include "../../lib/rmg/CMapGenOptions.h"
#include "../../lib/rmg/CMapGenerator.h"
#include "../../lib/mapping/MapFormatJson.h"
2015-08-08 18:16:33 +02:00
2015-08-13 02:29:13 +02:00
#include "../lib/VCMIDirs.h"
2015-08-08 18:16:33 +02:00
#include "MapComparer.h"
2018-03-05 20:26:00 +02:00
#include "../JsonComparer.h"
#include "mock/ZoneOptionsFake.h"
2015-07-27 07:33:47 +02:00
static const int TEST_RANDOM_SEED = 1337;
2017-08-03 14:09:30 +02:00
static void saveTestMap(CMemoryBuffer & serializeBuffer, const std::string & filename)
{
auto path = VCMIDirs::get().userDataPath() / filename;
boost::filesystem::remove(path);
boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
tmp.write((const char *)serializeBuffer.getBuffer().data(), serializeBuffer.getSize());
tmp.flush();
tmp.close();
}
2017-06-14 10:56:35 +02:00
TEST(MapFormat, Random)
2015-07-27 07:33:47 +02:00
{
SCOPED_TRACE("MapFormat_Random start");
2015-08-18 01:21:35 +02:00
CMapGenOptions opt;
2021-07-05 19:04:17 +02:00
CRmgTemplate tmpl;
std::shared_ptr<ZoneOptionsFake> zoneOptions = std::make_shared<ZoneOptionsFake>();
2021-07-05 19:04:17 +02:00
const_cast<CRmgTemplate::CPlayerCountRange &>(tmpl.getCpuPlayers()).addRange(1, 4);
const_cast<CRmgTemplate::Zones &>(tmpl.getZones())[0] = zoneOptions;
2021-07-05 19:04:17 +02:00
zoneOptions->setOwner(1);
2021-07-05 19:04:17 +02:00
opt.setMapTemplate(&tmpl);
2015-08-18 01:21:35 +02:00
opt.setHeight(CMapHeader::MAP_SIZE_MIDDLE);
opt.setWidth(CMapHeader::MAP_SIZE_MIDDLE);
opt.setHasTwoLevels(true);
opt.setPlayerCount(4);
2015-11-15 17:29:00 +02:00
opt.setPlayerTypeForStandardPlayer(PlayerColor(0), EPlayerType::HUMAN);
opt.setPlayerTypeForStandardPlayer(PlayerColor(1), EPlayerType::AI);
opt.setPlayerTypeForStandardPlayer(PlayerColor(2), EPlayerType::AI);
opt.setPlayerTypeForStandardPlayer(PlayerColor(3), EPlayerType::AI);
2015-08-18 01:21:35 +02:00
CMapGenerator gen(opt, TEST_RANDOM_SEED);
2015-07-27 07:33:47 +02:00
std::unique_ptr<CMap> initialMap = gen.generate();
initialMap->name = "Test";
SCOPED_TRACE("MapFormat_Random generated");
2015-07-27 07:33:47 +02:00
CMemoryBuffer serializeBuffer;
2015-07-27 07:33:47 +02:00
{
CMapSaverJson saver(&serializeBuffer);
saver.saveMap(initialMap);
2015-07-27 07:33:47 +02:00
}
SCOPED_TRACE("MapFormat_Random serialized");
2017-08-03 14:09:30 +02:00
saveTestMap(serializeBuffer, "test_random.vmap");
SCOPED_TRACE("MapFormat_Random saved");
2015-08-18 01:21:35 +02:00
serializeBuffer.seek(0);
{
CMapLoaderJson loader(&serializeBuffer);
std::unique_ptr<CMap> serialized = loader.loadMap();
MapComparer c;
c(serialized, initialMap);
}
SCOPED_TRACE("MapFormat_Random finish");
}
static JsonNode getFromArchive(CZipLoader & archive, const std::string & archiveFilename)
{
ResourceID resource(archiveFilename, EResType::TEXT);
if(!archive.existsResource(resource))
throw std::runtime_error(archiveFilename + " not found");
auto data = archive.load(resource)->readAll();
JsonNode res(reinterpret_cast<char*>(data.first.get()), data.second);
return res;
}
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
{
2017-08-11 15:27:42 +02:00
auto s = data.toJson();
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
2017-08-11 15:27:42 +02:00
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())
throw std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
}
2017-08-03 12:50:54 +02:00
static std::unique_ptr<CMap> loadOriginal(const JsonNode & header, const JsonNode & objects, const JsonNode & surface, const JsonNode & underground)
{
CMemoryBuffer initialBuffer;
std::shared_ptr<CIOApi> originalDataIO(new CProxyIOApi(&initialBuffer));
{
CZipSaver initialSaver(originalDataIO, "_");
2017-08-03 14:09:30 +02:00
addToArchive(initialSaver, header, CMapFormatJson::HEADER_FILE_NAME);
addToArchive(initialSaver, objects, CMapFormatJson::OBJECTS_FILE_NAME);
2017-08-03 12:50:54 +02:00
addToArchive(initialSaver, surface, "surface_terrain.json");
addToArchive(initialSaver, underground, "underground_terrain.json");
}
initialBuffer.seek(0);
CMapLoaderJson initialLoader(&initialBuffer);
return initialLoader.loadMap();
}
static void loadActual(CMemoryBuffer * serializeBuffer, const std::unique_ptr<CMap> & originalMap, JsonNode & header, JsonNode & objects, JsonNode & surface, JsonNode & underground)
{
{
CMapSaverJson saver(serializeBuffer);
saver.saveMap(originalMap);
}
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(serializeBuffer));
CZipLoader actualDataLoader("", "_", actualDataIO);
2017-08-03 14:09:30 +02:00
header = getFromArchive(actualDataLoader, CMapFormatJson::HEADER_FILE_NAME);
objects = getFromArchive(actualDataLoader, CMapFormatJson::OBJECTS_FILE_NAME);
2017-08-03 12:50:54 +02:00
surface = getFromArchive(actualDataLoader, "surface_terrain.json");
underground = getFromArchive(actualDataLoader, "underground_terrain.json");
}
TEST(MapFormat, Objects)
{
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
const JsonNode initialHeader(ResourceID(MAP_DATA_PATH + CMapFormatJson::HEADER_FILE_NAME));
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH + CMapFormatJson::HEADER_FILE_NAME));//same as initial for now
const JsonNode initialObjects(ResourceID(MAP_DATA_PATH + CMapFormatJson::OBJECTS_FILE_NAME));
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH + "objects.ex.json"));
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH + "surface_terrain.json"));
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH + "underground_terrain.json"));
2017-08-03 12:50:54 +02:00
std::unique_ptr<CMap> originalMap = loadOriginal(initialHeader, initialObjects, expectedSurface, expectedUnderground);
2017-08-03 12:50:54 +02:00
CMemoryBuffer serializeBuffer;
2017-08-03 12:50:54 +02:00
JsonNode actualHeader;
JsonNode actualObjects;
JsonNode actualSurface;
JsonNode actualUnderground;
2017-08-03 12:50:54 +02:00
loadActual(&serializeBuffer, originalMap, actualHeader, actualObjects, actualSurface, actualUnderground);
2017-08-03 12:50:54 +02:00
saveTestMap(serializeBuffer, "test_object_property.vmap");
2017-08-03 12:50:54 +02:00
{
2018-03-05 20:26:00 +02:00
JsonComparer c(false);
c.compare("hdr", actualHeader, expectedHeader);
c.compare("obj", actualObjects, expectedObjects);
}
{
2018-03-05 20:26:00 +02:00
JsonComparer c(true);
2017-08-03 12:50:54 +02:00
c.compare("surface", actualSurface, expectedSurface);
c.compare("underground", actualUnderground, expectedUnderground);
}
2017-08-03 12:50:54 +02:00
}
2017-08-03 12:50:54 +02:00
TEST(MapFormat, Terrain)
{
static const std::string MAP_DATA_PATH = "test/TerrainTest/";
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH + CMapFormatJson::HEADER_FILE_NAME));
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH + CMapFormatJson::OBJECTS_FILE_NAME));
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH + "surface_terrain.json"));
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH + "underground_terrain.json"));
2017-08-03 14:09:30 +02:00
std::unique_ptr<CMap> originalMap = loadOriginal(expectedHeader, expectedObjects, expectedSurface, expectedUnderground);
2017-08-03 12:50:54 +02:00
CMemoryBuffer serializeBuffer;
JsonNode actualHeader;
JsonNode actualObjects;
JsonNode actualSurface;
JsonNode actualUnderground;
loadActual(&serializeBuffer, originalMap, actualHeader, actualObjects, actualSurface, actualUnderground);
saveTestMap(serializeBuffer, "test_terrain.vmap");
{
2018-03-05 20:26:00 +02:00
JsonComparer c(false);
c.compare("hdr", actualHeader, expectedHeader);
c.compare("obj", actualObjects, expectedObjects);
}
{
2018-03-05 20:26:00 +02:00
JsonComparer c(true);
2017-08-03 12:50:54 +02:00
c.compare("surface", actualSurface, expectedSurface);
c.compare("underground", actualUnderground, expectedUnderground);
}
2015-07-27 07:33:47 +02:00
}