00001
00002 #include "util/file_piece.hh"
00003
00004 #include "util/file_stream.hh"
00005 #include "util/file.hh"
00006 #include "util/scoped.hh"
00007
00008 #define BOOST_TEST_MODULE FilePieceTest
00009 #include <boost/test/unit_test.hpp>
00010 #include <fstream>
00011 #include <iostream>
00012 #include <cstdio>
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015
00016 namespace util {
00017 namespace {
00018
00019 std::string FileLocation() {
00020 if (boost::unit_test::framework::master_test_suite().argc < 2) {
00021 return "file_piece.cc";
00022 }
00023 std::string ret(boost::unit_test::framework::master_test_suite().argv[1]);
00024 return ret;
00025 }
00026
00027
00028 BOOST_AUTO_TEST_CASE(IStream) {
00029 std::fstream ref(FileLocation().c_str(), std::ios::in);
00030 std::fstream backing(FileLocation().c_str(), std::ios::in);
00031 FilePiece test(backing);
00032 std::string ref_line;
00033 while (getline(ref, ref_line)) {
00034 StringPiece test_line(test.ReadLine());
00035 BOOST_CHECK_EQUAL(ref_line, test_line);
00036 }
00037 BOOST_CHECK_THROW(test.get(), EndOfFileException);
00038 BOOST_CHECK_THROW(test.get(), EndOfFileException);
00039 }
00040
00041
00042 BOOST_AUTO_TEST_CASE(MMapReadLine) {
00043 std::fstream ref(FileLocation().c_str(), std::ios::in);
00044 FilePiece test(FileLocation().c_str(), NULL, 1);
00045 std::string ref_line;
00046 while (getline(ref, ref_line)) {
00047 StringPiece test_line(test.ReadLine());
00048
00049 if (!test_line.empty() || !ref_line.empty()) {
00050 BOOST_CHECK_EQUAL(ref_line, test_line);
00051 }
00052 }
00053 BOOST_CHECK_THROW(test.get(), EndOfFileException);
00054 }
00055
00056 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__APPLE__)
00057
00058
00059
00060
00061 BOOST_AUTO_TEST_CASE(StreamReadLine) {
00062 std::fstream ref(FileLocation().c_str(), std::ios::in);
00063
00064 std::string popen_args = "cat \"";
00065 popen_args += FileLocation();
00066 popen_args += '"';
00067
00068 FILE *catter = popen(popen_args.c_str(), "r");
00069 BOOST_REQUIRE(catter);
00070
00071 FilePiece test(dup(fileno(catter)), "file_piece.cc", NULL, 1);
00072 std::string ref_line;
00073 while (getline(ref, ref_line)) {
00074 StringPiece test_line(test.ReadLine());
00075
00076 if (!test_line.empty() || !ref_line.empty()) {
00077 BOOST_CHECK_EQUAL(ref_line, test_line);
00078 }
00079 }
00080 BOOST_CHECK_THROW(test.get(), EndOfFileException);
00081 BOOST_REQUIRE(!pclose(catter));
00082 }
00083 #endif
00084
00085 #ifdef HAVE_ZLIB
00086
00087
00088 BOOST_AUTO_TEST_CASE(PlainZipReadLine) {
00089 std::string location(FileLocation());
00090 std::fstream ref(location.c_str(), std::ios::in);
00091
00092 std::string command("gzip <\"");
00093 command += location + "\" >\"" + location + "\".gz";
00094
00095 BOOST_REQUIRE_EQUAL(0, system(command.c_str()));
00096 FilePiece test((location + ".gz").c_str(), NULL, 1);
00097 unlink((location + ".gz").c_str());
00098 std::string ref_line;
00099 while (getline(ref, ref_line)) {
00100 StringPiece test_line(test.ReadLine());
00101
00102 if (!test_line.empty() || !ref_line.empty()) {
00103 BOOST_CHECK_EQUAL(ref_line, test_line);
00104 }
00105 }
00106 BOOST_CHECK_THROW(test.get(), EndOfFileException);
00107 }
00108
00109
00110
00111 #if !defined __APPLE__ && !defined __MINGW32__
00112 BOOST_AUTO_TEST_CASE(StreamZipReadLine) {
00113 std::fstream ref(FileLocation().c_str(), std::ios::in);
00114
00115 std::string command("gzip <\"");
00116 command += FileLocation() + "\"";
00117
00118 FILE * catter = popen(command.c_str(), "r");
00119 BOOST_REQUIRE(catter);
00120
00121 FilePiece test(dup(fileno(catter)), "file_piece.cc.gz", NULL, 1);
00122 std::string ref_line;
00123 while (getline(ref, ref_line)) {
00124 StringPiece test_line(test.ReadLine());
00125
00126 if (!test_line.empty() || !ref_line.empty()) {
00127 BOOST_CHECK_EQUAL(ref_line, test_line);
00128 }
00129 }
00130 BOOST_CHECK_THROW(test.get(), EndOfFileException);
00131 BOOST_REQUIRE(!pclose(catter));
00132 }
00133 #endif // __APPLE__
00134
00135 #endif // HAVE_ZLIB
00136
00137 BOOST_AUTO_TEST_CASE(Numbers) {
00138 scoped_fd file(MakeTemp(FileLocation()));
00139 const float floating = 3.2;
00140 {
00141 util::FileStream writing(file.get());
00142 writing << "94389483984398493890287 " << floating << " 5";
00143 }
00144 SeekOrThrow(file.get(), 0);
00145 util::FilePiece f(file.release());
00146 BOOST_CHECK_THROW(f.ReadULong(), ParseNumberException);
00147 BOOST_CHECK_EQUAL("94389483984398493890287", f.ReadDelimited());
00148
00149 BOOST_CHECK_EQUAL(floating, f.ReadFloat());
00150 BOOST_CHECK_EQUAL(5, f.ReadULong());
00151 }
00152
00153 }
00154 }