2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2005-08-12 14:40:58 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/torrent_peer.hpp"
|
2008-05-28 04:35:02 +02:00
|
|
|
#include "libtorrent/bitfield.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/performance_counters.hpp"
|
2014-07-19 04:17:19 +02:00
|
|
|
#include "libtorrent/random.hpp"
|
2016-12-22 16:42:33 +01:00
|
|
|
#include "libtorrent/units.hpp"
|
2014-07-19 04:17:19 +02:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2007-08-27 07:13:46 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2007-09-03 09:10:09 +02:00
|
|
|
#include <set>
|
2010-10-09 04:58:56 +02:00
|
|
|
#include <map>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2005-08-12 14:40:58 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
|
2007-08-27 07:13:46 +02:00
|
|
|
using namespace libtorrent;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2005-08-12 14:40:58 +02:00
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
const int blocks_per_piece = 4;
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
typed_bitfield<piece_index_t> string2vec(char const* have_str)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
const int num_pieces = int(strlen(have_str));
|
2016-12-22 16:42:33 +01:00
|
|
|
typed_bitfield<piece_index_t> have(num_pieces, false);
|
|
|
|
for (piece_index_t i(0); i < have.end_index(); ++i)
|
|
|
|
if (have_str[static_cast<int>(i)] != ' ') have.set_bit(i);
|
2007-08-27 07:13:46 +02:00
|
|
|
return have;
|
|
|
|
}
|
2007-07-04 20:05:29 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
tcp::endpoint endp;
|
|
|
|
ipv4_peer tmp0(endp, false, 0);
|
|
|
|
ipv4_peer tmp1(endp, false, 0);
|
|
|
|
ipv4_peer tmp2(endp, false, 0);
|
|
|
|
ipv4_peer tmp3(endp, false, 0);
|
|
|
|
ipv4_peer tmp4(endp, false, 0);
|
|
|
|
ipv4_peer tmp5(endp, false, 0);
|
|
|
|
ipv4_peer tmp6(endp, false, 0);
|
|
|
|
ipv4_peer tmp7(endp, false, 0);
|
|
|
|
ipv4_peer tmp8(endp, false, 0);
|
|
|
|
ipv4_peer tmp9(endp, false, 0);
|
|
|
|
ipv4_peer peer_struct(endp, true, 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
ipv4_peer* tmp_peer = &tmp1;
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
static std::vector<piece_index_t> const empty_vector;
|
2016-08-06 19:49:23 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
namespace {
|
|
|
|
static struct initializer
|
|
|
|
{
|
|
|
|
initializer()
|
|
|
|
{
|
|
|
|
tmp0.in_use = true;
|
|
|
|
tmp1.in_use = true;
|
|
|
|
tmp2.in_use = true;
|
|
|
|
tmp3.in_use = true;
|
|
|
|
tmp4.in_use = true;
|
|
|
|
tmp5.in_use = true;
|
|
|
|
tmp6.in_use = true;
|
|
|
|
tmp7.in_use = true;
|
|
|
|
tmp8.in_use = true;
|
|
|
|
tmp9.in_use = true;
|
|
|
|
peer_struct.in_use = true;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
} initializer_dummy;
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-06 05:02:29 +01:00
|
|
|
|
2007-08-27 07:13:46 +02:00
|
|
|
// availability is a string where each character is the
|
|
|
|
// availability of that piece, '1', '2' etc.
|
|
|
|
// have_str is a string where each character represents a
|
|
|
|
// piece, ' ' means we don't have the piece and any other
|
|
|
|
// character means we have it
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<piece_picker> setup_picker(
|
2007-08-27 07:13:46 +02:00
|
|
|
char const* availability
|
|
|
|
, char const* have_str
|
|
|
|
, char const* priority
|
|
|
|
, char const* partial)
|
|
|
|
{
|
2016-04-25 23:22:09 +02:00
|
|
|
const int num_pieces = int(strlen(availability));
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(int(strlen(have_str)) == num_pieces);
|
2007-07-04 20:05:29 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::shared_ptr<piece_picker> p = std::make_shared<piece_picker>();
|
2010-02-25 20:56:30 +01:00
|
|
|
p->init(blocks_per_piece, blocks_per_piece, num_pieces);
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
const int avail = availability[static_cast<int>(i)] - '0';
|
2008-09-06 23:04:57 +02:00
|
|
|
assert(avail >= 0);
|
2015-08-20 01:33:20 +02:00
|
|
|
|
2015-03-14 01:42:27 +01:00
|
|
|
static const torrent_peer* peers[10] = { &tmp0, &tmp1, &tmp2
|
2014-07-06 21:18:00 +02:00
|
|
|
, &tmp3, &tmp4, &tmp5, &tmp6, &tmp7, &tmp8, &tmp9 };
|
|
|
|
TORRENT_ASSERT(avail < 10);
|
|
|
|
for (int j = 0; j < avail; ++j) p->inc_refcount(i, peers[j]);
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
auto have = string2vec(have_str);
|
2007-07-04 20:05:29 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < have.end_index(); ++i)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
int const idx = static_cast<int>(i);
|
|
|
|
if (partial[idx] == 0) break;
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
if (partial[idx] == ' ') continue;
|
2007-08-27 07:13:46 +02:00
|
|
|
|
|
|
|
int blocks = 0;
|
2016-12-22 16:42:33 +01:00
|
|
|
if (partial[idx] >= '0' && partial[idx] <= '9')
|
|
|
|
blocks = partial[idx] - '0';
|
2007-08-27 07:13:46 +02:00
|
|
|
else
|
2016-12-22 16:42:33 +01:00
|
|
|
blocks = partial[idx] - 'a' + 10;
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2008-03-14 11:17:27 +01:00
|
|
|
int counter = 0;
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int j = 0; j < 4; ++j)
|
2008-03-14 11:17:27 +01:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(!p->is_finished(piece_block(i, j)));
|
|
|
|
if ((blocks & (1 << j)) == 0) continue;
|
2008-03-14 11:17:27 +01:00
|
|
|
++counter;
|
2015-08-18 10:25:13 +02:00
|
|
|
bool ret = p->mark_as_downloading(piece_block(i, j), tmp_peer);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(ret == true);
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(p->is_requested(piece_block(i, j)) == ((blocks & (1 << j)) != 0));
|
2015-08-18 10:25:13 +02:00
|
|
|
p->mark_as_writing(piece_block(i, j), tmp_peer);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(!p->is_finished(piece_block(i, j)));
|
|
|
|
// trying to mark a block as requested after it has been completed
|
|
|
|
// should fail (return false)
|
2015-08-18 10:25:13 +02:00
|
|
|
ret = p->mark_as_downloading(piece_block(i, j), tmp_peer);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(ret == false);
|
2015-08-18 10:25:13 +02:00
|
|
|
p->mark_as_finished(piece_block(i, j), tmp_peer);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
TEST_CHECK(p->is_downloaded(piece_block(i, j)) == ((blocks & (1 << j)) != 0));
|
|
|
|
TEST_CHECK(p->is_finished(piece_block(i, j)) == ((blocks & (1 << j)) != 0));
|
2008-03-14 11:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
piece_picker::downloading_piece st;
|
|
|
|
p->piece_info(i, st);
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(st.writing), 0);
|
|
|
|
TEST_EQUAL(int(st.requested), 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(int(st.index), idx);
|
2008-03-14 11:17:27 +01:00
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(st.finished, counter);
|
|
|
|
TEST_EQUAL(st.finished + st.requested + st.writing, counter);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
TEST_CHECK(p->is_piece_finished(i) == (counter == 4));
|
2007-08-27 07:13:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
int const idx = static_cast<int>(i);
|
|
|
|
if (priority[idx] == 0) break;
|
|
|
|
const int prio = priority[idx] - '0';
|
2007-08-27 07:13:46 +02:00
|
|
|
assert(prio >= 0);
|
|
|
|
p->set_piece_priority(i, prio);
|
|
|
|
|
|
|
|
TEST_CHECK(p->piece_priority(i) == prio);
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
|
|
|
if (!have[i]) continue;
|
2008-06-07 04:58:28 +02:00
|
|
|
p->we_have(i);
|
2007-08-27 07:13:46 +02:00
|
|
|
for (int j = 0; j < blocks_per_piece; ++j)
|
|
|
|
TEST_CHECK(p->is_finished(piece_block(i, j)));
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<int, piece_index_t> availability_vec;
|
2007-08-27 07:13:46 +02:00
|
|
|
p->get_availability(availability_vec);
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
const int avail = availability[static_cast<int>(i)] - '0';
|
2007-08-27 07:13:46 +02:00
|
|
|
assert(avail >= 0);
|
|
|
|
TEST_CHECK(avail == availability_vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-06-11 06:48:13 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2007-12-16 02:35:42 +01:00
|
|
|
p->check_invariant();
|
|
|
|
#endif
|
2007-08-27 07:13:46 +02:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
bool verify_pick(std::shared_ptr<piece_picker> p
|
2008-09-06 23:04:57 +02:00
|
|
|
, std::vector<piece_block> const& picked, bool allow_multi_blocks = false)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2016-06-11 06:48:13 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2007-12-16 02:35:42 +01:00
|
|
|
p->check_invariant();
|
|
|
|
#endif
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!allow_multi_blocks)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
for (std::vector<piece_block>::const_iterator i = picked.begin()
|
|
|
|
, end(picked.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (p->num_peers(*i) > 0) return false;
|
|
|
|
}
|
2007-08-27 07:13:46 +02:00
|
|
|
}
|
2007-09-03 09:10:09 +02:00
|
|
|
|
|
|
|
// make sure there are no duplicated
|
|
|
|
std::set<piece_block> blocks;
|
2008-09-06 23:04:57 +02:00
|
|
|
std::copy(picked.begin(), picked.end()
|
2016-10-25 23:27:48 +02:00
|
|
|
, std::insert_iterator<std::set<piece_block>>(blocks, blocks.end()));
|
2016-10-09 23:00:20 +02:00
|
|
|
std::cout << " verify: " << picked.size() << " " << blocks.size() << std::endl;
|
2007-09-03 09:10:09 +02:00
|
|
|
return picked.size() == blocks.size();
|
2007-08-27 07:13:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
void print_availability(std::shared_ptr<piece_picker> const& p)
|
2012-05-14 06:48:23 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<int, piece_index_t> avail;
|
2012-05-14 06:48:23 +02:00
|
|
|
p->get_availability(avail);
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("[ ");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (auto p : avail)
|
|
|
|
std::printf("%d ", static_cast<int>(p));
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("]\n");
|
2012-05-14 06:48:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
bool verify_availability(std::shared_ptr<piece_picker> const& p, char const* a)
|
2012-05-14 06:48:23 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<int, piece_index_t> avail;
|
2012-05-14 06:48:23 +02:00
|
|
|
p->get_availability(avail);
|
2016-12-22 16:42:33 +01:00
|
|
|
for (auto i = avail.begin(), end(avail.end()); i != end; ++i, ++a)
|
2012-05-14 06:48:23 +02:00
|
|
|
{
|
|
|
|
if (*a - '0' != *i) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-08-27 07:13:46 +02:00
|
|
|
void print_pick(std::vector<piece_block> const& picked)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
|
|
|
{
|
|
|
|
std::cout << "(" << picked[i].piece_index << ", " << picked[i].block_index << ") ";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::vector<piece_block> pick_pieces(std::shared_ptr<piece_picker> const& p
|
2015-01-20 03:34:55 +01:00
|
|
|
, char const* availability
|
|
|
|
, int num_blocks
|
|
|
|
, int prefer_contiguous_blocks
|
2015-08-18 10:25:13 +02:00
|
|
|
, torrent_peer* peer_struct
|
2015-02-08 22:17:00 +01:00
|
|
|
, int options = piece_picker::rarest_first
|
2016-12-22 16:42:33 +01:00
|
|
|
, std::vector<piece_index_t> const& suggested_pieces = empty_vector)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
|
|
|
std::vector<piece_block> picked;
|
2014-07-06 21:18:00 +02:00
|
|
|
counters pc;
|
2015-01-26 03:04:58 +01:00
|
|
|
p->pick_pieces(string2vec(availability), picked
|
|
|
|
, num_blocks, prefer_contiguous_blocks, peer_struct
|
2015-02-08 22:17:00 +01:00
|
|
|
, options, suggested_pieces, 20, pc);
|
2008-01-31 18:52:29 +01:00
|
|
|
print_pick(picked);
|
|
|
|
TEST_CHECK(verify_pick(p, picked));
|
2008-09-06 23:04:57 +02:00
|
|
|
return picked;
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t test_pick(std::shared_ptr<piece_picker> const& p
|
2015-01-26 03:04:58 +01:00
|
|
|
, int options = piece_picker::rarest_first)
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
std::vector<piece_block> picked = pick_pieces(p, "*******", 1, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
if (picked.size() != 1) return piece_index_t(-1);
|
2008-01-31 18:52:29 +01:00
|
|
|
return picked[0].piece_index;
|
|
|
|
}
|
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
const int options = piece_picker::rarest_first;
|
|
|
|
counters pc;
|
2015-02-15 03:17:57 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(piece_block)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const zero(0);
|
|
|
|
piece_index_t const one(1);
|
|
|
|
|
|
|
|
TEST_CHECK(piece_block(zero, 0) != piece_block(zero, 1));
|
|
|
|
TEST_CHECK(piece_block(zero, 0) != piece_block(one, 0));
|
|
|
|
TEST_CHECK(!(piece_block(zero, 0) != piece_block(zero, 0)));
|
2015-02-15 03:17:57 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(!(piece_block(zero, 0) == piece_block(zero, 1)));
|
|
|
|
TEST_CHECK(!(piece_block(zero, 0) == piece_block(one, 0)));
|
|
|
|
TEST_CHECK(piece_block(zero, 0) == piece_block(zero, 0));
|
2015-02-15 03:17:57 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(!(piece_block(zero, 1) < piece_block(zero, 0)));
|
|
|
|
TEST_CHECK(!(piece_block(one, 0) < piece_block(zero, 0)));
|
|
|
|
TEST_CHECK(piece_block(zero, 0) < piece_block(zero, 1));
|
|
|
|
TEST_CHECK(piece_block(zero, 0) < piece_block(one, 0));
|
|
|
|
TEST_CHECK(!(piece_block(zero, 0) < piece_block(zero, 0)));
|
|
|
|
TEST_CHECK(!(piece_block(one, 0) < piece_block(one, 0)));
|
|
|
|
TEST_CHECK(!(piece_block(zero, 1) < piece_block(zero, 1)));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 03:17:57 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(abort_download)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test abort_download
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "7110000", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) != picked.end());
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->abort_download({piece_index_t(0), 0}, tmp_peer);
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) != picked.end());
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1);
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == true);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) == picked.end());
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->abort_download({piece_index_t(0), 0}, tmp_peer);
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) != picked.end());
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(0), 1}, &tmp1);
|
|
|
|
p->abort_download({piece_index_t(0), 0}, tmp_peer);
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) != picked.end());
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->mark_as_writing({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->write_failed({piece_index_t(0), 0});
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2014-07-06 21:18:00 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(1), 0)) != picked.end()
|
|
|
|
|| std::find(picked.begin(), picked.end(), piece_block(piece_index_t(2), 0)) != picked.end());
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) == picked.end());
|
|
|
|
p->restore_piece(piece_index_t(0));
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) != picked.end());
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->mark_as_writing({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->abort_download({piece_index_t(0), 0}, tmp_peer);
|
2015-02-08 22:17:00 +01:00
|
|
|
picked = pick_pieces(p, "*******", blocks_per_piece, 0, tmp_peer
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) == picked.end());
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(abort_download2)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "7110000", "");
|
|
|
|
piece_picker::downloading_piece st;
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 1}, nullptr);
|
|
|
|
p->piece_info(piece_index_t(0), st);
|
2015-01-26 03:04:58 +01:00
|
|
|
TEST_EQUAL(st.requested, 1);
|
|
|
|
TEST_EQUAL(st.finished, 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->abort_download({piece_index_t(0), 0}, tmp_peer);
|
|
|
|
p->piece_info(piece_index_t(0), st);
|
2015-01-26 03:04:58 +01:00
|
|
|
TEST_EQUAL(st.requested, 0);
|
|
|
|
TEST_EQUAL(st.finished, 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, "*******", blocks_per_piece, 0, nullptr
|
2008-09-06 23:04:57 +02:00
|
|
|
, options, empty_vector);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->is_requested({piece_index_t(0), 0}) == false);
|
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(0), 0)) != picked.end());
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(get_downloaders)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "7110000", "");
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 2}, &tmp1);
|
|
|
|
p->mark_as_writing({piece_index_t(0), 2}, &tmp1);
|
|
|
|
p->abort_download({piece_index_t(0), 2}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(0), 2}, &tmp2);
|
|
|
|
p->mark_as_writing({piece_index_t(0), 2}, &tmp2);
|
2012-04-08 18:03:39 +02:00
|
|
|
|
2015-08-18 10:25:13 +02:00
|
|
|
std::vector<torrent_peer*> d;
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(d, piece_index_t(0));
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(d.size(), 4);
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_CHECK(d[0] == nullptr);
|
|
|
|
TEST_CHECK(d[1] == nullptr);
|
2012-04-12 19:10:22 +02:00
|
|
|
TEST_CHECK(d[2] == &tmp2);
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_CHECK(d[3] == nullptr);
|
2012-04-08 18:03:39 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 3}, &tmp1);
|
|
|
|
p->abort_download({piece_index_t(0), 3}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(0), 3}, &tmp2);
|
|
|
|
p->mark_as_writing({piece_index_t(0), 3}, &tmp2);
|
2012-04-08 18:03:39 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(d, piece_index_t(0));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(d.size(), 4);
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_CHECK(d[0] == nullptr);
|
|
|
|
TEST_CHECK(d[1] == nullptr);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_CHECK(d[2] == &tmp2);
|
2012-04-12 19:10:22 +02:00
|
|
|
TEST_CHECK(d[3] == &tmp2);
|
2012-04-08 18:03:39 +02:00
|
|
|
|
2015-02-12 04:16:53 +01:00
|
|
|
// if we ask for downloaders for a piece that's not
|
|
|
|
// curently being downloaded, we get zeroes back
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(d, piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(d.size(), 4);
|
2016-07-09 22:26:26 +02:00
|
|
|
TEST_CHECK(d[0] == nullptr);
|
|
|
|
TEST_CHECK(d[1] == nullptr);
|
|
|
|
TEST_CHECK(d[2] == nullptr);
|
|
|
|
TEST_CHECK(d[3] == nullptr);
|
2015-02-12 04:16:53 +01:00
|
|
|
|
|
|
|
// ========================================================
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
p = setup_picker("2222", " ", "", "");
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(4); ++i)
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int k = 0; k < blocks_per_piece; ++k)
|
2015-02-08 22:17:00 +01:00
|
|
|
p->mark_as_downloading(piece_block(i, k), &tmp1);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp2);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::printf("num_peers: %d\n", p->num_peers({piece_index_t(0), 0}));
|
|
|
|
TEST_EQUAL(p->num_peers({piece_index_t(0), 0}), 2);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->abort_download({piece_index_t(0), 0}, &tmp1);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::printf("num_peers: %d\n", p->num_peers({piece_index_t(0), 0}));
|
|
|
|
TEST_EQUAL(p->num_peers({piece_index_t(0), 0}), 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(pick_lowest_availability)
|
|
|
|
{
|
2007-08-27 07:13:46 +02:00
|
|
|
// make sure the block that is picked is from piece 1, since it
|
|
|
|
// it is the piece with the lowest availability
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("2223333", "* * * ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(1));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2016-05-25 06:31:52 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(random_pick_at_same_priority)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// make sure pieces with equal priority and availability
|
|
|
|
// are picked at random
|
2016-12-22 16:42:33 +01:00
|
|
|
std::map<piece_index_t, int> random_prio_pieces;
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
{
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111112", " ", "", "");
|
2008-09-06 23:04:57 +02:00
|
|
|
++random_prio_pieces[test_pick(p)];
|
|
|
|
}
|
|
|
|
TEST_CHECK(random_prio_pieces.size() == 6);
|
2016-12-22 16:42:33 +01:00
|
|
|
for (auto i = random_prio_pieces.begin()
|
2008-09-06 23:04:57 +02:00
|
|
|
, end(random_prio_pieces.end()); i != end; ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
std::cout << static_cast<int>(i->first) << ": " << i->second << " ";
|
2008-09-06 23:04:57 +02:00
|
|
|
std::cout << std::endl;
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(pick_highest_priority)
|
|
|
|
{
|
2007-08-27 07:13:46 +02:00
|
|
|
// make sure the block that is picked is from piece 5, since it
|
|
|
|
// has the highest priority among the available pieces
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "1111121", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(5));
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
p = setup_picker("1111111", " ", "1171121", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(2));
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
p = setup_picker("1111111", " ", "1131521", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(4));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-01-26 03:04:58 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(reverse_rarest_first)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("4179253", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, &peer_struct
|
2008-09-06 23:04:57 +02:00
|
|
|
, piece_picker::rarest_first | piece_picker::reverse, empty_vector);
|
|
|
|
int expected_common_pieces[] = {3, 2, 5, 0, 6, 4, 1};
|
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-11-12 19:50:49 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(
|
|
|
|
expected_common_pieces[i / blocks_per_piece])
|
2016-11-12 19:50:49 +01:00
|
|
|
, i % blocks_per_piece));
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
// piece 3 should NOT be prioritized since it's a partial, and not
|
|
|
|
// reversed. Reversed partials are considered reversed
|
2008-09-06 23:04:57 +02:00
|
|
|
p = setup_picker("1122111", " ", "3333333", " 1 ");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p, piece_picker::rarest_first | piece_picker::reverse)
|
|
|
|
== piece_index_t(2));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(pick_whole_pieces)
|
|
|
|
{
|
2007-08-27 07:13:46 +02:00
|
|
|
// make sure the 4 blocks are picked from the same piece if
|
2008-09-06 23:04:57 +02:00
|
|
|
// whole pieces are preferred. Priority and availability is more
|
|
|
|
// important. Piece 1 has the lowest availability even though
|
|
|
|
// it is not a whole piece
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("2212222", " ", "1111111", "1023460");
|
|
|
|
auto picked = pick_pieces(p, "****** ", 1, blocks_per_piece
|
2015-02-08 22:17:00 +01:00
|
|
|
, &peer_struct, options, empty_vector);
|
2015-01-20 03:34:55 +01:00
|
|
|
TEST_EQUAL(int(picked.size()), 3);
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int i = 0; i < blocks_per_piece && i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[i].piece_index, piece_index_t(2));
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
p = setup_picker("1111111", " ", "1111111", "");
|
2015-01-26 03:04:58 +01:00
|
|
|
picked = pick_pieces(p, "****** ", 1, blocks_per_piece
|
2015-02-08 22:17:00 +01:00
|
|
|
, &peer_struct, options, empty_vector);
|
2015-01-20 03:34:55 +01:00
|
|
|
TEST_EQUAL(int(picked.size()), blocks_per_piece);
|
2007-09-03 23:16:24 +02:00
|
|
|
for (int i = 0; i < blocks_per_piece && i < int(picked.size()); ++i)
|
2015-01-20 03:34:55 +01:00
|
|
|
TEST_EQUAL(picked[i].block_index, i);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
p = setup_picker("2221222", " ", "", "");
|
2015-01-20 03:34:55 +01:00
|
|
|
picked = pick_pieces(p, "*******", 1, 7 * blocks_per_piece
|
2015-02-08 22:17:00 +01:00
|
|
|
, &peer_struct, options, empty_vector);
|
2015-01-20 03:34:55 +01:00
|
|
|
TEST_EQUAL(int(picked.size()), 7 * blocks_per_piece);
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(i / blocks_per_piece)
|
|
|
|
, i % blocks_per_piece));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(distributed_copies)
|
|
|
|
{
|
2007-08-27 07:13:46 +02:00
|
|
|
// test the distributed copies function. It should include ourself
|
|
|
|
// in the availability. i.e. piece 0 has availability 2.
|
|
|
|
// there are 2 pieces with availability 2 and 5 with availability 3
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1233333", "* ", "", "");
|
|
|
|
auto dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
TEST_CHECK(dc == std::make_pair(2, 5000 / 7));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(filtered_pieces)
|
|
|
|
{
|
2007-08-27 07:13:46 +02:00
|
|
|
// make sure filtered pieces are ignored
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "0010000", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p, piece_picker::rarest_first) == piece_index_t(2));
|
|
|
|
TEST_CHECK(test_pick(p, piece_picker::rarest_first | piece_picker::reverse) == piece_index_t(2));
|
|
|
|
TEST_CHECK(test_pick(p, piece_picker::sequential) == piece_index_t(2));
|
|
|
|
TEST_CHECK(test_pick(p, piece_picker::sequential | piece_picker::reverse) == piece_index_t(2));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-07-04 20:05:29 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(we_dont_have)
|
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
// make sure we_dont_have works
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", "*******", "0100000", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->have_piece(piece_index_t(1)));
|
|
|
|
TEST_CHECK(p->have_piece(piece_index_t(2)));
|
|
|
|
p->we_dont_have(piece_index_t(1));
|
|
|
|
p->we_dont_have(piece_index_t(2));
|
|
|
|
TEST_CHECK(!p->have_piece(piece_index_t(1)));
|
|
|
|
TEST_CHECK(!p->have_piece(piece_index_t(2)));
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, "*** ** ", 1, 0, nullptr, options, empty_vector);
|
2008-06-07 04:58:28 +02:00
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front().piece_index == piece_index_t(1));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-06-07 04:58:28 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(dec_refcount_split_seed)
|
|
|
|
{
|
2014-09-04 10:55:24 +02:00
|
|
|
// make sure we can split m_seed when removing a refcount
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("0000000", " ", "0000000", "");
|
2016-07-09 22:26:26 +02:00
|
|
|
p->inc_refcount_all(nullptr);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<int, piece_index_t> avail;
|
2014-09-04 10:55:24 +02:00
|
|
|
p->get_availability(avail);
|
|
|
|
TEST_EQUAL(avail.size(), 7);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(avail[piece_index_t(0)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(1)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(2)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(3)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(4)] != 0);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->dec_refcount(piece_index_t(3), nullptr);
|
2014-09-04 10:55:24 +02:00
|
|
|
|
|
|
|
p->get_availability(avail);
|
|
|
|
TEST_EQUAL(avail.size(), 7);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(avail[piece_index_t(0)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(1)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(2)] != 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(3)] == 0);
|
|
|
|
TEST_CHECK(avail[piece_index_t(4)] != 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-09-04 10:55:24 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(init)
|
|
|
|
{
|
2008-08-26 00:32:50 +02:00
|
|
|
// make sure init preserves priorities
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "1111111", "");
|
2008-08-26 00:32:50 +02:00
|
|
|
|
|
|
|
TEST_CHECK(p->num_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have() == 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
2008-08-26 00:32:50 +02:00
|
|
|
TEST_CHECK(p->num_filtered() == 1);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have() == 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_have(piece_index_t(0));
|
2008-08-26 00:32:50 +02:00
|
|
|
|
|
|
|
TEST_CHECK(p->num_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 1);
|
|
|
|
TEST_CHECK(p->num_have() == 1);
|
|
|
|
|
2010-02-25 20:56:30 +01:00
|
|
|
p->init(blocks_per_piece, blocks_per_piece, blocks_per_piece * 7);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->piece_priority(piece_index_t(0)) == 0);
|
2008-08-26 00:32:50 +02:00
|
|
|
TEST_CHECK(p->num_filtered() == 1);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have() == 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-08-26 00:32:50 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(dont_pick_requested_blocks)
|
|
|
|
{
|
2007-08-27 12:12:09 +02:00
|
|
|
// make sure requested blocks aren't picked
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2007-08-27 12:12:09 +02:00
|
|
|
piece_block first = picked.front();
|
2015-02-08 22:17:00 +01:00
|
|
|
p->mark_as_downloading(picked.front(), &peer_struct);
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(p->num_peers(picked.front()) == 1);
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(picked.front() != first);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 12:12:09 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(downloading_piece_priority)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// make sure downloading pieces have higher priority
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
auto first = picked.front();
|
2015-02-08 22:17:00 +01:00
|
|
|
p->mark_as_downloading(picked.front(), &peer_struct);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(p->num_peers(picked.front()) == 1);
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
|
|
|
TEST_CHECK(picked.front() != first);
|
|
|
|
TEST_CHECK(picked.front().piece_index == first.piece_index);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(partial_piece_order_rarest_first)
|
|
|
|
{
|
2015-02-08 19:01:29 +01:00
|
|
|
// when we're prioritizing partial pieces, make sure to first pick the
|
|
|
|
// rarest of them. The blocks in this test are:
|
|
|
|
// 0: [ ] avail: 1
|
|
|
|
// 1: [x ] avail: 1
|
|
|
|
// 2: [xx ] avail: 1
|
|
|
|
// 3: [xxx ] avail: 2
|
|
|
|
// 4: [ ] avail: 1
|
|
|
|
// 5: [ ] avail: 1
|
|
|
|
// 6: [xxxx] avail: 1
|
|
|
|
// piece 6 does not have any blocks left to pick, even though piece 3 only
|
|
|
|
// has a single block left before it completes, it is less rare than piece
|
|
|
|
// 2. Piece 2 is the best pick in this case.
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1112111", " ", "", "013700f");
|
|
|
|
auto picked = pick_pieces(p, "*******", 1, 0, nullptr
|
2015-02-08 19:01:29 +01:00
|
|
|
, options | piece_picker::prioritize_partials, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front() == piece_block(piece_index_t(2), 2)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(2), 3));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-08 19:01:29 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(partial_piece_order_most_complete)
|
|
|
|
{
|
2015-02-08 19:01:29 +01:00
|
|
|
// as a tie breaker, make sure downloading pieces closer to completion have
|
|
|
|
// higher priority. piece 3 is only 1 block from being completed, and should
|
|
|
|
// be picked
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "013700f");
|
|
|
|
auto picked = pick_pieces(p, "*******", 1, 0, nullptr
|
2015-02-08 19:01:29 +01:00
|
|
|
, options | piece_picker::prioritize_partials, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front() == piece_block(piece_index_t(3), 3));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(partial_piece_order_sequential)
|
|
|
|
{
|
2015-02-08 22:44:58 +01:00
|
|
|
// if we don't use rarest first when we prioritize partials, but instead use
|
|
|
|
// sequential order, make sure we pick the right one
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "013700f");
|
|
|
|
auto picked = pick_pieces(p, "*******", 1, 0, nullptr
|
2015-02-08 22:44:58 +01:00
|
|
|
, piece_picker::sequential | piece_picker::prioritize_partials, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front() == piece_block(piece_index_t(1), 1)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(1), 2)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(1), 3));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-08 22:12:10 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(random_picking_downloading_piece)
|
|
|
|
{
|
2015-02-08 22:12:10 +01:00
|
|
|
// make sure the random piece picker can still pick partial pieces
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "013700f");
|
|
|
|
auto picked = pick_pieces(p, " *** *", 1, 0, nullptr
|
2015-02-08 22:12:10 +01:00
|
|
|
, 0, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front() == piece_block(piece_index_t(1), 1)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(2), 2)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(3), 3));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-08 22:12:10 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(random_picking_downloading_piece_prefer_contiguous)
|
|
|
|
{
|
2015-02-08 22:12:10 +01:00
|
|
|
// make sure the random piece picker can still pick partial pieces
|
|
|
|
// even when prefer_contiguous_blocks is set
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "013700f");
|
|
|
|
auto picked = pick_pieces(p, " *** *", 1, 4, nullptr
|
2015-02-08 22:12:10 +01:00
|
|
|
, 0, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) > 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front() == piece_block(piece_index_t(1), 1)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(2), 2)
|
|
|
|
|| picked.front() == piece_block(piece_index_t(3), 3));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-08 22:12:10 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(sequential_download)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test sequential download
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, nullptr
|
2008-09-06 23:04:57 +02:00
|
|
|
, piece_picker::sequential, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece);
|
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(i / blocks_per_piece)
|
|
|
|
, i % blocks_per_piece));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(reverse_sequential_download)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test reverse sequential download
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, nullptr
|
2008-09-06 23:04:57 +02:00
|
|
|
, piece_picker::sequential | piece_picker::reverse, empty_vector);
|
|
|
|
TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece);
|
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(6 - (i / blocks_per_piece))
|
|
|
|
, i % blocks_per_piece));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(priority_sequential_download)
|
|
|
|
{
|
2014-04-03 04:03:14 +02:00
|
|
|
// test priority sequential download
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "1117071", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, nullptr
|
2014-04-03 04:03:14 +02:00
|
|
|
, piece_picker::sequential, empty_vector);
|
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
// the piece with priority 0 was not picked, everything else should
|
|
|
|
// be picked
|
|
|
|
TEST_EQUAL(int(picked.size()), 6 * blocks_per_piece);
|
2014-04-03 04:03:14 +02:00
|
|
|
|
|
|
|
// 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)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i].piece_index == piece_index_t(3) || picked[i].piece_index == piece_index_t(5));
|
2014-04-03 04:03:14 +02:00
|
|
|
|
|
|
|
int expected[] = {-1, -1, 0, 1, 2, 6};
|
|
|
|
for (int i = 2 * blocks_per_piece; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[i].piece_index, piece_index_t(expected[i / blocks_per_piece]));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-04-03 04:03:14 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_up_we_have)
|
|
|
|
{
|
2015-02-15 21:01:30 +01:00
|
|
|
// sweep up, we_have()
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(7); ++i)
|
2015-02-15 21:01:30 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(p->cursor(), i);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
2015-02-15 21:01:30 +01:00
|
|
|
p->we_have(i);
|
|
|
|
}
|
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 21:01:30 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_up_set_piece_priority)
|
|
|
|
{
|
2015-02-15 21:01:30 +01:00
|
|
|
// sweep up, set_piece_priority()
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(0); i < piece_index_t(7); ++i)
|
2015-02-15 21:01:30 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(p->cursor(), i);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
2015-02-15 21:01:30 +01:00
|
|
|
p->set_piece_priority(i, 0);
|
|
|
|
}
|
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(!p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 21:01:30 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_down_we_have)
|
|
|
|
{
|
2015-02-15 21:01:30 +01:00
|
|
|
// sweep down, we_have()
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(6); i >= piece_index_t(0); --i)
|
2015-02-15 21:01:30 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), next(i));
|
2015-02-15 21:01:30 +01:00
|
|
|
p->we_have(i);
|
|
|
|
}
|
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 21:01:30 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_down_set_piece_priority)
|
|
|
|
{
|
2015-02-15 21:01:30 +01:00
|
|
|
// sweep down, set_piece_priority()
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t i(6); i >= piece_index_t(0); --i)
|
2015-02-15 21:01:30 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), next(i));
|
2015-02-15 21:01:30 +01:00
|
|
|
p->set_piece_priority(i, 0);
|
|
|
|
}
|
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(!p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 21:01:30 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_in_set_priority)
|
|
|
|
{
|
2015-02-15 23:01:15 +01:00
|
|
|
// sweep in, set_piece_priority()
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t left(0), right(6); left <= piece_index_t(3)
|
|
|
|
&& right >= piece_index_t(3); ++left, --right)
|
2015-02-15 23:01:15 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(p->cursor(), left);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->reverse_cursor(), next(right));
|
2015-02-15 23:01:15 +01:00
|
|
|
p->set_piece_priority(left, 0);
|
|
|
|
p->set_piece_priority(right, 0);
|
|
|
|
}
|
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(!p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 23:01:15 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_in_we_have)
|
|
|
|
{
|
2015-02-15 23:01:15 +01:00
|
|
|
// sweep in, we_have()
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
for (piece_index_t left(0), right(6); left <= piece_index_t(3)
|
|
|
|
&& right >= piece_index_t(3); ++left, --right)
|
2015-02-15 23:01:15 +01:00
|
|
|
{
|
|
|
|
TEST_EQUAL(p->cursor(), left);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->reverse_cursor(), next(right));
|
2015-02-15 23:01:15 +01:00
|
|
|
p->we_have(left);
|
|
|
|
p->we_have(right);
|
|
|
|
}
|
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 23:01:15 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_up_we_dont_have)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("7654321", "*******", "", "");
|
2015-02-15 21:01:30 +01:00
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
|
|
|
for (piece_index_t i(0); i < piece_index_t(7); ++i)
|
2015-02-15 21:01:30 +01:00
|
|
|
{
|
|
|
|
p->we_dont_have(i);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), next(i));
|
2015-02-15 21:01:30 +01:00
|
|
|
}
|
|
|
|
TEST_CHECK(!p->is_finished());
|
|
|
|
TEST_CHECK(!p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 21:01:30 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_down_we_dont_have)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("7654321", "*******", "", "");
|
2015-02-15 21:01:30 +01:00
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
|
|
|
for (piece_index_t i(6); i >= piece_index_t(0); --i)
|
2015-02-15 21:01:30 +01:00
|
|
|
{
|
|
|
|
p->we_dont_have(i);
|
|
|
|
TEST_EQUAL(p->cursor(), i);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
2015-02-15 21:01:30 +01:00
|
|
|
}
|
|
|
|
TEST_CHECK(!p->is_finished());
|
|
|
|
TEST_CHECK(!p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 21:01:30 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors_sweep_out_we_dont_have)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("7654321", "*******", "", "");
|
2015-02-15 23:01:15 +01:00
|
|
|
TEST_CHECK(p->is_finished());
|
|
|
|
TEST_CHECK(p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
|
|
|
for (piece_index_t left(3), right(3);
|
|
|
|
left >= piece_index_t(0) && right < piece_index_t(7);
|
|
|
|
--left, ++right)
|
2015-02-15 23:01:15 +01:00
|
|
|
{
|
|
|
|
p->we_dont_have(left);
|
|
|
|
p->we_dont_have(right);
|
|
|
|
TEST_EQUAL(p->cursor(), left);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->reverse_cursor(), next(right));
|
2015-02-15 23:01:15 +01:00
|
|
|
}
|
|
|
|
TEST_CHECK(!p->is_finished());
|
|
|
|
TEST_CHECK(!p->is_seeding());
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-15 23:01:15 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(cursors)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test cursors
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->we_have(piece_index_t(1));
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->we_have(piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->we_have(piece_index_t(5));
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->we_have(piece_index_t(6));
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(5));
|
|
|
|
p->we_have(piece_index_t(4));
|
|
|
|
p->we_have(piece_index_t(3));
|
|
|
|
p->we_have(piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
p = setup_picker("7654321", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->set_piece_priority(piece_index_t(1), 0);
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(0));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->set_piece_priority(piece_index_t(5), 0);
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(7));
|
|
|
|
p->set_piece_priority(piece_index_t(6), 0);
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(2));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(5));
|
|
|
|
p->set_piece_priority(piece_index_t(4), 0);
|
|
|
|
p->set_piece_priority(piece_index_t(3), 0);
|
|
|
|
p->set_piece_priority(piece_index_t(2), 0);
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(7));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(0));
|
|
|
|
p->set_piece_priority(piece_index_t(3), 1);
|
|
|
|
TEST_EQUAL(p->cursor(), piece_index_t(3));
|
|
|
|
TEST_EQUAL(p->reverse_cursor(), piece_index_t(4));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(piece_priorities)
|
|
|
|
{
|
2007-09-03 09:10:09 +02:00
|
|
|
// test piece priorities
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("5555555", " ", "7654321", "");
|
2007-09-03 09:10:09 +02:00
|
|
|
TEST_CHECK(p->num_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
2007-09-03 09:10:09 +02:00
|
|
|
TEST_CHECK(p->num_filtered() == 1);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 0);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 0}, nullptr);
|
|
|
|
p->we_have(piece_index_t(0));
|
2007-09-03 09:10:09 +02:00
|
|
|
TEST_CHECK(p->num_filtered() == 0);
|
|
|
|
TEST_CHECK(p->num_have_filtered() == 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_dont_have(piece_index_t(0));
|
|
|
|
p->set_piece_priority(piece_index_t(0), 7);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, options, empty_vector);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(int(picked.size()) == 7 * blocks_per_piece);
|
|
|
|
|
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(i / blocks_per_piece), i % blocks_per_piece));
|
2016-08-06 19:49:23 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// test changing priority on a piece we have
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_have(piece_index_t(0));
|
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
|
|
|
p->set_piece_priority(piece_index_t(0), 1);
|
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
2007-09-03 09:10:09 +02:00
|
|
|
|
|
|
|
std::vector<int> prios;
|
|
|
|
p->piece_priorities(prios);
|
|
|
|
TEST_CHECK(prios.size() == 7);
|
2008-09-06 23:04:57 +02:00
|
|
|
int prio_comp[] = {0, 6, 5, 4, 3, 2, 1};
|
2007-09-03 09:10:09 +02:00
|
|
|
TEST_CHECK(std::equal(prios.begin(), prios.end(), prio_comp));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(restore_piece)
|
|
|
|
{
|
2007-09-03 09:10:09 +02:00
|
|
|
// test restore_piece
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1234567", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 0}, nullptr);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 1}, nullptr);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 2}, nullptr);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 3}, nullptr);
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front().piece_index == piece_index_t(1));
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->restore_piece(piece_index_t(0));
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front().piece_index == piece_index_t(0));
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 0}, nullptr);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 1}, nullptr);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 2}, nullptr);
|
|
|
|
p->mark_as_finished({piece_index_t(0), 3}, nullptr);
|
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front().piece_index == piece_index_t(1));
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->restore_piece(piece_index_t(0));
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front().piece_index == piece_index_t(1));
|
2007-09-03 09:10:09 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->set_piece_priority(piece_index_t(0), 7);
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, "*******", 1, 0, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.front().piece_index == piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(random_pick)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test random mode
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1234567", " ", "1111122", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
std::set<piece_index_t> random_pieces;
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
random_pieces.insert(test_pick(p, 0));
|
|
|
|
TEST_CHECK(random_pieces.size() == 7);
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
random_pieces.clear();
|
|
|
|
for (int i = 0; i < 7; ++i)
|
2007-08-27 07:13:46 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const piece = test_pick(p, 0);
|
2008-09-06 23:04:57 +02:00
|
|
|
p->we_have(piece);
|
|
|
|
random_pieces.insert(piece);
|
2007-08-27 07:13:46 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(random_pieces.size() == 7);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(picking_downloading_blocks)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// make sure the piece picker will pick pieces that
|
|
|
|
// are already requested from other peers if it has to
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(2), 2}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(1), 2}, &tmp1);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
std::vector<piece_block> picked;
|
2008-09-06 23:04:57 +02:00
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec("*******"), picked, 7 * blocks_per_piece, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::prioritize_partials, empty_vector, 20
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
2014-07-06 21:18:00 +02:00
|
|
|
// don't pick both busy pieces, if there are already other blocks picked
|
|
|
|
TEST_EQUAL(picked.size(), 7 * blocks_per_piece - 2);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec("*******"), picked, 7 * blocks_per_piece, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::prioritize_partials
|
2014-07-06 21:18:00 +02:00
|
|
|
| piece_picker::rarest_first, empty_vector, 20
|
|
|
|
, pc);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
2014-07-06 21:18:00 +02:00
|
|
|
// don't pick both busy pieces, if there are already other blocks picked
|
|
|
|
TEST_EQUAL(picked.size(), 7 * blocks_per_piece - 2);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec("*******"), picked, 7 * blocks_per_piece, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::rarest_first, empty_vector, 20
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
2014-07-06 21:18:00 +02:00
|
|
|
// don't pick both busy pieces, if there are already other blocks picked
|
|
|
|
TEST_EQUAL(picked.size(), 7 * blocks_per_piece - 2);
|
|
|
|
|
|
|
|
// make sure we still pick from a partial piece even when prefering whole pieces
|
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec(" * "), picked, 1, blocks_per_piece, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::rarest_first
|
2015-01-20 03:34:55 +01:00
|
|
|
| piece_picker::align_expanded_pieces, empty_vector, 20
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
|
|
|
// always only pick one busy piece
|
|
|
|
TEST_EQUAL(picked.size(), 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.size() >= 1 && picked[0].piece_index == piece_index_t(1));
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// don't pick locked pieces
|
|
|
|
picked.clear();
|
2016-12-22 16:42:33 +01:00
|
|
|
p->lock_piece(piece_index_t(1));
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec(" ** "), picked, 7, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::rarest_first, empty_vector, 20
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
|
|
|
// always only pick one busy piece
|
|
|
|
TEST_EQUAL(picked.size(), 3);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked.size() >= 1 && picked[0].piece_index == piece_index_t(2));
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->restore_piece(piece_index_t(1));
|
|
|
|
p->mark_as_downloading({piece_index_t(2), 0}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(2), 1}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(2), 3}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(1), 0}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(1), 1}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(1), 2}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(1), 3}, &tmp1);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec(" ** "), picked, 2, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::rarest_first, empty_vector, 20
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
|
|
|
// always only pick one busy piece
|
|
|
|
TEST_EQUAL(picked.size(), 1);
|
|
|
|
|
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec(" ** "), picked, 2 * blocks_per_piece, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::prioritize_partials, empty_vector, 0
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
|
|
|
// always only pick one busy piece
|
|
|
|
TEST_EQUAL(picked.size(), 1);
|
|
|
|
|
|
|
|
picked.clear();
|
2016-07-09 22:26:26 +02:00
|
|
|
p->pick_pieces(string2vec(" ** "), picked, 2 * blocks_per_piece, 0, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, piece_picker::prioritize_partials, empty_vector, 20
|
2014-07-06 21:18:00 +02:00
|
|
|
, pc);
|
|
|
|
TEST_CHECK(verify_pick(p, picked, true));
|
|
|
|
print_pick(picked);
|
|
|
|
// always only pick one busy piece
|
|
|
|
TEST_EQUAL(picked.size(), 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(clear_peer)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test clear_peer
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1123333", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(0), 1}, &tmp2);
|
|
|
|
p->mark_as_downloading({piece_index_t(0), 2}, &tmp3);
|
|
|
|
p->mark_as_downloading({piece_index_t(1), 1}, &tmp1);
|
|
|
|
p->mark_as_downloading({piece_index_t(2), 1}, &tmp2);
|
|
|
|
p->mark_as_downloading({piece_index_t(3), 1}, &tmp3);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2015-08-18 10:25:13 +02:00
|
|
|
std::vector<torrent_peer*> dls;
|
2016-07-09 22:26:26 +02:00
|
|
|
torrent_peer* expected_dls1[] = {&tmp1, &tmp2, &tmp3, nullptr};
|
|
|
|
torrent_peer* expected_dls2[] = {nullptr, &tmp1, nullptr, nullptr};
|
|
|
|
torrent_peer* expected_dls3[] = {nullptr, &tmp2, nullptr, nullptr};
|
|
|
|
torrent_peer* expected_dls4[] = {nullptr, &tmp3, nullptr, nullptr};
|
|
|
|
torrent_peer* expected_dls5[] = {&tmp1, nullptr, &tmp3, nullptr};
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(dls, piece_index_t(0));
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(std::equal(dls.begin(), dls.end(), expected_dls1));
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(dls, piece_index_t(1));
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(std::equal(dls.begin(), dls.end(), expected_dls2));
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(dls, piece_index_t(2));
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(std::equal(dls.begin(), dls.end(), expected_dls3));
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(dls, piece_index_t(3));
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(std::equal(dls.begin(), dls.end(), expected_dls4));
|
|
|
|
|
|
|
|
p->clear_peer(&tmp2);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->get_downloaders(dls, piece_index_t(0));
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(std::equal(dls.begin(), dls.end(), expected_dls5));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 07:13:46 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(have_all_have_none)
|
|
|
|
{
|
2007-08-27 07:13:46 +02:00
|
|
|
// test have_all and have_none
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("0123333", "* ", "", "");
|
|
|
|
auto dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
std::cout << "distributed copies: " << dc.first << "." << (dc.second / 1000.f) << std::endl;
|
|
|
|
TEST_CHECK(dc == std::make_pair(1, 5000 / 7));
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount_all(&tmp8);
|
2007-08-27 07:13:46 +02:00
|
|
|
dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
TEST_CHECK(dc == std::make_pair(2, 5000 / 7));
|
2014-07-06 21:18:00 +02:00
|
|
|
p->dec_refcount_all(&tmp8);
|
2007-08-27 07:13:46 +02:00
|
|
|
dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
std::cout << "distributed copies: " << dc.first << "." << (dc.second / 1000.f) << std::endl;
|
|
|
|
TEST_CHECK(dc == std::make_pair(1, 5000 / 7));
|
2016-12-22 16:42:33 +01:00
|
|
|
p->inc_refcount(piece_index_t(0), &tmp0);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->dec_refcount_all(&tmp0);
|
2008-06-11 10:30:06 +02:00
|
|
|
dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
std::cout << "distributed copies: " << dc.first << "." << (dc.second / 1000.f) << std::endl;
|
|
|
|
TEST_CHECK(dc == std::make_pair(0, 6000 / 7));
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(2));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-06-11 10:30:06 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(have_all_have_none_seq_download)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// test have_all and have_none
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("0123333", "* ", "", "");
|
|
|
|
auto dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
std::cout << "distributed copies: " << dc.first << "." << (dc.second / 1000.f) << std::endl;
|
|
|
|
TEST_CHECK(dc == std::make_pair(1, 5000 / 7));
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount_all(&tmp8);
|
2008-06-11 10:30:06 +02:00
|
|
|
dc = p->distributed_copies();
|
2009-07-23 06:37:51 +02:00
|
|
|
std::cout << "distributed copies: " << dc.first << "." << (dc.second / 1000.f) << std::endl;
|
|
|
|
TEST_CHECK(dc == std::make_pair(2, 5000 / 7));
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(1));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(inc_ref_dec_ref)
|
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
// test inc_ref and dec_ref
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1233333", " * ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(0));
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->dec_refcount(piece_index_t(0), &tmp0);
|
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(1));
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->dec_refcount(piece_index_t(4), &tmp0);
|
|
|
|
p->dec_refcount(piece_index_t(4), &tmp1);
|
|
|
|
TEST_CHECK(test_pick(p) == piece_index_t(4));
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
// decrease refcount on something that's not in the piece list
|
2016-12-22 16:42:33 +01:00
|
|
|
p->dec_refcount(piece_index_t(5), &tmp0);
|
|
|
|
p->inc_refcount(piece_index_t(5), &tmp0);
|
|
|
|
|
|
|
|
typed_bitfield<piece_index_t> bits = string2vec("* ");
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(2)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(3)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(4)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(5)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(6)), false);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount(bits, &tmp0);
|
|
|
|
bits = string2vec(" * ");
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(0)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(2)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(3)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(4)), true);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(5)), false);
|
|
|
|
TEST_EQUAL(bits.get_bit(piece_index_t(6)), false);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->dec_refcount(bits, &tmp2);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(unverified_blocks)
|
|
|
|
{
|
2007-08-27 12:12:09 +02:00
|
|
|
// test unverified_blocks, marking blocks and get_downloader
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "0300700");
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(p->unverified_blocks() == 2 + 3);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 0}) == tmp_peer);
|
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 1}) == tmp_peer);
|
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 2}) == tmp_peer);
|
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 3}) == nullptr);
|
|
|
|
p->mark_as_downloading({piece_index_t(4), 3}, &peer_struct);
|
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 3}) == &peer_struct);
|
2016-08-06 19:49:23 +02:00
|
|
|
|
|
|
|
piece_picker::downloading_piece st;
|
2016-12-22 16:42:33 +01:00
|
|
|
p->piece_info(piece_index_t(4), st);
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(st.requested == 1);
|
|
|
|
TEST_CHECK(st.writing == 0);
|
|
|
|
TEST_CHECK(st.finished == 3);
|
|
|
|
TEST_CHECK(p->unverified_blocks() == 2 + 3);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_writing({piece_index_t(4), 3}, &peer_struct);
|
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 3}) == &peer_struct);
|
|
|
|
p->piece_info(piece_index_t(4), st);
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(st.requested == 0);
|
|
|
|
TEST_CHECK(st.writing == 1);
|
|
|
|
TEST_CHECK(st.finished == 3);
|
|
|
|
TEST_CHECK(p->unverified_blocks() == 2 + 3);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(4), 3}, &peer_struct);
|
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 3}) == &peer_struct);
|
|
|
|
p->piece_info(piece_index_t(4), st);
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(st.requested == 0);
|
|
|
|
TEST_CHECK(st.writing == 0);
|
|
|
|
TEST_CHECK(st.finished == 4);
|
|
|
|
TEST_CHECK(p->unverified_blocks() == 2 + 4);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_have(piece_index_t(4));
|
|
|
|
p->piece_info(piece_index_t(4), st);
|
2007-09-03 09:10:09 +02:00
|
|
|
TEST_CHECK(st.requested == 0);
|
|
|
|
TEST_CHECK(st.writing == 0);
|
|
|
|
TEST_CHECK(st.finished == 4);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(p->get_downloader({piece_index_t(4), 3}) == nullptr);
|
2007-08-27 12:12:09 +02:00
|
|
|
TEST_CHECK(p->unverified_blocks() == 2);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-08-27 12:12:09 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(prefer_cnotiguous_blocks)
|
|
|
|
{
|
2015-01-20 03:34:55 +01:00
|
|
|
// test prefer_contiguous_blocks
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "*******", 1, 3 * blocks_per_piece
|
2016-07-09 22:26:26 +02:00
|
|
|
, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 3 * blocks_per_piece);
|
2007-09-03 23:16:24 +02:00
|
|
|
piece_block b = picked.front();
|
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
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);
|
2007-09-03 23:16:24 +02:00
|
|
|
b = picked[i];
|
|
|
|
}
|
|
|
|
|
2015-01-20 03:34:55 +01:00
|
|
|
picked = pick_pieces(p, "*******", 1, 3 * blocks_per_piece
|
2016-07-09 22:26:26 +02:00
|
|
|
, nullptr, options, empty_vector);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= 3 * blocks_per_piece);
|
2007-09-03 23:16:24 +02:00
|
|
|
b = picked.front();
|
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
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);
|
2007-09-03 23:16:24 +02:00
|
|
|
b = picked[i];
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
// make sure pieces that don't match the 'whole pieces' requirement
|
|
|
|
// are picked if there's no other choice
|
|
|
|
p = setup_picker("1111111", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(2), 2}, &tmp1);
|
2015-01-26 03:04:58 +01:00
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece - 1, blocks_per_piece
|
2016-07-09 22:26:26 +02:00
|
|
|
, nullptr, options, empty_vector);
|
2008-09-06 23:04:57 +02:00
|
|
|
TEST_CHECK(picked.size() == 7 * blocks_per_piece - 1);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(std::find(picked.begin(), picked.end(), piece_block(piece_index_t(2), 2)) == picked.end());
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(prefer_aligned_whole_pieces)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("2222221222222222", " ", "", "");
|
|
|
|
auto picked = pick_pieces(p, "****************", 1, 4 * blocks_per_piece, nullptr
|
2015-02-08 22:17:00 +01:00
|
|
|
, options | piece_picker::align_expanded_pieces, empty_vector);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// the piece picker should pick piece 5, and then align it to even 4 pieces
|
|
|
|
// i.e. it should have picked pieces: 4,5,6,7
|
|
|
|
print_pick(picked);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked.size(), 4 * blocks_per_piece);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::set<piece_index_t> picked_pieces;
|
|
|
|
for (auto p : picked) picked_pieces.insert(p.piece_index);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
TEST_CHECK(picked_pieces.size() == 4);
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t expected_pieces[] = {piece_index_t(4),piece_index_t(5),piece_index_t(6),piece_index_t(7)};
|
2014-07-06 21:18:00 +02:00
|
|
|
TEST_CHECK(std::equal(picked_pieces.begin(), picked_pieces.end(), expected_pieces))
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(parole_mode)
|
|
|
|
{
|
2007-09-05 23:21:11 +02:00
|
|
|
// test parole mode
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("3333133", " ", "", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 0}, nullptr);
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, "*******", 1, blocks_per_piece, nullptr
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
, options | piece_picker::on_parole | piece_picker::prioritize_partials, empty_vector);
|
2015-01-20 03:34:55 +01:00
|
|
|
TEST_EQUAL(int(picked.size()), blocks_per_piece - 1);
|
2007-09-05 23:21:11 +02:00
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(0), i + 1));
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
// make sure that the partial piece is not picked by a
|
2007-09-05 23:21:11 +02:00
|
|
|
// peer that is has not downloaded/requested the other blocks
|
2015-01-26 03:04:58 +01:00
|
|
|
picked = pick_pieces(p, "*******", 1, blocks_per_piece
|
2015-02-08 22:17:00 +01:00
|
|
|
, &peer_struct
|
2008-09-06 23:04:57 +02:00
|
|
|
, options | piece_picker::on_parole | piece_picker::prioritize_partials, empty_vector);
|
2015-01-20 03:34:55 +01:00
|
|
|
TEST_EQUAL(int(picked.size()), blocks_per_piece);
|
2007-09-05 23:21:11 +02:00
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(4), i));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(suggested_pieces)
|
|
|
|
{
|
2007-09-06 01:32:05 +02:00
|
|
|
// test suggested pieces
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("1111222233334444", " ", "", "");
|
2007-09-06 01:32:05 +02:00
|
|
|
int v[] = {1, 5};
|
2016-12-22 16:42:33 +01:00
|
|
|
const std::vector<piece_index_t> suggested_pieces(v, v + 2);
|
2016-08-06 19:49:23 +02:00
|
|
|
|
|
|
|
auto picked = pick_pieces(p, "****************", 1, blocks_per_piece
|
2016-07-09 22:26:26 +02:00
|
|
|
, nullptr, options, suggested_pieces);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= blocks_per_piece);
|
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(1), i));
|
|
|
|
p->set_piece_priority(piece_index_t(0), 0);
|
|
|
|
p->set_piece_priority(piece_index_t(1), 0);
|
|
|
|
p->set_piece_priority(piece_index_t(2), 0);
|
|
|
|
p->set_piece_priority(piece_index_t(3), 0);
|
2007-09-06 01:32:05 +02:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
picked = pick_pieces(p, "****************", 1, blocks_per_piece
|
2016-07-09 22:26:26 +02:00
|
|
|
, nullptr, options, suggested_pieces);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= blocks_per_piece);
|
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(5), i));
|
2007-09-06 01:32:05 +02:00
|
|
|
|
|
|
|
p = setup_picker("1111222233334444", "**** ", "", "");
|
2015-01-26 03:04:58 +01:00
|
|
|
picked = pick_pieces(p, "****************", 1, blocks_per_piece
|
2016-07-09 22:26:26 +02:00
|
|
|
, nullptr, options, suggested_pieces);
|
2007-09-06 01:32:05 +02:00
|
|
|
TEST_CHECK(int(picked.size()) >= blocks_per_piece);
|
|
|
|
for (int i = 1; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_CHECK(picked[i] == piece_block(piece_index_t(5), i));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(bitfield_optimization)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
// test bitfield optimization
|
|
|
|
// we have less than half of the pieces
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("2122222211221222", " ", "", "");
|
2014-07-06 21:18:00 +02:00
|
|
|
// make sure it's not dirty
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
print_availability(p);
|
|
|
|
p->dec_refcount(string2vec("** ** ** * "), &tmp0);
|
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1022112200220222"));
|
|
|
|
// make sure it's not dirty
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount(string2vec(" ** ** * * "), &tmp8);
|
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1132123201220322"));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(seed_optimization)
|
|
|
|
{
|
2012-05-14 06:48:23 +02:00
|
|
|
// test seed optimizaton
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("0000000000000000", " ", "", "");
|
2012-05-14 06:48:23 +02:00
|
|
|
|
|
|
|
// make sure it's not dirty
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2012-05-14 06:48:23 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount_all(&tmp0);
|
2012-05-14 06:48:23 +02:00
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1111111111111111"));
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->dec_refcount(string2vec(" **** ** "), &tmp0);
|
2012-05-14 06:48:23 +02:00
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1100001100111111"));
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount(string2vec(" **** ** "), &tmp0);
|
2012-05-14 06:48:23 +02:00
|
|
|
TEST_CHECK(verify_availability(p, "1111111111111111"));
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
p->dec_refcount_all(&tmp0);
|
2012-05-14 06:48:23 +02:00
|
|
|
TEST_CHECK(verify_availability(p, "0000000000000000"));
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
p->inc_refcount_all(&tmp1);
|
2012-05-14 06:48:23 +02:00
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1111111111111111"));
|
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
pick_pieces(p, "****************", 1, blocks_per_piece, nullptr);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->dec_refcount(piece_index_t(3), &tmp1);
|
2012-05-14 06:48:23 +02:00
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1110111111111111"));
|
|
|
|
|
2015-02-12 04:16:53 +01:00
|
|
|
p->inc_refcount(string2vec("****************"), &tmp2);
|
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "2221222222222222"));
|
|
|
|
|
|
|
|
p->inc_refcount(string2vec("* * * * * * * * "), &tmp3);
|
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "3231323232323232"));
|
|
|
|
|
|
|
|
p->dec_refcount(string2vec("****************"), &tmp2);
|
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "2120212121212121"));
|
|
|
|
|
|
|
|
p->dec_refcount(string2vec("* * * * * * * * "), &tmp3);
|
|
|
|
print_availability(p);
|
|
|
|
TEST_CHECK(verify_availability(p, "1110111111111111"));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(reversed_peers)
|
|
|
|
{
|
2015-01-26 03:04:58 +01:00
|
|
|
// test reversed peers
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("3333333", " *****", "", "");
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// a reversed peer picked a block from piece 0
|
|
|
|
// This should make the piece reversed
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1
|
2015-01-26 03:04:58 +01:00
|
|
|
, piece_picker::reverse);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p, piece_picker::rarest_first), piece_index_t(1));
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// make sure another reversed peer pick the same piece
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p, piece_picker::rarest_first | piece_picker::reverse), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-01-26 03:04:58 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(reversed_piece_upgrade)
|
|
|
|
{
|
2015-01-26 03:04:58 +01:00
|
|
|
// test reversed pieces upgrading to normal pieces
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("3333333", " *****", "", "");
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// make piece 0 partial and reversed
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 1}, &tmp1
|
2015-01-26 03:04:58 +01:00
|
|
|
, piece_picker::reverse);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p), piece_index_t(1));
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// now have a regular peer pick the reversed block. It should now
|
|
|
|
// have turned into a regular one and be prioritized
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 2}, &tmp1);
|
|
|
|
TEST_EQUAL(test_pick(p), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-01-26 03:04:58 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(reversed_piece_downgrade)
|
|
|
|
{
|
|
|
|
// test pieces downgrading to reversed pieces
|
2015-01-26 03:04:58 +01:00
|
|
|
// now make sure a piece can be demoted to reversed if there are no
|
|
|
|
// other outstanding requests
|
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
auto p = setup_picker("3333333", " ", "", "");
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// make piece 0 partial and not reversed
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 1}, &tmp1);
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// a reversed peer picked a block from piece 0
|
|
|
|
// This should make the piece reversed
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_downloading({piece_index_t(0), 0}, &tmp1
|
2015-01-26 03:04:58 +01:00
|
|
|
, piece_picker::reverse);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p, piece_picker::rarest_first | piece_picker::reverse), piece_index_t(0));
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-01-26 03:04:58 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(piece_stats)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("3456789", "* ", "", "0300000");
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_picker::piece_stats_t stat = p->piece_stats(piece_index_t(0));
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(stat.peer_count, 3);
|
|
|
|
TEST_EQUAL(stat.have, 1);
|
|
|
|
TEST_EQUAL(stat.downloading, 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
stat = p->piece_stats(piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(stat.peer_count, 4);
|
|
|
|
TEST_EQUAL(stat.have, 0);
|
|
|
|
TEST_EQUAL(stat.downloading, 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(piece_passed)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* ", "", "0300000");
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 1);
|
|
|
|
TEST_EQUAL(p->num_have(), 1);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->piece_passed(piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 2);
|
|
|
|
TEST_EQUAL(p->num_have(), 1);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_have(piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_have(), 2);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(2), 0}, &tmp1);
|
|
|
|
p->piece_passed(piece_index_t(2));
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 3);
|
|
|
|
// just because the hash check passed doesn't mean
|
|
|
|
// we "have" the piece. We need to write it to disk first
|
|
|
|
TEST_EQUAL(p->num_have(), 2);
|
|
|
|
|
|
|
|
// piece 2 already passed the hash check, as soon as we've
|
|
|
|
// written all the blocks to disk, we should have that piece too
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(2), 1}, &tmp1);
|
|
|
|
p->mark_as_finished({piece_index_t(2), 2}, &tmp1);
|
|
|
|
p->mark_as_finished({piece_index_t(2), 3}, &tmp1);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_have(), 3);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(2)), true);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(piece_passed_causing_we_have)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* ", "", "0700000");
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
2015-02-12 07:43:46 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 1);
|
|
|
|
TEST_EQUAL(p->num_have(), 1);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(1), 3}, &tmp1);
|
2015-02-12 07:43:46 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 1);
|
|
|
|
TEST_EQUAL(p->num_have(), 1);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->piece_passed(piece_index_t(1));
|
2015-02-12 07:43:46 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 2);
|
|
|
|
TEST_EQUAL(p->num_have(), 2);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(break_one_seed)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("0000000", "* ", "", "0700000");
|
2015-02-12 07:43:46 +01:00
|
|
|
p->inc_refcount_all(&tmp1);
|
|
|
|
p->inc_refcount_all(&tmp2);
|
|
|
|
p->inc_refcount_all(&tmp3);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->piece_stats(piece_index_t(0)).peer_count, 3);
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->dec_refcount(piece_index_t(0), &tmp1);
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->piece_stats(piece_index_t(0)).peer_count, 2);
|
|
|
|
TEST_EQUAL(p->piece_stats(piece_index_t(1)).peer_count, 3);
|
|
|
|
TEST_EQUAL(p->piece_stats(piece_index_t(2)).peer_count, 3);
|
|
|
|
TEST_EQUAL(p->piece_stats(piece_index_t(3)).peer_count, 3);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(we_dont_have2)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* * ", "1101111", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(2)), true);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 2);
|
|
|
|
TEST_EQUAL(p->num_have(), 2);
|
|
|
|
TEST_EQUAL(p->num_have_filtered(), 1);
|
|
|
|
TEST_EQUAL(p->num_filtered(), 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_dont_have(piece_index_t(0));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), false);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(2)), true);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 1);
|
|
|
|
TEST_EQUAL(p->num_have(), 1);
|
|
|
|
TEST_EQUAL(p->num_have_filtered(), 1);
|
|
|
|
|
|
|
|
p = setup_picker("1111111", "* * ", "1101111", "");
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(2)), true);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 2);
|
|
|
|
TEST_EQUAL(p->num_have(), 2);
|
|
|
|
TEST_EQUAL(p->num_have_filtered(), 1);
|
|
|
|
TEST_EQUAL(p->num_filtered(), 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_dont_have(piece_index_t(2));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(2)), false);
|
2015-02-12 04:16:53 +01:00
|
|
|
TEST_EQUAL(p->num_passed(), 1);
|
|
|
|
TEST_EQUAL(p->num_have(), 1);
|
|
|
|
TEST_EQUAL(p->num_have_filtered(), 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(dont_have_but_passed_hash_check)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* * ", "1101111", "0200000");
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(0)), true)
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(1)), false)
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->piece_passed(piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), true);
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(1)), false)
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->we_dont_have(piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(1)), false)
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(write_failed)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* * ", "1101111", "0200000");
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(1)), false);
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->piece_passed(piece_index_t(1));
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), true);
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(1)), false);
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_writing({piece_index_t(1), 0}, &tmp1);
|
|
|
|
p->write_failed({piece_index_t(1), 0});
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(0)), true);
|
|
|
|
TEST_EQUAL(p->has_piece_passed(piece_index_t(1)), false);
|
|
|
|
TEST_EQUAL(p->have_piece(piece_index_t(1)), false);
|
2015-02-12 04:16:53 +01:00
|
|
|
|
2015-02-12 07:43:46 +01:00
|
|
|
// make sure write_failed() and lock_piece() actually
|
|
|
|
// locks the piece, and that it won't be picked.
|
|
|
|
// also make sure restore_piece() unlocks it and makes
|
|
|
|
// it available for picking again.
|
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, " * ", 1, blocks_per_piece, nullptr);
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(picked.size(), 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->restore_piece(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, " * ", 1, blocks_per_piece, nullptr);
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
|
|
|
|
|
|
|
// locking pieces only works on partial pieces
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_writing({piece_index_t(1), 0}, &tmp1);
|
|
|
|
p->lock_piece(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
picked = pick_pieces(p, " * ", 1, blocks_per_piece, nullptr);
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(picked.size(), 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(write_failed_clear_piece)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* * ", "1101111", "");
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
auto stat = p->piece_stats(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(stat.downloading, 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_writing({piece_index_t(1), 0}, &tmp1);
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
stat = p->piece_stats(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(stat.downloading, 1);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->write_failed({piece_index_t(1), 0});
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
stat = p->piece_stats(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(stat.downloading, 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(mark_as_canceled)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", "* * ", "1101111", "");
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
auto stat = p->piece_stats(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(stat.downloading, 0);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_writing({piece_index_t(1), 0}, &tmp1);
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
stat = p->piece_stats(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(stat.downloading, 1);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_canceled({piece_index_t(1), 0}, &tmp1);
|
|
|
|
stat = p->piece_stats(piece_index_t(1));
|
2015-02-13 03:58:21 +01:00
|
|
|
TEST_EQUAL(stat.downloading, 0);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-13 03:58:21 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(get_download_queue)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "1101111", "0327000");
|
2015-02-13 03:58:21 +01:00
|
|
|
|
|
|
|
std::vector<piece_picker::downloading_piece> downloads
|
|
|
|
= p->get_download_queue();
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2015-02-13 03:58:21 +01:00
|
|
|
// the download queue should have piece 1, 2 and 3 in it
|
|
|
|
TEST_EQUAL(downloads.size(), 3);
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
TEST_EQUAL(std::count_if(downloads.begin(), downloads.end()
|
2016-12-22 16:42:33 +01:00
|
|
|
, [](piece_picker::downloading_piece const& p) { return p.index == piece_index_t(1); }), 1);
|
2016-05-25 06:31:52 +02:00
|
|
|
TEST_EQUAL(std::count_if(downloads.begin(), downloads.end()
|
2016-12-22 16:42:33 +01:00
|
|
|
, [](piece_picker::downloading_piece const& p) { return p.index == piece_index_t(2); }), 1);
|
2016-05-25 06:31:52 +02:00
|
|
|
TEST_EQUAL(std::count_if(downloads.begin(), downloads.end()
|
2016-12-22 16:42:33 +01:00
|
|
|
, [](piece_picker::downloading_piece const& p) { return p.index == piece_index_t(3); }), 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-12 07:43:46 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(get_download_queue_size)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "1111111", "0327ff0");
|
2015-02-14 07:14:15 +01:00
|
|
|
|
|
|
|
TEST_EQUAL(p->get_download_queue_size(), 5);
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
p->set_piece_priority(piece_index_t(1), 0);
|
2015-02-14 07:14:15 +01:00
|
|
|
|
|
|
|
int partial;
|
|
|
|
int full;
|
|
|
|
int finished;
|
|
|
|
int zero_prio;
|
|
|
|
p->get_download_queue_sizes(&partial, &full, &finished, &zero_prio);
|
|
|
|
|
|
|
|
TEST_EQUAL(partial, 2);
|
|
|
|
TEST_EQUAL(full, 0);
|
|
|
|
TEST_EQUAL(finished, 2);
|
|
|
|
TEST_EQUAL(zero_prio, 1);
|
2016-08-06 19:49:23 +02:00
|
|
|
}
|
2015-02-14 07:14:15 +01:00
|
|
|
|
2016-08-06 19:49:23 +02:00
|
|
|
TORRENT_TEST(time_critical_mode)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "1654741", "0352000");
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// rarest-first
|
2016-08-06 19:49:23 +02:00
|
|
|
auto picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
2015-02-15 03:17:57 +01:00
|
|
|
, piece_picker::rarest_first | piece_picker::time_critical_mode, empty_vector);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// reverse rarest-first
|
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
|
|
|
, piece_picker::reverse | piece_picker::rarest_first
|
|
|
|
| piece_picker::time_critical_mode, empty_vector);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// sequential
|
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
|
|
|
, piece_picker::sequential | piece_picker::time_critical_mode, empty_vector);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// reverse sequential
|
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
|
|
|
, piece_picker::reverse | piece_picker::sequential
|
|
|
|
| piece_picker::time_critical_mode, empty_vector);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// just critical
|
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
|
|
|
, piece_picker::time_critical_mode, empty_vector);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// prioritize_partials
|
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
|
|
|
, piece_picker::prioritize_partials | piece_picker::time_critical_mode, empty_vector);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2007-07-04 20:05:29 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// even when a non-critical piece is suggested should we ignore it
|
2016-08-06 19:49:23 +02:00
|
|
|
int v[] = {1, 5};
|
2016-12-22 16:42:33 +01:00
|
|
|
const std::vector<piece_index_t> suggested_pieces(v, v + 2);
|
2016-08-06 19:49:23 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
picked = pick_pieces(p, "*******", 7 * blocks_per_piece, 0, tmp_peer
|
|
|
|
, piece_picker::rarest_first | piece_picker::time_critical_mode
|
|
|
|
, suggested_pieces);
|
|
|
|
TEST_EQUAL(picked.size(), blocks_per_piece);
|
2015-05-06 04:13:03 +02:00
|
|
|
for (int i = 0; i < int(picked.size()); ++i)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(picked[0].piece_index, piece_index_t(4));
|
2005-08-12 14:40:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:05:20 +02:00
|
|
|
TORRENT_TEST(reprioritize_downloading)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
|
|
|
bool ret;
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->mark_as_downloading({piece_index_t(0), 0}, tmp_peer);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 1}, tmp_peer);
|
|
|
|
ret = p->mark_as_writing({piece_index_t(0), 2}, tmp_peer);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we pick the partial piece (i.e. piece 0)
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p, piece_picker::rarest_first | piece_picker::prioritize_partials), piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
|
|
|
|
// set the priority of the piece to 0 (while downloading it)
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->set_piece_priority(piece_index_t(0), 0);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we _DON'T_ pick the partial piece, since it has priority zero
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const picked_piece = test_pick(p, piece_picker::rarest_first
|
2016-11-12 19:50:49 +01:00
|
|
|
| piece_picker::prioritize_partials);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_NE(picked_piece, piece_index_t(-1));
|
|
|
|
TEST_NE(picked_piece, piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
|
|
|
|
// set the priority of the piece back to 1. It should now be the best pick
|
|
|
|
// again (since it's partial)
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->set_piece_priority(piece_index_t(0), 1);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we pick the partial piece
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_EQUAL(test_pick(p, piece_picker::rarest_first
|
2016-12-22 16:42:33 +01:00
|
|
|
| piece_picker::prioritize_partials), piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(reprioritize_fully_downloading)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
for (int i = 0; i < blocks_per_piece; ++i)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->mark_as_downloading(piece_block(piece_index_t(0), i), tmp_peer);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we _DON'T_ pick the downloading piece
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const picked_piece = test_pick(p, piece_picker::rarest_first
|
2016-11-12 19:50:49 +01:00
|
|
|
| piece_picker::prioritize_partials);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_NE(picked_piece, piece_index_t(-1));
|
|
|
|
TEST_NE(picked_piece, piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the priority of the piece to 0 (while downloading it)
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->set_piece_priority(piece_index_t(0), 0);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we still _DON'T_ pick the downloading piece
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const picked_piece = test_pick(p, piece_picker::rarest_first
|
2016-11-12 19:50:49 +01:00
|
|
|
| piece_picker::prioritize_partials);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_NE(picked_piece, piece_index_t(-1));
|
|
|
|
TEST_NE(picked_piece, piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the priority of the piece back to 1. It should now be the best pick
|
|
|
|
// again (since it's partial)
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->set_piece_priority(piece_index_t(0), 1);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we still _DON'T_ pick the downloading piece
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const picked_piece = test_pick(p, piece_picker::rarest_first
|
2016-11-12 19:50:49 +01:00
|
|
|
| piece_picker::prioritize_partials);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_NE(picked_piece, piece_index_t(-1));
|
|
|
|
TEST_NE(picked_piece, piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(download_filtered_piece)
|
|
|
|
{
|
|
|
|
auto p = setup_picker("1111111", " ", "", "");
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
// set the priority of the piece to 0
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->set_piece_priority(piece_index_t(0), 0);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we _DON'T_ pick piece 0
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const picked_piece = test_pick(p, piece_picker::rarest_first
|
2016-11-12 19:50:49 +01:00
|
|
|
| piece_picker::prioritize_partials);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_NE(picked_piece, piece_index_t(-1));
|
|
|
|
TEST_NE(picked_piece, piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// then mark it for downloading
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->mark_as_downloading({piece_index_t(0), 0}, tmp_peer);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
2016-12-22 16:42:33 +01:00
|
|
|
p->mark_as_finished({piece_index_t(0), 1}, tmp_peer);
|
|
|
|
ret = p->mark_as_writing({piece_index_t(0), 2}, tmp_peer);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
{
|
|
|
|
// we still should not pick it
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const picked_piece = test_pick(p, piece_picker::rarest_first
|
2016-11-12 19:50:49 +01:00
|
|
|
| piece_picker::prioritize_partials);
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_NE(picked_piece, piece_index_t(-1));
|
|
|
|
TEST_NE(picked_piece, piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the priority of the piece back to 1. It should now be the best pick
|
|
|
|
// again (since it's partial)
|
2016-12-22 16:42:33 +01:00
|
|
|
ret = p->set_piece_priority(piece_index_t(0), 1);
|
2016-08-07 22:05:20 +02:00
|
|
|
TEST_EQUAL(ret, true);
|
|
|
|
|
|
|
|
// make sure we pick piece 0
|
2016-12-22 16:42:33 +01:00
|
|
|
TEST_EQUAL(test_pick(p, piece_picker::rarest_first | piece_picker::prioritize_partials), piece_index_t(0));
|
2016-08-07 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 19:50:49 +01:00
|
|
|
//TODO: 2 test picking with partial pieces and other peers present so that both
|
|
|
|
// backup_pieces and backup_pieces2 are used
|