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 "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 }
TEST_CASE("MemoryBufferFile")
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition: File.cc:118
bool isReadOnly() const
Check if this file is readonly.
Definition: File.cc:154
void read(void *buffer, size_t num)
Read from file.
Definition: File.cc:93
size_t getSize()
Returns the size of this file.
Definition: File.cc:113
size_t getPos()
Get the current position of the read/write pointer.
Definition: File.cc:123
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:98
CHECK(m3==m3)
This file implemented 3 utility functions:
Definition: Autofire.cc:9
File memory_buffer_file(span< const uint8_t > buffer)
constexpr auto xrange(T e)
Definition: xrange.hh:155