From 84fe616ccb24249f5189b3906b39a9f3f0bd5c76 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 8 Feb 2015 21:44:58 +0000 Subject: [PATCH] extend piece picker unit test --- src/piece_picker.cpp | 20 +++++++++++--------- test/test_piece_picker.cpp | 13 +++++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index cabf603c7..205d3b9eb 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -2027,14 +2027,17 @@ namespace libtorrent } // now, sort the list. - // TODO: this could probably be optimized by incrementally - // calling partial_sort to sort one more element in the list. Because - // chances are that we'll just need a single piece, and once we've - // picked from it we're done. Sorting the rest of the list in that - // case is a waste of time. - std::sort(ordered_partials, ordered_partials + num_ordered_partials - , boost::bind(&piece_picker::partial_compare_rarest_first, this - , _1, _2)); + if (options & rarest_first) + { + // TODO: this could probably be optimized by incrementally + // calling partial_sort to sort one more element in the list. Because + // chances are that we'll just need a single piece, and once we've + // picked from it we're done. Sorting the rest of the list in that + // case is a waste of time. + std::sort(ordered_partials, ordered_partials + num_ordered_partials + , boost::bind(&piece_picker::partial_compare_rarest_first, this + , _1, _2)); + } for (int i = 0; i < num_ordered_partials; ++i) { @@ -2212,7 +2215,6 @@ namespace libtorrent int piece = start_piece; while (num_blocks > 0) { - bool done = false; // skip pieces we can't pick, and suggested pieces // since we've already picked those while (!is_piece_free(piece, pieces) diff --git a/test/test_piece_picker.cpp b/test/test_piece_picker.cpp index 263a4b4a1..332fca06b 100644 --- a/test/test_piece_picker.cpp +++ b/test/test_piece_picker.cpp @@ -639,8 +639,17 @@ int test_main() TEST_CHECK(int(picked.size()) > 0); TEST_CHECK(picked.front() == piece_block(3, 3)); - // TODO: 3 if we use prioritize_partials + sequential, we should prefer - // the the partial pieces based on piece index instead + // if we don't use rarest first when we prioritize partials, but instead use + // sequential order, make sure we pick the right one + + print_title("test partial piece order (sequential)"); + p = setup_picker("1111111", " ", "", "013700f"); + picked = pick_pieces(p, "*******", 1, 0, 0 + , piece_picker::sequential | piece_picker::prioritize_partials, empty_vector); + TEST_CHECK(int(picked.size()) > 0); + TEST_CHECK(picked.front() == piece_block(1, 1) + || picked.front() == piece_block(1, 2) + || picked.front() == piece_block(1, 3)); // ========================================================