2010-09-28 20:26:55 +04:00
|
|
|
#include "util/file_piece.hh"
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2010-10-23 09:21:10 +04:00
|
|
|
#include "util/scoped.hh"
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
#define BOOST_TEST_MODULE FilePieceTest
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2010-10-23 09:21:10 +04:00
|
|
|
#include <stdio.h>
|
2010-12-10 00:58:54 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
namespace util {
|
|
|
|
namespace {
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
/* mmap implementation */
|
2010-10-23 09:21:10 +04:00
|
|
|
BOOST_AUTO_TEST_CASE(MMapReadLine) {
|
2010-09-15 01:33:11 +04:00
|
|
|
std::fstream ref("file_piece.cc", std::ios::in);
|
|
|
|
FilePiece test("file_piece.cc", NULL, 1);
|
|
|
|
std::string ref_line;
|
|
|
|
while (getline(ref, ref_line)) {
|
|
|
|
StringPiece test_line(test.ReadLine());
|
|
|
|
// I submitted a bug report to ICU: http://bugs.icu-project.org/trac/ticket/7924
|
|
|
|
if (!test_line.empty() || !ref_line.empty()) {
|
|
|
|
BOOST_CHECK_EQUAL(ref_line, test_line);
|
|
|
|
}
|
|
|
|
}
|
2010-10-23 09:21:10 +04:00
|
|
|
BOOST_CHECK_THROW(test.get(), EndOfFileException);
|
2010-09-15 01:33:11 +04:00
|
|
|
}
|
|
|
|
|
2010-12-10 00:58:54 +03:00
|
|
|
#ifndef __APPLE__
|
|
|
|
/* Apple isn't happy with the popen, fileno, dup. And I don't want to
|
|
|
|
* reimplement popen. This is an issue with the test.
|
|
|
|
*/
|
2010-09-15 01:33:11 +04:00
|
|
|
/* read() implementation */
|
2010-10-23 09:21:10 +04:00
|
|
|
BOOST_AUTO_TEST_CASE(StreamReadLine) {
|
2010-09-15 01:33:11 +04:00
|
|
|
std::fstream ref("file_piece.cc", std::ios::in);
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2010-12-10 00:58:54 +03:00
|
|
|
FILE *catter = popen("cat file_piece.cc", "r");
|
|
|
|
BOOST_REQUIRE(catter);
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2010-12-10 00:58:54 +03:00
|
|
|
FilePiece test(dup(fileno(catter)), "file_piece.cc", NULL, 1);
|
2010-09-10 04:36:07 +04:00
|
|
|
std::string ref_line;
|
|
|
|
while (getline(ref, ref_line)) {
|
|
|
|
StringPiece test_line(test.ReadLine());
|
2010-09-15 01:33:11 +04:00
|
|
|
// I submitted a bug report to ICU: http://bugs.icu-project.org/trac/ticket/7924
|
|
|
|
if (!test_line.empty() || !ref_line.empty()) {
|
|
|
|
BOOST_CHECK_EQUAL(ref_line, test_line);
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
}
|
2010-10-23 09:21:10 +04:00
|
|
|
BOOST_CHECK_THROW(test.get(), EndOfFileException);
|
2010-12-10 00:58:54 +03:00
|
|
|
BOOST_REQUIRE(!pclose(catter));
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
2010-12-10 00:58:54 +03:00
|
|
|
#endif // __APPLE__
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2010-10-27 21:50:40 +04:00
|
|
|
#ifdef HAVE_ZLIB
|
2010-10-23 09:21:10 +04:00
|
|
|
|
|
|
|
// gzip file
|
|
|
|
BOOST_AUTO_TEST_CASE(PlainZipReadLine) {
|
|
|
|
std::fstream ref("file_piece.cc", std::ios::in);
|
|
|
|
|
|
|
|
BOOST_REQUIRE_EQUAL(0, system("gzip <file_piece.cc >file_piece.cc.gz"));
|
|
|
|
FilePiece test("file_piece.cc.gz", NULL, 1);
|
|
|
|
std::string ref_line;
|
|
|
|
while (getline(ref, ref_line)) {
|
|
|
|
StringPiece test_line(test.ReadLine());
|
|
|
|
// I submitted a bug report to ICU: http://bugs.icu-project.org/trac/ticket/7924
|
|
|
|
if (!test_line.empty() || !ref_line.empty()) {
|
|
|
|
BOOST_CHECK_EQUAL(ref_line, test_line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_CHECK_THROW(test.get(), EndOfFileException);
|
|
|
|
}
|
2010-12-10 00:58:54 +03:00
|
|
|
|
|
|
|
// gzip stream. Apple doesn't like popen, fileno, dup. This is an issue with
|
|
|
|
// the test.
|
|
|
|
#ifndef __APPLE__
|
2010-10-23 09:21:10 +04:00
|
|
|
BOOST_AUTO_TEST_CASE(StreamZipReadLine) {
|
|
|
|
std::fstream ref("file_piece.cc", std::ios::in);
|
|
|
|
|
2010-12-10 00:58:54 +03:00
|
|
|
FILE * catter = popen("gzip <file_piece.cc", "r");
|
|
|
|
BOOST_REQUIRE(catter);
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2010-12-10 00:58:54 +03:00
|
|
|
FilePiece test(dup(fileno(catter)), "file_piece.cc", NULL, 1);
|
2010-10-23 09:21:10 +04:00
|
|
|
std::string ref_line;
|
|
|
|
while (getline(ref, ref_line)) {
|
|
|
|
StringPiece test_line(test.ReadLine());
|
|
|
|
// I submitted a bug report to ICU: http://bugs.icu-project.org/trac/ticket/7924
|
|
|
|
if (!test_line.empty() || !ref_line.empty()) {
|
|
|
|
BOOST_CHECK_EQUAL(ref_line, test_line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_CHECK_THROW(test.get(), EndOfFileException);
|
2010-12-10 00:58:54 +03:00
|
|
|
BOOST_REQUIRE(!pclose(catter));
|
2010-10-23 09:21:10 +04:00
|
|
|
}
|
2010-12-10 00:58:54 +03:00
|
|
|
#endif // __APPLE__
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2010-12-10 00:58:54 +03:00
|
|
|
#endif // HAVE_ZLIB
|
2010-10-23 09:21:10 +04:00
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
} // namespace
|
|
|
|
} // namespace util
|