Consolidate file reading through the new FileReader class/interface. (#3321)
This commit is contained in:
+105
-4
@@ -1,22 +1,27 @@
|
||||
#include "util/io.hpp"
|
||||
#include "storage/io.hpp"
|
||||
#include "util/exception.hpp"
|
||||
#include "util/typedefs.hpp"
|
||||
|
||||
#include <boost/test/test_case_template.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <exception>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
|
||||
const static std::string IO_TMP_FILE = "test_io.tmp";
|
||||
const static std::string IO_NONEXISTENT_FILE = "non_existent_test_io.tmp";
|
||||
const static std::string IO_TOO_SMALL_FILE = "file_too_small_test_io.tmp";
|
||||
const static std::string IO_CORRUPT_FINGERPRINT_FILE = "corrupt_fingerprint_file_test_io.tmp";
|
||||
const static std::string IO_TEXT_FILE = "plain_text_file.tmp";
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(osrm_io)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(io_data)
|
||||
{
|
||||
std::vector<int> data_in, data_out;
|
||||
data_in.resize(53);
|
||||
for (std::size_t i = 0; i < data_in.size(); ++i)
|
||||
data_in[i] = i;
|
||||
std::vector<int> data_in(53), data_out;
|
||||
std::iota(begin(data_in), end(data_in), 0);
|
||||
|
||||
osrm::util::serializeVector(IO_TMP_FILE, data_in);
|
||||
|
||||
@@ -28,4 +33,100 @@ BOOST_AUTO_TEST_CASE(io_data)
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(data_out.begin(), data_out.end(), data_in.begin(), data_in.end());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(io_nonexistent_file)
|
||||
{
|
||||
try
|
||||
{
|
||||
osrm::storage::io::FileReader infile(IO_NONEXISTENT_FILE,
|
||||
osrm::storage::io::FileReader::VerifyFingerprint);
|
||||
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
||||
}
|
||||
catch (const osrm::util::exception &e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
BOOST_REQUIRE(std::string(e.what()) ==
|
||||
"Error opening non_existent_test_io.tmp:No such file or directory");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(file_too_small)
|
||||
{
|
||||
{
|
||||
std::vector<int> v(53);
|
||||
std::iota(begin(v), end(v), 0);
|
||||
|
||||
osrm::util::serializeVector(IO_TOO_SMALL_FILE, v);
|
||||
|
||||
std::ofstream f(IO_TOO_SMALL_FILE);
|
||||
f.seekp(0, std::ios_base::beg);
|
||||
std::uint64_t garbage = 0xDEADBEEFCAFEFACE;
|
||||
f.write(reinterpret_cast<char *>(&garbage), sizeof(garbage));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
osrm::storage::io::FileReader infile(IO_TOO_SMALL_FILE,
|
||||
osrm::storage::io::FileReader::VerifyFingerprint);
|
||||
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
||||
}
|
||||
catch (const osrm::util::exception &e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
BOOST_REQUIRE(std::string(e.what()) ==
|
||||
"Error reading from file_too_small_test_io.tmp: Unexpected end of file");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(io_corrupt_fingerprint)
|
||||
{
|
||||
{
|
||||
std::vector<int> v(153);
|
||||
std::iota(begin(v), end(v), 0);
|
||||
osrm::util::serializeVector(IO_CORRUPT_FINGERPRINT_FILE, v);
|
||||
|
||||
std::fstream f(IO_CORRUPT_FINGERPRINT_FILE);
|
||||
f.seekp(0, std::ios_base::beg);
|
||||
std::uint64_t garbage = 0xDEADBEEFCAFEFACE;
|
||||
f.write(reinterpret_cast<char *>(&garbage), sizeof(garbage));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
osrm::storage::io::FileReader infile(IO_CORRUPT_FINGERPRINT_FILE,
|
||||
osrm::storage::io::FileReader::VerifyFingerprint);
|
||||
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
||||
}
|
||||
catch (const osrm::util::exception &e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
BOOST_REQUIRE(std::string(e.what()) ==
|
||||
"Fingerprint mismatch in corrupt_fingerprint_file_test_io.tmp");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(io_read_lines)
|
||||
{
|
||||
{
|
||||
std::ofstream f(IO_TEXT_FILE);
|
||||
char str[] = "A\nB\nC\nD";
|
||||
f.write(str, strlen(str));
|
||||
}
|
||||
{
|
||||
osrm::storage::io::FileReader infile(IO_TEXT_FILE,
|
||||
osrm::storage::io::FileReader::HasNoFingerprint);
|
||||
auto startiter = infile.GetLineIteratorBegin();
|
||||
auto enditer = infile.GetLineIteratorEnd();
|
||||
std::vector<std::string> resultlines;
|
||||
while (startiter != enditer) {
|
||||
resultlines.push_back(*startiter);
|
||||
++startiter;
|
||||
}
|
||||
BOOST_REQUIRE_MESSAGE(resultlines.size() == 4, "Expected 4 lines of text");
|
||||
BOOST_REQUIRE_MESSAGE(resultlines[0] == "A", "Expected the first line to be A");
|
||||
BOOST_REQUIRE_MESSAGE(resultlines[1] == "B", "Expected the first line to be B");
|
||||
BOOST_REQUIRE_MESSAGE(resultlines[2] == "C", "Expected the first line to be C");
|
||||
BOOST_REQUIRE_MESSAGE(resultlines[3] == "D", "Expected the first line to be D");
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user