add unit test

This commit is contained in:
Anton Popov 2022-02-06 02:57:49 +03:00 committed by mergify-bot
parent 10b8684003
commit ae1fc94fb5
2 changed files with 37 additions and 0 deletions

View File

@ -234,6 +234,7 @@ void ReadBufferFromFileDescriptor::rewind()
working_buffer.resize(0);
pos = working_buffer.begin();
file_offset_of_buffer_end = 0;
buffer_is_dirty = true;
}

View File

@ -0,0 +1,36 @@
#include <gtest/gtest.h>
#include <Common/filesystemHelpers.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/WriteHelpers.h>
#include <IO/ReadHelpers.h>
using namespace DB;
TEST(ReadBufferFromFile, seekBackwards)
{
static constexpr size_t N = 256;
static constexpr size_t BUF_SIZE = 64;
auto tmp_file = createTemporaryFile("/tmp/");
{
WriteBufferFromFile out(tmp_file->path());
for (size_t i = 0; i < N; ++i)
writeIntBinary(i, out);
}
ReadBufferFromFile in(tmp_file->path(), BUF_SIZE);
size_t x;
/// Read something to initialize the buffer.
in.seek(BUF_SIZE * 10, SEEK_SET);
readIntBinary(x, in);
/// Check 2 consecutive seek calls without reading.
in.seek(BUF_SIZE * 2, SEEK_SET);
in.seek(BUF_SIZE, SEEK_SET);
readIntBinary(x, in);
ASSERT_EQ(x, 8);
}