BitfieldManTest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(testGetSparceMissingUnusedIndex);
  13. CPPUNIT_TEST_SUITE_END();
  14. private:
  15. public:
  16. void setUp() {
  17. }
  18. void testGetBlockSize();
  19. void testGetFirstMissingUnusedIndex();
  20. void testIsAllBitSet();
  21. void testFilter();
  22. void testGetMissingIndex();
  23. void testGetSparceMissingUnusedIndex();
  24. };
  25. CPPUNIT_TEST_SUITE_REGISTRATION( BitfieldManTest );
  26. void BitfieldManTest::testGetBlockSize() {
  27. BitfieldMan bt1(1024, 1024*10);
  28. CPPUNIT_ASSERT_EQUAL(1024, bt1.getBlockLength(9));
  29. BitfieldMan bt2(1024, 1024*10+1);
  30. CPPUNIT_ASSERT_EQUAL(1024, bt2.getBlockLength(9));
  31. CPPUNIT_ASSERT_EQUAL(1, bt2.getBlockLength(10));
  32. CPPUNIT_ASSERT_EQUAL(0, bt2.getBlockLength(11));
  33. }
  34. void BitfieldManTest::testGetFirstMissingUnusedIndex() {
  35. BitfieldMan bt1(1024, 1024*10);
  36. unsigned char bitfield[2];
  37. memset(bitfield, 0xff, sizeof(bitfield));
  38. CPPUNIT_ASSERT_EQUAL(0, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  39. CPPUNIT_ASSERT(bt1.setUseBit(0));
  40. CPPUNIT_ASSERT_EQUAL(1, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  41. CPPUNIT_ASSERT(bt1.unsetUseBit(0));
  42. CPPUNIT_ASSERT_EQUAL(0, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  43. CPPUNIT_ASSERT(bt1.setBit(0));
  44. CPPUNIT_ASSERT_EQUAL(1, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  45. for(int i = 0; i < 8; i++) {
  46. CPPUNIT_ASSERT(bt1.setBit(i));
  47. }
  48. CPPUNIT_ASSERT_EQUAL(8, bt1.getFirstMissingUnusedIndex(bitfield, sizeof(bitfield)));
  49. CPPUNIT_ASSERT_EQUAL(8, bt1.getFirstMissingUnusedIndex());
  50. CPPUNIT_ASSERT(bt1.setUseBit(8));
  51. CPPUNIT_ASSERT_EQUAL(9, bt1.getFirstMissingUnusedIndex());
  52. }
  53. void BitfieldManTest::testIsAllBitSet() {
  54. BitfieldMan bt1(1024, 1024*10);
  55. CPPUNIT_ASSERT(!bt1.isAllBitSet());
  56. bt1.setBit(1);
  57. CPPUNIT_ASSERT(!bt1.isAllBitSet());
  58. for(int i = 0; i < 8; i++) {
  59. CPPUNIT_ASSERT(bt1.setBit(i));
  60. }
  61. CPPUNIT_ASSERT(!bt1.isAllBitSet());
  62. for(int i = 0; i < bt1.countBlock(); i++) {
  63. CPPUNIT_ASSERT(bt1.setBit(i));
  64. }
  65. CPPUNIT_ASSERT(bt1.isAllBitSet());
  66. }
  67. void BitfieldManTest::testFilter() {
  68. // set random seed here in order to get same random numbers.
  69. srandom(100);
  70. BitfieldMan btman(2, 32);
  71. // test offset=4, length=12
  72. btman.addFilter(4, 12);
  73. btman.enableFilter();
  74. unsigned char peerBt[2];
  75. memset(peerBt, 0xff, sizeof(peerBt));
  76. int index;
  77. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  78. btman.setUseBit(index);
  79. CPPUNIT_ASSERT_EQUAL(3, index);
  80. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  81. btman.setUseBit(index);
  82. CPPUNIT_ASSERT_EQUAL(4, index);
  83. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  84. btman.setUseBit(index);
  85. CPPUNIT_ASSERT_EQUAL(2, index);
  86. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  87. btman.setUseBit(index);
  88. CPPUNIT_ASSERT_EQUAL(6, index);
  89. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  90. btman.setUseBit(index);
  91. CPPUNIT_ASSERT_EQUAL(5, index);
  92. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  93. btman.setUseBit(index);
  94. CPPUNIT_ASSERT_EQUAL(7, index);
  95. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  96. btman.setUseBit(index);
  97. CPPUNIT_ASSERT_EQUAL(-1, index);
  98. CPPUNIT_ASSERT_EQUAL((long long int)12, btman.getFilteredTotalLength());
  99. // test offset=5, length=2
  100. btman.clearAllBit();
  101. btman.clearFilter();
  102. btman.addFilter(5, 2);
  103. btman.enableFilter();
  104. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  105. btman.setUseBit(index);
  106. btman.setBit(index);
  107. CPPUNIT_ASSERT_EQUAL(3, index);
  108. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  109. btman.setUseBit(index);
  110. btman.setBit(index);
  111. CPPUNIT_ASSERT_EQUAL(2, index);
  112. index = btman.getMissingUnusedIndex(peerBt, sizeof(peerBt));
  113. btman.setUseBit(index);
  114. CPPUNIT_ASSERT_EQUAL(-1, index);
  115. CPPUNIT_ASSERT_EQUAL((long long int)4, btman.getFilteredTotalLength());
  116. CPPUNIT_ASSERT(btman.isAllBitSet());
  117. BitfieldMan btman2(2, 31);
  118. btman2.addFilter(0, 31);
  119. btman2.enableFilter();
  120. CPPUNIT_ASSERT_EQUAL((long long int)31, btman2.getFilteredTotalLength());
  121. }
  122. void BitfieldManTest::testGetMissingIndex() {
  123. srandom(100);
  124. BitfieldMan bt1(1024, 1024*256);
  125. unsigned char bitArray[] = {
  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. 0xff, 0xff, 0xff, 0xff,
  133. 0xff, 0xff, 0xff, 0xff,
  134. };
  135. CPPUNIT_ASSERT_EQUAL(80, bt1.getMissingIndex(bitArray, 32));
  136. unsigned char bitArray2[] = {
  137. 0xff, 0xff, 0xff, 0xff,
  138. 0xff, 0xff, 0xff, 0xff,
  139. 0xff, 0x00, 0xff, 0xff,
  140. 0xff, 0xff, 0xff, 0xff,
  141. 0xff, 0xff, 0xff, 0xff,
  142. 0xff, 0xff, 0xff, 0xff,
  143. 0xff, 0xff, 0xff, 0xff,
  144. 0xff, 0xff, 0xff, 0xff,
  145. };
  146. CPPUNIT_ASSERT_EQUAL(80, bt1.getMissingIndex(bitArray2, 32));
  147. unsigned char bitArray3[] = {
  148. 0xff, 0xff, 0xff, 0xff,
  149. 0xff, 0xff, 0xff, 0x0f,
  150. 0xff, 0xff, 0xff, 0xff,
  151. 0xff, 0xff, 0xff, 0xff,
  152. 0xff, 0xff, 0xff, 0xff,
  153. 0xff, 0xff, 0xff, 0xff,
  154. 0xff, 0xff, 0xff, 0xff,
  155. 0xff, 0xff, 0xff, 0xff,
  156. };
  157. CPPUNIT_ASSERT_EQUAL(60, bt1.getMissingIndex(bitArray3, 32));
  158. unsigned char bitArray4[] = {
  159. 0xff, 0xff, 0xff, 0xff,
  160. 0xff, 0xff, 0xff, 0xff,
  161. 0xff, 0xff, 0xff, 0xff,
  162. 0xff, 0xff, 0xff, 0x00,
  163. 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00,
  166. 0x00, 0x00, 0x00, 0x00,
  167. };
  168. CPPUNIT_ASSERT_EQUAL(0, bt1.getMissingIndex(bitArray4, 32));
  169. unsigned char bitArray5[] = {
  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. 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00,
  178. };
  179. CPPUNIT_ASSERT_EQUAL(-1, bt1.getMissingIndex(bitArray5, 32));
  180. }
  181. void BitfieldManTest::testGetSparceMissingUnusedIndex() {
  182. BitfieldMan bitfield(1024*1024, 10*1024*1024);
  183. CPPUNIT_ASSERT_EQUAL(0, bitfield.getSparseMissingUnusedIndex());
  184. bitfield.setBit(0);
  185. CPPUNIT_ASSERT_EQUAL(5, bitfield.getSparseMissingUnusedIndex());
  186. bitfield.setUseBit(5);
  187. CPPUNIT_ASSERT_EQUAL(3, bitfield.getSparseMissingUnusedIndex());
  188. bitfield.setBit(3);
  189. CPPUNIT_ASSERT_EQUAL(8, bitfield.getSparseMissingUnusedIndex());
  190. bitfield.setBit(8);
  191. CPPUNIT_ASSERT_EQUAL(2, bitfield.getSparseMissingUnusedIndex());
  192. bitfield.setBit(2);
  193. CPPUNIT_ASSERT_EQUAL(7, bitfield.getSparseMissingUnusedIndex());
  194. bitfield.setBit(7);
  195. CPPUNIT_ASSERT_EQUAL(1, bitfield.getSparseMissingUnusedIndex());
  196. bitfield.setBit(1);
  197. CPPUNIT_ASSERT_EQUAL(4, bitfield.getSparseMissingUnusedIndex());
  198. bitfield.setBit(4);
  199. CPPUNIT_ASSERT_EQUAL(6, bitfield.getSparseMissingUnusedIndex());
  200. bitfield.setBit(6);
  201. CPPUNIT_ASSERT_EQUAL(9, bitfield.getSparseMissingUnusedIndex());
  202. bitfield.setBit(9);
  203. CPPUNIT_ASSERT_EQUAL(-1, bitfield.getSparseMissingUnusedIndex());
  204. }