OXYGENHUB
◆ main openmsx / src / unittest / MappedFile_test.cc
1.4 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "catch.hpp"
2
3
#include "FileOperations.hh"
4
#include "LocalFile.hh"
5
#include "MappedFile.hh"
6
#include "scope_exit.hh"
7
8
#include <algorithm>
9
#include <array>
10
#include <span>
11
12
using namespace openmsx;
13
14
TEST_CASE("MappedFile preserves the local file position")
15
{
16
	std::string filename;
17
	auto temporary = FileOperations::openUniqueFile(FileOperations::getTempDir(), filename);
18
	REQUIRE(temporary);
19
	temporary.reset();
20
	scope_exit cleanup([&] { FileOperations::unlink(filename); });
21
22
	LocalFile file(filename, File::OpenMode::TRUNCATE);
23
	const std::array<uint8_t, 5> content = {0x12, 0x34, 0x56, 0x78, 0x9a};
24
	file.write(content);
25
	file.flush();
26
27
	for (size_t position : {size_t(0), size_t(2), content.size()}) {
28
		for (size_t extra : {size_t(0), size_t(3)}) {
29
			CAPTURE(position, extra);
30
			file.seek(position);
31
			// A file-pool hash calculation followed by ROM loading maps
32
			// the same file twice. Neither mapping may consume the file.
33
			for (int repeat = 0; repeat < 2; ++repeat) {
34
				CAPTURE(repeat);
35
				MappedFile<const uint8_t> mapped = file.mmap(extra, true);
36
				CHECK(file.getPos() == position);
37
				REQUIRE(mapped.size() == content.size() + extra);
38
				CHECK(std::ranges::equal(content, std::span(mapped.data(), content.size())));
39
				CHECK(std::all_of(mapped.begin() + content.size(), mapped.end(),
40
				                 [](uint8_t value) { return value == 0; }));
41
			}
42
		}
43
	}
44
}