Repository navigation
Expand file tree
/
Copy pathmapped_file_test.cpp
More file actions
106 lines (89 loc) · 3.15 KB
/
Copy pathmapped_file_test.cpp
File metadata and controls
106 lines (89 loc) · 3.15 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
#include <trueform/io.hpp>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
namespace {
auto write_file(const std::string &path, std::string_view contents) -> bool {
std::ofstream output(path, std::ios::binary | std::ios::trunc);
if (!output)
return false;
output.write(contents.data(), static_cast<std::streamsize>(contents.size()));
return static_cast<bool>(output);
}
auto has_contents(const tf::io::mapped_file &file, std::string_view contents)
-> bool {
return file && file.size() == contents.size() &&
std::memcmp(file.data(), contents.data(), contents.size()) == 0;
}
auto is_empty(const tf::io::mapped_file &file) -> bool {
return !file && file.data() == nullptr && file.size() == 0;
}
} // namespace
static_assert(!std::is_copy_constructible_v<tf::io::mapped_file>);
static_assert(!std::is_copy_assignable_v<tf::io::mapped_file>);
static_assert(std::is_nothrow_move_constructible_v<tf::io::mapped_file>);
static_assert(std::is_nothrow_move_assignable_v<tf::io::mapped_file>);
int main(int argc, char **argv) {
if (argc != 2)
return 1;
const std::string first_path(argv[1]);
const auto second_path = first_path + ".second";
const auto empty_path = first_path + ".empty";
const auto missing_path = first_path + ".missing";
std::remove(first_path.c_str());
std::remove(second_path.c_str());
std::remove(empty_path.c_str());
std::remove(missing_path.c_str());
constexpr std::string_view first_contents = "first mapped file";
constexpr std::string_view second_contents = "second mapped file";
if (!write_file(first_path, first_contents) ||
!write_file(second_path, second_contents) || !write_file(empty_path, {}))
return 2;
tf::io::mapped_file assigned;
if (!is_empty(assigned))
return 3;
{
tf::io::mapped_file first(first_path);
if (!has_contents(first, first_contents))
return 4;
tf::io::mapped_file moved(std::move(first));
if (!is_empty(first) || !has_contents(moved, first_contents))
return 5;
assigned = tf::io::mapped_file(second_path);
if (!has_contents(assigned, second_contents))
return 6;
assigned = std::move(moved);
if (!is_empty(moved) || !has_contents(assigned, first_contents))
return 7;
}
if (!has_contents(assigned, first_contents))
return 8;
if (std::remove(second_path.c_str()) != 0 ||
!write_file(second_path, "replacement"))
return 9;
const tf::io::mapped_file empty(empty_path);
const tf::io::mapped_file missing(missing_path);
const tf::io::mapped_file no_path;
if (!is_empty(empty) || !is_empty(missing) || !is_empty(no_path))
return 10;
auto embedded_null_path = first_path;
embedded_null_path.push_back('\0');
embedded_null_path += ".suffix";
{
const tf::io::mapped_file embedded_null(embedded_null_path);
if (!has_contents(embedded_null, first_contents))
return 11;
}
assigned = {};
if (!is_empty(assigned))
return 12;
if (std::remove(first_path.c_str()) != 0 ||
std::remove(second_path.c_str()) != 0 ||
std::remove(empty_path.c_str()) != 0)
return 13;
return 0;
}