From 5486e17b15db3555500687f1405c75d8c6f3df76 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 29 Aug 2008 12:19:21 +0000 Subject: [PATCH] fixed bug in bitfield and updated test --- include/libtorrent/bitfield.hpp | 19 ++++++++++++++++++- test/test_primitives.cpp | 16 +++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index fe14cfe1c..740b20ffe 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -187,8 +187,23 @@ namespace libtorrent void resize(int bits, bool val) { + int s = m_size; + int b = m_size & 7; resize(bits); - if (val) set_all(); else clear_all(); + if (s >= m_size) return; + int old_size_bytes = (s + 7) / 8; + int new_size_bytes = (m_size + 7) / 8; + if (val) + { + if (old_size_bytes && b) m_bytes[old_size_bytes - 1] |= (0xff >> b); + if (old_size_bytes < new_size_bytes) + memset(m_bytes + old_size_bytes, 0xff, new_size_bytes - old_size_bytes); + } + else + { + if (old_size_bytes < new_size_bytes) + memset(m_bytes + old_size_bytes, 0x00, new_size_bytes - old_size_bytes); + } } void set_all() @@ -225,6 +240,8 @@ namespace libtorrent m_own = true; } m_size = bits; + // clear the tail bits in the last byte + if (m_size && (bits & 7)) m_bytes[(m_size + 7) / 8 - 1] &= 0xff << (7 - (bits & 7)); } private: diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index b66a205c9..22b9b765c 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -74,7 +74,7 @@ tuple feed_bytes(http_parser& parser, char const* str) ret.get<0>() += payload; ret.get<1>() += protocol; ret.get<2>() += error; - std::cerr << payload << ", " << protocol << ", " << chunk_size << std::endl; +// std::cerr << payload << ", " << protocol << ", " << chunk_size << std::endl; TORRENT_ASSERT(payload + protocol == chunk_size); } TEST_CHECK(prev == make_tuple(0, 0, false) || ret == prev); @@ -441,7 +441,7 @@ int test_main() { int hit = std::find_if(nodes.begin(), nodes.end() , bind(&node_entry::id, _1) == i->id) - nodes.begin(); - std::cerr << hit << std::endl; +// std::cerr << hit << std::endl; if (hit < int(temp.size())) ++hits; } TEST_CHECK(hits > int(temp.size()) / 2); @@ -461,7 +461,7 @@ int test_main() { int hit = std::find_if(nodes.begin(), nodes.end() , bind(&node_entry::id, _1) == i->id) - nodes.begin(); - std::cerr << hit << std::endl; +// std::cerr << hit << std::endl; if (hit < int(temp.size())) ++hits; } TEST_CHECK(hits > int(temp.size()) / 2); @@ -543,6 +543,16 @@ int test_main() test1.clear_all(); TEST_CHECK(test1.count() == 0); + + test1.resize(2); + test1.set_bit(0); + test1.resize(16, true); + TEST_CHECK(test1.count() == 15); + test1.resize(20, true); + TEST_CHECK(test1.count() == 19); + test1.set_bit(1); + test1.resize(1); + TEST_CHECK(test1.count() == 1); return 0; }