openMSX
unittest
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
8
using namespace
openmsx
;
9
10
TEST_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
}
File.hh
MemoryBufferFile.hh
TEST_CASE
TEST_CASE("MemoryBufferFile")
Definition
MemoryBufferFile_test.cc:10
openmsx::File
Definition
File.hh:18
openmsx::File::seek
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition
File.cc:117
openmsx::File::isReadOnly
bool isReadOnly() const
Check if this file is readonly.
Definition
File.cc:153
openmsx::File::read
void read(std::span< uint8_t > buffer)
Read from file.
Definition
File.cc:92
openmsx::File::write
void write(std::span< const uint8_t > buffer)
Write to file.
Definition
File.cc:97
openmsx::File::getSize
size_t getSize()
Returns the size of this file.
Definition
File.cc:112
openmsx::File::getPos
size_t getPos()
Get the current position of the read/write pointer.
Definition
File.cc:122
CHECK
CHECK(m3==m3)
openmsx
This file implemented 3 utility functions:
Definition
Autofire.cc:11
openmsx::memory_buffer_file
File memory_buffer_file(std::span< const uint8_t > buffer)
Definition
MemoryBufferFile.cc:60
ranges.hh
subspan
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition
ranges.hh:481
xrange.hh
xrange
constexpr auto xrange(T e)
Definition
xrange.hh:132
Generated on Thu Dec 19 2024 23:13:46 for openMSX by
1.9.8