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 "
xrange.hh
"
5
6
using namespace
openmsx
;
7
8
TEST_CASE
(
"MemoryBufferFile"
)
9
{
10
uint8_t buffer[] = {
11
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
12
};
13
uint8_t tmp[100];
14
15
// create memory-backed-file
16
File
file =
memory_buffer_file
(buffer);
17
CHECK
(file.
getPos
() == 0);
18
CHECK
(file.
isReadOnly
());
19
CHECK
(file.
getSize
() == 10);
20
21
// seek and read small part
22
file.
seek
(2);
23
CHECK
(file.
getPos
() == 2);
24
file.
read
(tmp, 3);
25
CHECK
(file.
getPos
() == 5);
26
CHECK
(tmp[0] == 3);
27
CHECK
(tmp[1] == 4);
28
CHECK
(tmp[2] == 5);
29
30
// seek beyond end of file is ok, but reading there is not
31
file.
seek
(100);
32
CHECK
(file.
getPos
() == 100);
33
CHECK_THROWS(file.
read
(tmp, 2));
34
35
// try to read more than file size
36
file.
seek
(0);
37
CHECK
(file.
getPos
() == 0);
38
CHECK_THROWS(file.
read
(tmp, 100));
39
CHECK
(file.
getPos
() == 0);
40
41
// read full file
42
file.
seek
(0);
43
file.
read
(tmp, file.
getSize
());
44
for
(
auto
i :
xrange
(10)) {
45
CHECK
(tmp[i] == (i + 1));
46
}
47
CHECK
(file.
getPos
() == file.
getSize
());
48
49
// writing is not supported
50
CHECK_THROWS(file.
write
(tmp, file.
getSize
()));
51
}
File.hh
MemoryBufferFile.hh
TEST_CASE
TEST_CASE("MemoryBufferFile")
Definition:
MemoryBufferFile_test.cc:8
openmsx::File
Definition:
File.hh:16
openmsx::File::seek
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition:
File.cc:116
openmsx::File::isReadOnly
bool isReadOnly() const
Check if this file is readonly.
Definition:
File.cc:152
openmsx::File::read
void read(void *buffer, size_t num)
Read from file.
Definition:
File.cc:91
openmsx::File::getSize
size_t getSize()
Returns the size of this file.
Definition:
File.cc:111
openmsx::File::getPos
size_t getPos()
Get the current position of the read/write pointer.
Definition:
File.cc:121
openmsx::File::write
void write(const void *buffer, size_t num)
Write to file.
Definition:
File.cc:96
CHECK
CHECK(m3==m3)
openmsx
This file implemented 3 utility functions:
Definition:
Autofire.cc:9
openmsx::memory_buffer_file
File memory_buffer_file(std::span< const uint8_t > buffer)
Definition:
MemoryBufferFile.cc:60
xrange.hh
xrange
constexpr auto xrange(T e)
Definition:
xrange.hh:133
Generated on Sat Jul 23 2022 15:17:15 for openMSX by
1.9.1