14static void createFile(
const std::string& filename,
const std::string& content)
16 std::ofstream of(filename);
20static std::vector<std::string> readLines(
const std::string& filename)
22 std::vector<std::string> result;
23 std::ifstream is(filename);
25 while (std::getline(is, line)) {
26 result.push_back(line);
36 createFile(tmp +
"/a",
"aaa");
37 createFile(tmp +
"/a2",
"aaa");
38 createFile(tmp +
"/b",
"bbb");
40 auto getDirectories = [&] {
42 result.emplace_back(tmp, FileType::ROM);
50 [](std::string_view,
float) { });
54 auto file = pool.
getFile(FileType::ROM,
Sha1Sum(
"7e240de74fb1ed08fa08d38063f6a6a91462a815"));
55 CHECK(file.is_open());
56 CHECK(file.getURL() ==
one_of(tmp +
"/a", tmp +
"/a2"));
60 auto file = pool.getFile(FileType::ROM,
Sha1Sum(
"f36b4825e5db2cf7dd2d2593b3f5c24c0311d8b2"));
61 CHECK(!file.is_open());
65 auto file = pool.getFile(FileType::ROM,
Sha1Sum(
"5cb138284d431abd6a053a56625ec088bfb88912"));
66 CHECK(file.is_open());
67 CHECK(file.getURL() == tmp +
"/b");
70 createFile(tmp +
"/c",
"ccc");
72 auto file = pool.getFile(FileType::ROM,
Sha1Sum(
"f36b4825e5db2cf7dd2d2593b3f5c24c0311d8b2"));
73 CHECK(file.is_open());
74 CHECK(file.getURL() == tmp +
"/c");
79 createFile(tmp +
"/b",
"BBB");
81 auto file1 = pool.getFile(FileType::ROM,
Sha1Sum(
"5cb138284d431abd6a053a56625ec088bfb88912"));
82 CHECK(!file1.is_open());
83 auto file2 = pool.getFile(FileType::ROM,
Sha1Sum(
"aa6878b1c31a9420245df1daffb7b223338737a3"));
84 CHECK(file2.is_open());
85 CHECK(file2.getURL() == tmp +
"/b");
90 createFile(tmp +
"/b",
"BBB");
92 auto file = pool.getFile(FileType::ROM,
Sha1Sum(
"aa6878b1c31a9420245df1daffb7b223338737a3"));
93 CHECK(file.is_open());
94 CHECK(file.getURL() == tmp +
"/b");
99 auto file = pool.getFile(FileType::ROM,
Sha1Sum(
"aa6878b1c31a9420245df1daffb7b223338737a3"));
100 CHECK(!file.is_open());
105 File file(tmp +
"/a2");
107 auto sum = pool.getSha1Sum(file);
111 createFile(tmp +
"/e",
"eee");
113 File file(tmp +
"/e");
115 auto sum = pool.getSha1Sum(file);
121 auto lines = readLines(tmp +
"/cache");
122 CHECK(lines.size() == 4);
123 CHECK(lines[0].starts_with(
"637a81ed8e8217bb01c15c67c39b43b0ab4e20f1"));
124 CHECK(lines[0].ends_with(tmp +
"/e"));
125 CHECK(lines[1].starts_with(
"7e240de74fb1ed08fa08d38063f6a6a91462a815"));
126 CHECK((lines[1].ends_with(tmp +
"/a") ||
127 lines[1].ends_with(tmp +
"/a2")));
128 CHECK(lines[2].starts_with(
"7e240de74fb1ed08fa08d38063f6a6a91462a815"));
129 CHECK((lines[2].ends_with(tmp +
"/a") ||
130 lines[2].ends_with(tmp +
"/a2")));
131 CHECK(lines[3].starts_with(
"f36b4825e5db2cf7dd2d2593b3f5c24c0311d8b2"));
132 CHECK(lines[3].ends_with(tmp +
"/c"));
TEST_CASE("FilePoolCore")
File getFile(FileType fileType, const Sha1Sum &sha1sum)
Search file with the given sha1sum.
std::vector< Dir > Directories
bool is_open() const
Return true iff this file handle refers to an open file.
This class represents the result of a sha1 calculation (a 160-bit value).
string getTempDir()
Get the name of the temp directory on the system.
int deleteRecursive(const std::string &path)
void mkdirp(string path)
Acts like the unix command "mkdir -p".
int unlink(zstring_view path)
Call unlink() in a platform-independent manner.
void sleep(uint64_t us)
Sleep for the specified amount of time (in us).
This file implemented 3 utility functions:
constexpr auto sum(InputRange &&range, Proj proj={})