2009-03-28 13:29:38 +00:00
|
|
|
#include "bitfield.h"
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2013-07-03 14:22:40 +00:00
|
|
|
#include "TimerA2.h"
|
|
|
|
|
2009-03-28 13:29:38 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class bitfieldTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(bitfieldTest);
|
|
|
|
CPPUNIT_TEST(testTest);
|
|
|
|
CPPUNIT_TEST(testCountBit32);
|
|
|
|
CPPUNIT_TEST(testCountSetBit);
|
|
|
|
CPPUNIT_TEST(testLastByteMask);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void testTest();
|
|
|
|
void testCountBit32();
|
|
|
|
void testCountSetBit();
|
|
|
|
void testLastByteMask();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( bitfieldTest );
|
|
|
|
|
|
|
|
void bitfieldTest::testTest()
|
|
|
|
{
|
|
|
|
unsigned char bitfield[] = { 0xaa };
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(bitfield::test(bitfield, 8, 0));
|
|
|
|
CPPUNIT_ASSERT(!bitfield::test(bitfield, 8, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitfieldTest::testCountBit32()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)32, bitfield::countBit32(UINT32_MAX));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)8, bitfield::countBit32(255));
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitfieldTest::testCountSetBit()
|
|
|
|
{
|
2009-04-25 10:30:25 +00:00
|
|
|
unsigned char bitfield[] = { 0xff, 0xff, 0xff, 0xff,
|
2010-01-05 16:01:46 +00:00
|
|
|
0xff, 0xff, 0xff, 0xf9 };
|
2009-04-25 10:30:25 +00:00
|
|
|
// (nbits+7)/8 == 0 && nbits%32 == 0
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)62, bitfield::countSetBit(bitfield, 64));
|
2013-07-03 14:22:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)62, bitfield::countSetBitSlow(bitfield, 64));
|
2009-04-25 10:30:25 +00:00
|
|
|
// (nbits+7)/8 != 0 && nbits%32 != 0 && len%4 == 0
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)56, bitfield::countSetBit(bitfield, 56));
|
2013-07-03 14:22:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)56, bitfield::countSetBitSlow(bitfield, 56));
|
2009-04-25 10:30:25 +00:00
|
|
|
// (nbits+7)/8 != 0 && nbits%32 != 0 && len%4 != 0
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)40, bitfield::countSetBit(bitfield, 40));
|
2013-07-03 14:22:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)40, bitfield::countSetBitSlow(bitfield, 40));
|
2009-04-25 10:30:25 +00:00
|
|
|
// (nbits+7)/8 == 0 && nbits%32 != 0
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)61, bitfield::countSetBit(bitfield, 63));
|
2013-07-03 14:22:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)61, bitfield::countSetBitSlow(bitfield, 63));
|
2009-04-25 10:30:25 +00:00
|
|
|
// nbts == 0
|
2009-04-17 13:19:17 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, bitfield::countSetBit(bitfield, 0));
|
2013-07-03 14:22:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, bitfield::countSetBitSlow(bitfield, 0));
|
2009-03-28 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bitfieldTest::testLastByteMask()
|
|
|
|
{
|
2010-03-19 08:56:17 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned int)0,
|
|
|
|
(unsigned int)bitfield::lastByteMask(0));
|
2009-03-28 13:29:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned int)128,
|
2010-01-05 16:01:46 +00:00
|
|
|
(unsigned int)bitfield::lastByteMask(9));
|
2009-03-28 13:29:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned int)240,
|
2010-01-05 16:01:46 +00:00
|
|
|
(unsigned int)bitfield::lastByteMask(12));
|
2009-03-28 13:29:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned int)255,
|
2010-01-05 16:01:46 +00:00
|
|
|
(unsigned int)bitfield::lastByteMask(16));
|
2009-03-28 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|