mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
WiP on zip serialization
(-) still not works correctly
This commit is contained in:
parent
fc2cbc841a
commit
0cc7213a27
@ -15,7 +15,7 @@
|
||||
CMemoryBuffer::CMemoryBuffer():
|
||||
position(0)
|
||||
{
|
||||
|
||||
buffer.reserve(4096);
|
||||
}
|
||||
|
||||
si64 CMemoryBuffer::write(const ui8 * data, si64 size)
|
||||
@ -48,7 +48,7 @@ si64 CMemoryBuffer::read(ui8 * data, si64 size)
|
||||
si64 CMemoryBuffer::seek(si64 position)
|
||||
{
|
||||
this->position = position;
|
||||
if (this->position >=getSize())
|
||||
if (this->position >getSize())
|
||||
this->position = getSize();
|
||||
return this->position;
|
||||
}
|
||||
|
@ -65,16 +65,24 @@ long ZCALLBACK CIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T off
|
||||
switch (origin)
|
||||
{
|
||||
case ZLIB_FILEFUNC_SEEK_CUR :
|
||||
actualStream->skip(offset);//TODO: should we check actual skipped?
|
||||
if(actualStream->skip(offset) != offset)
|
||||
ret = -1;
|
||||
break;
|
||||
case ZLIB_FILEFUNC_SEEK_END :
|
||||
actualStream->seek(actualStream->getSize() - offset);
|
||||
case ZLIB_FILEFUNC_SEEK_END:
|
||||
{
|
||||
const si64 pos = actualStream->getSize() - offset;
|
||||
if(actualStream->seek(pos) != pos)
|
||||
ret = -1;
|
||||
}
|
||||
break;
|
||||
case ZLIB_FILEFUNC_SEEK_SET :
|
||||
actualStream->seek(offset);
|
||||
if(actualStream->seek(offset) != offset)
|
||||
ret = -1;
|
||||
break;
|
||||
default: ret = -1;
|
||||
}
|
||||
if(ret == -1)
|
||||
logGlobal->error("CIOApi::seekFileProxy: seek failed");
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -213,6 +213,7 @@ void CMapLoaderJson::readHeader()
|
||||
|
||||
static const std::map<std::string, ui8> difficultyMap =
|
||||
{
|
||||
{"", 1},
|
||||
{"EASY", 0},
|
||||
{"NORMAL", 1},
|
||||
{"HARD", 2},
|
||||
|
@ -55,39 +55,32 @@ BOOST_GLOBAL_FIXTURE(CMapTestFixture);
|
||||
|
||||
BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple)
|
||||
{
|
||||
try
|
||||
logGlobal->info("CMapFormatVCMI_Simple start");
|
||||
CMemoryBuffer serializeBuffer;
|
||||
{
|
||||
logGlobal->info("CMapFormatVCMI_Simple start");
|
||||
CMemoryBuffer serializeBuffer;
|
||||
{
|
||||
CMapSaverJson saver(&serializeBuffer);
|
||||
saver.saveMap(initialMap);
|
||||
}
|
||||
|
||||
#if 1
|
||||
{
|
||||
std::ofstream tmp((VCMIDirs::get().userDataPath()/"temp.zip").string());
|
||||
tmp.write((const char *)&serializeBuffer.getBuffer()[0],serializeBuffer.getSize());
|
||||
tmp.flush();
|
||||
}
|
||||
|
||||
|
||||
#endif // 1
|
||||
|
||||
serializeBuffer.seek(0);
|
||||
{
|
||||
CMapLoaderJson loader(&serializeBuffer);
|
||||
std::unique_ptr<CMap> serialized = loader.loadMap();
|
||||
|
||||
MapComparer c;
|
||||
c(initialMap, serialized);
|
||||
}
|
||||
|
||||
logGlobal->info("CMapFormatVCMI_Simple finish");
|
||||
CMapSaverJson saver(&serializeBuffer);
|
||||
saver.saveMap(initialMap);
|
||||
}
|
||||
catch(...)
|
||||
|
||||
#if 1
|
||||
{
|
||||
handleException();
|
||||
BOOST_FAIL("Test case crashed");
|
||||
auto path = VCMIDirs::get().userDataPath()/"temp.zip";
|
||||
boost::filesystem::remove(path);
|
||||
boost::filesystem::ofstream tmp(path);
|
||||
tmp.write((const char *)serializeBuffer.getBuffer().data(),serializeBuffer.getSize());
|
||||
}
|
||||
|
||||
|
||||
#endif // 1
|
||||
|
||||
serializeBuffer.seek(0);
|
||||
{
|
||||
CMapLoaderJson loader(&serializeBuffer);
|
||||
std::unique_ptr<CMap> serialized = loader.loadMap();
|
||||
|
||||
MapComparer c;
|
||||
c(serialized, initialMap);
|
||||
}
|
||||
|
||||
logGlobal->info("CMapFormatVCMI_Simple finish");
|
||||
}
|
||||
|
52
test/CMemoryBufferTest.cpp
Normal file
52
test/CMemoryBufferTest.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
|
||||
/*
|
||||
* CMemoryBufferTest.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"
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "../lib/filesystem/CMemoryBuffer.h"
|
||||
|
||||
|
||||
struct CMemoryBufferFixture
|
||||
{
|
||||
CMemoryBuffer subject;
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(CMemoryBuffer_Empty, CMemoryBufferFixture)
|
||||
{
|
||||
BOOST_REQUIRE_EQUAL(0, subject.tell());
|
||||
BOOST_REQUIRE_EQUAL(0, subject.getSize());
|
||||
|
||||
si32 dummy = 1337;
|
||||
|
||||
auto ret = subject.read((ui8 *)&dummy, sizeof(si32));
|
||||
|
||||
BOOST_CHECK_EQUAL(0, ret);
|
||||
BOOST_CHECK_EQUAL(1337, dummy);
|
||||
BOOST_CHECK_EQUAL(0, subject.tell());
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(CMemoryBuffer_Write, CMemoryBufferFixture)
|
||||
{
|
||||
const si32 initial = 1337;
|
||||
|
||||
subject.write((const ui8 *)&initial, sizeof(si32));
|
||||
|
||||
BOOST_CHECK_EQUAL(4, subject.tell());
|
||||
subject.seek(0);
|
||||
BOOST_CHECK_EQUAL(0, subject.tell());
|
||||
|
||||
si32 current = 0;
|
||||
auto ret = subject.read((ui8 *)¤t, sizeof(si32));
|
||||
BOOST_CHECK_EQUAL(sizeof(si32), ret);
|
||||
BOOST_CHECK_EQUAL(initial, current);
|
||||
BOOST_CHECK_EQUAL(4, subject.tell());
|
||||
}
|
@ -82,7 +82,7 @@ void MapComparer::compare()
|
||||
{
|
||||
BOOST_REQUIRE_NE((void *) actual, (void *) expected); //should not point to the same object
|
||||
BOOST_REQUIRE_MESSAGE(actual != nullptr, "Actual map is not defined");
|
||||
BOOST_REQUIRE_MESSAGE(actual != nullptr, "Expected map is not defined");
|
||||
BOOST_REQUIRE_MESSAGE(expected != nullptr, "Expected map is not defined");
|
||||
|
||||
compareHeader();
|
||||
compareOptions();
|
||||
|
@ -60,6 +60,7 @@
|
||||
</Linker>
|
||||
<Unit filename="CMapEditManagerTest.cpp" />
|
||||
<Unit filename="CMapFormatTest.cpp" />
|
||||
<Unit filename="CMemoryBufferTest.cpp" />
|
||||
<Unit filename="CVcmiTestConfig.cpp" />
|
||||
<Unit filename="CVcmiTestConfig.h" />
|
||||
<Unit filename="MapComparer.cpp" />
|
||||
|
Loading…
Reference in New Issue
Block a user