From b89e306599c0af3908a2a33c0165caf1f712f371 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 11 Feb 2010 08:22:37 +0000 Subject: [PATCH] 2010-02-11 Tatsuhiro Tsujikawa Removed unused methods from BitfieldMan class. Moved getFirstMissingIndex to bitfield.h. Updated doc for BitfieldMan class. * src/BitfieldMan.cc * src/BitfieldMan.h * src/BitfieldManFactory.cc * src/BitfieldManFactory.h * src/bitfield.h * src/main.cc * test/BitfieldManTest.cc * test/DefaultPieceStorageTest.cc --- ChangeLog | 14 ++ src/BitfieldMan.cc | 138 +-------------- src/BitfieldMan.h | 138 +++++---------- src/BitfieldManFactory.cc | 22 --- src/BitfieldManFactory.h | 10 -- src/bitfield.h | 21 +++ src/main.cc | 1 - test/BitfieldManTest.cc | 303 ++------------------------------ test/DefaultPieceStorageTest.cc | 8 - 9 files changed, 99 insertions(+), 556 deletions(-) diff --git a/ChangeLog b/ChangeLog index e0a8a8f2..646342d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2010-02-11 Tatsuhiro Tsujikawa + + Removed unused methods from BitfieldMan class. Moved + getFirstMissingIndex to bitfield.h. Updated doc for BitfieldMan + class. + * src/BitfieldMan.cc + * src/BitfieldMan.h + * src/BitfieldManFactory.cc + * src/BitfieldManFactory.h + * src/bitfield.h + * src/main.cc + * test/BitfieldManTest.cc + * test/DefaultPieceStorageTest.cc + 2010-02-11 Tatsuhiro Tsujikawa Moved getFirstNMissingIndex to bitfield.h and made it return the diff --git a/src/BitfieldMan.cc b/src/BitfieldMan.cc index 2ab00812..a0555e6a 100644 --- a/src/BitfieldMan.cc +++ b/src/BitfieldMan.cc @@ -37,7 +37,6 @@ #include #include -#include "Randomizer.h" #include "util.h" #include "array_fun.h" #include "bitfield.h" @@ -81,7 +80,6 @@ BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan) bitfield(new unsigned char[bitfieldLength]), useBitfield(new unsigned char[bitfieldLength]), filterBitfield(0), - randomizer(bitfieldMan.randomizer), cachedNumMissingBlock(0), cachedNumFilteredBlock(0), cachedCompletedLength(0), @@ -144,51 +142,9 @@ size_t BitfieldMan::getBlockLength(size_t index) const } } -size_t -BitfieldMan::getNthBitIndex(const unsigned char bitfield, size_t nth) const +bool BitfieldMan::hasMissingPiece +(const unsigned char* peerBitfield, size_t length) const { - size_t index = 0; - for(int bs = 7; bs >= 0; --bs) { - unsigned char mask = 1 << bs; - if(bitfield & mask) { - nth--; - if(nth == 0) { - index = 7-bs; - break; - } - } - } - return index; -} - -template -bool BitfieldMan::getMissingIndexRandomly(size_t& index, - const Array& bitfield, - size_t bitfieldLength) const -{ - size_t byte = randomizer->getRandomNumber(bitfieldLength); - for(size_t i = 0; i < bitfieldLength; ++i) { - unsigned char mask; - if(byte == bitfieldLength-1) { - mask = bitfield::lastByteMask(blocks); - } else { - mask = 0xff; - } - unsigned char bits = bitfield[byte]; - - if(bits&mask) { - index = byte*8+getNthBitIndex(bits, 1); - return true; - } - ++byte; - if(byte == bitfieldLength) { - byte = 0; - } - } - return false; -} - -bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, size_t length) const { if(bitfieldLength != length) { return false; } @@ -206,65 +162,15 @@ bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, size_t leng return retval; } -bool BitfieldMan::getMissingIndex(size_t& index, const unsigned char* peerBitfield, size_t length) const { - if(bitfieldLength != length) { - return false; - } - if(filterEnabled) { - return getMissingIndexRandomly - (index, - ~array(bitfield)&array(peerBitfield)&array(filterBitfield), - bitfieldLength); - } else { - return getMissingIndexRandomly - (index, ~array(bitfield)&array(peerBitfield), bitfieldLength); - } -} - -bool BitfieldMan::getMissingUnusedIndex(size_t& index, const unsigned char* peerBitfield, size_t length) const { - if(bitfieldLength != length) { - return false; - } - if(filterEnabled) { - return getMissingIndexRandomly - (index, - ~array(bitfield)&~array(useBitfield)&array(peerBitfield)&array(filterBitfield), - bitfieldLength); - } else { - return getMissingIndexRandomly - (index, - ~array(bitfield)&~array(useBitfield)&array(peerBitfield), - bitfieldLength); - } -} - -template -bool BitfieldMan::getFirstMissingIndex(size_t& index, const Array& bitfield, size_t bitfieldLength) const -{ - for(size_t i = 0; i < bitfieldLength; ++i) { - unsigned char bits = bitfield[i]; - unsigned char mask = 128; - size_t tindex = i*8; - for(size_t bi = 0; bi < 8 && tindex < blocks; ++bi, mask >>= 1, ++tindex) { - if(bits & mask) { - index = tindex; - return true; - } - } - } - return false; -} - bool BitfieldMan::getFirstMissingUnusedIndex(size_t& index) const { if(filterEnabled) { - return getFirstMissingIndex + return bitfield::getFirstMissingIndex (index, ~array(bitfield)&~array(useBitfield)&array(filterBitfield), - bitfieldLength); + blocks); } else { - return getFirstMissingIndex - (index, ~array(bitfield)&~array(useBitfield), - bitfieldLength); + return bitfield::getFirstMissingIndex + (index, ~array(bitfield)&~array(useBitfield), blocks); } } @@ -285,31 +191,10 @@ size_t BitfieldMan::getFirstNMissingUnusedIndex bool BitfieldMan::getFirstMissingIndex(size_t& index) const { if(filterEnabled) { - return getFirstMissingIndex(index, ~array(bitfield)&array(filterBitfield), - bitfieldLength); + return bitfield::getFirstMissingIndex + (index, ~array(bitfield)&array(filterBitfield), blocks); } else { - return getFirstMissingIndex(index, ~array(bitfield), bitfieldLength); - } -} - -bool BitfieldMan::getMissingIndex(size_t& index) const { - if(filterEnabled) { - return getMissingIndexRandomly - (index, ~array(bitfield)&array(filterBitfield), bitfieldLength); - } else { - return getMissingIndexRandomly(index, ~array(bitfield), bitfieldLength); - } -} - -bool BitfieldMan::getMissingUnusedIndex(size_t& index) const -{ - if(filterEnabled) { - return getMissingIndexRandomly - (index, ~array(bitfield)&~array(useBitfield)&array(filterBitfield), - bitfieldLength); - } else { - return getMissingIndexRandomly - (index, ~array(bitfield)&~array(useBitfield), bitfieldLength); + return bitfield::getFirstMissingIndex(index, ~array(bitfield), blocks); } } @@ -797,9 +682,4 @@ uint64_t BitfieldMan::getMissingUnusedLength(size_t startingIndex) const return length; } -void BitfieldMan::setRandomizer(const SharedHandle& randomizer) -{ - this->randomizer = randomizer; -} - } // namespace aria2 diff --git a/src/BitfieldMan.h b/src/BitfieldMan.h index 0a6c3359..489cecba 100644 --- a/src/BitfieldMan.h +++ b/src/BitfieldMan.h @@ -42,8 +42,6 @@ namespace aria2 { -class Randomizer; - class BitfieldMan { private: size_t blockLength; @@ -54,7 +52,6 @@ private: unsigned char* bitfield; unsigned char* useBitfield; unsigned char* filterBitfield; - SharedHandle randomizer; // for caching size_t cachedNumMissingBlock; @@ -63,20 +60,6 @@ private: uint64_t cachedFilteredComletedLength; uint64_t cachedFilteredTotalLength; - size_t getNthBitIndex(const unsigned char bit, size_t nth) const; - bool getMissingIndexRandomly(size_t& index, const unsigned char* bitfield, size_t len) const; - - template - bool getMissingIndexRandomly(size_t& index, const Array& bitfield, - size_t bitfieldLength) const; - template - bool getFirstMissingIndex(size_t& index, const Array& bitfield, size_t bitfieldLength) const; - - template - bool getAllMissingIndexes(std::deque& indexes, - const Array& bitfield, - size_t bitfieldLength) const; - bool setBitInternal(unsigned char* bitfield, size_t index, bool on); bool setFilterBit(size_t index); @@ -130,72 +113,53 @@ public: uint64_t getTotalLength() const { return totalLength; } - /** - * affected by filter - */ + // Returns true iff there is a bit index which is set in bitfield, + // but not set in this object. + // + // affected by filter bool hasMissingPiece(const unsigned char* bitfield, size_t len) const; - /** - * affected by filter - */ - bool getMissingIndex(size_t& index, const unsigned char* bitfield, size_t len) const; - /** - * affected by filter - */ - bool getMissingIndex(size_t& index) const; - /** - * affected by filter - */ + + // affected by filter bool getFirstMissingUnusedIndex(size_t& index) const; - /** - * Appends at most n missing unused index to out. This function - * doesn't delete existing elements in out. Returns the number of - * appended elements. - * - * affected by filter - */ + + // Appends at most n missing unused index to out. This function + // doesn't delete existing elements in out. Returns the number of + // appended elements. + // + // affected by filter size_t getFirstNMissingUnusedIndex(std::vector& out, size_t n) const; - /** - * affected by filter - */ + + // Stores first missing bit index to index. Returns true if such bit + // index is found. Otherwise returns false. + // + // affected by filter bool getFirstMissingIndex(size_t& index) const; - /** - * affected by filter - */ - bool getMissingUnusedIndex(size_t& index, const unsigned char* bitfield, size_t len) const; - /** - * affected by filter - */ - bool getMissingUnusedIndex(size_t& index) const; - /** - * affected by filter - */ + + // Stores missing bit index to index. index is selected so that it + // divides longest missing bit subarray into 2 equally sized + // subarray. Set bits in ignoreBitfield are excluded. Returns true + // if such bit index is found. Otherwise returns false. + // + // affected by filter bool getSparseMissingUnusedIndex (size_t& index, const unsigned char* ignoreBitfield, size_t ignoreBitfieldLength) const; - /** - * affected by filter - */ + // affected by filter bool getAllMissingIndexes(unsigned char* misbitfield, size_t mislen) const; - /** - * affected by filter - */ + + // affected by filter bool getAllMissingIndexes(unsigned char* misbitfield, size_t mislen, const unsigned char* bitfield, size_t len) const; - /** - * affected by filter - */ + // affected by filter bool getAllMissingUnusedIndexes(unsigned char* misbitfield, size_t mislen, const unsigned char* bitfield, size_t len) const; - /** - * affected by filter - */ + // affected by filter size_t countMissingBlock() const; - /** - * affected by filter - */ + + // affected by filter size_t countMissingBlockNow() const; bool setUseBit(size_t index); @@ -207,9 +171,7 @@ public: bool isBitSet(size_t index) const; bool isUseBitSet(size_t index) const; - /** - * affected by filter - */ + // affected by filter bool isFilteredAllBitSet() const; bool isAllBitSet() const; @@ -226,9 +188,7 @@ public: return bitfieldLength; } - /** - * affected by filter - */ + // affected by filter size_t countFilteredBlock() const { return cachedNumFilteredBlock; @@ -239,9 +199,7 @@ public: return blocks; } - /** - * affected by filter - */ + // affected by filter size_t countFilteredBlockNow() const; size_t getMaxIndex() const @@ -262,9 +220,7 @@ public: // Add filter not in the range of [offset, offset+length) bytes void addNotFilter(uint64_t offset, uint64_t length); - /** - * Clears filter and disables filter - */ + // Clears filter and disables filter void clearFilter(); void enableFilter(); @@ -274,17 +230,13 @@ public: return filterEnabled; } - /** - * affected by filter - */ + // affected by filter uint64_t getFilteredTotalLength() const { return cachedFilteredTotalLength; } - /** - * affected by filter - */ + // affected by filter uint64_t getFilteredTotalLengthNow() const; uint64_t getCompletedLength() const @@ -294,25 +246,15 @@ public: uint64_t getCompletedLengthNow() const; - /** - * affected by filter - */ + // affected by filter uint64_t getFilteredCompletedLength() const { return cachedFilteredComletedLength; } - /** - * affected by filter - */ + + // affected by filter uint64_t getFilteredCompletedLengthNow() const; - void setRandomizer(const SharedHandle& randomizer); - - const SharedHandle& getRandomizer() const - { - return randomizer; - } - void updateCache(); bool isBitRangeSet(size_t startIndex, size_t endIndex) const; diff --git a/src/BitfieldManFactory.cc b/src/BitfieldManFactory.cc index 0e24d7d8..bac17f96 100644 --- a/src/BitfieldManFactory.cc +++ b/src/BitfieldManFactory.cc @@ -34,7 +34,6 @@ /* copyright --> */ #include "BitfieldManFactory.h" #include "BitfieldMan.h" -#include "Randomizer.h" namespace aria2 { @@ -56,28 +55,7 @@ BitfieldMan* BitfieldManFactory::createBitfieldMan(size_t blockLength, uint64_t totalLength) { BitfieldMan* bitfieldMan = new BitfieldMan(blockLength, totalLength); - bitfieldMan->setRandomizer(randomizer); return bitfieldMan; } -void BitfieldManFactory::setDefaultRandomizer(const RandomizerHandle& randomizer) { - BitfieldManFactoryHandle factory = getFactoryInstance(); - factory->setRandomizer(randomizer); -} - -RandomizerHandle BitfieldManFactory::getDefaultRandomizer() -{ - return getFactoryInstance()->getRandomizer(); -} - -void BitfieldManFactory::setRandomizer(const RandomizerHandle& randomizer) -{ - this->randomizer = randomizer; -} - -RandomizerHandle BitfieldManFactory::getRandomizer() const -{ - return randomizer; -} - } // namespace aria2 diff --git a/src/BitfieldManFactory.h b/src/BitfieldManFactory.h index dbbc97a6..317ccccc 100644 --- a/src/BitfieldManFactory.h +++ b/src/BitfieldManFactory.h @@ -47,8 +47,6 @@ class BitfieldManFactory { private: static SharedHandle factory; - SharedHandle randomizer; - BitfieldManFactory(); public: ~BitfieldManFactory(); @@ -56,14 +54,6 @@ public: static SharedHandle getFactoryInstance(); BitfieldMan* createBitfieldMan(size_t blockLength, uint64_t totalLength); - - static void setDefaultRandomizer(const SharedHandle& randomizer); - - static SharedHandle getDefaultRandomizer(); - - void setRandomizer(const SharedHandle& randomizer); - - SharedHandle getRandomizer() const; }; typedef SharedHandle BitfieldManFactoryHandle; diff --git a/src/bitfield.h b/src/bitfield.h index 0a253124..c918adc8 100644 --- a/src/bitfield.h +++ b/src/bitfield.h @@ -118,6 +118,27 @@ inline size_t countSetBit(const unsigned char* bitfield, size_t nbits) void flipBit(unsigned char* data, size_t length, size_t bitIndex); +// Stores first missing bit index of bitfield to index. bitfield +// contains nbits. Returns true if missing bit index is +// found. Otherwise returns false. +template +bool getFirstMissingIndex +(size_t& index, const Array& bitfield, size_t nbits) +{ + const size_t bitfieldLength = (nbits+7)/8; + for(size_t i = 0; i < bitfieldLength; ++i) { + unsigned char mask = 128; + size_t tindex = i*8; + for(size_t bi = 0; bi < 8 && tindex < nbits; ++bi, mask >>= 1, ++tindex) { + if(bitfield[i] & mask) { + index = tindex; + return true; + } + } + } + return false; +} + // Appends first at most n set bit index in bitfield to out. bitfield // contains nbits bits. Returns the number of appended bit indexes. template diff --git a/src/main.cc b/src/main.cc index a968dce1..bf5efb5a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -173,7 +173,6 @@ downloadresultcode::RESULT main(int argc, char* argv[]) option_processing(*op.get(), args, argc, argv); SimpleRandomizer::init(); - BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance()); #ifdef ENABLE_BITTORRENT bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX)); #endif // ENABLE_BITTORRENT diff --git a/test/BitfieldManTest.cc b/test/BitfieldManTest.cc index 76f7aa9a..f7bcd76a 100644 --- a/test/BitfieldManTest.cc +++ b/test/BitfieldManTest.cc @@ -5,8 +5,8 @@ #include -#include "FixedNumberRandomizer.h" #include "bitfield.h" +#include "array_fun.h" namespace aria2 { @@ -22,7 +22,6 @@ class BitfieldManTest:public CppUnit::TestFixture { CPPUNIT_TEST(testAddNotFilter); CPPUNIT_TEST(testAddNotFilter_zeroLength); CPPUNIT_TEST(testAddNotFilter_overflow); - CPPUNIT_TEST(testGetMissingIndex); CPPUNIT_TEST(testGetSparceMissingUnusedIndex); CPPUNIT_TEST(testGetSparceMissingUnusedIndex_setBit); CPPUNIT_TEST(testIsBitSetOffsetRange); @@ -32,34 +31,15 @@ class BitfieldManTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGetAllMissingIndexes_noarg); CPPUNIT_TEST(testGetAllMissingIndexes_checkLastByte); CPPUNIT_TEST(testGetAllMissingUnusedIndexes); - CPPUNIT_TEST(testGetMissingUnusedIndex); - CPPUNIT_TEST(testGetMissingIndex_noarg); - CPPUNIT_TEST(testGetMissingUnusedIndex_noarg); CPPUNIT_TEST(testCountFilteredBlock); CPPUNIT_TEST(testCountMissingBlock); CPPUNIT_TEST(testZeroLengthFilter); CPPUNIT_TEST(testGetFirstNMissingUnusedIndex); CPPUNIT_TEST_SUITE_END(); -private: - SharedHandle fixedNumberRandomizer; - public: - BitfieldManTest() { - FixedNumberRandomizer* randomizer = new FixedNumberRandomizer(); - randomizer->setFixedNumber(0); - this->fixedNumberRandomizer.reset(randomizer); - } - - void setUp() { - } - void testGetBlockSize(); void testGetFirstMissingUnusedIndex(); void testGetFirstMissingIndex(); - void testGetMissingIndex(); - void testGetMissingIndex_noarg(); - void testGetMissingUnusedIndex(); - void testGetMissingUnusedIndex_noarg(); void testGetAllMissingIndexes(); void testGetAllMissingIndexes_noarg(); void testGetAllMissingIndexes_checkLastByte(); @@ -201,116 +181,6 @@ void BitfieldManTest::testGetFirstMissingIndex() } } -void BitfieldManTest::testGetMissingUnusedIndex_noarg() -{ - { - BitfieldMan bt1(1024, 1024*10); - bt1.setRandomizer(fixedNumberRandomizer); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)0, index); - } - bt1.setUseBit(0); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - } - bt1.unsetUseBit(0); - bt1.setBit(0); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - } - bt1.setAllBit(); - { - size_t index; - CPPUNIT_ASSERT(!bt1.getMissingUnusedIndex(index)); - } - } - { - BitfieldMan bt1(1024, 1024*10); - bt1.setRandomizer(fixedNumberRandomizer); - - bt1.addFilter(1024, 1024*10); - bt1.enableFilter(); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - } - bt1.setUseBit(1); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)2, index); - } - bt1.setBit(2); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)3, index); - } - } -} - -void BitfieldManTest::testGetMissingIndex_noarg() -{ - { - BitfieldMan bt1(1024, 1024*10); - bt1.setRandomizer(fixedNumberRandomizer); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)0, index); - } - bt1.setUseBit(0); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)0, index); - } - bt1.unsetUseBit(0); - bt1.setBit(0); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - } - bt1.setAllBit(); - { - size_t index; - CPPUNIT_ASSERT(!bt1.getMissingIndex(index)); - } - } - { - BitfieldMan bt1(1024, 1024*10); - bt1.setRandomizer(fixedNumberRandomizer); - - bt1.addFilter(1024, 1024*10); - bt1.enableFilter(); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - } - bt1.setUseBit(1); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - } - bt1.setBit(1); - { - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index)); - CPPUNIT_ASSERT_EQUAL((size_t)2, index); - } - } -} - void BitfieldManTest::testIsAllBitSet() { BitfieldMan bt1(1024, 1024*10); CPPUNIT_ASSERT(!bt1.isAllBitSet()); @@ -331,53 +201,32 @@ void BitfieldManTest::testIsAllBitSet() { CPPUNIT_ASSERT(btzero.isAllBitSet()); } -void BitfieldManTest::testFilter() { +void BitfieldManTest::testFilter() +{ BitfieldMan btman(2, 32); - btman.setRandomizer(fixedNumberRandomizer); - // test offset=4, length=12 btman.addFilter(4, 12); btman.enableFilter(); - unsigned char peerBt[2]; - memset(peerBt, 0xff, sizeof(peerBt)); - - size_t index; - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)2, index); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)3, index); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)4, index); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)5, index); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)6, index); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)7, index); - CPPUNIT_ASSERT(!btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); + std::vector out; + CPPUNIT_ASSERT_EQUAL((size_t)6, btman.getFirstNMissingUnusedIndex(out, 32)); + const size_t ans[] = { 2, 3, 4, 5, 6, 7 }; + for(size_t i = 0; i < arrayLength(ans); ++i) { + CPPUNIT_ASSERT_EQUAL(ans[i], out[i]); + } CPPUNIT_ASSERT_EQUAL((uint64_t)12ULL, btman.getFilteredTotalLength()); // test offset=5, length=2 + out.clear(); btman.clearAllBit(); btman.clearAllUseBit(); btman.clearFilter(); btman.addFilter(5, 2); btman.enableFilter(); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - btman.setBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)2, index); - CPPUNIT_ASSERT(btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); - btman.setUseBit(index); - btman.setBit(index); - CPPUNIT_ASSERT_EQUAL((size_t)3, index); - CPPUNIT_ASSERT(!btman.getMissingUnusedIndex(index, peerBt, sizeof(peerBt))); + CPPUNIT_ASSERT_EQUAL((size_t)2, btman.getFirstNMissingUnusedIndex(out, 32)); + CPPUNIT_ASSERT_EQUAL((size_t)2, out[0]); + CPPUNIT_ASSERT_EQUAL((size_t)3, out[1]); + btman.setBit(2); + btman.setBit(3); CPPUNIT_ASSERT_EQUAL((uint64_t)4ULL, btman.getFilteredTotalLength()); CPPUNIT_ASSERT(btman.isFilteredAllBitSet()); @@ -385,7 +234,6 @@ void BitfieldManTest::testFilter() { btman2.addFilter(0, 31); btman2.enableFilter(); CPPUNIT_ASSERT_EQUAL((uint64_t)31ULL, btman2.getFilteredTotalLength()); - } void BitfieldManTest::testAddFilter_zeroLength() @@ -426,73 +274,6 @@ void BitfieldManTest::testAddNotFilter_overflow() { CPPUNIT_ASSERT(bitfield::test(btman.getFilterBitfield(), 3, 2)); } -void BitfieldManTest::testGetMissingIndex() { - BitfieldMan bt1(1024, 1024*256); - bt1.setRandomizer(fixedNumberRandomizer); - - unsigned char bitArray[] = { - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - }; - size_t index; - CPPUNIT_ASSERT(bt1.getMissingIndex(index, bitArray, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)0, index); - - bt1.addFilter(1024, 1024*256); - bt1.enableFilter(); - CPPUNIT_ASSERT(bt1.getMissingIndex(index, bitArray, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - bt1.disableFilter(); - - unsigned char bitArray2[] = { - 0x0f, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - }; - - CPPUNIT_ASSERT(bt1.getMissingIndex(index, bitArray2, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)4, index); - - unsigned char bitArray3[] = { - 0x00, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - }; - - CPPUNIT_ASSERT(bt1.getMissingIndex(index, bitArray3, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)8, index); - - unsigned char bitArray4[] = { - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - }; - - CPPUNIT_ASSERT(!bt1.getMissingIndex(index, bitArray4, 32)); - -} - // TODO1.5 add test using ignoreBitfield void BitfieldManTest::testGetSparceMissingUnusedIndex() { BitfieldMan bitfield(1024*1024, 10*1024*1024); @@ -761,60 +542,6 @@ void BitfieldManTest::testGetAllMissingUnusedIndexes() CPPUNIT_ASSERT(bitfield::test(misbitfield, nbits, 63)); } -void BitfieldManTest::testGetMissingUnusedIndex() -{ - BitfieldMan bt1(1024, 1024*256); - bt1.setRandomizer(fixedNumberRandomizer); - size_t index; - { - unsigned char bitArray[] = { - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, - }; - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index, bitArray, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)0, index); - - bt1.addFilter(1024, 1024*256); - bt1.enableFilter(); - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index, bitArray, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)1, index); - bt1.setUseBit(1); - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index, bitArray, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)2, index); - bt1.disableFilter(); - - bt1.setBit(0); - CPPUNIT_ASSERT(bt1.getMissingUnusedIndex(index, bitArray, 32)); - CPPUNIT_ASSERT_EQUAL((size_t)2, index); - - bt1.setAllBit(); - CPPUNIT_ASSERT(!bt1.getMissingUnusedIndex(index, bitArray, 32)); - - bt1.clearAllBit(); - bt1.setAllUseBit(); - CPPUNIT_ASSERT(!bt1.getMissingUnusedIndex(index, bitArray, 32)); - } - { - unsigned char bitArray4[] = { - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - }; - CPPUNIT_ASSERT(!bt1.getMissingUnusedIndex(index, bitArray4, 32)); - } -} - void BitfieldManTest::testCountFilteredBlock() { BitfieldMan bt(1024, 1024*256); diff --git a/test/DefaultPieceStorageTest.cc b/test/DefaultPieceStorageTest.cc index 508f03ea..e17f4f6c 100644 --- a/test/DefaultPieceStorageTest.cc +++ b/test/DefaultPieceStorageTest.cc @@ -4,7 +4,6 @@ #include "util.h" #include "Exception.h" -#include "FixedNumberRandomizer.h" #include "BitfieldManFactory.h" #include "Piece.h" #include "Peer.h" @@ -40,13 +39,6 @@ private: Option* option; SharedHandle _pieceSelector; public: - DefaultPieceStorageTest() { - SharedHandle randomizer - (new FixedNumberRandomizer()); - randomizer->setFixedNumber(0); - BitfieldManFactory::setDefaultRandomizer(randomizer); - } - void setUp() { _dctx.reset(new DownloadContext()); bittorrent::load("test.torrent", _dctx);