fixing warnings in tests code, part6 (#2745)

This commit is contained in:
Alden Torres 2018-01-29 06:40:44 -05:00 committed by Arvid Norberg
parent 50a293730b
commit 11aa4ee66a
7 changed files with 106 additions and 91 deletions

View File

@ -90,7 +90,7 @@ TORRENT_TEST(buffer_subscript)
TEST_CHECK(b.size() >= 50); TEST_CHECK(b.size() >= 50);
for (int i = 0; i < int(sizeof(data)/sizeof(data[0])); ++i) for (int i = 0; i < int(sizeof(data)/sizeof(data[0])); ++i)
TEST_CHECK(b[i] == data[i]); TEST_CHECK(b[std::size_t(i)] == data[i]);
} }
TORRENT_TEST(buffer_subscript2) TORRENT_TEST(buffer_subscript2)
@ -99,10 +99,10 @@ TORRENT_TEST(buffer_subscript2)
TEST_CHECK(b.size() >= 1); TEST_CHECK(b.size() >= 1);
for (int i = 0; i < int(b.size()); ++i) for (int i = 0; i < int(b.size()); ++i)
b[i] = i & 0xff; b[std::size_t(i)] = char(i & 0xff);
for (int i = 0; i < int(b.size()); ++i) for (int i = 0; i < int(b.size()); ++i)
TEST_CHECK(b[i] == (i & 0xff)); TEST_CHECK(b[std::size_t(i)] == (i & 0xff));
} }
TORRENT_TEST(buffer_move_construct) TORRENT_TEST(buffer_move_construct)
@ -113,7 +113,7 @@ TORRENT_TEST(buffer_move_construct)
buffer b2(std::move(b1)); buffer b2(std::move(b1));
TEST_CHECK(b1.size() == 0); TEST_CHECK(b1.empty());
TEST_CHECK(std::memcmp(b2.data(), data, 10) == 0); TEST_CHECK(std::memcmp(b2.data(), data, 10) == 0);
TEST_CHECK(b2.size() >= 50); TEST_CHECK(b2.size() >= 50);
@ -152,7 +152,7 @@ void free_buffer(char* m)
char* allocate_buffer(int size) char* allocate_buffer(int size)
{ {
char* mem = (char*)std::malloc(size); char* mem = static_cast<char*>(std::malloc(std::size_t(size)));
buffer_list.insert(mem); buffer_list.insert(mem);
return mem; return mem;
} }
@ -174,11 +174,11 @@ int copy_buffers(T const& b, char* target)
bool compare_chained_buffer(chained_buffer& b, char const* mem, int size) bool compare_chained_buffer(chained_buffer& b, char const* mem, int size)
{ {
if (size == 0) return true; if (size == 0) return true;
std::vector<char> flat(size); std::vector<char> flat((std::size_t(size)));
std::vector<boost::asio::const_buffer> const& iovec2 = b.build_iovec(size); std::vector<boost::asio::const_buffer> const& iovec2 = b.build_iovec(size);
int copied = copy_buffers(iovec2, &flat[0]); int copied = copy_buffers(iovec2, &flat[0]);
TEST_CHECK(copied == size); TEST_CHECK(copied == size);
return std::memcmp(&flat[0], mem, size) == 0; return std::memcmp(&flat[0], mem, std::size_t(size)) == 0;
} }
struct holder struct holder

View File

