fixed bug in bitfield and updated test

This commit is contained in:
Arvid Norberg 2008-08-29 12:19:21 +00:00
parent e670b2fa7f
commit 5486e17b15
2 changed files with 31 additions and 4 deletions

View File

@ -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:

View File

@ -74,7 +74,7 @@ tuple<int, int, bool> 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;
}