BitfieldManTest.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "BitfieldMan.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. using namespace std;
  5. class BitfieldManTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(BitfieldManTest);
  7. CPPUNIT_TEST(testGetBlockSize);
  8. CPPUNIT_TEST(testGetFirstMissingUnusedIndex);
  9. CPPUNIT_TEST(testIsAllBitSet);
  10. CPPUNIT_TEST(testFilter);
  11. CPPUNIT_TEST(testGetMissingIndex);
  12. CPPUNIT_TEST_SUITE_END();
  13. private:
  14. public:
  15. void setUp() {
  16. }
  17. void testGetBlockSize();
  18. void testGetFirstMissingUnusedIndex();
  19. void testIsAllBitSet();
  20. void testFilter();
  21. void testGetMissingIndex();
  22. };
  23. CPPUNIT_TEST_SUITE_REGISTRATION( BitfieldManTest );
  24. void BitfieldManTest::testGetBlockSize() {
  25. BitfieldMan bt1(1024, 1024*10);
  26. CPPUNIT_ASSERT_EQUAL(1024, bt1.getBlockLength(9));
  27. BitfieldMan bt2(1024, 1024*10+1);
  28. CPPUNIT_ASSERT_EQUAL(1024, bt2.getBlockLength(9));
  29. CPPUNIT_ASSERT_EQUAL(1, bt2.getBlockLength(10));
  30. CPPUNIT_ASSERT_EQUAL(0, bt2.getBlockLength(11));
  31. }
  32. void BitfieldManTest::testGetFirstMissingUnusedIndex() {
  33. BitfieldMan bt1(1024, 1024*10);
  34. unsigned char bitfield[2];
  35. memset(bitfield, 0xff, sizeof(bitfield));
  36. CPPUNIT_ASSERT_EQUAL(0, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  37. CPPUNIT_ASSERT(bt1.setUseBit(0));
  38. CPPUNIT_ASSERT_EQUAL(1, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  39. CPPUNIT_ASSERT(bt1.unsetUseBit(0));
  40. CPPUNIT_ASSERT_EQUAL(0, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  41. CPPUNIT_ASSERT(bt1.setBit(0));
  42. CPPUNIT_ASSERT_EQUAL(1, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  43. for(int i = 0; i < 8; i++) {
  44. CPPUNIT_ASSERT(bt1.setBit(i));
  45. }
  46. CPPUNIT_ASSERT_EQUAL(8, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  47. CPPUNIT_ASSERT_EQUAL(8, bt1.getFirstMissingUnusedIndex());
  48. CPPUNIT_ASSERT(bt1.setUseBit(8));
  49. CPPUNIT_ASSERT_EQUAL(9, bt1.getFirstMissingUnusedIndex());
  50. }
  51. void BitfieldManTest::testIsAllBitSet() {
  52. BitfieldMan bt1(1024, 1024*10);
  53. CPPUNIT_ASSERT(!bt1.isAllBitSet());
  54. bt1.setBit(1);
  55. CPPUNIT_ASSERT(!bt1.isAllBitSet());
  56. for(int i = 0; i < 8; i++) {
  57. CPPUNIT_ASSERT(bt1.setBit(i));
  58. }
  59. CPPUNIT_ASSERT(!bt1.isAllBitSet());
  60. for(int i = 0; i < bt1.countBlock(); i++) {
  61. CPPUNIT_ASSERT(bt1.setBit(i));
  62. }
  63. CPPUNIT_ASSERT(bt1.isAllBitSet());
  64. }
  65. void BitfieldManTest::testFilter() {
  66. // set random seed here in order to get same random numbers.
  67. srandom(100);
  68. BitfieldMan btman(2, 32);
  69. // test offset=4, length=12
  70. btman.addFilter(4, 12);
  71. btman.enableFilter();
  72. unsigned char peerBt[2];
  73. memset(peerBt, 0xff, sizeof(peerBt));
  74. int index;
  75. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  76. btman.setUseBit(index);
  77. CPPUNIT_ASSERT_EQUAL(3, index);
  78. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  79. btman.setUseBit(index);
  80. CPPUNIT_ASSERT_EQUAL(4, index);
  81. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  82. btman.setUseBit(index);
  83. CPPUNIT_ASSERT_EQUAL(2, index);
  84. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  85. btman.setUseBit(index);
  86. CPPUNIT_ASSERT_EQUAL(6, index);
  87. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  88. btman.setUseBit(index);
  89. CPPUNIT_ASSERT_EQUAL(5, index);
  90. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  91. btman.setUseBit(index);
  92. CPPUNIT_ASSERT_EQUAL(7, index);
  93. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  94. btman.setUseBit(index);
  95. CPPUNIT_ASSERT_EQUAL(-1, index);
  96. CPPUNIT_ASSERT_EQUAL((long long int)12, btman.getFilteredTotalLength());
  97. // test offset=5, length=2
  98. btman.clearAllBit();
  99. btman.clearFilter();
  100. btman.addFilter(5, 2);
  101. btman.enableFilter();
  102. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  103. btman.setUseBit(index);
  104. btman.setBit(index);
  105. CPPUNIT_ASSERT_EQUAL(3, index);
  106. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  107. btman.setUseBit(index);
  108. btman.setBit(index);
  109. CPPUNIT_ASSERT_EQUAL(2, index);
  110. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  111. btman.setUseBit(index);
  112. CPPUNIT_ASSERT_EQUAL(-1, index);
  113. CPPUNIT_ASSERT_EQUAL((long long int)4, btman.getFilteredTotalLength());
  114. CPPUNIT_ASSERT(btman.isAllBitSet());
  115. BitfieldMan btman2(2, 31);
  116. btman2.addFilter(0, 31);
  117. btman2.enableFilter();
  118. CPPUNIT_ASSERT_EQUAL((long long int)31, btman2.getFilteredTotalLength());
  119. }
  120. void BitfieldManTest::testGetMissingIndex() {
  121. srandom(100);
  122. BitfieldMan bt1(1024, 1024*256);
  123. unsigned char bitArray[] = {
  124. 0xff, 0xff, 0xff, 0xff,
  125. 0xff, 0xff, 0xff, 0xff,
  126. 0xff, 0xff, 0xff, 0xff,
  127. 0xff, 0xff, 0xff, 0xff,
  128. 0xff, 0xff, 0xff, 0xff,
  129. 0xff, 0xff, 0xff, 0xff,
  130. 0xff, 0xff, 0xff, 0xff,
  131. 0xff, 0xff, 0xff, 0xff,
  132. };
  133. CPPUNIT_ASSERT_EQUAL(80, bt1.getMissingIndex(bitArray, 32));
  134. unsigned char bitArray2[] = {
  135. 0xff, 0xff, 0xff, 0xff,
  136. 0xff, 0xff, 0xff, 0xff,
  137. 0xff, 0x00, 0xff, 0xff,
  138. 0xff, 0xff, 0xff, 0xff,
  139. 0xff, 0xff, 0xff, 0xff,
  140. 0xff, 0xff, 0xff, 0xff,
  141. 0xff, 0xff, 0xff, 0xff,
  142. 0xff, 0xff, 0xff, 0xff,
  143. };
  144. CPPUNIT_ASSERT_EQUAL(80, bt1.getMissingIndex(bitArray2, 32));
  145. unsigned char bitArray3[] = {
  146. 0xff, 0xff, 0xff, 0xff,
  147. 0xff, 0xff, 0xff, 0x0f,
  148. 0xff, 0xff, 0xff, 0xff,
  149. 0xff, 0xff, 0xff, 0xff,
  150. 0xff, 0xff, 0xff, 0xff,
  151. 0xff, 0xff, 0xff, 0xff,
  152. 0xff, 0xff, 0xff, 0xff,
  153. 0xff, 0xff, 0xff, 0xff,
  154. };
  155. CPPUNIT_ASSERT_EQUAL(60, bt1.getMissingIndex(bitArray3, 32));
  156. unsigned char bitArray4[] = {
  157. 0xff, 0xff, 0xff, 0xff,
  158. 0xff, 0xff, 0xff, 0xff,
  159. 0xff, 0xff, 0xff, 0xff,
  160. 0xff, 0xff, 0xff, 0x00,
  161. 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00,
  163. 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00,
  165. };
  166. CPPUNIT_ASSERT_EQUAL(0, bt1.getMissingIndex(bitArray4, 32));
  167. unsigned char bitArray5[] = {
  168. 0x00, 0x00, 0x00, 0x00,
  169. 0x00, 0x00, 0x00, 0x00,
  170. 0x00, 0x00, 0x00, 0x00,
  171. 0x00, 0x00, 0x00, 0x00,
  172. 0x00, 0x00, 0x00, 0x00,
  173. 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00,
  176. };
  177. CPPUNIT_ASSERT_EQUAL(-1, bt1.getMissingIndex(bitArray5, 32));
  178. }