@ -95,8 +95,9 @@ void add_and_replace(node_id& dst, node_id const& add)
bool carry = false; bool carry = false;
for (int k = 19; k >= 0; --k) for (int k = 19; k >= 0; --k)
{ {
int sum = dst[k] + add[k] + (carry ? 1 : 0); std::size_t idx = std::size_t(k);
dst[k] = sum & 255; int sum = dst[idx] + add[idx] + (carry ? 1 : 0);
dst[idx] = sum & 255;
carry = sum > 255; carry = sum > 255;
} }
} }
@ -115,7 +116,7 @@ std::list<std::pair<udp::endpoint, entry>> g_sent_packets;
struct mock_socket final : socket_manager struct mock_socket final : socket_manager
{ {
bool has_quota() override { return true; } bool has_quota() override { return true; }
bool send_packet(aux::listen_socket_handle const& s, entry& msg, udp::endpoint const& ep) override bool send_packet(aux::listen_socket_handle const&, entry& msg, udp::endpoint const& ep) override
{ {
// TODO: 3 ideally the mock_socket would contain this queue of packets, to // TODO: 3 ideally the mock_socket would contain this queue of packets, to
// make tests independent // make tests independent
@ -161,7 +162,7 @@ node* get_foreign_node_stub(node_id const&, std::string const&)
sha1_hash generate_next() sha1_hash generate_next()
{ {
sha1_hash ret; sha1_hash ret;
for (int i = 0; i < 20; ++i) ret[i] = rand() & 0xff; aux::random_bytes(ret);
return ret; return ret;
} }
@ -191,7 +192,7 @@ entry write_peers(std::set<tcp::endpoint> const& peers)
std::string endpoint(18, '\0'); std::string endpoint(18, '\0');
std::string::iterator out = endpoint.begin(); std::string::iterator out = endpoint.begin();
lt::detail::write_endpoint(p, out); lt::detail::write_endpoint(p, out);
endpoint.resize(out - endpoint.begin()); endpoint.resize(std::size_t(out - endpoint.begin()));
pe.push_back(entry(endpoint)); pe.push_back(entry(endpoint));
} }
return r; return r;
@ -520,19 +521,19 @@ void put_immutable_item_cb(int num, int expect)
struct obs : dht::dht_observer struct obs : dht::dht_observer
{ {
void set_external_address(aux::listen_socket_handle const& s, address const& addr void set_external_address(aux::listen_socket_handle const& s, address const& addr
, address const& source) override , address const& /*source*/) override
{ {
s.get()->external_address.cast_vote(addr s.get()->external_address.cast_vote(addr
, aux::session_interface::source_dht, rand_v4()); , aux::session_interface::source_dht, rand_v4());
} }
void get_peers(sha1_hash const& ih) override {} void get_peers(sha1_hash const&) override {}
void outgoing_get_peers(sha1_hash const& target void outgoing_get_peers(sha1_hash const& /*target*/
, sha1_hash const& sent_target, udp::endpoint const& ep) override {} , sha1_hash const& /*sent_target*/, udp::endpoint const&) override {}
void announce(sha1_hash const& ih, address const& addr, int port) override {} void announce(sha1_hash const&, address const&, int) override {}
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
bool should_log(module_t) const override { return true; } bool should_log(module_t) const override { return true; }
void log(dht_logger::module_t l, char const* fmt, ...) override void log(dht_logger::module_t, char const* fmt, ...) override
{ {
va_list v; va_list v;
va_start(v, fmt); va_start(v, fmt);
@ -542,11 +543,13 @@ struct obs : dht::dht_observer
std::printf("%s\n", buf); std::printf("%s\n", buf);
m_log.push_back(buf); m_log.push_back(buf);
} }
void log_packet(message_direction_t dir, span<char const> pkt void log_packet(message_direction_t, span<char const>
, udp::endpoint const& node) override {} , udp::endpoint const&) override {}
#endif #endif
bool on_dht_request(string_view query bool on_dht_request(string_view
, dht::msg const& request, entry& response) override { return false; } , dht::msg const&, entry&) override { return false; }
virtual ~obs() = default;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
std::vector<std::string> m_log; std::vector<std::string> m_log;
@ -932,6 +935,8 @@ TORRENT_TEST(get_peers_announce)
} }
} }
namespace {
void test_scrape(address(&rand_addr)()) void test_scrape(address(&rand_addr)())
{ {
dht_test_setup t(udp::endpoint(rand_addr(), 20)); dht_test_setup t(udp::endpoint(rand_addr(), 20));
@ -1001,8 +1006,8 @@ void test_scrape(address(&rand_addr)())
downloaders.from_string(peer2_keys[2].string_ptr()); downloaders.from_string(peer2_keys[2].string_ptr());
seeds.from_string(peer2_keys[3].string_ptr()); seeds.from_string(peer2_keys[3].string_ptr());
std::printf("seeds: %f\n", seeds.size()); std::printf("seeds: %f\n", double(seeds.size()));
std::printf("downloaders: %f\n", downloaders.size()); std::printf("downloaders: %f\n", double(downloaders.size()));
TEST_CHECK(fabs(seeds.size() - 50.f) <= 3.f); TEST_CHECK(fabs(seeds.size() - 50.f) <= 3.f);
TEST_CHECK(fabs(downloaders.size() - 50.f) <= 3.f); TEST_CHECK(fabs(downloaders.size() - 50.f) <= 3.f);
@ -1014,6 +1019,8 @@ void test_scrape(address(&rand_addr)())
} }
} }
} // anonymous namespace
TORRENT_TEST(scrape_v4) TORRENT_TEST(scrape_v4)
{ {
test_scrape(rand_v4); test_scrape(rand_v4);
@ -1027,6 +1034,8 @@ TORRENT_TEST(scrape_v6)
} }
#endif #endif
namespace {
void test_id_enforcement(address(&rand_addr)()) void test_id_enforcement(address(&rand_addr)())
{ {
dht_test_setup t(udp::endpoint(rand_addr(), 20)); dht_test_setup t(udp::endpoint(rand_addr(), 20));
@ -1118,6 +1127,8 @@ void test_id_enforcement(address(&rand_addr)())
TEST_EQUAL(std::get<0>(t.dht_node.size()), nodes_num + 1); TEST_EQUAL(std::get<0>(t.dht_node.size()), nodes_num + 1);
} }
} // anonymous namespace
TORRENT_TEST(id_enforcement_v4) TORRENT_TEST(id_enforcement_v4)
{ {
test_id_enforcement(rand_v4); test_id_enforcement(rand_v4);
@ -1410,7 +1421,7 @@ void test_put(address(&rand_addr)())
char* ptr = value; char* ptr = value;
int const value_len = bencode(ptr, items[0].ent); int const value_len = bencode(ptr, items[0].ent);
TEST_EQUAL(value_len, int(desc3_keys[2].data_section().size())); TEST_EQUAL(value_len, int(desc3_keys[2].data_section().size()));
TEST_CHECK(memcmp(desc3_keys[2].data_section().data(), value, value_len) == 0); TEST_CHECK(std::memcmp(desc3_keys[2].data_section().data(), value, std::size_t(value_len)) == 0);
TEST_EQUAL(int(seq.value), desc3_keys[3].int_value()); TEST_EQUAL(int(seq.value), desc3_keys[3].int_value());
} }
@ -2475,14 +2486,14 @@ TORRENT_TEST(mutable_put)
enum { num_test_nodes = 8 }; enum { num_test_nodes = 8 };
std::array<node_entry, num_test_nodes> const nodes = build_nodes(); std::array<node_entry, num_test_nodes> const nodes = build_nodes();
for (int i = 0; i < num_test_nodes; ++i) for (auto const& n : nodes)
t.dht_node.m_table.add_node(nodes[i]); t.dht_node.m_table.add_node(n);
g_put_item.assign(items[0].ent, empty_salt, seq, pk, sk); g_put_item.assign(items[0].ent, empty_salt, seq, pk, sk);
signature const sig = g_put_item.sig(); signature const sig = g_put_item.sig();
t.dht_node.put_item(pk, std::string() t.dht_node.put_item(pk, std::string()
, std::bind(&put_mutable_item_cb, _1, _2, loop) , std::bind(&put_mutable_item_cb, _1, _2, loop)
, put_mutable_item_data_cb); , put_mutable_item_data_cb);
TEST_EQUAL(g_sent_packets.size(), 8); TEST_EQUAL(g_sent_packets.size(), 8);
if (g_sent_packets.size() != 8) break; if (g_sent_packets.size() != 8) break;
@ -2583,11 +2594,11 @@ TORRENT_TEST(traversal_done)
// invert the ith most significant byte so that the test nodes are // invert the ith most significant byte so that the test nodes are
// progressively closer to the target item // progressively closer to the target item
for (int i = 0; i < num_test_nodes; ++i) for (std::size_t i = 0; i < num_test_nodes; ++i)
nodes[i].id[i] = ~nodes[i].id[i]; nodes[i].id[i] = ~nodes[i].id[i];
// add the first k nodes to the subject's routing table // add the first k nodes to the subject's routing table
for (int i = 0; i < 8; ++i) for (std::size_t i = 0; i < 8; ++i)
t.dht_node.m_table.add_node(nodes[i]); t.dht_node.m_table.add_node(nodes[i]);
// kick off a mutable put request // kick off a mutable put request
@ -2623,13 +2634,13 @@ TORRENT_TEST(traversal_done)
std::snprintf(tok, sizeof(tok), "%02d", i); std::snprintf(tok, sizeof(tok), "%02d", i);
msg_args args; msg_args args;
args.token(tok).port(1234).nid(nodes[i].id); args.token(tok).port(1234).nid(nodes[std::size_t(i)].id);
// add the address of the closest node to the first response // add the address of the closest node to the first response
if (i == 1) if (i == 1)
args.nodes({nodes[8]}); args.nodes({nodes[8]});
send_dht_response(t.dht_node, response, nodes[i].ep(), args); send_dht_response(t.dht_node, response, nodes[std::size_t(i)].ep(), args);
g_sent_packets.erase(packet); g_sent_packets.erase(packet);
// once we've sent the response from the farthest node, we're done // once we've sent the response from the farthest node, we're done
@ -3027,7 +3038,7 @@ TORRENT_TEST(routing_table_balance)
// and make sure we don't end up with a table completely out of balance // and make sure we don't end up with a table completely out of balance
for (int i = 0; i < 32; ++i) for (int i = 0; i < 32; ++i)
{ {
id[4] = i; id[4] = i & 0xff;
tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff)); tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff));
} }
std::printf("num_active_buckets: %d\n", tbl.num_active_buckets()); std::printf("num_active_buckets: %d\n", tbl.num_active_buckets());
@ -3048,7 +3059,7 @@ TORRENT_TEST(routing_table_extended)
// table would get unbalanced and intermediate nodes would be dropped // table would get unbalanced and intermediate nodes would be dropped
std::vector<std::uint8_t> node_id_prefix; std::vector<std::uint8_t> node_id_prefix;
node_id_prefix.reserve(256); node_id_prefix.reserve(256);
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i); for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i & 0xff);
aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end()); aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
routing_table tbl(id, udp::v4(), 8, sett, &observer); routing_table tbl(id, udp::v4(), 8, sett, &observer);
@ -3126,7 +3137,7 @@ TORRENT_TEST(routing_table_for_each)
for (int i = 0; i < 32; ++i) for (int i = 0; i < 32; ++i)
{ {
id[4] = i; id[4] = i & 0xff;
tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff)); tbl.node_seen(id, rand_udp_ep(), 20 + (id[19] & 0xff));
} }
@ -3305,13 +3316,13 @@ TORRENT_TEST(invalid_error_msg)
node.incoming(node.m_sock, m); node.incoming(node.m_sock, m);
bool found = false; bool found = false;
for (int i = 0; i < int(observer.m_log.size()); ++i) for (auto const& log : observer.m_log)
{ {
if (observer.m_log[i].find("INCOMING ERROR") != std::string::npos if (log.find("INCOMING ERROR") != std::string::npos
&& observer.m_log[i].find("(malformed)") != std::string::npos) && log.find("(malformed)") != std::string::npos)
found = true; found = true;
std::printf("%s\n", observer.m_log[i].c_str()); std::printf("%s\n", log.c_str());
} }
TEST_EQUAL(found, true); TEST_EQUAL(found, true);
@ -3323,7 +3334,7 @@ struct test_algo : dht::traversal_algorithm
: traversal_algorithm(dht_node, target) : traversal_algorithm(dht_node, target)
{} {}
std::vector<observer_ptr> const& results() const { return m_results; }; std::vector<observer_ptr> const& results() const { return m_results; }
using traversal_algorithm::num_sorted_results; using traversal_algorithm::num_sorted_results;
}; };
@ -3350,11 +3361,11 @@ TORRENT_TEST(unsorted_traversal_results)
TEST_CHECK(algo->num_sorted_results() == 0); TEST_CHECK(algo->num_sorted_results() == 0);
auto results = algo->results(); auto results = algo->results();
TEST_CHECK(results.size() == eps.size()); TEST_CHECK(results.size() == eps.size());
for (int i = 0; i < int(eps.size()); ++i) for (std::size_t i = 0; i < eps.size(); ++i)
TEST_CHECK(eps[i] == results[i]->target_ep()); TEST_CHECK(eps[i] == results[i]->target_ep());
// setting the node ID, regardless of what we set it to, should cause this // setting the node ID, regardless of what we set it to, should cause this
// observer to become sorted. i.e. be moved to the beginning of the restult // observer to become sorted. i.e. be moved to the beginning of the result
// list. // list.
results[5]->set_id(node_id("abababababababababab")); results[5]->set_id(node_id("abababababababababab"));
@ -3415,14 +3426,14 @@ TORRENT_TEST(rpc_invalid_error_msg)
rpc.incoming(m, &nid); rpc.incoming(m, &nid);
bool found = false; bool found = false;
for (std::size_t i = 0; i < observer.m_log.size(); ++i) for (auto const& log : observer.m_log)
{ {
if (observer.m_log[i].find("reply with") != std::string::npos if (log.find("reply with") != std::string::npos
&& observer.m_log[i].find("(malformed)") != std::string::npos && log.find("(malformed)") != std::string::npos
&& observer.m_log[i].find("error") != std::string::npos) && log.find("error") != std::string::npos)
found = true; found = true;
std::printf("%s\n", observer.m_log[i].c_str()); std::printf("%s\n", log.c_str());
} }
TEST_EQUAL(found, true); TEST_EQUAL(found, true);

