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
|
|
|
|
2015-07-27 07:33:47 +02:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2015-08-08 18:16:33 +02:00
|
|
|
#include "../lib/filesystem/CMemoryBuffer.h"
|
2016-11-13 12:38:42 +02:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2015-08-08 18:16:33 +02:00
|
|
|
|
2015-07-27 07:33:47 +02:00
|
|
|
#include "../lib/mapping/CMap.h"
|
|
|
|
#include "../lib/rmg/CMapGenOptions.h"
|
|
|
|
#include "../lib/rmg/CMapGenerator.h"
|
2015-08-08 21:08:19 +02:00
|
|
|
#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"
|
2015-07-27 07:33:47 +02:00
|
|
|
|
|
|
|
static const int TEST_RANDOM_SEED = 1337;
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(MapFormat_Random)
|
2015-07-27 07:33:47 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
logGlobal->info("MapFormat_Random start");
|
|
|
|
BOOST_TEST_CHECKPOINT("MapFormat_Random start");
|
|
|
|
std::unique_ptr<CMap> initialMap;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
CMapGenOptions opt;
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2016-11-13 12:38:42 +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
|
|
|
|
2016-11-13 12:38:42 +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
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
CMapGenerator gen;
|
2015-07-27 07:33:47 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
|
|
|
|
initialMap->name = "Test";
|
|
|
|
BOOST_TEST_CHECKPOINT("MapFormat_Random generated");
|
2015-07-27 07:33:47 +02:00
|
|
|
|
2015-08-13 23:16:31 +02:00
|
|
|
CMemoryBuffer serializeBuffer;
|
2015-07-27 07:33:47 +02:00
|
|
|
{
|
2015-08-13 23:16:31 +02:00
|
|
|
CMapSaverJson saver(&serializeBuffer);
|
|
|
|
saver.saveMap(initialMap);
|
2015-07-27 07:33:47 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
BOOST_TEST_CHECKPOINT("MapFormat_Random serialized");
|
2015-08-13 23:16:31 +02:00
|
|
|
#if 1
|
2015-07-27 07:33:47 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
auto path = VCMIDirs::get().userDataPath()/"test_random.vmap";
|
2015-08-13 23:16:31 +02:00
|
|
|
boost::filesystem::remove(path);
|
2015-08-18 00:56:16 +02:00
|
|
|
boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
|
|
|
|
|
2015-08-13 23:16:31 +02:00
|
|
|
tmp.write((const char *)serializeBuffer.getBuffer().data(),serializeBuffer.getSize());
|
2015-08-14 00:42:52 +02:00
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
2015-11-15 17:29:00 +02:00
|
|
|
|
2016-08-11 10:06:06 +02:00
|
|
|
logGlobal->info("Test map has been saved to:");
|
|
|
|
logGlobal->info(path.string());
|
2015-07-27 07:33:47 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
BOOST_TEST_CHECKPOINT("MapFormat_Random saved");
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-13 23:16:31 +02:00
|
|
|
#endif // 1
|
2015-08-18 01:21:35 +02:00
|
|
|
|
2015-08-13 23:16:31 +02:00
|
|
|
serializeBuffer.seek(0);
|
|
|
|
{
|
|
|
|
CMapLoaderJson loader(&serializeBuffer);
|
|
|
|
std::unique_ptr<CMap> serialized = loader.loadMap();
|
|
|
|
|
|
|
|
MapComparer c;
|
|
|
|
c(serialized, initialMap);
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
logGlobal->info("MapFormat_Random finish");
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonNode getFromArchive(CZipLoader & archive, const std::string & archiveFilename)
|
|
|
|
{
|
|
|
|
ResourceID resource(archiveFilename, EResType::TEXT);
|
|
|
|
|
|
|
|
if(!archive.existsResource(resource))
|
|
|
|
throw new std::runtime_error(archiveFilename+" not found");
|
|
|
|
|
|
|
|
auto data = archive.load(resource)->readAll();
|
|
|
|
|
|
|
|
JsonNode res(reinterpret_cast<char*>(data.first.get()), data.second);
|
|
|
|
|
|
|
|
return std::move(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << data;
|
|
|
|
out.flush();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto s = out.str();
|
|
|
|
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
|
|
|
|
|
|
|
|
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())
|
|
|
|
throw new std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(MapFormat_Objects)
|
|
|
|
{
|
|
|
|
logGlobal->info("MapFormat_Objects start");
|
|
|
|
|
|
|
|
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
|
|
|
|
|
|
|
|
const JsonNode initialHeader(ResourceID(MAP_DATA_PATH+"header.json"));
|
|
|
|
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+"header.json"));//same as initial for now
|
|
|
|
|
|
|
|
const JsonNode initialObjects(ResourceID(MAP_DATA_PATH+"objects.json"));
|
|
|
|
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"));
|
|
|
|
|
|
|
|
std::unique_ptr<CMap> originalMap;
|
|
|
|
{
|
|
|
|
CMemoryBuffer initialBuffer;
|
|
|
|
|
|
|
|
std::shared_ptr<CIOApi> originalDataIO(new CProxyIOApi(&initialBuffer));
|
|
|
|
|
|
|
|
{
|
|
|
|
CZipSaver initialSaver(originalDataIO, "_");
|
|
|
|
|
|
|
|
addToArchive(initialSaver, initialHeader, "header.json");
|
|
|
|
addToArchive(initialSaver, initialObjects, "objects.json");
|
|
|
|
addToArchive(initialSaver, expectedSurface, "surface_terrain.json");
|
|
|
|
addToArchive(initialSaver, expectedUnderground, "underground_terrain.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
initialBuffer.seek(0);
|
|
|
|
|
|
|
|
{
|
|
|
|
CMapLoaderJson initialLoader(&initialBuffer);
|
|
|
|
|
|
|
|
originalMap = initialLoader.loadMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CMemoryBuffer serializeBuffer;
|
|
|
|
{
|
|
|
|
CMapSaverJson saver(&serializeBuffer);
|
|
|
|
saver.saveMap(originalMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(&serializeBuffer));
|
|
|
|
CZipLoader actualDataLoader("", "_", actualDataIO);
|
|
|
|
|
|
|
|
const JsonNode actualHeader = getFromArchive(actualDataLoader, "header.json");
|
|
|
|
const JsonNode actualObjects = getFromArchive(actualDataLoader, "objects.json");
|
|
|
|
const JsonNode actualSurface = getFromArchive(actualDataLoader, "surface_terrain.json");
|
|
|
|
const JsonNode actualUnderground = getFromArchive(actualDataLoader, "underground_terrain.json");
|
|
|
|
|
|
|
|
{
|
|
|
|
auto path = VCMIDirs::get().userDataPath()/"test_object_property.vmap";
|
|
|
|
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();
|
|
|
|
|
|
|
|
logGlobal->infoStream() << "Test map has been saved to " << path;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JsonMapComparer c;
|
|
|
|
c.compareHeader(actualHeader, expectedHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JsonMapComparer c;
|
|
|
|
c.compareObjects(actualObjects, expectedObjects);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JsonMapComparer c;
|
|
|
|
c.compareTerrain("surface", actualSurface, expectedSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JsonMapComparer c;
|
|
|
|
c.compareTerrain("underground", actualUnderground, expectedUnderground);
|
|
|
|
}
|
|
|
|
|
|
|
|
logGlobal->info("MapFormat_Objects finish");
|
2015-07-27 07:33:47 +02:00
|
|
|
}
|