2012-10-30 23:33:22 +04:00
|
|
|
#include "util/read_compressed.hh"
|
|
|
|
|
|
|
|
#include "util/file.hh"
|
|
|
|
#include "util/have.hh"
|
|
|
|
|
|
|
|
#define BOOST_TEST_MODULE ReadCompressedTest
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void ReadLoop(ReadCompressed &reader, void *to_void, std::size_t amount) {
|
|
|
|
uint8_t *to = static_cast<uint8_t*>(to_void);
|
|
|
|
while (amount) {
|
|
|
|
std::size_t ret = reader.Read(to, amount);
|
|
|
|
BOOST_REQUIRE(ret);
|
|
|
|
to += ret;
|
|
|
|
amount -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
const uint32_t kSize4 = 100000 / 4;
|
|
|
|
|
|
|
|
std::string WriteRandom() {
|
2012-10-30 23:33:22 +04:00
|
|
|
char name[] = "tempXXXXXX";
|
2013-01-23 00:23:35 +04:00
|
|
|
scoped_fd original(mkstemp(name));
|
|
|
|
BOOST_REQUIRE(original.get() > 0);
|
|
|
|
for (uint32_t i = 0; i < kSize4; ++i) {
|
|
|
|
WriteOrThrow(original.get(), &i, sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
2012-10-30 23:33:22 +04:00
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
void VerifyRead(ReadCompressed &reader) {
|
|
|
|
for (uint32_t i = 0; i < kSize4; ++i) {
|
|
|
|
uint32_t got;
|
|
|
|
ReadLoop(reader, &got, sizeof(uint32_t));
|
|
|
|
BOOST_CHECK_EQUAL(i, got);
|
2012-10-30 23:33:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
char ignored;
|
|
|
|
BOOST_CHECK_EQUAL((std::size_t)0, reader.Read(&ignored, 1));
|
|
|
|
// Test double EOF call.
|
|
|
|
BOOST_CHECK_EQUAL((std::size_t)0, reader.Read(&ignored, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRandom(const char *compressor) {
|
|
|
|
std::string name(WriteRandom());
|
|
|
|
|
2012-10-30 23:33:22 +04:00
|
|
|
char gzname[] = "tempXXXXXX";
|
|
|
|
scoped_fd gzipped(mkstemp(gzname));
|
|
|
|
|
|
|
|
std::string command(compressor);
|
2012-11-15 16:00:15 +04:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
command += ".exe";
|
|
|
|
#endif
|
2012-10-30 23:33:22 +04:00
|
|
|
command += " <\"";
|
|
|
|
command += name;
|
|
|
|
command += "\" >\"";
|
|
|
|
command += gzname;
|
|
|
|
command += "\"";
|
|
|
|
BOOST_REQUIRE_EQUAL(0, system(command.c_str()));
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
BOOST_CHECK_EQUAL(0, unlink(name.c_str()));
|
2012-10-30 23:33:22 +04:00
|
|
|
BOOST_CHECK_EQUAL(0, unlink(gzname));
|
|
|
|
|
|
|
|
ReadCompressed reader(gzipped.release());
|
2013-01-23 00:23:35 +04:00
|
|
|
VerifyRead(reader);
|
2012-10-30 23:33:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(Uncompressed) {
|
|
|
|
TestRandom("cat");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
BOOST_AUTO_TEST_CASE(ReadGZ) {
|
|
|
|
TestRandom("gzip");
|
|
|
|
}
|
|
|
|
#endif // HAVE_ZLIB
|
|
|
|
|
|
|
|
#ifdef HAVE_BZLIB
|
|
|
|
BOOST_AUTO_TEST_CASE(ReadBZ) {
|
|
|
|
TestRandom("bzip2");
|
|
|
|
}
|
|
|
|
#endif // HAVE_BZLIB
|
|
|
|
|
|
|
|
#ifdef HAVE_XZLIB
|
|
|
|
BOOST_AUTO_TEST_CASE(ReadXZ) {
|
|
|
|
TestRandom("xz");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-23 00:23:35 +04:00
|
|
|
BOOST_AUTO_TEST_CASE(IStream) {
|
|
|
|
std::string name(WriteRandom());
|
|
|
|
std::fstream stream(name.c_str(), std::ios::in);
|
|
|
|
BOOST_CHECK_EQUAL(0, unlink(name.c_str()));
|
|
|
|
ReadCompressed reader;
|
|
|
|
reader.Reset(stream);
|
|
|
|
VerifyRead(reader);
|
|
|
|
}
|
|
|
|
|
2012-10-30 23:33:22 +04:00
|
|
|
} // namespace
|
|
|
|
} // namespace util
|