View File

@ -182,7 +182,7 @@ std::shared_ptr<piece_picker> setup_picker(
{ {
int const idx = static_cast<int>(i); int const idx = static_cast<int>(i);
if (priority[idx] == 0) break; if (priority[idx] == 0) break;
download_priority_t const prio(priority[idx] - '0'); download_priority_t const prio((priority[idx] - '0') & 0xff);
assert(prio >= dont_download); assert(prio >= dont_download);
p->set_piece_priority(i, prio); p->set_piece_priority(i, prio);
@ -240,8 +240,8 @@ void print_availability(std::shared_ptr<piece_picker> const& p)
aux::vector<int, piece_index_t> avail; aux::vector<int, piece_index_t> avail;
p->get_availability(avail); p->get_availability(avail);
std::printf("[ "); std::printf("[ ");
for (auto p : avail) for (auto i : avail)
std::printf("%d ", static_cast<int>(p)); std::printf("%d ", i);
std::printf("]\n"); std::printf("]\n");
} }
@ -269,14 +269,14 @@ std::vector<piece_block> pick_pieces(std::shared_ptr<piece_picker> const& p
, char const* availability , char const* availability
, int num_blocks , int num_blocks
, int prefer_contiguous_blocks , int prefer_contiguous_blocks
, torrent_peer* peer_struct , torrent_peer* peer_struct_arg
, picker_options_t const options = piece_picker::rarest_first , picker_options_t const options = piece_picker::rarest_first
, std::vector<piece_index_t> const& suggested_pieces = empty_vector) , std::vector<piece_index_t> const& suggested_pieces = empty_vector)
{ {
std::vector<piece_block> picked; std::vector<piece_block> picked;
counters pc; counters pc;
p->pick_pieces(string2vec(availability), picked p->pick_pieces(string2vec(availability), picked
, num_blocks, prefer_contiguous_blocks, peer_struct , num_blocks, prefer_contiguous_blocks, peer_struct_arg
, options, suggested_pieces, 20, pc); , options, suggested_pieces, 20, pc);
print_pick(picked); print_pick(picked);
TEST_CHECK(verify_pick(p, picked)); TEST_CHECK(verify_pick(p, picked));
@ -504,7 +504,7 @@ TORRENT_TEST(reverse_rarest_first)
int expected_common_pieces[] = {3, 2, 5, 0, 6, 4, 1}; int expected_common_pieces[] = {3, 2, 5, 0, 6, 4, 1};
for (int i = 0; i < int(picked.size()); ++i) for (int i = 0; i < int(picked.size()); ++i)
{ {
TEST_CHECK(picked[i] == piece_block(piece_index_t( TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(
expected_common_pieces[i / blocks_per_piece]) expected_common_pieces[i / blocks_per_piece])
, i % blocks_per_piece)); , i % blocks_per_piece));
} }
@ -541,7 +541,7 @@ TORRENT_TEST(pick_whole_pieces)
, &peer_struct, options, empty_vector); , &peer_struct, options, empty_vector);
TEST_EQUAL(int(picked.size()), 7 * blocks_per_piece); TEST_EQUAL(int(picked.size()), 7 * blocks_per_piece);
for (int i = 0; i < int(picked.size()); ++i) for (int i = 0; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(i / blocks_per_piece) TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(i / blocks_per_piece)
, i % blocks_per_piece)); , i % blocks_per_piece));
} }
@ -743,7 +743,7 @@ TORRENT_TEST(sequential_download)
, piece_picker::sequential, empty_vector); , piece_picker::sequential, empty_vector);
TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece); TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece);
for (int i = 0; i < int(picked.size()); ++i) for (int i = 0; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(i / blocks_per_piece) TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(i / blocks_per_piece)
, i % blocks_per_piece)); , i % blocks_per_piece));
} }
@ -755,7 +755,7 @@ TORRENT_TEST(reverse_sequential_download)
, piece_picker::sequential | piece_picker::reverse, empty_vector); , piece_picker::sequential | piece_picker::reverse, empty_vector);
TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece); TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece);
for (int i = 0; i < int(picked.size()); ++i) for (int i = 0; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(6 - (i / blocks_per_piece)) TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(6 - (i / blocks_per_piece))
, i % blocks_per_piece)); , i % blocks_per_piece));
} }
@ -772,11 +772,11 @@ TORRENT_TEST(priority_sequential_download)
// the first two pieces picked should be 3 and 5 since those have priority 7 // the first two pieces picked should be 3 and 5 since those have priority 7
for (int i = 0; i < 2 * blocks_per_piece; ++i) for (int i = 0; i < 2 * blocks_per_piece; ++i)
TEST_CHECK(picked[i].piece_index == piece_index_t(3) || picked[i].piece_index == piece_index_t(5)); TEST_CHECK(picked[std::size_t(i)].piece_index == piece_index_t(3) || picked[std::size_t(i)].piece_index == piece_index_t(5));
int expected[] = {-1, -1, 0, 1, 2, 6}; int expected[] = {-1, -1, 0, 1, 2, 6};
for (int i = 2 * blocks_per_piece; i < int(picked.size()); ++i) for (int i = 2 * blocks_per_piece; i < int(picked.size()); ++i)
TEST_EQUAL(picked[i].piece_index, piece_index_t(expected[i / blocks_per_piece])); TEST_EQUAL(picked[std::size_t(i)].piece_index, piece_index_t(expected[i / blocks_per_piece]));
} }
TORRENT_TEST(cursors_sweep_up_we_have) TORRENT_TEST(cursors_sweep_up_we_have)
@ -1010,7 +1010,7 @@ TORRENT_TEST(piece_priorities)
TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece); TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece);
for (int i = 0; i < int(picked.size()); ++i) for (int i = 0; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(i / blocks_per_piece), i % blocks_per_piece)); TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(i / blocks_per_piece), i % blocks_per_piece));
// test changing priority on a piece we have // test changing priority on a piece we have
p->we_have(piece_index_t(0)); p->we_have(piece_index_t(0));
@ -1336,7 +1336,7 @@ TORRENT_TEST(prefer_cnotiguous_blocks)
, nullptr, options, empty_vector); , nullptr, options, empty_vector);
TEST_CHECK(int(picked.size()) >= 3 * blocks_per_piece); TEST_CHECK(int(picked.size()) >= 3 * blocks_per_piece);
piece_block b = picked.front(); piece_block b = picked.front();
for (int i = 1; i < int(picked.size()); ++i) for (std::size_t i = 1; i < picked.size(); ++i)
{ {
TEST_CHECK(static_cast<int>(picked[i].piece_index) * blocks_per_piece + picked[i].block_index TEST_CHECK(static_cast<int>(picked[i].piece_index) * blocks_per_piece + picked[i].block_index
== static_cast<int>(b.piece_index) * blocks_per_piece + b.block_index + 1); == static_cast<int>(b.piece_index) * blocks_per_piece + b.block_index + 1);
@ -1347,7 +1347,7 @@ TORRENT_TEST(prefer_cnotiguous_blocks)
, nullptr, options, empty_vector); , nullptr, options, empty_vector);
TEST_CHECK(int(picked.size()) >= 3 * blocks_per_piece); TEST_CHECK(int(picked.size()) >= 3 * blocks_per_piece);
b = picked.front(); b = picked.front();
for (int i = 1; i < int(picked.size()); ++i) for (std::size_t i = 1; i < picked.size(); ++i)
{ {
TEST_CHECK(static_cast<int>(picked[i].piece_index) * blocks_per_piece + picked[i].block_index TEST_CHECK(static_cast<int>(picked[i].piece_index) * blocks_per_piece + picked[i].block_index
== static_cast<int>(b.piece_index) * blocks_per_piece + b.block_index + 1); == static_cast<int>(b.piece_index) * blocks_per_piece + b.block_index + 1);
@ -1376,7 +1376,7 @@ TORRENT_TEST(prefer_aligned_whole_pieces)
TEST_EQUAL(picked.size(), 4 * blocks_per_piece); TEST_EQUAL(picked.size(), 4 * blocks_per_piece);
std::set<piece_index_t> picked_pieces; std::set<piece_index_t> picked_pieces;
for (auto p : picked) picked_pieces.insert(p.piece_index); for (auto idx : picked) picked_pieces.insert(idx.piece_index);
TEST_CHECK(picked_pieces.size() == 4); TEST_CHECK(picked_pieces.size() == 4);
piece_index_t expected_pieces[] = {piece_index_t(4),piece_index_t(5),piece_index_t(6),piece_index_t(7)}; piece_index_t expected_pieces[] = {piece_index_t(4),piece_index_t(5),piece_index_t(6),piece_index_t(7)};
@ -1393,7 +1393,7 @@ TORRENT_TEST(parole_mode)
, options | piece_picker::on_parole | piece_picker::prioritize_partials, empty_vector); , options | piece_picker::on_parole | piece_picker::prioritize_partials, empty_vector);
TEST_EQUAL(int(picked.size()), blocks_per_piece - 1); TEST_EQUAL(int(picked.size()), blocks_per_piece - 1);
for (int i = 1; i < int(picked.size()); ++i) for (int i = 1; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(0), i + 1)); TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(0), i + 1));
// make sure that the partial piece is not picked by a // make sure that the partial piece is not picked by a
// peer that is has not downloaded/requested the other blocks // peer that is has not downloaded/requested the other blocks
@ -1402,7 +1402,7 @@ TORRENT_TEST(parole_mode)
, options | piece_picker::on_parole | piece_picker::prioritize_partials, empty_vector); , options | piece_picker::on_parole | piece_picker::prioritize_partials, empty_vector);
TEST_EQUAL(int(picked.size()), blocks_per_piece); TEST_EQUAL(int(picked.size()), blocks_per_piece);
for (int i = 1; i < int(picked.size()); ++i) for (int i = 1; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(4), i)); TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(4), i));
} }
TORRENT_TEST(suggested_pieces) TORRENT_TEST(suggested_pieces)
@ -1416,7 +1416,7 @@ TORRENT_TEST(suggested_pieces)
, nullptr, options, suggested_pieces); , nullptr, options, suggested_pieces);
TEST_CHECK(int(picked.size()) >= blocks_per_piece); TEST_CHECK(int(picked.size()) >= blocks_per_piece);
for (int i = 1; i < int(picked.size()); ++i) for (int i = 1; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(1), i)); TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(1), i));
p->set_piece_priority(piece_index_t(0), dont_download); p->set_piece_priority(piece_index_t(0), dont_download);
p->set_piece_priority(piece_index_t(1), dont_download); p->set_piece_priority(piece_index_t(1), dont_download);
p->set_piece_priority(piece_index_t(2), dont_download); p->set_piece_priority(piece_index_t(2), dont_download);
@ -1426,14 +1426,14 @@ TORRENT_TEST(suggested_pieces)
, nullptr, options, suggested_pieces); , nullptr, options, suggested_pieces);
TEST_CHECK(int(picked.size()) >= blocks_per_piece); TEST_CHECK(int(picked.size()) >= blocks_per_piece);
for (int i = 1; i < int(picked.size()); ++i) for (int i = 1; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(5), i)); TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(5), i));
p = setup_picker("1111222233334444", "**** ", "", ""); p = setup_picker("1111222233334444", "**** ", "", "");
picked = pick_pieces(p, "****************", 1, blocks_per_piece picked = pick_pieces(p, "****************", 1, blocks_per_piece
, nullptr, options, suggested_pieces); , nullptr, options, suggested_pieces);
TEST_CHECK(int(picked.size()) >= blocks_per_piece); TEST_CHECK(int(picked.size()) >= blocks_per_piece);
for (int i = 1; i < int(picked.size()); ++i) for (int i = 1; i < int(picked.size()); ++i)
TEST_CHECK(picked[i] == piece_block(piece_index_t(5), i)); TEST_CHECK(picked[std::size_t(i)] == piece_block(piece_index_t(5), i));
} }
TORRENT_TEST(bitfield_optimization) TORRENT_TEST(bitfield_optimization)

