modernize piece-size selection in torrent creator and add a test

This commit is contained in:
arvidn 2018-01-09 01:34:04 +01:00 committed by Arvid Norberg
parent ad82be145b
commit a93429c5db
2 changed files with 43 additions and 5 deletions

View File

@ -333,15 +333,25 @@ namespace {
// target_piece_size = total_size / (target_list_size / hash_size); // target_piece_size = total_size / (target_list_size / hash_size);
// Given hash_size = 20 bytes, target_piece_size = (16*1024 * pow(2, i)) // Given hash_size = 20 bytes, target_piece_size = (16*1024 * pow(2, i))
// we can determine size_table = (total_size = pow(2 * target_piece_size / hash_size, 2)) // we can determine size_table = (total_size = pow(2 * target_piece_size / hash_size, 2))
std::int64_t const size_table[] = {2684355, 10737418, 42949673, 171798692, 687194767, std::array<std::int64_t, 10> const size_table{{
2748779069LL, 10995116278LL, 43980465111LL, 175921860444LL, 703687441777LL}; 2684355LL // -> 16kiB
, 10737418LL // -> 32 kiB
, 42949673LL // -> 64 kiB
, 171798692LL // -> 128 kiB
, 687194767LL // -> 256 kiB
, 2748779069LL // -> 512 kiB
, 10995116278LL // -> 1 MiB
, 43980465111LL // -> 2 MiB
, 175921860444LL // -> 4 MiB
, 703687441777LL}}; // -> 8 MiB
int i = 0; int i = 0;
for (int max = sizeof(size_table) / sizeof(size_table[0]); i < max; ++i) for (auto const s : size_table)
{ {
if (size_table[i] >= fs.total_size()) break; if (s >= fs.total_size()) break;
++i;
} }
piece_size = 0x4000 << i; piece_size = default_block_size << i;
} }
else if (piece_size == 0 && m_merkle_torrent) else if (piece_size == 0 && m_merkle_torrent)
{ {

View File

@ -70,3 +70,31 @@ TORRENT_TEST(create_verbatim_torrent)
TEST_CHECK(memcmp(dest_info, test_torrent + 1, sizeof(test_torrent)-3) == 0); TEST_CHECK(memcmp(dest_info, test_torrent + 1, sizeof(test_torrent)-3) == 0);
} }
TORRENT_TEST(piece_size)
{
std::int64_t const kiB = 1024;
std::int64_t const MiB = 1024 * 1024;
std::int64_t const GiB = 1024 * 1024 * 1024;
std::array<std::pair<std::int64_t, std::int64_t>, 11> samples{{
{100LL, 16 * kiB},
{3 * MiB, 32 * kiB},
{11 * MiB, 64 * kiB},
{43 * MiB, 128 * kiB},
{172 * MiB, 256 * kiB},
{688 * MiB, 512 * kiB},
{3 * GiB, 1 * MiB},
{11 * GiB, 2 * MiB},
{44 * GiB, 4 * MiB},
{176 * GiB, 8 * MiB},
{704 * GiB, 16 * MiB},
}};
for (auto const& t : samples)
{
lt::file_storage fs;
fs.add_file("a", t.first);
lt::create_torrent ct(fs, 0);
TEST_CHECK(ct.piece_length() == static_cast<int>(t.second));
}
}