openMSX
MemoryBufferFile_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "MemoryBufferFile.hh"
3#include "File.hh"
4#include "ranges.hh"
5#include "xrange.hh"
6#include <array>
7
8using namespace openmsx;
9
10TEST_CASE("MemoryBufferFile")
11{
12 std::array<uint8_t, 10> buffer = {
13 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
14 };
15 std::array<uint8_t, 100> tmp;
16
17 // create memory-backed-file
18 File file = memory_buffer_file(buffer);
19 CHECK(file.getPos() == 0);
20 CHECK(file.isReadOnly());
21 CHECK(file.getSize() == 10);
22
23 // seek and read small part
24 file.seek(2);
25 CHECK(file.getPos() == 2);
26 file.read(subspan(tmp, 0, 3));
27 CHECK(file.getPos() == 5);
28 CHECK(tmp[0] == 3);
29 CHECK(tmp[1] == 4);
30 CHECK(tmp[2] == 5);
31
32 // seek beyond end of file is ok, but reading there is not
33 file.seek(100);
34 CHECK(file.getPos() == 100);
35 CHECK_THROWS(file.read(subspan(tmp, 0, 2)));
36
37 // try to read more than file size
38 file.seek(0);
39 CHECK(file.getPos() == 0);
40 CHECK_THROWS(file.read(subspan(tmp, 0, 100)));
41 CHECK(file.getPos() == 0);
42
43 // read full file
44 file.seek(0);
45 file.read(std::span{tmp.data(), file.getSize()});
46 for (auto i : xrange(10)) {
47 CHECK(tmp[i] == (i + 1));
48 }
49 CHECK(file.getPos() == file.getSize());
50
51 // writing is not supported
52 CHECK_THROWS(file.write(std::span{tmp.data(), file.getSize()}));
53}
TEST_CASE("MemoryBufferFile")
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition File.cc:117
bool isReadOnly() const
Check if this file is readonly.
Definition File.cc:153
void read(std::span< uint8_t > buffer)
Read from file.
Definition File.cc:92
void write(std::span< const uint8_t > buffer)
Write to file.
Definition File.cc:97
size_t getSize()
Returns the size of this file.
Definition File.cc:112
size_t getPos()
Get the current position of the read/write pointer.
Definition File.cc:122
CHECK(m3==m3)
This file implemented 3 utility functions:
Definition Autofire.cc:11
File memory_buffer_file(std::span< const uint8_t > buffer)
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:481
constexpr auto xrange(T e)
Definition xrange.hh:132