mirror of
https://github.com/vimpunk/mio.git
synced 2025-12-06 16:57:01 +08:00
Add tests for mappings with different file offsets
This commit is contained in:
parent
eb6f4068d3
commit
dc885d0984
125
test/test.cpp
125
test/test.cpp
@ -15,29 +15,32 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int handle_error(const std::error_code& error)
|
|
||||||
{
|
|
||||||
const auto& errmsg = error.message();
|
|
||||||
std::printf("error mapping file: %s, exiting...\n", errmsg.c_str());
|
|
||||||
return error.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Just make sure this compiles.
|
// Just make sure this compiles.
|
||||||
#ifdef CXX17
|
#ifdef CXX17
|
||||||
# include <cstddef>
|
# include <cstddef>
|
||||||
using mmap_source = mio::basic_mmap_source<std::byte>;
|
using mmap_source = mio::basic_mmap_source<std::byte>;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
template<class MMap>
|
||||||
|
void test_at_offset(const MMap& file_view, const std::string& buffer,
|
||||||
|
const size_t offset);
|
||||||
|
void test_at_offset(const std::string& buffer, const char* path,
|
||||||
|
const size_t offset, std::error_code& error);
|
||||||
|
int handle_error(const std::error_code& error);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const char _path[] = "test-file";
|
std::error_code error;
|
||||||
|
|
||||||
// Make sure mio compiles with non-const char* strings too.
|
// Make sure mio compiles with non-const char* strings too.
|
||||||
|
const char _path[] = "test-file";
|
||||||
const int path_len = sizeof(_path);
|
const int path_len = sizeof(_path);
|
||||||
char* path = new char[path_len];
|
char* path = new char[path_len];
|
||||||
std::copy(_path, _path + path_len, path);
|
std::copy(_path, _path + path_len, path);
|
||||||
std::error_code error;
|
|
||||||
|
const auto page_size = mio::page_size();
|
||||||
// Fill buffer, then write it to file.
|
// Fill buffer, then write it to file.
|
||||||
const int file_size = 0x4000 - 250; // 16134
|
const int file_size = 4 * page_size - 250; // 16134, if page size is 4KiB
|
||||||
std::string buffer(file_size, 0);
|
std::string buffer(file_size, 0);
|
||||||
// Start at first printable ASCII character.
|
// Start at first printable ASCII character.
|
||||||
char v = 33;
|
char v = 33;
|
||||||
@ -50,54 +53,26 @@ int main()
|
|||||||
v = 33;
|
v = 33;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ofstream file(path);
|
std::ofstream file(path);
|
||||||
file << buffer;
|
file << buffer;
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
const size_t offset = 300;
|
// Test whole file mapping.
|
||||||
assert(offset < buffer.size());
|
test_at_offset(buffer, path, 0, error);
|
||||||
|
|
||||||
{
|
|
||||||
// Map the region of the file to which buffer was written.
|
|
||||||
mio::mmap_source file_view = mio::make_mmap_source(
|
|
||||||
path, offset, mio::map_entire_file, error);
|
|
||||||
if (error) { return handle_error(error); }
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
const size_t mapped_size = buffer.size() - offset; // 15834
|
// Test starting from below the page size.
|
||||||
assert(file_view.is_open());
|
test_at_offset(buffer, path, page_size - 3, error);
|
||||||
assert(file_view.size() == mapped_size);
|
if (error) { return handle_error(error); }
|
||||||
|
|
||||||
// Then verify that mmap's bytes correspond to that of buffer.
|
// Test starting from above the page size.
|
||||||
for(size_t buf_idx = offset, view_idx = 0;
|
test_at_offset(buffer, path, page_size + 3, error);
|
||||||
buf_idx < buffer.size() && view_idx < mapped_size;
|
if (error) { return handle_error(error); }
|
||||||
++buf_idx, ++view_idx) {
|
|
||||||
if(file_view[view_idx] != buffer[buf_idx]) {
|
|
||||||
std::printf("%luth byte mismatch: expected(%d) <> actual(%d)",
|
|
||||||
buf_idx, buffer[buf_idx], file_view[view_idx]);
|
|
||||||
std::cout << std::flush;
|
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Turn file_view into a shared mmap.
|
// Test starting from above the page size.
|
||||||
mio::shared_mmap_source shared_file_view(std::move(file_view));
|
test_at_offset(buffer, path, 2 * page_size + 3, error);
|
||||||
|
if (error) { return handle_error(error); }
|
||||||
assert(!file_view.is_open());
|
|
||||||
assert(shared_file_view.is_open());
|
|
||||||
assert(shared_file_view.size() == mapped_size);
|
|
||||||
|
|
||||||
// Then verify that shared_mmap's bytes correspond to that of buffer.
|
|
||||||
for(size_t buf_idx = offset, view_idx = 0;
|
|
||||||
buf_idx < buffer.size() && view_idx < mapped_size;
|
|
||||||
++buf_idx, ++view_idx) {
|
|
||||||
if(shared_file_view[view_idx] != buffer[buf_idx]) {
|
|
||||||
std::printf("%luth byte mismatch: expected(%d) <> actual(%d)",
|
|
||||||
buf_idx, buffer[buf_idx], shared_file_view[view_idx]);
|
|
||||||
std::cout << std::flush;
|
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
#define CHECK_INVALID_MMAP(m) do { \
|
#define CHECK_INVALID_MMAP(m) do { \
|
||||||
@ -155,3 +130,53 @@ int main()
|
|||||||
|
|
||||||
std::printf("all tests passed!\n");
|
std::printf("all tests passed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_at_offset(const std::string& buffer, const char* path,
|
||||||
|
const size_t offset, std::error_code& error)
|
||||||
|
{
|
||||||
|
// Sanity check.
|
||||||
|
assert(offset < buffer.size());
|
||||||
|
|
||||||
|
// Map the region of the file to which buffer was written.
|
||||||
|
mio::mmap_source file_view = mio::make_mmap_source(
|
||||||
|
path, offset, mio::map_entire_file, error);
|
||||||
|
if(error) { return; }
|
||||||
|
|
||||||
|
assert(file_view.is_open());
|
||||||
|
const size_t mapped_size = buffer.size() - offset;
|
||||||
|
assert(file_view.size() == mapped_size);
|
||||||
|
|
||||||
|
test_at_offset(file_view, buffer, offset);
|
||||||
|
|
||||||
|
// Turn file_view into a shared mmap.
|
||||||
|
mio::shared_mmap_source shared_file_view(std::move(file_view));
|
||||||
|
assert(!file_view.is_open());
|
||||||
|
assert(shared_file_view.is_open());
|
||||||
|
assert(shared_file_view.size() == mapped_size);
|
||||||
|
|
||||||
|
//test_at_offset(shared_file_view, buffer, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class MMap>
|
||||||
|
void test_at_offset(const MMap& file_view, const std::string& buffer,
|
||||||
|
const size_t offset)
|
||||||
|
{
|
||||||
|
// Then verify that mmap's bytes correspond to that of buffer.
|
||||||
|
for(size_t buf_idx = offset, view_idx = 0;
|
||||||
|
buf_idx < buffer.size() && view_idx < file_view.size();
|
||||||
|
++buf_idx, ++view_idx) {
|
||||||
|
if(file_view[view_idx] != buffer[buf_idx]) {
|
||||||
|
std::printf("%luth byte mismatch: expected(%d) <> actual(%d)",
|
||||||
|
buf_idx, buffer[buf_idx], file_view[view_idx]);
|
||||||
|
std::cout << std::flush;
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int handle_error(const std::error_code& error)
|
||||||
|
{
|
||||||
|
const auto& errmsg = error.message();
|
||||||
|
std::printf("Error mapping file: %s, exiting...\n", errmsg.c_str());
|
||||||
|
return error.value();
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user