forked from rogersce/cnpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mmap_util.cpp
More file actions
120 lines (105 loc) · 3.89 KB
/
test_mmap_util.cpp
File metadata and controls
120 lines (105 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// test_mmap_util.cpp
#include "mmap_util.h"
#include <catch2/catch_test_macros.hpp>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <fstream>
#include <string>
#include <unistd.h>
#include <utility>
TEST_CASE("MMapFile read-only mapping", "[mmap]") {
const std::string filename = "test_mmap_file.bin";
// Create a temporary file with known content
{
std::ofstream ofs(filename, std::ios::binary);
std::string data = "HelloWorld";
ofs.write(data.c_str(), data.size());
}
// Open file in read-only mode
cnpy::MMapFile mmapFile(filename, "r");
REQUIRE(mmapFile.is_open());
REQUIRE(mmapFile.is_readonly());
REQUIRE(mmapFile.size() == 10);
REQUIRE(std::memcmp(mmapFile.data(), "HelloWorld", 10) == 0);
// Move constructor
cnpy::MMapFile moved(std::move(mmapFile));
REQUIRE(moved.is_open());
REQUIRE(!mmapFile.is_open()); // original should be empty
// Clean up
std::remove(filename.c_str());
}
TEST_CASE("MMapFile read-write mapping and move assignment", "[mmap]") {
const std::string filename = "test_mmap_file_rw.bin";
// Create a temporary file with known content
{
std::ofstream ofs(filename, std::ios::binary);
std::string data = "ABCDEFGHIJ";
ofs.write(data.c_str(), data.size());
}
// Open file in read-write mode
cnpy::MMapFile mmapFile(filename, "rw");
REQUIRE(mmapFile.is_open());
REQUIRE(!mmapFile.is_readonly());
REQUIRE(mmapFile.size() == 10);
// Modify the mapped data
std::memcpy(mmapFile.data(), "12345", 5);
REQUIRE(std::memcmp(mmapFile.data(), "12345FGHIJ", 10) == 0);
// Move assignment
cnpy::MMapFile other(filename, "r");
other = std::move(mmapFile);
REQUIRE(other.is_open());
REQUIRE(!mmapFile.is_open());
// Ensure data is still correct after move
REQUIRE(std::memcmp(other.data(), "12345FGHIJ", 10) == 0);
// other will be destroyed at end of scope, flushing changes
}
// Verify that changes were written to disk
TEST_CASE("MMapFile changes persisted to file", "[mmap]") {
const std::string filename = "test_mmap_file_rw.bin";
std::ifstream ifs(filename, std::ios::binary);
std::string fileContent(10, '\0');
ifs.read(&fileContent[0], 10);
REQUIRE(fileContent == "12345FGHIJ");
std::remove(filename.c_str());
}
TEST_CASE("MMapFile mapping from file descriptor", "[mmap]") {
const std::string filename = "test_mmap_fd.bin";
// Create a temporary file with known content
{
std::ofstream ofs(filename, std::ios::binary);
std::string data = "DataFDTest";
ofs.write(data.c_str(), data.size());
}
// Open file descriptor for read-only
int fd = ::open(filename.c_str(), O_RDONLY);
REQUIRE(fd >= 0);
cnpy::MMapFile mmapFd(fd, "r");
REQUIRE(mmapFd.is_open());
REQUIRE(mmapFd.is_readonly());
REQUIRE(mmapFd.size() == 10);
REQUIRE(std::memcmp(mmapFd.data(), "DataFDTest", 10) == 0);
// Invalid file descriptors should throw
REQUIRE_THROWS_AS(cnpy::MMapFile(-1, "r"), std::invalid_argument);
REQUIRE_THROWS_AS(cnpy::MMapFile(STDIN_FILENO, "r"), std::invalid_argument);
::close(fd);
std::remove(filename.c_str());
}
TEST_CASE("MMapFile invalid file handling", "[mmap]") {
// Non-existent file should throw
REQUIRE_THROWS_AS(cnpy::MMapFile("non_existent_file.bin", "r"), std::runtime_error);
// Invalid mode should default to read-only
const std::string filename = "test_invalid_mode.bin";
{
std::ofstream ofs(filename, std::ios::binary);
std::string data = "InvalidMode";
ofs.write(data.c_str(), data.size());
}
cnpy::MMapFile mmapFile(filename, "invalid_mode");
REQUIRE(mmapFile.is_open());
REQUIRE(mmapFile.is_readonly());
REQUIRE(mmapFile.size() == 11);
REQUIRE(std::memcmp(mmapFile.data(), "InvalidMode", 11) == 0);
// Clean up
std::remove(filename.c_str());
}