View File

@ -62,8 +62,8 @@ void wait_for_complete(lt::session& ses, torrent_handle h)
print_alerts(ses, "ses1"); print_alerts(ses, "ses1");
torrent_status st = h.status(); torrent_status st = h.status();
std::printf("%f s - %f %%\n" std::printf("%f s - %f %%\n"
, total_milliseconds(clock_type::now() - last_change) / 1000.f , total_milliseconds(clock_type::now() - last_change) / 1000.0
, st.progress_ppm / 10000.f); , st.progress_ppm / 10000.0);
if (st.progress_ppm == 1000000) return; if (st.progress_ppm == 1000000) return;
if (st.progress_ppm != last_progress) if (st.progress_ppm != last_progress)
{ {

View File

@ -112,8 +112,8 @@ TORRENT_TEST(resolve_links)
aux::vector<resolve_links::link_t, file_index_t> const& links = l.get_links(); aux::vector<resolve_links::link_t, file_index_t> const& links = l.get_links();
std::string::size_type num_matches = std::count_if(links.begin(), links.end() auto const num_matches = std::size_t(std::count_if(links.begin(), links.end()
, std::bind(&resolve_links::link_t::ti, _1)); , std::bind(&resolve_links::link_t::ti, _1)));
// some debug output in case the test fails // some debug output in case the test fails
if (num_matches > e.expected_matches) if (num_matches > e.expected_matches)
@ -166,7 +166,7 @@ TORRENT_TEST(range_lookup_duplicated_files)
aux::vector<resolve_links::link_t, file_index_t> const& links = l.get_links(); aux::vector<resolve_links::link_t, file_index_t> const& links = l.get_links();
std::string::size_type num_matches = std::count_if(links.begin(), links.end() auto const num_matches = std::count_if(links.begin(), links.end()
, std::bind(&resolve_links::link_t::ti, _1)); , std::bind(&resolve_links::link_t::ti, _1));
TEST_EQUAL(num_matches, 1); TEST_EQUAL(num_matches, 1);

View File

@ -188,6 +188,7 @@ void run_storage_tests(std::shared_ptr<torrent_info> info
, bool unbuffered) , bool unbuffered)
{ {
TORRENT_ASSERT(fs.num_files() > 0); TORRENT_ASSERT(fs.num_files() > 0);
{
error_code ec; error_code ec;
create_directory(combine_path(test_path, "temp_storage"), ec); create_directory(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cout << "create_directory '" << combine_path(test_path, "temp_storage") if (ec) std::cout << "create_directory '" << combine_path(test_path, "temp_storage")
@ -200,7 +201,7 @@ void run_storage_tests(std::shared_ptr<torrent_info> info
if (ec && ec != boost::system::errc::no_such_file_or_directory) if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "part0") std::cout << "remove_all '" << combine_path(test_path, "part0")
<< "': " << ec.message() << std::endl; << "': " << ec.message() << std::endl;
}
int num_pieces = fs.num_pieces(); int num_pieces = fs.num_pieces();
TEST_CHECK(info->num_pieces() == num_pieces); TEST_CHECK(info->num_pieces() == num_pieces);

View File

@ -40,6 +40,8 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace lt; using namespace lt;
namespace {
void verify_transforms(char const* utf8_source, int utf8_source_len = -1) void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
{ {
if (utf8_source_len == -1) if (utf8_source_len == -1)
@ -47,8 +49,8 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
// utf8 -> utf16 -> utf32 -> utf8 // utf8 -> utf16 -> utf32 -> utf8
{ {
std::vector<UTF16> utf16(utf8_source_len); std::vector<UTF16> utf16((std::size_t(utf8_source_len)));
UTF8 const* in8 = (UTF8 const*)utf8_source; UTF8 const* in8 = reinterpret_cast<UTF8 const*>(utf8_source);
UTF16* out16 = &utf16[0]; UTF16* out16 = &utf16[0];
ConversionResult ret = ConvertUTF8toUTF16(&in8, in8 + utf8_source_len ConversionResult ret = ConvertUTF8toUTF16(&in8, in8 + utf8_source_len
, &out16, out16 + utf16.size(), strictConversion); , &out16, out16 + utf16.size(), strictConversion);
@ -60,7 +62,7 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
std::printf("%x ", UTF8(*i)); std::printf("%x ", UTF8(*i));
} }
std::vector<UTF32> utf32(utf8_source_len); std::vector<UTF32> utf32((std::size_t(utf8_source_len)));
UTF16 const* in16 = &utf16[0]; UTF16 const* in16 = &utf16[0];
UTF32* out32 = &utf32[0]; UTF32* out32 = &utf32[0];
ret = ConvertUTF16toUTF32(&in16, out16 ret = ConvertUTF16toUTF32(&in16, out16
@ -73,7 +75,7 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
std::printf("%x ", UTF8(*i)); std::printf("%x ", UTF8(*i));
} }
std::vector<UTF8> utf8(utf8_source_len); std::vector<UTF8> utf8((std::size_t(utf8_source_len)));
UTF32 const* in32 = &utf32[0]; UTF32 const* in32 = &utf32[0];
UTF8* out8 = &utf8[0]; UTF8* out8 = &utf8[0];
ret = ConvertUTF32toUTF8(&in32, out32 ret = ConvertUTF32toUTF8(&in32, out32
@ -87,13 +89,13 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
} }
TEST_EQUAL(out8 - &utf8[0], utf8_source_len); TEST_EQUAL(out8 - &utf8[0], utf8_source_len);
TEST_CHECK(std::equal(&utf8[0], out8, (UTF8 const*)utf8_source)); TEST_CHECK(std::equal(&utf8[0], out8, reinterpret_cast<UTF8 const*>(utf8_source)));
} }
// utf8 -> utf32 -> utf16 -> utf8 // utf8 -> utf32 -> utf16 -> utf8
{ {
std::vector<UTF32> utf32(utf8_source_len); std::vector<UTF32> utf32((std::size_t(utf8_source_len)));
UTF8 const* in8 = (UTF8 const*)utf8_source; UTF8 const* in8 = reinterpret_cast<UTF8 const*>(utf8_source);
UTF32* out32 = &utf32[0]; UTF32* out32 = &utf32[0];
ConversionResult ret = ConvertUTF8toUTF32(&in8, in8 + utf8_source_len ConversionResult ret = ConvertUTF8toUTF32(&in8, in8 + utf8_source_len
, &out32, out32 + utf32.size(), strictConversion); , &out32, out32 + utf32.size(), strictConversion);
@ -105,7 +107,7 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
std::printf("%x ", UTF8(*i)); std::printf("%x ", UTF8(*i));
} }
std::vector<UTF16> utf16(utf8_source_len); std::vector<UTF16> utf16((std::size_t(utf8_source_len)));
UTF32 const* in32 = &utf32[0]; UTF32 const* in32 = &utf32[0];
UTF16* out16 = &utf16[0]; UTF16* out16 = &utf16[0];
ret = ConvertUTF32toUTF16(&in32, out32 ret = ConvertUTF32toUTF16(&in32, out32
@ -118,7 +120,7 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
std::printf("%x ", UTF8(*i)); std::printf("%x ", UTF8(*i));
} }
std::vector<UTF8> utf8(utf8_source_len); std::vector<UTF8> utf8((std::size_t(utf8_source_len)));
UTF16 const* in16 = &utf16[0]; UTF16 const* in16 = &utf16[0];
UTF8* out8 = &utf8[0]; UTF8* out8 = &utf8[0];
ret = ConvertUTF16toUTF8(&in16, out16 ret = ConvertUTF16toUTF8(&in16, out16
@ -132,16 +134,16 @@ void verify_transforms(char const* utf8_source, int utf8_source_len = -1)
} }
TEST_EQUAL(out8 - &utf8[0], utf8_source_len); TEST_EQUAL(out8 - &utf8[0], utf8_source_len);
TEST_CHECK(std::equal(&utf8[0], out8, (UTF8 const*)utf8_source)); TEST_CHECK(std::equal(&utf8[0], out8, reinterpret_cast<UTF8 const*>(utf8_source)));
} }
} }
void expect_error(char const* utf8, ConversionResult expect) void expect_error(char const* utf8, ConversionResult expect)
{ {
UTF8 const* in8 = (UTF8 const*)utf8; UTF8 const* in8 = reinterpret_cast<UTF8 const*>(utf8);
std::vector<UTF32> utf32(strlen(utf8)); std::vector<UTF32> utf32(strlen(utf8));
UTF32* out32 = &utf32[0]; UTF32* out32 = &utf32[0];
ConversionResult ret = ConvertUTF8toUTF32(&in8, in8 + strlen(utf8) ConversionResult ret = ConvertUTF8toUTF32(&in8, in8 + std::strlen(utf8)
, &out32, out32 + utf32.size(), strictConversion); , &out32, out32 + utf32.size(), strictConversion);
TEST_EQUAL(ret, expect); TEST_EQUAL(ret, expect);
@ -152,10 +154,10 @@ void expect_error(char const* utf8, ConversionResult expect)
std::printf("%x ", UTF8(*i)); std::printf("%x ", UTF8(*i));
} }
in8 = (UTF8 const*)utf8; in8 = reinterpret_cast<UTF8 const*>(utf8);
std::vector<UTF16> utf16(strlen(utf8)); std::vector<UTF16> utf16(std::strlen(utf8));
UTF16* out16 = &utf16[0]; UTF16* out16 = &utf16[0];
ret = ConvertUTF8toUTF16(&in8, in8 + strlen(utf8) ret = ConvertUTF8toUTF16(&in8, in8 + std::strlen(utf8)
, &out16, out16 + utf16.size(), strictConversion); , &out16, out16 + utf16.size(), strictConversion);
TEST_EQUAL(ret, expect); TEST_EQUAL(ret, expect);
@ -167,6 +169,8 @@ void expect_error(char const* utf8, ConversionResult expect)
} }
} }
} // anonymous namespace
TORRENT_TEST(utf8) TORRENT_TEST(utf8)
{ {
std::vector<char> utf8_source; std::vector<char> utf8_source;
@ -257,7 +261,7 @@ TORRENT_TEST(invalid_encoding)
0x70, 0x2e, 0x31, 0x30, 0x38, 0x30, 0x70, 0x2e, 0x6d, 0x6b, 0x76, 0x00 0x70, 0x2e, 0x31, 0x30, 0x38, 0x30, 0x70, 0x2e, 0x6d, 0x6b, 0x76, 0x00
}; };
error_code ec; error_code ec;
std::wstring wide = utf8_wchar((char const*)test_string, ec); std::wstring wide = utf8_wchar(reinterpret_cast<char const*>(test_string), ec);
TEST_CHECK(ec); TEST_CHECK(ec);
std::wstring cmp_wide; std::wstring cmp_wide;
@ -265,4 +269,3 @@ TORRENT_TEST(invalid_encoding)
std::back_inserter(cmp_wide)); std::back_inserter(cmp_wide));
TEST_CHECK(wide == cmp_wide); TEST_CHECK(wide == cmp_wide);
} }