00001 #include "util/read_compressed.hh"
00002
00003 #include "util/file.hh"
00004 #include "util/have.hh"
00005
00006 #define BOOST_TEST_MODULE ReadCompressedTest
00007 #include <boost/test/unit_test.hpp>
00008 #include <boost/scoped_ptr.hpp>
00009
00010 #include <fstream>
00011 #include <string>
00012 #include <cstdlib>
00013
00014 #if defined __MINGW32__
00015 #include <ctime>
00016 #include <fcntl.h>
00017
00018 #if !defined mkstemp
00019
00020 int mkstemp(char * stemplate)
00021 {
00022 char *filename = mktemp(stemplate);
00023 if (filename == NULL)
00024 return -1;
00025 return open(filename, O_RDWR | O_CREAT, 0600);
00026 }
00027 #endif
00028
00029 #endif // defined
00030
00031 namespace util {
00032 namespace {
00033
00034 void ReadLoop(ReadCompressed &reader, void *to_void, std::size_t amount) {
00035 uint8_t *to = static_cast<uint8_t*>(to_void);
00036 while (amount) {
00037 std::size_t ret = reader.Read(to, amount);
00038 BOOST_REQUIRE(ret);
00039 to += ret;
00040 amount -= ret;
00041 }
00042 }
00043
00044 const uint32_t kSize4 = 100000 / 4;
00045
00046 std::string WriteRandom() {
00047 char name[] = "tempXXXXXX";
00048 scoped_fd original(mkstemp(name));
00049 BOOST_REQUIRE(original.get() > 0);
00050 for (uint32_t i = 0; i < kSize4; ++i) {
00051 WriteOrThrow(original.get(), &i, sizeof(uint32_t));
00052 }
00053 return name;
00054 }
00055
00056 void VerifyRead(ReadCompressed &reader) {
00057 for (uint32_t i = 0; i < kSize4; ++i) {
00058 uint32_t got;
00059 ReadLoop(reader, &got, sizeof(uint32_t));
00060 BOOST_CHECK_EQUAL(i, got);
00061 }
00062
00063 char ignored;
00064 BOOST_CHECK_EQUAL((std::size_t)0, reader.Read(&ignored, 1));
00065
00066 BOOST_CHECK_EQUAL((std::size_t)0, reader.Read(&ignored, 1));
00067 }
00068
00069 void TestRandom(const char *compressor) {
00070 std::string name(WriteRandom());
00071
00072 char gzname[] = "tempXXXXXX";
00073 scoped_fd gzipped(mkstemp(gzname));
00074
00075 std::string command(compressor);
00076 #ifdef __CYGWIN__
00077 command += ".exe";
00078 #endif
00079 command += " <\"";
00080 command += name;
00081 command += "\" >\"";
00082 command += gzname;
00083 command += "\"";
00084 BOOST_REQUIRE_EQUAL(0, system(command.c_str()));
00085
00086 BOOST_CHECK_EQUAL(0, unlink(name.c_str()));
00087 BOOST_CHECK_EQUAL(0, unlink(gzname));
00088
00089 ReadCompressed reader(gzipped.release());
00090 VerifyRead(reader);
00091 }
00092
00093 BOOST_AUTO_TEST_CASE(Uncompressed) {
00094 TestRandom("cat");
00095 }
00096
00097 #ifdef HAVE_ZLIB
00098 BOOST_AUTO_TEST_CASE(ReadGZ) {
00099 TestRandom("gzip");
00100 }
00101 #endif // HAVE_ZLIB
00102
00103 #ifdef HAVE_BZLIB
00104 BOOST_AUTO_TEST_CASE(ReadBZ) {
00105 TestRandom("bzip2");
00106 }
00107 #endif // HAVE_BZLIB
00108
00109 #ifdef HAVE_XZLIB
00110 BOOST_AUTO_TEST_CASE(ReadXZ) {
00111 TestRandom("xz");
00112 }
00113 #endif
00114
00115 #ifdef HAVE_ZLIB
00116 BOOST_AUTO_TEST_CASE(AppendGZ) {
00117 }
00118 #endif
00119
00120 BOOST_AUTO_TEST_CASE(IStream) {
00121 std::string name(WriteRandom());
00122 std::fstream stream(name.c_str(), std::ios::in);
00123 BOOST_CHECK_EQUAL(0, unlink(name.c_str()));
00124 ReadCompressed reader;
00125 reader.Reset(stream);
00126 VerifyRead(reader);
00127 }
00128
00129 }
00130 }