mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 20:41:04 -04:00
test: support adding files with random contents
This commit is contained in:
parent
46bd4dd11d
commit
f5e97a3212
@ -24,6 +24,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
@ -258,7 +259,7 @@ void os_access_mock::add_dir(fs::path const& path) {
|
|||||||
add(path, st);
|
add(path, st);
|
||||||
}
|
}
|
||||||
|
|
||||||
void os_access_mock::add_file(fs::path const& path, size_t size) {
|
void os_access_mock::add_file(fs::path const& path, size_t size, bool random) {
|
||||||
simplestat st;
|
simplestat st;
|
||||||
std::memset(&st, 0, sizeof(st));
|
std::memset(&st, 0, sizeof(st));
|
||||||
st.ino = ino_++;
|
st.ino = ino_++;
|
||||||
@ -266,6 +267,29 @@ void os_access_mock::add_file(fs::path const& path, size_t size) {
|
|||||||
st.uid = 1000;
|
st.uid = 1000;
|
||||||
st.gid = 100;
|
st.gid = 100;
|
||||||
st.size = size;
|
st.size = size;
|
||||||
|
|
||||||
|
if (random) {
|
||||||
|
thread_local std::mt19937_64 rng{42};
|
||||||
|
|
||||||
|
std::uniform_int_distribution<> choice_dist{0, 3};
|
||||||
|
|
||||||
|
switch (choice_dist(rng)) {
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
add(path, st, [size, seed = rng()] {
|
||||||
|
std::mt19937_64 tmprng{seed};
|
||||||
|
std::string rv;
|
||||||
|
rv.resize(size);
|
||||||
|
std::uniform_int_distribution<> byte_dist{0, 255};
|
||||||
|
std::generate(rv.begin(), rv.end(), [&] { return byte_dist(tmprng); });
|
||||||
|
return rv;
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
add(path, st, [size] { return loremipsum(size); });
|
add(path, st, [size] { return loremipsum(size); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,8 @@ class os_access_mock : public os_access {
|
|||||||
std::function<std::string()> generator);
|
std::function<std::string()> generator);
|
||||||
|
|
||||||
void add_dir(std::filesystem::path const& path);
|
void add_dir(std::filesystem::path const& path);
|
||||||
void add_file(std::filesystem::path const& path, size_t size);
|
void
|
||||||
|
add_file(std::filesystem::path const& path, size_t size, bool random = false);
|
||||||
void add_file(std::filesystem::path const& path, std::string const& contents);
|
void add_file(std::filesystem::path const& path, std::string const& contents);
|
||||||
|
|
||||||
void set_access_fail(std::filesystem::path const& path);
|
void set_access_fail(std::filesystem::path const& path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user