diff --git a/lib/filesystem/CMemoryBuffer.cpp b/lib/filesystem/CMemoryBuffer.cpp index 39b25dc9c..849502e7a 100644 --- a/lib/filesystem/CMemoryBuffer.cpp +++ b/lib/filesystem/CMemoryBuffer.cpp @@ -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; } diff --git a/lib/filesystem/MinizipExtensions.cpp b/lib/filesystem/MinizipExtensions.cpp index 8057a907f..5810babe9 100644 --- a/lib/filesystem/MinizipExtensions.cpp +++ b/lib/filesystem/MinizipExtensions.cpp @@ -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; } diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index 027719301..285393236 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -213,6 +213,7 @@ void CMapLoaderJson::readHeader() static const std::map difficultyMap = { + {"", 1}, {"EASY", 0}, {"NORMAL", 1}, {"HARD", 2}, diff --git a/test/CMapFormatTest.cpp b/test/CMapFormatTest.cpp index c37352e40..6d9d9353d 100644 --- a/test/CMapFormatTest.cpp +++ b/test/CMapFormatTest.cpp @@ -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 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 serialized = loader.loadMap(); + + MapComparer c; + c(serialized, initialMap); + } + + logGlobal->info("CMapFormatVCMI_Simple finish"); } diff --git a/test/CMemoryBufferTest.cpp b/test/CMemoryBufferTest.cpp new file mode 100644 index 000000000..a4935be51 --- /dev/null +++ b/test/CMemoryBufferTest.cpp @@ -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 + +#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()); +} diff --git a/test/MapComparer.cpp b/test/MapComparer.cpp index 2ba2878fb..7e0544c6f 100644 --- a/test/MapComparer.cpp +++ b/test/MapComparer.cpp @@ -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(); diff --git a/test/Test.cbp b/test/Test.cbp index 6665380f5..d9fdb6e93 100644 --- a/test/Test.cbp +++ b/test/Test.cbp @@ -60,6 +60,7 @@ +