diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-09-15 14:01:35 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-09-15 14:01:35 +0200 |
| commit | c782034564ed422de61235917917963c5e5c3fca (patch) | |
| tree | 403a69650fe94324f2f64e404b6e01c6dcd563dd /test | |
| parent | ec792d93a73e49738d4ded361f23acfae3a00e94 (diff) | |
| parent | 7a8a1833dc766abc1626a1e590e3f39a7d503062 (diff) | |
| download | Project-Tick-c782034564ed422de61235917917963c5e5c3fca.tar.gz Project-Tick-c782034564ed422de61235917917963c5e5c3fca.zip | |
Merge branch 'zlibstream'
Diffstat (limited to 'test')
| -rw-r--r-- | test/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | test/testfiles/bigtest_corrupt.nbt | bin | 0 -> 561 bytes | |||
| -rw-r--r-- | test/testfiles/bigtest_eof.nbt | bin | 0 -> 426 bytes | |||
| -rw-r--r-- | test/zlibstream_test.h | 115 |
4 files changed, 118 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c13f09f2da..c640ef054e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,9 @@ add_custom_command(TARGET read_test POST_BUILD CXXTEST_ADD_TEST(write_test write_test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/write_test.h) target_link_libraries(write_test nbt++) +CXXTEST_ADD_TEST(zlibstream_test zlibstream_test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/zlibstream_test.h) +target_link_libraries(zlibstream_test nbt++ z) + add_executable(format_test format_test.cpp) target_link_libraries(format_test nbt++) add_test(format_test format_test) diff --git a/test/testfiles/bigtest_corrupt.nbt b/test/testfiles/bigtest_corrupt.nbt Binary files differnew file mode 100644 index 0000000000..71eba42a7b --- /dev/null +++ b/test/testfiles/bigtest_corrupt.nbt diff --git a/test/testfiles/bigtest_eof.nbt b/test/testfiles/bigtest_eof.nbt Binary files differnew file mode 100644 index 0000000000..eeedb9d26d --- /dev/null +++ b/test/testfiles/bigtest_eof.nbt diff --git a/test/zlibstream_test.h b/test/zlibstream_test.h new file mode 100644 index 0000000000..f37dcdbd91 --- /dev/null +++ b/test/zlibstream_test.h @@ -0,0 +1,115 @@ +/* + * libnbt++ - A library for the Minecraft Named Binary Tag format. + * Copyright (C) 2013, 2015 ljfa-ag + * + * This file is part of libnbt++. + * + * libnbt++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libnbt++ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libnbt++. If not, see <http://www.gnu.org/licenses/>. + */ +#include <cxxtest/TestSuite.h> +#include "io/izlibstream.h" +#include "io/ozlibstream.h" +#include <fstream> +#include <sstream> + +using namespace zlib; + +class zlibstream_test : public CxxTest::TestSuite +{ +private: + std::stringbuf bigtest; + +public: + zlibstream_test() + { + std::ifstream bigtest_f("bigtest_uncompr", std::ios::binary); + bigtest_f >> &bigtest; + if(!bigtest_f || bigtest.str().size() == 0) + throw std::runtime_error("Could not read bigtest_uncompr file"); + } + + void test_inflate_gzip() + { + std::ifstream gzip_in("bigtest.nbt", std::ios::binary); + TS_ASSERT(gzip_in); + + std::stringbuf data; + //Small buffer so not all fits at once (the compressed file is 561 bytes) + { + izlibstream igzs(gzip_in, 256); + igzs.exceptions(std::ios::failbit); + TS_ASSERT(igzs.good()); + + TS_ASSERT_THROWS_NOTHING(igzs >> &data); + TS_ASSERT(igzs); + TS_ASSERT(igzs.eof()); + TS_ASSERT_EQUALS(data.str(), bigtest.str()); + } + + //Clear and reuse buffers + data.str(""); + gzip_in.clear(); + gzip_in.seekg(0); + //Now try the same with larger buffer (but not large enough for all output, uncompressed size 1561 bytes) + { + izlibstream igzs(gzip_in, 1000); + igzs.exceptions(std::ios::failbit); + TS_ASSERT(igzs.good()); + + TS_ASSERT_THROWS_NOTHING(igzs >> &data); + TS_ASSERT(igzs); + TS_ASSERT(igzs.eof()); + TS_ASSERT_EQUALS(data.str(), bigtest.str()); + } + + data.str(""); + gzip_in.clear(); + gzip_in.seekg(0); + //Now with large buffer + { + izlibstream igzs(gzip_in, 4000); + igzs.exceptions(std::ios::failbit); + TS_ASSERT(igzs.good()); + + TS_ASSERT_THROWS_NOTHING(igzs >> &data); + TS_ASSERT(igzs); + TS_ASSERT(igzs.eof()); + TS_ASSERT_EQUALS(data.str(), bigtest.str()); + } + } + + void test_inflate_corrupt() + { + std::ifstream gzip_in("bigtest_corrupt.nbt", std::ios::binary); + TS_ASSERT(gzip_in); + + std::stringbuf data; + { + izlibstream igzs(gzip_in); + igzs.exceptions(std::ios::failbit); + TS_ASSERT_THROWS(igzs >> &data, zlib_error); + } + + gzip_in.close(); + gzip_in.open("bigtest_eof.nbt", std::ios::binary); + TS_ASSERT(gzip_in); + + data.str(""); + { + izlibstream igzs(gzip_in); + igzs.exceptions(std::ios::failbit); + TS_ASSERT_THROWS(igzs >> &data, zlib_error); + } + } +}; |
