diff --git a/simulation/test_checking.cpp b/simulation/test_checking.cpp index c9707d83f..3be86f3a6 100644 --- a/simulation/test_checking.cpp +++ b/simulation/test_checking.cpp @@ -109,11 +109,10 @@ TORRENT_TEST(no_truncate_checking) std::shared_ptr 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 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 diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index c0205c5df..aa4a2a4cc 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -595,18 +595,18 @@ std::vector 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 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 make_torrent(const int file_sizes[] - , int const num_files, int const piece_size) +std::shared_ptr make_torrent(span 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 make_torrent(const int file_sizes[] return std::make_shared(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 file_sizes , file_storage* fs) { error_code ec; aux::vector 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(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()); diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 200156518..523227453 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -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 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 file_sizes , int const piece_size, std::string base_name = "test_dir-"); -EXPORT std::shared_ptr 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 make_torrent(lt::span file_sizes + , int piece_size); +EXPORT void create_random_files(std::string const& path, lt::span file_sizes + , libtorrent::file_storage* fs = nullptr); EXPORT std::shared_ptr create_torrent(std::ostream* file = nullptr , char const* name = "temporary", int piece_size = 16 * 1024, int num_pieces = 13 diff --git a/test/test_checking.cpp b/test/test_checking.cpp index 0db9a6052..bb2c3d2a1 100644 --- a/test/test_checking.cpp +++ b/test/test_checking.cpp @@ -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 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 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 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); diff --git a/test/test_read_piece.cpp b/test/test_read_piece.cpp index d8c3bb8d4..dbaf53eb0 100644 --- a/test/test_read_piece.cpp +++ b/test/test_read_piece.cpp @@ -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 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); diff --git a/test/test_remap_files.cpp b/test/test_remap_files.cpp index 84d4e53ef..110e1b31a 100644 --- a/test/test_remap_files.cpp +++ b/test/test_remap_files.cpp @@ -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 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 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)