2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferAIO.h>
|
|
|
|
#include <Core/Defines.h>
|
2015-03-13 11:02:43 +00:00
|
|
|
|
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>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
2015-03-06 15:34:42 +00:00
|
|
|
|
2015-04-15 10:07:48 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2015-03-12 15:38:22 +00:00
|
|
|
void run();
|
|
|
|
void die(const std::string & msg);
|
2015-04-15 10:07:48 +00:00
|
|
|
void runTest(unsigned int num, const std::function<bool()> func);
|
|
|
|
std::string createTmpFile();
|
|
|
|
std::string generateString(size_t n);
|
2015-03-12 15:38:22 +00:00
|
|
|
|
|
|
|
bool test1();
|
|
|
|
bool test2();
|
|
|
|
bool test3();
|
|
|
|
bool test4();
|
2015-03-24 11:03:26 +00:00
|
|
|
bool test5();
|
2015-03-24 14:01:53 +00:00
|
|
|
bool test6();
|
2015-03-25 14:14:06 +00:00
|
|
|
bool test7();
|
|
|
|
bool test8();
|
|
|
|
bool test9();
|
|
|
|
bool test10();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::vector<std::function<bool()> > tests =
|
|
|
|
{
|
|
|
|
test1,
|
|
|
|
test2,
|
|
|
|
test3,
|
|
|
|
test4,
|
|
|
|
test5,
|
|
|
|
test6,
|
|
|
|
test7,
|
|
|
|
test8,
|
|
|
|
test9,
|
|
|
|
test10
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int num = 0;
|
|
|
|
for (const auto & test : tests)
|
|
|
|
{
|
|
|
|
++num;
|
|
|
|
runTest(num, test);
|
|
|
|
}
|
2015-03-12 15:38:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 15:34:42 +00:00
|
|
|
void die(const std::string & msg)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::cout << msg;
|
|
|
|
::exit(EXIT_FAILURE);
|
2015-03-06 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 10:07:48 +00:00
|
|
|
void runTest(unsigned int num, const std::function<bool()> func)
|
2015-03-12 15:38:22 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool ok;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ok = func();
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
std::cout << "Test " << num << " passed\n";
|
|
|
|
else
|
|
|
|
std::cout << "Test " << num << " failed\n";
|
2015-03-12 15:38:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 10:07:48 +00:00
|
|
|
std::string createTmpFile()
|
2015-03-10 16:34:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
char pattern[] = "/tmp/fileXXXXXX";
|
|
|
|
char * dir = ::mkdtemp(pattern);
|
|
|
|
if (dir == nullptr)
|
|
|
|
die("Could not create directory");
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::string(dir) + "/foo";
|
2015-04-15 10:07:48 +00:00
|
|
|
}
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2015-04-15 10:07:48 +00:00
|
|
|
std::string generateString(size_t n)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static const std::string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf;
|
|
|
|
buf.reserve(n);
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
buf += symbols[i % symbols.length()];
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return buf;
|
2015-04-15 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool test1()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-04-15 10:07:48 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-04-15 10:07:48 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-04-15 10:07:48 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.write(&buf[0], buf.length());
|
|
|
|
}
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-10 16:34:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (received == buf);
|
2015-03-10 16:34:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:28:43 +00:00
|
|
|
bool test2()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.write(&buf[0], buf.length() / 2);
|
|
|
|
out.seek(DEFAULT_AIO_FILE_BLOCK_SIZE, SEEK_CUR);
|
|
|
|
out.write(&buf[buf.length() / 2], buf.length() / 2);
|
|
|
|
}
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(0, buf.length() / 2) != buf.substr(0, buf.length() / 2))
|
|
|
|
return false;
|
|
|
|
if (received.substr(buf.length() / 2, DEFAULT_AIO_FILE_BLOCK_SIZE) != std::string(DEFAULT_AIO_FILE_BLOCK_SIZE, '\0'))
|
|
|
|
return false;
|
|
|
|
if (received.substr(buf.length() / 2 + DEFAULT_AIO_FILE_BLOCK_SIZE) != buf.substr(buf.length() / 2))
|
|
|
|
return false;
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-03-11 15:28:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 15:38:22 +00:00
|
|
|
bool test3()
|
2015-03-06 15:34:42 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-06 15:34:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.write(&buf[0], buf.length());
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t pos1 = out.getPositionInFile();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.truncate(buf.length() / 2);
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t pos2 = out.getPositionInFile();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pos1 != pos2)
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-06 15:34:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (received == buf.substr(0, buf.length() / 2));
|
2015-03-11 15:28:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 15:38:22 +00:00
|
|
|
bool test4()
|
2015-03-11 15:28:43 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-11 15:28:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.write(&buf[0], buf.length());
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t pos1 = out.getPositionInFile();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.truncate(3 * buf.length() / 2);
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t pos2 = out.getPositionInFile();
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pos1 != pos2)
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(0, buf.length()) != buf)
|
|
|
|
return false;
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(buf.length()) != std::string(buf.length() / 2, '\0'))
|
|
|
|
return false;
|
2015-03-12 15:38:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-03-10 16:34:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 11:03:26 +00:00
|
|
|
bool test5()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.seek(1, SEEK_SET);
|
|
|
|
out.write(&buf[0], buf.length());
|
|
|
|
}
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-24 11:03:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return received.substr(1) == buf;
|
2015-03-24 11:03:26 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 14:01:53 +00:00
|
|
|
bool test6()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf2 = "1111111111";
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.seek(3, SEEK_SET);
|
|
|
|
out.write(&buf[0], buf.length());
|
|
|
|
out.seek(-2 * DEFAULT_AIO_FILE_BLOCK_SIZE, SEEK_CUR);
|
|
|
|
out.write(&buf2[0], buf2.length());
|
|
|
|
}
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(3, 8 * DEFAULT_AIO_FILE_BLOCK_SIZE) != buf.substr(0, 8 * DEFAULT_AIO_FILE_BLOCK_SIZE))
|
|
|
|
return false;
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(3 + 8 * DEFAULT_AIO_FILE_BLOCK_SIZE, 10) != buf2)
|
|
|
|
return false;
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(13 + 8 * DEFAULT_AIO_FILE_BLOCK_SIZE) != buf.substr(10 + 8 * DEFAULT_AIO_FILE_BLOCK_SIZE))
|
|
|
|
return false;
|
2015-03-24 14:01:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-03-24 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 14:14:06 +00:00
|
|
|
bool test7()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf2 = "11111111112222222222";
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.seek(DEFAULT_AIO_FILE_BLOCK_SIZE - (buf2.length() / 2), SEEK_SET);
|
|
|
|
out.write(&buf2[0], buf2.length());
|
|
|
|
}
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.length() != 4106)
|
|
|
|
return false;
|
|
|
|
if (received.substr(0, 4086) != std::string(4086, '\0'))
|
|
|
|
return false;
|
|
|
|
if (received.substr(4086, 20) != buf2)
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-03-25 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 12:17:09 +00:00
|
|
|
bool test8()
|
2015-03-25 14:14:06 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf2 = "11111111112222222222";
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 2 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.seek(2 * DEFAULT_AIO_FILE_BLOCK_SIZE - (buf2.length() / 2), SEEK_SET);
|
|
|
|
out.write(&buf2[0], buf2.length());
|
|
|
|
}
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.length() != 8202)
|
|
|
|
return false;
|
|
|
|
if (received.substr(0, 8182) != std::string(8182, '\0'))
|
|
|
|
return false;
|
|
|
|
if (received.substr(8182, 20) != buf2)
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-03-25 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 12:17:09 +00:00
|
|
|
bool test9()
|
2015-03-25 14:14:06 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 3 * DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf2(DEFAULT_AIO_FILE_BLOCK_SIZE + 10, '1');
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 2 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.seek(3, SEEK_SET);
|
|
|
|
out.write(&buf[0], buf.length());
|
|
|
|
out.seek(-DEFAULT_AIO_FILE_BLOCK_SIZE, SEEK_CUR);
|
|
|
|
out.write(&buf2[0], buf2.length());
|
|
|
|
}
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(3, 2 * DEFAULT_AIO_FILE_BLOCK_SIZE) != buf.substr(0, 2 * DEFAULT_AIO_FILE_BLOCK_SIZE))
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (received.substr(3 + 2 * DEFAULT_AIO_FILE_BLOCK_SIZE, DEFAULT_AIO_FILE_BLOCK_SIZE + 10) != buf2)
|
|
|
|
return false;
|
2015-03-25 14:14:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-03-25 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 12:17:09 +00:00
|
|
|
bool test10()
|
2015-03-30 11:42:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string filename = createTmpFile();
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = 10 * DEFAULT_AIO_FILE_BLOCK_SIZE + 3;
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string buf = generateString(n);
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferAIO out(filename, 3 * DEFAULT_AIO_FILE_BLOCK_SIZE);
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (out.getFileName() != filename)
|
|
|
|
return false;
|
|
|
|
if (out.getFD() == -1)
|
|
|
|
return false;
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
out.write(&buf[0], buf.length());
|
|
|
|
}
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ifstream in(filename.c_str());
|
|
|
|
if (!in.is_open())
|
|
|
|
die("Could not open file");
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string received{ std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>() };
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
in.close();
|
|
|
|
fs::remove_all(fs::path(filename).parent_path().string());
|
2015-03-30 11:42:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return (received == buf);
|
2015-03-30 11:42:10 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
run();
|
|
|
|
return 0;
|
2015-03-06 15:34:42 +00:00
|
|
|
}
|