|
15 | 15 |
|
16 | 16 | #include <thread.hpp>
|
17 | 17 |
|
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | + |
18 | 22 | class vfs vfs;
|
19 | 23 |
|
20 | 24 | struct vfs_initializer
|
@@ -140,3 +144,39 @@ TEST_CASE("File loading and saving")
|
140 | 144 | REQUIRE(strcmp(fileContents.c_str(), "this is a test") == 0);
|
141 | 145 | REQUIRE(vfs.remove("module-vfs/test_dirx/testWrite.txt") == 0);
|
142 | 146 | }
|
| 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