-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathTest_DiskFileSystem.cpp
More file actions
155 lines (130 loc) · 6.66 KB
/
Copy pathTest_DiskFileSystem.cpp
File metadata and controls
155 lines (130 loc) · 6.66 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// __________ ___ ______ _
// / ____/ __ \____ / (_)___ ___ / ____/___ ____ _(_)___ ___
// / /_ / / / / __ \/ / / __ \/ _ \ / __/ / __ \/ __ `/ / __ \/ _ `
// / __/ / /_/ / / / / / / / / / __/ / /___/ / / / /_/ / / / / / __/
// /_/ \____/_/ /_/_/_/_/ /_/\___/ /_____/_/ /_/\__, /_/_/ /_/\___/
// /____/
// FOnline Engine
// https://fonline.ru
// https://github.com/cvet/fonline
//
// MIT License
//
// Copyright (c) 2006 - 2026, Anton Tsvetinskiy aka cvet <cvet@tut.by>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "catch_amalgamated.hpp"
#include "DiskFileSystem.h"
#include "SafeArithmetics.h"
FO_BEGIN_NAMESPACE
static auto MakeTempTestDir(string_view name) -> string
{
const auto base = std::filesystem::temp_directory_path() / std::format("lf_{}_{}", name, std::chrono::steady_clock::now().time_since_epoch().count());
return fs_path_to_string(base);
}
TEST_CASE("DiskFileSystem")
{
SECTION("ReadWriteRenameAndRemoveRoundtrip")
{
const auto temp_dir = MakeTempTestDir("diskfs_roundtrip");
const auto file_path = strex(temp_dir).combine_path("nested/data.txt").str();
const auto renamed_path = strex(temp_dir).combine_path("nested/renamed.txt").str();
const string_view content {"hello filesystem"};
const auto removed_before_roundtrip = fs_remove_dir_tree(temp_dir);
ignore_unused(removed_before_roundtrip);
REQUIRE(fs_write_file(file_path, content));
CHECK(fs_exists(file_path));
CHECK_FALSE(fs_is_dir(file_path));
REQUIRE(fs_file_size(file_path).has_value());
CHECK(*fs_file_size(file_path) == content.size());
REQUIRE(fs_read_file(file_path).has_value());
CHECK(*fs_read_file(file_path) == content);
CHECK(fs_compare_file_content(file_path, {reinterpret_cast<const uint8_t*>(content.data()), content.size()}));
REQUIRE(fs_rename(file_path, renamed_path));
CHECK_FALSE(fs_exists(file_path));
CHECK(fs_exists(renamed_path));
REQUIRE(fs_remove_file(renamed_path));
CHECK_FALSE(fs_exists(renamed_path));
CHECK(fs_remove_dir_tree(temp_dir));
}
SECTION("IterateDirRespectsRecursiveFlag")
{
const auto temp_dir = MakeTempTestDir("diskfs_iterate");
const auto top_file = strex(temp_dir).combine_path("top.txt").str();
const auto nested_file = strex(temp_dir).combine_path("sub/nested.txt").str();
const auto removed_before_iterate = fs_remove_dir_tree(temp_dir);
ignore_unused(removed_before_iterate);
REQUIRE(fs_write_file(top_file, string_view {"top"}));
REQUIRE(fs_write_file(nested_file, string_view {"nested"}));
vector<string> flat_entries;
fs_iterate_dir(temp_dir, false, [&](string_view path, size_t, uint64_t) { flat_entries.emplace_back(path); });
CHECK(flat_entries.size() == 1);
CHECK(flat_entries.front() == "top.txt");
vector<string> recursive_entries;
fs_iterate_dir(temp_dir, true, [&](string_view path, size_t, uint64_t) { recursive_entries.emplace_back(path); });
CHECK(recursive_entries.size() == 2);
CHECK(std::ranges::find(recursive_entries, string {"top.txt"}) != recursive_entries.end());
CHECK(std::ranges::find(recursive_entries, string {"sub/nested.txt"}) != recursive_entries.end());
CHECK(fs_remove_dir_tree(temp_dir));
}
SECTION("TouchAndStreamHelpersWork")
{
const auto temp_dir = MakeTempTestDir("diskfs_stream");
const auto file_path = strex(temp_dir).combine_path("touch.bin").str();
const auto removed_before_stream = fs_remove_dir_tree(temp_dir);
ignore_unused(removed_before_stream);
REQUIRE(fs_create_directories(temp_dir));
REQUIRE(fs_touch_file(file_path));
CHECK(fs_exists(file_path));
CHECK(fs_last_write_time(file_path) != 0);
std::istringstream stream {string {"abcdef"}, std::ios::binary};
CHECK(stream_get_size(stream) == 6);
CHECK(stream_get_read_pos(stream) == 0);
array<char, 3> buf {};
REQUIRE(stream_read_exact(stream, buf.data(), buf.size()));
CHECK(string_view {buf.data(), buf.size()} == "abc");
CHECK(stream_get_read_pos(stream) == 3);
REQUIRE(stream_set_read_pos(stream, 1, std::ios_base::cur));
CHECK(stream_get_read_pos(stream) == 4);
REQUIRE(stream_read_exact(stream, buf.data(), 2));
CHECK(string_view {buf.data(), 2} == "ef");
CHECK(fs_remove_dir_tree(temp_dir));
}
SECTION("FileHashMatchesInMemoryReference")
{
const auto temp_dir = MakeTempTestDir("diskfs_hash");
const auto file_path = strex(temp_dir).combine_path("hash.bin").str();
const auto removed_before_hash = fs_remove_dir_tree(temp_dir);
ignore_unused(removed_before_hash);
const auto check_hash = [&file_path](size_t size) {
vector<uint8_t> data(size);
for (size_t index = 0; index < size; index++) {
data[index] = numeric_cast<uint8_t>((index * 37u + 11u) & 0xFFu);
}
REQUIRE(fs_write_file(file_path, data));
REQUIRE(fs_hash_file(file_path).has_value());
CHECK(*fs_hash_file(file_path) == fs_hash_data(data.data(), data.size()));
};
for (const auto size : {size_t(0), size_t(1), size_t(3), size_t(4), size_t(15), size_t(16), size_t(17), size_t(47), size_t(48), size_t(49), size_t(63), size_t(64), size_t(65), size_t(96), size_t(97), size_t(70000)}) {
check_hash(size);
}
CHECK(fs_remove_dir_tree(temp_dir));
}
}
FO_END_NAMESPACE