openMSX
MemoryBufferFile_test.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "MemoryBufferFile.hh"
3 #include "File.hh"
4 
5 using namespace openmsx;
6 
7 TEST_CASE("MemoryBufferFile")
8 {
9  uint8_t buffer[] = {
10  1, 2, 3, 4, 5, 6, 7, 8, 9, 10
11  };
12  uint8_t tmp[100];
13 
14  // create memory-backed-file
15  File file = memory_buffer_file(buffer);
16  CHECK(file.getPos() == 0);
17  CHECK(file.isReadOnly());
18  CHECK(file.getSize() == 10);
19 
20  // seek and read small part
21  file.seek(2);
22  CHECK(file.getPos() == 2);
23  file.read(tmp, 3);
24  CHECK(file.getPos() == 5);
25  CHECK(tmp[0] == 3);
26  CHECK(tmp[1] == 4);
27  CHECK(tmp[2] == 5);
28 
29  // seek beyond end of file is ok, but reading there is not
30  file.seek(100);
31  CHECK(file.getPos() == 100);
32  CHECK_THROWS(file.read(tmp, 2));
33 
34  // try to read more than file size
35  file.seek(0);
36  CHECK(file.getPos() == 0);
37  CHECK_THROWS(file.read(tmp, 100));
38  CHECK(file.getPos() == 0);
39 
40  // read full file
41  file.seek(0);
42  file.read(tmp, file.getSize());
43  for (int i = 0; i < 10; ++i) {
44  CHECK(tmp[i] == (i + 1));
45  }
46  CHECK(file.getPos() == file.getSize());
47 
48  // writing is not supported
49  CHECK_THROWS(file.write(tmp, file.getSize()));
50 }
bool isReadOnly() const
Check if this file is readonly.
Definition: File.cc:144
size_t getSize()
Returns the size of this file.
Definition: File.cc:103
CHECK(m3==m3)
TEST_CASE("MemoryBufferFile")
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
size_t getPos()
Get the current position of the read/write pointer.
Definition: File.cc:113
File memory_buffer_file(span< const uint8_t > buffer)
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:88
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition: File.cc:108
void read(void *buffer, size_t num)
Read from file.
Definition: File.cc:83