Skip to content

Commit e848ce3

Browse files
Lucjan BryndzaLucjan Bryndza
andauthored
[EGD-4139] VFS unit tests for lseek (#885)
* [EGD-4139] VFS unit tests for lseek * [EGD-4139] lseek fat VFS test Co-authored-by: Lucjan Bryndza <[email protected]>
1 parent 8d3720d commit e848ce3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

module-vfs/tests/unittest_vfs.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include <thread.hpp>
1717

18+
#include <vector>
19+
20+
#include <algorithm>
21+
1822
class vfs vfs;
1923

2024
struct vfs_initializer
@@ -140,3 +144,39 @@ TEST_CASE("File loading and saving")
140144
REQUIRE(strcmp(fileContents.c_str(), "this is a test") == 0);
141145
REQUIRE(vfs.remove("module-vfs/test_dirx/testWrite.txt") == 0);
142146
}
147+
148+
TEST_CASE("VFS lseek check")
149+
{
150+
static constexpr auto seek_filename = "lseek_test.fil";
151+
static constexpr auto buf_size = 1024U;
152+
static constexpr auto buf_items = 1024U;
153+
static constexpr auto buf_elems = buf_size * buf_items;
154+
auto fd = vfs.fopen(seek_filename, "w+");
155+
REQUIRE(fd != nullptr);
156+
std::vector<int> buffer(buf_size);
157+
for (auto record = 0U; record < buf_items; ++record) {
158+
std::iota(std::begin(buffer), std::end(buffer), record * buf_size);
159+
REQUIRE(vfs.fwrite(buffer.data(), buffer.size(), 1, fd) == 1);
160+
}
161+
REQUIRE(vfs.fclose(fd) == 0);
162+
fd = vfs.fopen(seek_filename, "r");
163+
REQUIRE(fd != nullptr);
164+
std::vector<int> buf_out(buf_size);
165+
static constexpr auto offs_seek1 = 256;
166+
REQUIRE(vfs.fseek(fd, offs_seek1, SEEK_SET) == 0);
167+
REQUIRE(vfs.ftell(fd) == offs_seek1);
168+
REQUIRE(vfs.fread(buf_out.data(), buf_out.size(), 1, fd) == 1);
169+
std::iota(std::begin(buffer), std::end(buffer), offs_seek1);
170+
REQUIRE(buffer == buf_out);
171+
172+
REQUIRE(vfs.fseek(fd, 0UL, SEEK_END) == 0);
173+
REQUIRE(vfs.ftell(fd) == buf_elems);
174+
REQUIRE(vfs.fseek(fd, buf_elems + 10, SEEK_SET) < 0);
175+
REQUIRE(vfs.ftell(fd) == buf_elems);
176+
177+
REQUIRE(vfs.fseek(fd, 0UL, SEEK_SET) == 0);
178+
REQUIRE(vfs.fread(buf_out.data(), buf_out.size(), 1, fd) == 1);
179+
std::iota(std::begin(buffer), std::end(buffer), 0);
180+
REQUIRE(buffer == buf_out);
181+
REQUIRE(vfs.fclose(fd) == 0);
182+
}

0 commit comments

Comments
 (0)