2015-03-06 15:34:42 +00:00
|
|
|
#include <DB/IO/WriteBufferAIO.h>
|
2015-03-11 13:51:59 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-03-06 15:34:42 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
2015-03-11 15:28:43 +00:00
|
|
|
#include <functional>
|
2015-03-06 15:34:42 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
2015-03-06 15:34:42 +00:00
|
|
|
|
|
|
|
void die(const std::string & msg)
|
|
|
|
{
|
|
|
|
std::cout << msg;
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
bool test1()
|
|
|
|
{
|
2015-03-11 13:51:59 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
|
|
|
|
char pattern[] = "/tmp/fileXXXXXX";
|
|
|
|
char * dir = ::mkdtemp(pattern);
|
|
|
|
if (dir == nullptr)
|
|
|
|
die("Could not create directory");
|
|
|
|
|
2015-03-11 13:51:59 +00:00
|
|
|
const std::string directory = std::string(dir);
|
|
|
|
const std::string filename = directory + "/foo";
|
2015-03-10 16:34:00 +00:00
|
|
|
|
|
|
|
size_t n = 10 * DB::WriteBufferAIO::BLOCK_SIZE;
|
|
|
|
|
|
|
|
std::string buf;
|
|
|
|
buf.reserve(n);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
buf += symbols[i % symbols.length()];
|
|
|
|
|
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DB::WriteBufferAIO::BLOCK_SIZE);
|
|
|
|
|
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out.write(&buf[0], buf.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
|
|
|
|
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
|
|
|
|
|
|
|
in.close();
|
2015-03-11 13:51:59 +00:00
|
|
|
fs::remove_all(directory);
|
2015-03-10 16:34:00 +00:00
|
|
|
|
|
|
|
return (received == buf);
|
|
|
|
}
|
|
|
|
|
2015-03-11 15:28:43 +00:00
|
|
|
bool test2()
|
|
|
|
{
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
|
|
|
|
char pattern[] = "/tmp/fileXXXXXX";
|
|
|
|
char * dir = ::mkdtemp(pattern);
|
|
|
|
if (dir == nullptr)
|
|
|
|
die("Could not create directory");
|
|
|
|
|
|
|
|
const std::string directory = std::string(dir);
|
|
|
|
const std::string filename = directory + "/foo";
|
|
|
|
|
|
|
|
size_t n = 10 * DB::WriteBufferAIO::BLOCK_SIZE;
|
|
|
|
|
|
|
|
std::string buf;
|
|
|
|
buf.reserve(n);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
buf += symbols[i % symbols.length()];
|
|
|
|
|
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DB::WriteBufferAIO::BLOCK_SIZE);
|
|
|
|
|
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out.write(&buf[0], buf.length() / 2);
|
|
|
|
out.seek(DB::WriteBufferAIO::BLOCK_SIZE, SEEK_CUR);
|
|
|
|
out.write(&buf[buf.length() / 2], buf.length() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
|
|
|
|
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
fs::remove_all(directory);
|
|
|
|
|
|
|
|
if (received.substr(0, buf.length() / 2) != buf.substr(0, buf.length() / 2))
|
|
|
|
return false;
|
|
|
|
if (received.substr(buf.length() / 2, DB::WriteBufferAIO::BLOCK_SIZE) != std::string(DB::WriteBufferAIO::BLOCK_SIZE, '\0'))
|
|
|
|
return false;
|
|
|
|
if (received.substr(buf.length() / 2 + DB::WriteBufferAIO::BLOCK_SIZE) != buf.substr(buf.length() / 2))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_test(unsigned int num, const std::function<bool()> func)
|
2015-03-06 15:34:42 +00:00
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-03-11 15:28:43 +00:00
|
|
|
ok = func();
|
2015-03-06 15:34:42 +00:00
|
|
|
}
|
|
|
|
catch (const DB::Exception & ex)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
std::cout << "Caught exception " << ex.displayText() << "\n";
|
|
|
|
}
|
|
|
|
catch (const std::exception & ex)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
std::cout << "Caught exception " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
2015-03-11 15:28:43 +00:00
|
|
|
std::cout << "Test " << num << " passed\n";
|
2015-03-06 15:34:42 +00:00
|
|
|
else
|
2015-03-11 15:28:43 +00:00
|
|
|
std::cout << "Test " << num << " failed\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
const std::vector<std::function<bool()> > tests =
|
|
|
|
{
|
|
|
|
test1,
|
|
|
|
test2
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int num = 0;
|
|
|
|
for (const auto & test : tests)
|
|
|
|
{
|
|
|
|
++num;
|
|
|
|
run_test(num, test);
|
|
|
|
}
|
2015-03-10 16:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-03-06 15:34:42 +00:00
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
run();
|
2015-03-06 15:34:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|