OXYGENHUB
◆ main openmsx / src / unittest / MemoryBufferFile_test.cc
1.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "catch.hpp"
2
#include "MemoryBufferFile.hh"
3
4
#include "File.hh"
5
6
#include "ranges.hh"
7
#include "xrange.hh"
8
9
#include <array>
10
11
using namespace openmsx;
12
13
TEST_CASE("MemoryBufferFile")
14
{
15
	std::array<uint8_t, 10> buffer = {
16
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10
17
	};
18
	std::array<uint8_t, 100> tmp;
19
20
	// create memory-backed-file
21
	File file = memory_buffer_file(buffer);
22
	CHECK(file.getPos() == 0);
23
	CHECK(file.isReadOnly());
24
	CHECK(file.getSize() == 10);
25
26
	// seek and read small part
27
	file.seek(2);
28
	CHECK(file.getPos() == 2);
29
	file.read(subspan(tmp, 0, 3));
30
	CHECK(file.getPos() == 5);
31
	CHECK(tmp[0] == 3);
32
	CHECK(tmp[1] == 4);
33
	CHECK(tmp[2] == 5);
34
35
	// seek beyond end of file is ok, but reading there is not
36
	file.seek(100);
37
	CHECK(file.getPos() == 100);
38
	CHECK_THROWS(file.read(subspan(tmp, 0, 2)));
39
40
	// try to read more than file size
41
	file.seek(0);
42
	CHECK(file.getPos() == 0);
43
	CHECK_THROWS(file.read(subspan(tmp, 0, 100)));
44
	CHECK(file.getPos() == 0);
45
46
	// read full file
47
	file.seek(0);
48
	file.read(std::span{tmp.data(), file.getSize()});
49
	for (auto i : xrange(10)) {
50
		CHECK(tmp[i] == (i + 1));
51
	}
52
	CHECK(file.getPos() == file.getSize());
53
54
	// writing is not supported
55
	CHECK_THROWS(file.write(std::span{tmp.data(), file.getSize()}));
56
}