diff --git a/test/test_alert_manager.cpp b/test/test_alert_manager.cpp index 330f431a5..e4e58cc87 100644 --- a/test/test_alert_manager.cpp +++ b/test/test_alert_manager.cpp @@ -153,7 +153,7 @@ int plugin_alerts[3] = { 0, 0, 0 }; struct test_plugin : lt::plugin { explicit test_plugin(int index) : m_index(index) {} - void on_alert(alert const* a) override + void on_alert(alert const*) override { ++plugin_alerts[m_index]; } @@ -188,12 +188,16 @@ TORRENT_TEST(extensions) #endif } +namespace { + void post_torrent_added(alert_manager* mgr) { std::this_thread::sleep_for(lt::milliseconds(10)); mgr->emplace_alert(torrent_handle(), add_torrent_params(), error_code()); } +} // anonymous namespace + TORRENT_TEST(wait_for_alert) { alert_manager mgr(100, alert::all_categories); diff --git a/test/test_bandwidth_limiter.cpp b/test/test_bandwidth_limiter.cpp index 7f6aa033f..898993bdb 100644 --- a/test/test_bandwidth_limiter.cpp +++ b/test/test_bandwidth_limiter.cpp @@ -52,6 +52,8 @@ struct peer_connection; using namespace lt; using namespace std::placeholders; +namespace { + const float sample_time = 20.f; // seconds //#define VERBOSE_LOGGING @@ -455,6 +457,8 @@ void test_no_starvation(int limit) TEST_CHECK(close_to(p->m_quota / sample_time, float(limit) / 200 / num_peers, 5)); } +} // anonymous namespace + TORRENT_TEST(equal_connection) { test_equal_connections( 2, 20); diff --git a/test/test_bitfield.cpp b/test/test_bitfield.cpp index 987cb1218..138899b1a 100644 --- a/test/test_bitfield.cpp +++ b/test/test_bitfield.cpp @@ -37,10 +37,12 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + void print_bitfield(bitfield const& b) { std::string out; - out.reserve(b.size()); + out.reserve(std::size_t(b.size())); for (bool bit : b) out += bit ? '1' : '0'; std::printf("%s\n", out.c_str()); @@ -63,6 +65,8 @@ void test_iterators(bitfield& test1) TEST_EQUAL(num, test1.count()); } +} // anonymous namespace + TORRENT_TEST(bitfield) { bitfield test1(10, false); diff --git a/test/test_dht.cpp b/test/test_dht.cpp index d9107edc6..4550fd181 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -1560,6 +1560,8 @@ TORRENT_TEST(put_v6) } #endif +namespace { + void test_routing_table(address(&rand_addr)()) { dht_test_setup t(udp::endpoint(rand_addr(), 20)); @@ -1767,6 +1769,8 @@ void test_routing_table(address(&rand_addr)()) } } +} // anonymous namespace + TORRENT_TEST(routing_table_v4) { test_routing_table(rand_v4); diff --git a/test/test_enum_net.cpp b/test/test_enum_net.cpp index f291c4e37..b91f2209c 100644 --- a/test/test_enum_net.cpp +++ b/test/test_enum_net.cpp @@ -57,7 +57,6 @@ TORRENT_TEST(is_loopback) #if TORRENT_USE_IPV6 if (supports_ipv6()) { - error_code ec; TEST_CHECK(is_loopback(address::from_string("::1", ec))); TEST_CHECK(!ec); } diff --git a/test/test_file.cpp b/test/test_file.cpp index 25a4602e9..7b6433945 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file.hpp" #include "libtorrent/aux_/path.hpp" +#include "libtorrent/aux_/numeric_cast.hpp" #include "libtorrent/string_util.hpp" // for split_string #include "libtorrent/string_view.hpp" #include "test.hpp" @@ -41,12 +42,14 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + int touch_file(std::string const& filename, int size) { using namespace lt; std::vector v; - v.resize(size); + v.resize(aux::numeric_cast(size)); for (int i = 0; i < size; ++i) v[i] = i & 255; @@ -61,6 +64,8 @@ int touch_file(std::string const& filename, int size) return 0; } +} // anonymous namespace + TORRENT_TEST(create_directory) { error_code ec; diff --git a/test/test_flags.cpp b/test/test_flags.cpp index 666487879..cae8388a6 100644 --- a/test/test_flags.cpp +++ b/test/test_flags.cpp @@ -40,6 +40,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace libtorrent; namespace lt = libtorrent; +namespace { + void test_add_and_get_flags(torrent_flags_t const flags) { session ses(settings()); @@ -89,6 +91,8 @@ void test_unset_after_add(torrent_flags_t const flags) TEST_EQUAL(h.flags() & flags, torrent_flags_t{}); } +} // anonymous namespace + TORRENT_TEST(flag_seed_mode) { // seed-mode (can't be set after adding) diff --git a/test/test_hasher.cpp b/test/test_hasher.cpp index 55997d7e2..2a350dc0c 100644 --- a/test/test_hasher.cpp +++ b/test/test_hasher.cpp @@ -87,7 +87,7 @@ TORRENT_TEST(hasher) h.update(test_array[test], int(std::strlen(test_array[test]))); sha1_hash result; - aux::from_hex({result_array[test], 40}, (char*)&result[0]); + aux::from_hex({result_array[test], 40}, result.data()); TEST_CHECK(result == h.final()); } } diff --git a/test/test_listen_socket.cpp b/test/test_listen_socket.cpp index 4cde64b7f..039ddddf8 100644 --- a/test/test_listen_socket.cpp +++ b/test/test_listen_socket.cpp @@ -83,7 +83,8 @@ namespace , int const original_port, char const* device = "") { auto s = std::make_shared(); - s->local_endpoint = tcp::endpoint(address::from_string(ip), port); + s->local_endpoint = tcp::endpoint(address::from_string(ip) + , aux::numeric_cast(port)); s->original_port = original_port; s->device = device; return s; diff --git a/test/test_magnet.cpp b/test/test_magnet.cpp index 5dba875e3..0a4302f8f 100644 --- a/test/test_magnet.cpp +++ b/test/test_magnet.cpp @@ -450,6 +450,8 @@ TORRENT_TEST(invalid_web_seed_escaping) TEST_CHECK(ec); } +namespace { + auto const yes = default_priority; auto const no = dont_download; @@ -459,6 +461,8 @@ void test_select_only(string_view uri, std::vector expected TEST_CHECK(p.file_priorities == expected); } +} // anonymous namespace + TORRENT_TEST(parse_magnet_select_only) { test_select_only("magnet:?xt=urn:btih:cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" diff --git a/test/test_part_file.cpp b/test/test_part_file.cpp index 0f8f0425e..f5acb9d7e 100644 --- a/test/test_part_file.cpp +++ b/test/test_part_file.cpp @@ -67,7 +67,7 @@ TORRENT_TEST(part_file) TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts"))); // write something to the metadata file - for (int i = 0; i < 1024; ++i) buf[i] = i; + for (int i = 0; i < 1024; ++i) buf[i] = char(i & 0xff); iovec_t v = buf; pf.writev(v, piece_index_t(10), 0, ec); @@ -86,7 +86,7 @@ TORRENT_TEST(part_file) TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts"))); TEST_CHECK(exists(combine_path(combine_path(cwd, "partfile_test_dir2"), "partfile.parts"))); - memset(buf, 0, sizeof(buf)); + std::memset(buf, 0, sizeof(buf)); pf.readv(v, piece_index_t(10), 0, ec); if (ec) std::printf("part_file::readv: %s\n", ec.message().c_str()); @@ -99,7 +99,7 @@ TORRENT_TEST(part_file) // load the part file back in part_file pf(combine_path(cwd, "partfile_test_dir2"), "partfile.parts", 100, piece_size); - memset(buf, 0, sizeof(buf)); + std::memset(buf, 0, sizeof(buf)); iovec_t v = buf; pf.readv(v, piece_index_t(10), 0, ec); @@ -113,9 +113,9 @@ TORRENT_TEST(part_file) std::string output_filename = combine_path(combine_path(cwd, "partfile_test_dir") , "part_file_test_export"); - pf.export_file([](std::int64_t file_offset, span buf) + pf.export_file([](std::int64_t file_offset, span buf_data) { - for (char i : buf) + for (char i : buf_data) { // make sure we got the bytes we expected TEST_CHECK(i == static_cast(file_offset)); @@ -136,4 +136,3 @@ TORRENT_TEST(part_file) if (ec) std::printf("exists: %s\n", ec.message().c_str()); } } - diff --git a/test/test_peer_list.cpp b/test/test_peer_list.cpp index 322a23bed..c83dfceef 100644 --- a/test/test_peer_list.cpp +++ b/test/test_peer_list.cpp @@ -146,8 +146,8 @@ struct mock_torrent std::vector> m_connections; }; -void mock_peer_connection::disconnect(error_code const& ec - , operation_t op, int error) +void mock_peer_connection::disconnect(error_code const& + , operation_t, int /*error*/) { m_torrent.m_p->connection_closed(*this, 0, m_torrent.m_state); auto const i = std::find(m_torrent.m_connections.begin(), m_torrent.m_connections.end() diff --git a/test/test_piece_picker.cpp b/test/test_piece_picker.cpp index af5a48ebf..3a4b06d64 100644 --- a/test/test_piece_picker.cpp +++ b/test/test_piece_picker.cpp @@ -51,6 +51,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; using namespace std::placeholders; +namespace { + const int blocks_per_piece = 4; typed_bitfield string2vec(char const* have_str) @@ -79,7 +81,7 @@ ipv4_peer* tmp_peer = &tmp1; static std::vector const empty_vector; #if TORRENT_USE_ASSERTS -namespace { +namespace { // TODO: remove the nested namespace static struct initializer { initializer() @@ -293,6 +295,8 @@ piece_index_t test_pick(std::shared_ptr const& p const int options = piece_picker::rarest_first; counters pc; +} // anonymous namespace + TORRENT_TEST(piece_block) { piece_index_t const zero(0); diff --git a/test/test_privacy.cpp b/test/test_privacy.cpp index a297b4da0..fa8c2263a 100644 --- a/test/test_privacy.cpp +++ b/test/test_privacy.cpp @@ -48,6 +48,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + char const* proxy_name[] = { "none", "socks4", @@ -244,6 +246,8 @@ session_proxy test_proxy(settings_pack::proxy_type_t proxy_type, int flags) return pr; } +} // anonymous namespace + // not using anonymous mode // UDP fails open if we can't connect to the proxy // or if the proxy doesn't support UDP @@ -330,4 +334,3 @@ TORRENT_TEST(anon_i2p) test_proxy(settings_pack::i2p_proxy, force_proxy_mode); } #endif - diff --git a/test/test_recheck.cpp b/test/test_recheck.cpp index 756eb6f77..ad04234bb 100644 --- a/test/test_recheck.cpp +++ b/test/test_recheck.cpp @@ -49,6 +49,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + auto const mask = alert::all_categories & ~(alert::performance_warning | alert::stats_notification); void wait_for_complete(lt::session& ses, torrent_handle h) @@ -74,6 +76,8 @@ void wait_for_complete(lt::session& ses, torrent_handle h) TEST_ERROR("torrent did not finish"); } +} // anonymous namespace + TORRENT_TEST(recheck) { error_code ec; @@ -115,4 +119,3 @@ TORRENT_TEST(recheck) TEST_CHECK(st1.progress_ppm <= 1000000); wait_for_complete(ses1, tor1); } - diff --git a/test/test_resume.cpp b/test/test_resume.cpp index f9553c6f4..5928d9220 100644 --- a/test/test_resume.cpp +++ b/test/test_resume.cpp @@ -246,7 +246,7 @@ void test_piece_priorities(bool test_deprecated = false) TEST_EQUAL(int(prios.size()), ti->num_pieces()); TEST_EQUAL(prios[0], 0_pri); TEST_EQUAL(prios[1], 4_pri); - TEST_EQUAL(prios[ti->num_pieces()-1], 0_pri); + TEST_EQUAL(prios[std::size_t(ti->num_pieces() - 1)], 0_pri); std::vector resume_data = write_resume_data_buf(ra->params); @@ -819,6 +819,8 @@ TORRENT_TEST(file_priorities_seed_mode) TEST_EQUAL(file_priorities[2], 0_pri); } +namespace { + void test_zero_file_prio(bool test_deprecated = false) { std::printf("test_file_prio\n"); @@ -872,6 +874,8 @@ void test_zero_file_prio(bool test_deprecated = false) TEST_EQUAL(s.total_wanted, 0); } +} // anonymous namespace + #ifndef TORRENT_NO_DEPRECATE TORRENT_TEST(zero_file_prio_deprecated) { @@ -943,6 +947,8 @@ namespace test_mode { #endif } +namespace { + void test_seed_mode(test_mode_t const flags) { lt::session ses(settings()); @@ -1020,6 +1026,9 @@ void test_seed_mode(test_mode_t const flags) TEST_CHECK(s.flags & torrent_flags::seed_mode); } } + +} // anonymous namespace + #ifndef TORRENT_NO_DEPRECATE TORRENT_TEST(seed_mode_file_prio_deprecated) { @@ -1278,4 +1287,3 @@ TORRENT_TEST(paused) // more than just the torrent_status from test_resume_flags. Also http seeds // and trackers for instance } - diff --git a/test/test_sliding_average.cpp b/test/test_sliding_average.cpp index 08174e7cc..76b86917d 100644 --- a/test/test_sliding_average.cpp +++ b/test/test_sliding_average.cpp @@ -33,6 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "test.hpp" #include "libtorrent/sliding_average.hpp" +namespace { + // normal distributed samples. mean=60 stddev=10 int samples[] = { 49, 51, 60, 46, 65, 53, 76, 59, 57, 54, 56, 51, 45, 80, 53, 62, @@ -44,6 +46,8 @@ int samples[] = { 67, 51, 66, 52, 48, 57, 30, 51, 72, 65, 78, 56, 74, 68, 49, 66, 63, 57, 61, 62, 64, 62, 61, 52, 67, 64, 59, 61, 69, 60, 54, 69 }; +} // anonymous namespace + using namespace lt; // make sure we react quickly for the first few samples @@ -111,4 +115,3 @@ TORRENT_TEST(sliding_average) TEST_CHECK(abs(avg.mean() - 250) < 50); TEST_CHECK(abs(avg.avg_deviation() - 250) < 80); } - diff --git a/test/test_storage.cpp b/test/test_storage.cpp index a3ac9717d..c6b15168c 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -414,7 +414,7 @@ void test_rename(std::string const& test_path) void test_check_files(std::string const& test_path , lt::storage_mode_t storage_mode - , bool unbuffered) + , bool /*unbuffered*/) { std::shared_ptr info; @@ -748,6 +748,7 @@ TORRENT_TEST(fastresume_deprecated) } #endif +namespace { bool got_file_rename_alert(alert const* a) { @@ -755,6 +756,8 @@ bool got_file_rename_alert(alert const* a) || alert_cast(a); } +} // anonymous namespace + TORRENT_TEST(rename_file) { std::vector buf; @@ -777,7 +780,7 @@ TORRENT_TEST(rename_file) torrent_handle h = ses.add_torrent(std::move(p), ec); // make it a seed - std::vector tmp(info->piece_length()); + std::vector tmp(std::size_t(info->piece_length())); for (piece_index_t i(0); i < fs.end_piece(); ++i) h.add_piece(i, &tmp[0]); @@ -820,6 +823,8 @@ TORRENT_TEST(rename_file) } } +namespace { + void test_rename_file_fastresume(bool test_deprecated) { std::string test_path = current_working_directory(); @@ -928,6 +933,8 @@ void test_rename_file_fastresume(bool test_deprecated) << "': " << ec.message() << std::endl; } +} // anonymous namespace + TORRENT_TEST(rename_file_fastresume) { test_rename_file_fastresume(false); @@ -940,6 +947,8 @@ TORRENT_TEST(rename_file_fastresume_deprecated) } #endif +namespace { + void alloc_iov(iovec_t* iov, int num_bufs) { for (int i = 0; i < num_bufs; ++i) @@ -957,7 +966,7 @@ void fill_pattern(iovec_t* iov, int num_bufs) { for (char& v : iov[i]) { - v = counter & 0xff; + v = char(counter & 0xff); ++counter; } } @@ -965,7 +974,7 @@ void fill_pattern(iovec_t* iov, int num_bufs) bool check_pattern(std::vector const& buf, int counter) { - unsigned char* p = (unsigned char*)&buf[0]; + unsigned char const* p = reinterpret_cast(buf.data()); for (int k = 0; k < int(buf.size()); ++k) { if (p[k] != (counter & 0xff)) return false; @@ -974,15 +983,6 @@ bool check_pattern(std::vector const& buf, int counter) return true; } -// TODO: this should take a span -void fill_pattern2(iovec_t* iov, int num_bufs) -{ - for (int i = 0; i < num_bufs; ++i) - { - memset(iov[i].data(), 0xfe, iov[i].size()); - } -} - // TODO: this should take a span void free_iov(iovec_t* iov, int num_bufs) { @@ -993,6 +993,8 @@ void free_iov(iovec_t* iov, int num_bufs) } } +} // anonymous namespace + TORRENT_TEST(iovec_copy_bufs) { iovec_t iov1[10]; @@ -1088,6 +1090,8 @@ TORRENT_TEST(iovec_advance_bufs) TORRENT_TEST(unbuffered) { run_test(true); } TORRENT_TEST(buffered) { run_test(false); } +namespace { + file_storage make_fs() { file_storage fs; @@ -1105,15 +1109,15 @@ struct test_fileop explicit test_fileop(int stripe_size) : m_stripe_size(stripe_size) {} int operator()(file_index_t const file_index, std::int64_t const file_offset - , span bufs, storage_error& ec) + , span bufs, storage_error&) { - size_t offset = size_t(file_offset); + std::size_t offset = size_t(file_offset); if (file_index >= m_file_data.end_index()) { m_file_data.resize(static_cast(file_index) + 1); } - const int write_size = std::min(m_stripe_size, bufs_size(bufs)); + std::size_t const write_size = std::size_t(std::min(m_stripe_size, bufs_size(bufs))); std::vector& file = m_file_data[file_index]; @@ -1122,16 +1126,16 @@ struct test_fileop file.resize(offset + write_size); } - int left = write_size; + int left = int(write_size); while (left > 0) { - const int copy_size = std::min(left, int(bufs.front().size())); - memcpy(&file[offset], bufs.front().data(), copy_size); + std::size_t const copy_size = std::size_t(std::min(left, int(bufs.front().size()))); + std::memcpy(&file[offset], bufs.front().data(), copy_size); bufs = bufs.subspan(1); offset += copy_size; - left -= copy_size; + left -= int(copy_size); } - return write_size; + return int(write_size); } int m_stripe_size; @@ -1143,18 +1147,18 @@ struct test_read_fileop // EOF after size bytes read explicit test_read_fileop(int size) : m_size(size), m_counter(0) {} - int operator()(file_index_t const file_index, std::int64_t const file_offset - , span bufs, storage_error& ec) + int operator()(file_index_t, std::int64_t /*file_offset*/ + , span bufs, storage_error&) { int local_size = std::min(m_size, bufs_size(bufs)); const int read = local_size; while (local_size > 0) { int const len = std::min(int(bufs.front().size()), local_size); - auto local_buf = bufs.front().first(len); + auto local_buf = bufs.front().first(std::size_t(len)); for (char& v : local_buf) { - v = m_counter & 0xff; + v = char(m_counter & 0xff); ++m_counter; } local_size -= len; @@ -1174,7 +1178,7 @@ struct test_error_fileop explicit test_error_fileop(file_index_t error_file) : m_error_file(error_file) {} - int operator()(file_index_t const file_index, std::int64_t const file_offset + int operator()(file_index_t const file_index, std::int64_t /*file_offset*/ , span bufs, storage_error& ec) { if (m_error_file == file_index) @@ -1203,6 +1207,8 @@ int count_bufs(iovec_t const* bufs, int bytes) } } +} // anonymous namespace + TORRENT_TEST(readwritev_stripe_1) { const int num_bufs = 30; @@ -1345,6 +1351,8 @@ TORRENT_TEST(readwritev_zero_size_files) TEST_CHECK(check_pattern(buf, 0)); } +namespace { + void delete_dirs(std::string path) { error_code ec; @@ -1357,6 +1365,8 @@ void delete_dirs(std::string path) TEST_CHECK(!exists(path)); } +} // anonymous namespace + TORRENT_TEST(move_storage_to_self) { // call move_storage with the path to the exising storage. should be a no-op diff --git a/test/test_tailqueue.cpp b/test/test_tailqueue.cpp index e12efa597..eb47d4584 100644 --- a/test/test_tailqueue.cpp +++ b/test/test_tailqueue.cpp @@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. using namespace lt; +namespace { + struct test_node : tailqueue_node { explicit test_node(char n) : name(n) {} @@ -83,6 +85,8 @@ void build_chain(tailqueue& q, char const* str) check_chain(q, expected); } +} // anonymous namespace + TORRENT_TEST(tailqueue) { tailqueue t1; @@ -146,7 +150,7 @@ TORRENT_TEST(tailqueue) // test get_all build_chain(t1, "abcdef"); - test_node* n = (test_node*)t1.get_all(); + test_node* n = t1.get_all(); TEST_EQUAL(t1.empty(), true); TEST_EQUAL(t1.size(), 0); @@ -155,7 +159,7 @@ TORRENT_TEST(tailqueue) { test_node* del = n; TEST_EQUAL(n->name, *expected); - n = (test_node*)n->next; + n = n->next; ++expected; delete del; } @@ -163,4 +167,3 @@ TORRENT_TEST(tailqueue) free_chain(t1); free_chain(t2); } - diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp index 2c8d5820a..e0ca12081 100644 --- a/test/test_torrent_info.cpp +++ b/test/test_torrent_info.cpp @@ -864,6 +864,8 @@ TORRENT_TEST(parse_torrents) } } +namespace { + void test_resolve_duplicates(int test_case) { file_storage fs; @@ -952,6 +954,8 @@ void test_resolve_duplicates(int test_case) } } +} // anonymous namespace + TORRENT_TEST(resolve_duplicates) { for (int i = 0; i < 4; ++i) diff --git a/test/test_web_seed_redirect.cpp b/test/test_web_seed_redirect.cpp index 237e8fee5..18dccf07a 100644 --- a/test/test_web_seed_redirect.cpp +++ b/test/test_web_seed_redirect.cpp @@ -100,5 +100,3 @@ TORRENT_TEST(web_seed_redirect) stop_web_server(); } - -