make create_random_files() take a span instead of pointer, length parameters

This commit is contained in:
Arvid Norberg 2018-06-10 13:31:26 +02:00 committed by Arvid Norberg
parent b26467c04e
commit 0e49051f17
6 changed files with 47 additions and 53 deletions

View File

@ -109,11 +109,10 @@ TORRENT_TEST(no_truncate_checking)
std::shared_ptr<lt::torrent_info> create_multifile_torrent()
{
// the two first files are exactly the size of a piece
static const int file_sizes[] = { 0x40000, 0x40000, 4300, 0, 400, 4300, 6, 4};
const int num_files = sizeof(file_sizes)/sizeof(file_sizes[0]);
static std::array<const int, 8> const file_sizes{{ 0x40000, 0x40000, 4300, 0, 400, 4300, 6, 4}};
lt::file_storage fs;
create_random_files("test_torrent_dir", file_sizes, num_files, &fs);
create_random_files("test_torrent_dir", file_sizes, &fs);
lt::create_torrent t(fs, 0x40000, -1, {});
// calculate the hash for all pieces

View File

@ -595,18 +595,18 @@ std::vector<char> generate_piece(piece_index_t const idx, int const piece_size)
return ret;
}
lt::file_storage make_file_storage(const int file_sizes[], int num_files
lt::file_storage make_file_storage(span<const int> const file_sizes
, int const piece_size, std::string base_name)
{
using namespace lt;
file_storage fs;
for (int i = 0; i != num_files; ++i)
for (std::size_t i = 0; i != file_sizes.size(); ++i)
{
char filename[200];
std::snprintf(filename, sizeof(filename), "test%d", i);
std::snprintf(filename, sizeof(filename), "test%d", int(i));
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "%s%d", base_name.c_str()
, i / 5);
, int(i) / 5);
std::string full_path = combine_path(dirname, filename);
fs.add_file(full_path, file_sizes[i]);
@ -618,11 +618,11 @@ lt::file_storage make_file_storage(const int file_sizes[], int num_files
return fs;
}
std::shared_ptr<lt::torrent_info> make_torrent(const int file_sizes[]
, int const num_files, int const piece_size)
std::shared_ptr<lt::torrent_info> make_torrent(span<const int> const file_sizes
, int const piece_size)
{
using namespace lt;
file_storage fs = make_file_storage(file_sizes, num_files, piece_size);
file_storage fs = make_file_storage(file_sizes, piece_size);
lt::create_torrent ct(fs, piece_size, 0x4000
, lt::create_torrent::optimize_alignment);
@ -638,18 +638,18 @@ std::shared_ptr<lt::torrent_info> make_torrent(const int file_sizes[]
return std::make_shared<torrent_info>(buf, from_span);
}
void create_random_files(std::string const& path, const int file_sizes[], int num_files
void create_random_files(std::string const& path, span<const int> file_sizes
, file_storage* fs)
{
error_code ec;
aux::vector<char> random_data(300000);
for (int i = 0; i != num_files; ++i)
for (std::size_t i = 0; i != file_sizes.size(); ++i)
{
std::generate(random_data.begin(), random_data.end(), random_byte);
char filename[200];
std::snprintf(filename, sizeof(filename), "test%d", i);
std::snprintf(filename, sizeof(filename), "test%d", int(i));
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
std::string full_path = combine_path(path, dirname);
lt::create_directories(full_path, ec);
@ -668,7 +668,7 @@ void create_random_files(std::string const& path, const int file_sizes[], int nu
while (to_write > 0)
{
int const s = std::min(to_write, static_cast<int>(random_data.size()));
iovec_t b = { random_data.data(), size_t(s)};
iovec_t const b = { random_data.data(), size_t(s)};
f.writev(offset, b, ec);
if (ec) std::printf("failed to write file \"%s\": (%d) %s\n"
, full_path.c_str(), ec.value(), ec.message().c_str());

View File

@ -83,12 +83,12 @@ EXPORT void wait_for_listen(lt::session& ses, char const* name);
EXPORT void wait_for_downloading(lt::session& ses, char const* name);
EXPORT std::vector<char> generate_piece(lt::piece_index_t idx, int piece_size = 0x4000);
EXPORT lt::file_storage make_file_storage(const int file_sizes[], int num_files
EXPORT lt::file_storage make_file_storage(lt::span<const int> file_sizes
, int const piece_size, std::string base_name = "test_dir-");
EXPORT std::shared_ptr<lt::torrent_info> make_torrent(const int file_sizes[]
, int num_files, int piece_size);
EXPORT void create_random_files(std::string const& path, const int file_sizes[]
, int num_files, libtorrent::file_storage* fs = nullptr);
EXPORT std::shared_ptr<lt::torrent_info> make_torrent(lt::span<const int> file_sizes
, int piece_size);
EXPORT void create_random_files(std::string const& path, lt::span<const int> file_sizes
, libtorrent::file_storage* fs = nullptr);
EXPORT std::shared_ptr<lt::torrent_info> create_torrent(std::ostream* file = nullptr
, char const* name = "temporary", int piece_size = 16 * 1024, int num_pieces = 13

View File

@ -95,12 +95,11 @@ void test_checking(int flags)
std::srand(10);
int piece_size = 0x4000;
const int file_sizes[] =
{ 0, 5, 16 - 5, 16000, 17, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1
,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4 };
const int num_files = sizeof(file_sizes) / sizeof(file_sizes[0]);
static std::array<const int, 46> const file_sizes
{{ 0, 5, 16 - 5, 16000, 17, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1
,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4 }};
create_random_files("test_torrent_dir", file_sizes, num_files, &fs);
create_random_files("test_torrent_dir", file_sizes, &fs);
lt::create_torrent t(fs, piece_size, 0x4000
, lt::create_torrent::optimize_alignment);
@ -120,12 +119,12 @@ void test_checking(int flags)
// truncate every file in half
if (flags & incomplete_files)
{
for (int i = 0; i < num_files; ++i)
for (std::size_t i = 0; i < file_sizes.size(); ++i)
{
char name[1024];
std::snprintf(name, sizeof(name), "test%d", i);
std::snprintf(name, sizeof(name), "test%d", int(i));
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
std::string path = combine_path("test_torrent_dir", dirname);
path = combine_path(path, name);
@ -144,22 +143,22 @@ void test_checking(int flags)
std::printf("corrupt file test. overwriting files\n");
// increase the size of some files. When they're read only that forces
// the checker to open them in write-mode to truncate them
static const int file_sizes2[] =
{ 0, 5, 16 - 5, 16001, 30, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1
,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4};
create_random_files("test_torrent_dir", file_sizes2, num_files);
static std::array<const int, 46> const file_sizes2
{{ 0, 5, 16 - 5, 16001, 30, 10, 8000, 8000, 1,1,1,1,1,100,1,1,1,1,100,1,1,1,1,1,1
,1,1,1,1,1,1,13,65000,34,75,2,30,400,500,23000,900,43000,400,4300,6, 4}};
create_random_files("test_torrent_dir", file_sizes2);
}
// make the files read only
if (flags & read_only_files)
{
std::printf("making files read-only\n");
for (int i = 0; i < num_files; ++i)
for (std::size_t i = 0; i < file_sizes.size(); ++i)
{
char name[1024];
std::snprintf(name, sizeof(name), "test%d", i);
std::snprintf(name, sizeof(name), "test%d", int(i));
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
std::string path = combine_path("test_torrent_dir", dirname);
path = combine_path(path, name);
@ -251,12 +250,12 @@ void test_checking(int flags)
// make the files writable again
if (flags & read_only_files)
{
for (int i = 0; i < num_files; ++i)
for (std::size_t i = 0; i < file_sizes.size(); ++i)
{
char name[1024];
std::snprintf(name, sizeof(name), "test%d", i);
std::snprintf(name, sizeof(name), "test%d", int(i));
char dirname[200];
std::snprintf(dirname, sizeof(dirname), "test_dir%d", i / 5);
std::snprintf(dirname, sizeof(dirname), "test_dir%d", int(i) / 5);
std::string path = combine_path("test_torrent_dir", dirname);
path = combine_path(path, name);
@ -315,11 +314,10 @@ TORRENT_TEST(discrete_checking)
int const megabyte = 0x100000;
int const piece_size = 2 * megabyte;
int const file_sizes[] = { 9 * megabyte, 3 * megabyte };
int const num_files = sizeof(file_sizes) / sizeof(file_sizes[0]);
static std::array<int const, 2> const file_sizes{{ 9 * megabyte, 3 * megabyte }};
file_storage fs;
create_random_files("test_torrent_dir", file_sizes, num_files, &fs);
create_random_files("test_torrent_dir", file_sizes, &fs);
TEST_EQUAL(fs.num_files(), 2);
lt::create_torrent t(fs, piece_size, 1, lt::create_torrent::optimize_alignment);

View File

@ -71,10 +71,9 @@ void test_read_piece(int flags)
std::srand(10);
int piece_size = 0x4000;
static const int file_sizes[] = { 100000, 10000 };
static std::array<const int, 2> const file_sizes{{ 100000, 10000 }};
create_random_files(combine_path("tmp1_read_piece", "test_torrent")
, file_sizes, 2);
create_random_files(combine_path("tmp1_read_piece", "test_torrent"), file_sizes);
add_files(fs, combine_path("tmp1_read_piece", "test_torrent"));
lt::create_torrent t(fs, piece_size, 0x4000);

View File

@ -63,16 +63,14 @@ void test_remap_files(storage_mode_t storage_mode = storage_mode_sparse)
// create a torrent with 2 files, remap them into 3 files and make sure
// the file priorities don't break things
static const int file_sizes[] = {100000, 100000};
const int num_files = 2;
const int piece_size = 0x8000;
auto t = make_torrent(file_sizes, num_files, piece_size);
static std::array<const int, 2> const file_sizes{{100000, 100000}};
int const piece_size = 0x8000;
auto t = make_torrent(file_sizes, piece_size);
static const int remap_file_sizes[] = {10000, 10000, int(t->total_size() - 20000)};
int const num_new_files = 3;
static std::array<const int, 3> const remap_file_sizes
{{10000, 10000, int(t->total_size() - 20000)}};
file_storage fs = make_file_storage(remap_file_sizes, num_new_files
, piece_size, "multifile-");
file_storage fs = make_file_storage(remap_file_sizes, piece_size, "multifile-");
t->remap_files(fs);
@ -162,7 +160,7 @@ void test_remap_files(storage_mode_t storage_mode = storage_mode_sparse)
// just because we can read them back throught libtorrent, doesn't mean the
// files have hit disk yet (because of the cache). Retry a few times to try
// to pick up the files
for (file_index_t i(0); i < file_index_t(num_new_files); ++i)
for (file_index_t i(0); i < file_index_t(int(remap_file_sizes.size())); ++i)
{
std::string name = fs.file_path(i);
for (int j = 0; j < 10 && !exists(name); ++j)