File: bitfieldTest.cc

package info (click to toggle)
aria2 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,748 kB
  • ctags: 13,441
  • sloc: cpp: 86,740; ansic: 16,496; sh: 4,916; makefile: 1,312; ruby: 397; yacc: 291; xml: 170; sed: 16
file content (69 lines) | stat: -rw-r--r-- 2,054 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "bitfield.h"

#include <cppunit/extensions/HelperMacros.h>

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()
{
  unsigned char bitfield[] = { 0xff, 0xff, 0xff, 0xff,
                               0xff, 0xff, 0xff, 0xf9 };
  // (nbits+7)/8 == 0 && nbits%32 == 0
  CPPUNIT_ASSERT_EQUAL((size_t)62, bitfield::countSetBit(bitfield, 64));
  // (nbits+7)/8 != 0 && nbits%32 != 0 && len%4 == 0
  CPPUNIT_ASSERT_EQUAL((size_t)56, bitfield::countSetBit(bitfield, 56));
  // (nbits+7)/8 != 0 && nbits%32 != 0 && len%4 != 0
  CPPUNIT_ASSERT_EQUAL((size_t)40, bitfield::countSetBit(bitfield, 40));
  // (nbits+7)/8 == 0 && nbits%32 != 0
  CPPUNIT_ASSERT_EQUAL((size_t)61, bitfield::countSetBit(bitfield, 63));
  // nbts == 0
  CPPUNIT_ASSERT_EQUAL((size_t)0, bitfield::countSetBit(bitfield, 0));
}

void bitfieldTest::testLastByteMask()
{
  CPPUNIT_ASSERT_EQUAL((unsigned int)0,
                       (unsigned int)bitfield::lastByteMask(0));
  CPPUNIT_ASSERT_EQUAL((unsigned int)128,
                       (unsigned int)bitfield::lastByteMask(9));
  CPPUNIT_ASSERT_EQUAL((unsigned int)240,
                       (unsigned int)bitfield::lastByteMask(12));
  CPPUNIT_ASSERT_EQUAL((unsigned int)255,
                       (unsigned int)bitfield::lastByteMask(16));
}

} // namespace aria2