tests refactoring

This commit is contained in:
gabime
2015-12-13 14:59:48 +02:00
parent 6feaa29c62
commit 3d420a3bcf
4 changed files with 46 additions and 43 deletions

View File

@@ -9,3 +9,37 @@ void prepare_logdir()
auto rv = system("rm -f logs/*");
#endif
}
std::string file_contents(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
std::size_t count_lines(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
std::string line;
size_t counter = 0;
while(std::getline(ifs, line))
counter++;
return counter;
}
std::ifstream::pos_type filesize(const std::string& filename)
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
throw std::runtime_error("Failed open file ");
return ifs.tellg();
}