alloc-bitmap.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //! 动态位图的集成测试
  2. use bitmap::{traits::BitMapOps, AllocBitmap};
  3. /// 测试空的位图
  4. ///
  5. /// 这是一个测试空的位图的例子
  6. ///
  7. /// 测试空的位图
  8. #[test]
  9. fn test_empty_bitmap_32() {
  10. let mut bitmap = AllocBitmap::new(32);
  11. assert_eq!(bitmap.len(), 32);
  12. assert_eq!(bitmap.size(), 8);
  13. assert_eq!(bitmap.first_index(), None);
  14. assert_eq!(bitmap.first_false_index(), Some(0));
  15. assert_eq!(bitmap.last_index(), None);
  16. assert_eq!(bitmap.last_false_index(), Some(31));
  17. assert_eq!(bitmap.next_index(0), None);
  18. assert_eq!(bitmap.next_false_index(0), Some(1));
  19. assert_eq!(bitmap.prev_index(0), None);
  20. assert_eq!(bitmap.prev_false_index(0), None);
  21. assert_eq!(bitmap.is_full(), false);
  22. assert_eq!(bitmap.is_empty(), true);
  23. bitmap.invert();
  24. assert_eq!(bitmap.len(), 32);
  25. assert_eq!(bitmap.size(), 8);
  26. assert_eq!(bitmap.first_index(), Some(0));
  27. assert_eq!(bitmap.first_false_index(), None);
  28. assert_eq!(bitmap.last_index(), Some(31));
  29. assert_eq!(bitmap.last_false_index(), None);
  30. assert_eq!(bitmap.next_index(0), Some(1));
  31. assert_eq!(bitmap.next_false_index(0), None);
  32. assert_eq!(bitmap.prev_index(0), None);
  33. assert_eq!(bitmap.prev_false_index(0), None);
  34. assert_eq!(bitmap.is_full(), true);
  35. assert_eq!(bitmap.is_empty(), false);
  36. }
  37. #[test]
  38. fn test_empty_bitmap_64() {
  39. let mut bitmap = AllocBitmap::new(64);
  40. assert_eq!(bitmap.len(), 64);
  41. assert_eq!(bitmap.size(), 8);
  42. assert_eq!(bitmap.first_index(), None);
  43. assert_eq!(bitmap.first_false_index(), Some(0));
  44. assert_eq!(bitmap.last_index(), None);
  45. assert_eq!(bitmap.last_false_index(), Some(63));
  46. assert_eq!(bitmap.next_index(0), None);
  47. assert_eq!(bitmap.next_false_index(0), Some(1));
  48. assert_eq!(bitmap.prev_index(0), None);
  49. assert_eq!(bitmap.prev_false_index(0), None);
  50. assert_eq!(bitmap.is_full(), false);
  51. assert_eq!(bitmap.is_empty(), true);
  52. bitmap.invert();
  53. assert_eq!(bitmap.len(), 64);
  54. assert_eq!(bitmap.size(), 8);
  55. assert_eq!(bitmap.first_index(), Some(0));
  56. assert_eq!(bitmap.first_false_index(), None);
  57. assert_eq!(bitmap.last_index(), Some(63));
  58. assert_eq!(bitmap.last_false_index(), None);
  59. assert_eq!(bitmap.next_index(0), Some(1));
  60. assert_eq!(bitmap.next_false_index(0), None);
  61. assert_eq!(bitmap.prev_index(0), None);
  62. assert_eq!(bitmap.prev_false_index(0), None);
  63. assert_eq!(bitmap.is_full(), true);
  64. assert_eq!(bitmap.is_empty(), false);
  65. }
  66. /// 测试长度为32的bmp,其中第一个元素为1
  67. #[test]
  68. fn test_alloc_bitmap_32_first_1() {
  69. let mut bitmap = AllocBitmap::new(32);
  70. bitmap.set(0, true);
  71. assert_eq!(bitmap.len(), 32);
  72. assert_eq!(bitmap.size(), 8);
  73. assert_eq!(bitmap.first_index(), Some(0));
  74. assert_eq!(bitmap.first_false_index(), Some(1));
  75. assert_eq!(bitmap.last_index(), Some(0));
  76. assert_eq!(bitmap.last_false_index(), Some(31));
  77. assert_eq!(bitmap.next_index(0), None);
  78. assert_eq!(bitmap.next_false_index(0), Some(1));
  79. assert_eq!(bitmap.prev_index(0), None);
  80. assert_eq!(bitmap.prev_false_index(0), None);
  81. assert_eq!(bitmap.is_full(), false);
  82. assert_eq!(bitmap.is_empty(), false);
  83. assert_eq!(bitmap.prev_index(2), Some(0));
  84. assert_eq!(bitmap.prev_false_index(2), Some(1));
  85. assert_eq!(bitmap.next_index(2), None);
  86. assert_eq!(bitmap.next_false_index(2), Some(3));
  87. // 反转
  88. bitmap.invert();
  89. assert_eq!(bitmap.first_index(), Some(1));
  90. assert_eq!(bitmap.first_false_index(), Some(0));
  91. assert_eq!(bitmap.last_index(), Some(31));
  92. assert_eq!(bitmap.last_false_index(), Some(0));
  93. assert_eq!(bitmap.next_index(0), Some(1));
  94. assert_eq!(bitmap.next_false_index(0), None);
  95. assert_eq!(bitmap.prev_index(0), None);
  96. assert_eq!(bitmap.prev_false_index(0), None);
  97. assert_eq!(bitmap.is_full(), false);
  98. assert_eq!(bitmap.is_empty(), false);
  99. assert_eq!(bitmap.prev_index(2), Some(1));
  100. assert_eq!(bitmap.prev_false_index(2), Some(0));
  101. assert_eq!(bitmap.next_index(2), Some(3));
  102. }
  103. /// 测试长度为32的bmp,其中中间某个元素为1
  104. #[test]
  105. fn test_alloc_bitmap_32_middle_1() {
  106. let mut bitmap = AllocBitmap::new(32);
  107. bitmap.set(15, true);
  108. assert_eq!(bitmap.len(), 32);
  109. assert_eq!(bitmap.size(), 8);
  110. assert_eq!(bitmap.first_index(), Some(15));
  111. assert_eq!(bitmap.first_false_index(), Some(0));
  112. assert_eq!(bitmap.last_index(), Some(15));
  113. assert_eq!(bitmap.last_false_index(), Some(31));
  114. assert_eq!(bitmap.next_index(0), Some(15));
  115. assert_eq!(bitmap.next_index(15), None);
  116. assert_eq!(bitmap.next_false_index(15), Some(16));
  117. assert_eq!(bitmap.prev_index(15), None);
  118. assert_eq!(bitmap.prev_false_index(15), Some(14));
  119. assert_eq!(bitmap.is_full(), false);
  120. assert_eq!(bitmap.is_empty(), false);
  121. assert_eq!(bitmap.prev_index(20), Some(15));
  122. assert_eq!(bitmap.prev_false_index(20), Some(19));
  123. assert_eq!(bitmap.next_index(2), Some(15));
  124. assert_eq!(bitmap.next_false_index(2), Some(3));
  125. // 反转
  126. bitmap.invert();
  127. assert_eq!(bitmap.first_index(), Some(0));
  128. assert_eq!(bitmap.first_false_index(), Some(15));
  129. assert_eq!(bitmap.last_index(), Some(31));
  130. assert_eq!(bitmap.last_false_index(), Some(15));
  131. assert_eq!(bitmap.next_index(0), Some(1));
  132. assert_eq!(bitmap.next_false_index(1), Some(15));
  133. assert_eq!(bitmap.prev_index(15), Some(14));
  134. assert_eq!(bitmap.prev_false_index(15), None);
  135. assert_eq!(bitmap.is_full(), false);
  136. assert_eq!(bitmap.is_empty(), false);
  137. assert_eq!(bitmap.prev_index(30), Some(29));
  138. assert_eq!(bitmap.prev_false_index(30), Some(15));
  139. assert_eq!(bitmap.next_index(2), Some(3));
  140. }
  141. /// 测试长度为32的bmp,其中最后一个元素为1
  142. #[test]
  143. fn test_alloc_bitmap_32_last_1() {
  144. let mut bitmap = AllocBitmap::new(32);
  145. bitmap.set(31, true);
  146. assert_eq!(bitmap.len(), 32);
  147. assert_eq!(bitmap.size(), 8);
  148. assert_eq!(bitmap.first_index(), Some(31));
  149. assert_eq!(bitmap.first_false_index(), Some(0));
  150. assert_eq!(bitmap.last_index(), Some(31));
  151. assert_eq!(bitmap.last_false_index(), Some(30));
  152. assert_eq!(bitmap.next_index(0), Some(31));
  153. assert_eq!(bitmap.next_index(31), None);
  154. assert_eq!(bitmap.next_false_index(0), Some(1));
  155. assert_eq!(bitmap.prev_index(31), None);
  156. assert_eq!(bitmap.prev_false_index(31), Some(30));
  157. assert_eq!(bitmap.is_full(), false);
  158. assert_eq!(bitmap.is_empty(), false);
  159. assert_eq!(bitmap.prev_index(2), None);
  160. assert_eq!(bitmap.prev_false_index(2), Some(1));
  161. assert_eq!(bitmap.next_index(2), Some(31));
  162. assert_eq!(bitmap.next_false_index(2), Some(3));
  163. // 反转
  164. bitmap.invert();
  165. assert_eq!(bitmap.first_index(), Some(0));
  166. assert_eq!(bitmap.first_false_index(), Some(31));
  167. assert_eq!(bitmap.last_index(), Some(30));
  168. assert_eq!(bitmap.last_false_index(), Some(31));
  169. assert_eq!(bitmap.next_index(0), Some(1));
  170. assert_eq!(bitmap.next_false_index(0), Some(31));
  171. assert_eq!(bitmap.prev_index(31), Some(30));
  172. assert_eq!(bitmap.prev_false_index(31), None);
  173. assert_eq!(bitmap.is_full(), false);
  174. assert_eq!(bitmap.is_empty(), false);
  175. assert_eq!(bitmap.prev_index(2), Some(1));
  176. assert_eq!(bitmap.prev_false_index(2), None);
  177. assert_eq!(bitmap.next_index(2), Some(3));
  178. }
  179. /// 测试长度为64的bmp,其中第一个元素为1
  180. #[test]
  181. fn test_alloc_bitmap_64_first_1() {
  182. let mut bitmap = AllocBitmap::new(64);
  183. bitmap.set(0, true);
  184. assert_eq!(bitmap.len(), 64);
  185. assert_eq!(bitmap.size(), 8);
  186. assert_eq!(bitmap.first_index(), Some(0));
  187. assert_eq!(bitmap.first_false_index(), Some(1));
  188. assert_eq!(bitmap.last_index(), Some(0));
  189. assert_eq!(bitmap.last_false_index(), Some(63));
  190. assert_eq!(bitmap.next_index(0), None);
  191. assert_eq!(bitmap.next_false_index(0), Some(1));
  192. assert_eq!(bitmap.prev_index(0), None);
  193. assert_eq!(bitmap.prev_false_index(0), None);
  194. assert_eq!(bitmap.is_full(), false);
  195. assert_eq!(bitmap.is_empty(), false);
  196. assert_eq!(bitmap.prev_index(2), Some(0));
  197. assert_eq!(bitmap.prev_false_index(2), Some(1));
  198. assert_eq!(bitmap.next_index(2), None);
  199. assert_eq!(bitmap.next_false_index(2), Some(3));
  200. // 反转
  201. bitmap.invert();
  202. assert_eq!(bitmap.first_index(), Some(1));
  203. assert_eq!(bitmap.first_false_index(), Some(0));
  204. assert_eq!(bitmap.last_index(), Some(63));
  205. assert_eq!(bitmap.last_false_index(), Some(0));
  206. assert_eq!(bitmap.next_index(0), Some(1));
  207. assert_eq!(bitmap.next_false_index(0), None);
  208. assert_eq!(bitmap.prev_index(0), None);
  209. assert_eq!(bitmap.prev_false_index(0), None);
  210. assert_eq!(bitmap.is_full(), false);
  211. assert_eq!(bitmap.is_empty(), false);
  212. assert_eq!(bitmap.prev_index(2), Some(1));
  213. assert_eq!(bitmap.prev_false_index(2), Some(0));
  214. assert_eq!(bitmap.next_index(2), Some(3));
  215. }
  216. /// 测试长度为64的bmp,其中中间某个元素为1
  217. #[test]
  218. fn test_alloc_bitmap_64_middle_1() {
  219. let mut bitmap = AllocBitmap::new(64);
  220. bitmap.set(15, true);
  221. assert_eq!(bitmap.len(), 64);
  222. assert_eq!(bitmap.size(), 8);
  223. assert_eq!(bitmap.first_index(), Some(15));
  224. assert_eq!(bitmap.first_false_index(), Some(0));
  225. assert_eq!(bitmap.last_index(), Some(15));
  226. assert_eq!(bitmap.last_false_index(), Some(63));
  227. assert_eq!(bitmap.next_index(0), Some(15));
  228. assert_eq!(bitmap.next_index(15), None);
  229. assert_eq!(bitmap.next_false_index(15), Some(16));
  230. assert_eq!(bitmap.prev_index(15), None);
  231. assert_eq!(bitmap.prev_false_index(15), Some(14));
  232. assert_eq!(bitmap.is_full(), false);
  233. assert_eq!(bitmap.is_empty(), false);
  234. assert_eq!(bitmap.prev_index(20), Some(15));
  235. assert_eq!(bitmap.prev_false_index(20), Some(19));
  236. assert_eq!(bitmap.next_index(2), Some(15));
  237. assert_eq!(bitmap.next_false_index(2), Some(3));
  238. // 反转
  239. bitmap.invert();
  240. assert_eq!(bitmap.first_index(), Some(0));
  241. assert_eq!(bitmap.first_false_index(), Some(15));
  242. assert_eq!(bitmap.last_index(), Some(63));
  243. assert_eq!(bitmap.last_false_index(), Some(15));
  244. assert_eq!(bitmap.next_index(0), Some(1));
  245. assert_eq!(bitmap.next_false_index(1), Some(15));
  246. assert_eq!(bitmap.prev_index(15), Some(14));
  247. assert_eq!(bitmap.prev_false_index(15), None);
  248. assert_eq!(bitmap.is_full(), false);
  249. assert_eq!(bitmap.is_empty(), false);
  250. assert_eq!(bitmap.prev_index(63), Some(62));
  251. assert_eq!(bitmap.prev_false_index(62), Some(15));
  252. assert_eq!(bitmap.next_index(2), Some(3));
  253. }
  254. /// 测试长度为64的bmp,其中最后一个元素为1
  255. #[test]
  256. fn test_alloc_bitmap_64_last_1() {
  257. let mut bitmap = AllocBitmap::new(64);
  258. bitmap.set(63, true);
  259. assert_eq!(bitmap.len(), 64);
  260. assert_eq!(bitmap.size(), 8);
  261. assert_eq!(bitmap.first_index(), Some(63));
  262. assert_eq!(bitmap.first_false_index(), Some(0));
  263. assert_eq!(bitmap.last_index(), Some(63));
  264. assert_eq!(bitmap.last_false_index(), Some(62));
  265. assert_eq!(bitmap.next_index(0), Some(63));
  266. assert_eq!(bitmap.next_index(63), None);
  267. assert_eq!(bitmap.next_false_index(0), Some(1));
  268. assert_eq!(bitmap.prev_index(63), None);
  269. assert_eq!(bitmap.prev_false_index(63), Some(62));
  270. assert_eq!(bitmap.is_full(), false);
  271. assert_eq!(bitmap.is_empty(), false);
  272. assert_eq!(bitmap.prev_index(2), None);
  273. assert_eq!(bitmap.prev_false_index(2), Some(1));
  274. assert_eq!(bitmap.next_index(2), Some(63));
  275. assert_eq!(bitmap.next_false_index(2), Some(3));
  276. // 反转
  277. bitmap.invert();
  278. assert_eq!(bitmap.first_index(), Some(0));
  279. assert_eq!(bitmap.first_false_index(), Some(63));
  280. assert_eq!(bitmap.last_index(), Some(62));
  281. assert_eq!(bitmap.last_false_index(), Some(63));
  282. assert_eq!(bitmap.next_index(0), Some(1));
  283. assert_eq!(bitmap.next_false_index(0), Some(63));
  284. assert_eq!(bitmap.prev_index(63), Some(62));
  285. assert_eq!(bitmap.prev_false_index(63), None);
  286. assert_eq!(bitmap.is_full(), false);
  287. assert_eq!(bitmap.is_empty(), false);
  288. assert_eq!(bitmap.prev_index(2), Some(1));
  289. assert_eq!(bitmap.prev_false_index(2), None);
  290. assert_eq!(bitmap.next_index(2), Some(3));
  291. }
  292. /// 测试长度为64的bmp,其中第一个和最后一个元素为1
  293. #[test]
  294. fn test_alloc_bitmap_64_two_1_first() {
  295. let mut bitmap = AllocBitmap::new(64);
  296. bitmap.set(0, true);
  297. bitmap.set(63, true);
  298. assert_eq!(bitmap.len(), 64);
  299. assert_eq!(bitmap.size(), 8);
  300. assert_eq!(bitmap.first_index(), Some(0));
  301. assert_eq!(bitmap.first_false_index(), Some(1));
  302. assert_eq!(bitmap.last_index(), Some(63));
  303. assert_eq!(bitmap.last_false_index(), Some(62));
  304. assert_eq!(bitmap.next_index(0), Some(63));
  305. assert_eq!(bitmap.next_index(63), None);
  306. assert_eq!(bitmap.next_false_index(0), Some(1));
  307. assert_eq!(bitmap.prev_index(63), Some(0));
  308. assert_eq!(bitmap.prev_false_index(63), Some(62));
  309. assert_eq!(bitmap.is_full(), false);
  310. assert_eq!(bitmap.is_empty(), false);
  311. // 反转
  312. bitmap.invert();
  313. assert_eq!(bitmap.first_index(), Some(1));
  314. assert_eq!(bitmap.first_false_index(), Some(0));
  315. assert_eq!(bitmap.last_index(), Some(62));
  316. assert_eq!(bitmap.last_false_index(), Some(63));
  317. assert_eq!(bitmap.next_index(0), Some(1));
  318. assert_eq!(bitmap.next_false_index(0), Some(63));
  319. assert_eq!(bitmap.prev_index(63), Some(62));
  320. assert_eq!(bitmap.prev_false_index(63), Some(0));
  321. assert_eq!(bitmap.is_full(), false);
  322. assert_eq!(bitmap.is_empty(), false);
  323. assert_eq!(bitmap.prev_index(2), Some(1));
  324. assert_eq!(bitmap.prev_false_index(2), Some(0));
  325. assert_eq!(bitmap.next_index(2), Some(3));
  326. assert_eq!(bitmap.next_false_index(2), Some(63));
  327. }
  328. /// 测试长度为64的bmp,中间两个不相邻的元素为1
  329. #[test]
  330. fn test_alloc_bitmap_64_two_1_middle() {
  331. let mut bitmap = AllocBitmap::new(64);
  332. bitmap.set(15, true);
  333. bitmap.set(63, true);
  334. assert_eq!(bitmap.len(), 64);
  335. assert_eq!(bitmap.size(), 8);
  336. assert_eq!(bitmap.first_index(), Some(15));
  337. assert_eq!(bitmap.first_false_index(), Some(0));
  338. assert_eq!(bitmap.last_index(), Some(63));
  339. assert_eq!(bitmap.last_false_index(), Some(62));
  340. assert_eq!(bitmap.next_index(0), Some(15));
  341. assert_eq!(bitmap.next_index(15), Some(63));
  342. assert_eq!(bitmap.next_index(63), None);
  343. assert_eq!(bitmap.next_false_index(0), Some(1));
  344. assert_eq!(bitmap.prev_index(63), Some(15));
  345. assert_eq!(bitmap.prev_false_index(63), Some(62));
  346. assert_eq!(bitmap.is_full(), false);
  347. assert_eq!(bitmap.is_empty(), false);
  348. // 反转
  349. bitmap.invert();
  350. assert_eq!(bitmap.first_index(), Some(0));
  351. assert_eq!(bitmap.first_false_index(), Some(15));
  352. assert_eq!(bitmap.last_index(), Some(62));
  353. assert_eq!(bitmap.last_false_index(), Some(63));
  354. assert_eq!(bitmap.next_index(0), Some(1));
  355. assert_eq!(bitmap.next_false_index(0), Some(15));
  356. assert_eq!(bitmap.next_false_index(15), Some(63));
  357. assert_eq!(bitmap.prev_index(63), Some(62));
  358. assert_eq!(bitmap.prev_false_index(63), Some(15));
  359. assert_eq!(bitmap.is_full(), false);
  360. assert_eq!(bitmap.is_empty(), false);
  361. assert_eq!(bitmap.prev_index(2), Some(1));
  362. assert_eq!(bitmap.prev_false_index(2), None);
  363. assert_eq!(bitmap.next_index(2), Some(3));
  364. assert_eq!(bitmap.next_false_index(2), Some(15));
  365. }
  366. #[test]
  367. fn test_alloc_bitmap_128_two_1_seperate_first() {
  368. let mut bitmap = AllocBitmap::new(128);
  369. bitmap.set(0, true);
  370. bitmap.set(127, true);
  371. assert_eq!(bitmap.len(), 128);
  372. assert_eq!(bitmap.size(), 16);
  373. assert_eq!(bitmap.first_index(), Some(0));
  374. assert_eq!(bitmap.first_false_index(), Some(1));
  375. assert_eq!(bitmap.last_index(), Some(127));
  376. assert_eq!(bitmap.last_false_index(), Some(126));
  377. assert_eq!(bitmap.next_index(0), Some(127));
  378. assert_eq!(bitmap.next_index(127), None);
  379. assert_eq!(bitmap.next_false_index(0), Some(1));
  380. assert_eq!(bitmap.prev_index(127), Some(0));
  381. assert_eq!(bitmap.prev_false_index(127), Some(126));
  382. assert_eq!(bitmap.prev_index(64), Some(0));
  383. assert_eq!(bitmap.is_full(), false);
  384. assert_eq!(bitmap.is_empty(), false);
  385. // 反转
  386. bitmap.invert();
  387. assert_eq!(bitmap.first_index(), Some(1));
  388. assert_eq!(bitmap.first_false_index(), Some(0));
  389. assert_eq!(bitmap.last_index(), Some(126));
  390. assert_eq!(bitmap.last_false_index(), Some(127));
  391. assert_eq!(bitmap.next_index(0), Some(1));
  392. assert_eq!(bitmap.next_false_index(0), Some(127));
  393. assert_eq!(bitmap.prev_index(127), Some(126));
  394. assert_eq!(bitmap.prev_false_index(127), Some(0));
  395. assert_eq!(bitmap.prev_false_index(64), Some(0));
  396. assert_eq!(bitmap.is_empty(), false);
  397. assert_eq!(bitmap.is_full(), false);
  398. }
  399. /// 长度128, 第63、64bit为1
  400. #[test]
  401. fn test_alloc_bitmap_128_two_1_nearby_middle() {
  402. let mut bitmap = AllocBitmap::new(128);
  403. bitmap.set(63, true);
  404. bitmap.set(64, true);
  405. assert_eq!(bitmap.len(), 128);
  406. assert_eq!(bitmap.size(), 16);
  407. assert_eq!(bitmap.first_index(), Some(63));
  408. assert_eq!(bitmap.first_false_index(), Some(0));
  409. assert_eq!(bitmap.last_index(), Some(64));
  410. assert_eq!(bitmap.last_false_index(), Some(127));
  411. assert_eq!(bitmap.next_index(0), Some(63));
  412. assert_eq!(bitmap.next_index(63), Some(64));
  413. assert_eq!(bitmap.next_index(64), None);
  414. assert_eq!(bitmap.next_false_index(0), Some(1));
  415. assert_eq!(bitmap.next_false_index(63), Some(65));
  416. assert_eq!(bitmap.prev_index(64), Some(63));
  417. assert_eq!(bitmap.prev_false_index(64), Some(62));
  418. assert_eq!(bitmap.prev_index(63), None);
  419. assert_eq!(bitmap.prev_false_index(63), Some(62));
  420. assert_eq!(bitmap.prev_index(65), Some(64));
  421. assert_eq!(bitmap.is_full(), false);
  422. assert_eq!(bitmap.is_empty(), false);
  423. // 反转
  424. bitmap.invert();
  425. assert_eq!(bitmap.first_index(), Some(0));
  426. assert_eq!(bitmap.first_false_index(), Some(63));
  427. assert_eq!(bitmap.last_index(), Some(127));
  428. assert_eq!(bitmap.last_false_index(), Some(64));
  429. assert_eq!(bitmap.next_index(0), Some(1));
  430. assert_eq!(bitmap.next_false_index(0), Some(63));
  431. assert_eq!(bitmap.next_false_index(63), Some(64));
  432. assert_eq!(bitmap.next_index(63), Some(65));
  433. assert_eq!(bitmap.prev_false_index(127), Some(64));
  434. assert_eq!(bitmap.prev_index(127), Some(126));
  435. assert_eq!(bitmap.prev_false_index(64), Some(63));
  436. assert_eq!(bitmap.prev_index(64), Some(62));
  437. assert_eq!(bitmap.prev_index(63), Some(62));
  438. assert_eq!(bitmap.is_full(), false);
  439. assert_eq!(bitmap.is_empty(), false);
  440. }
  441. #[test]
  442. fn test_alloc_bitmap_full_32() {
  443. let mut bitmap = AllocBitmap::new(32);
  444. bitmap.set_all(true);
  445. assert_eq!(bitmap.len(), 32);
  446. assert_eq!(bitmap.size(), 8);
  447. assert_eq!(bitmap.first_index(), Some(0));
  448. assert_eq!(bitmap.first_false_index(), None);
  449. assert_eq!(bitmap.last_index(), Some(31));
  450. assert_eq!(bitmap.last_false_index(), None);
  451. assert_eq!(bitmap.next_index(0), Some(1));
  452. assert_eq!(bitmap.next_index(31), None);
  453. assert_eq!(bitmap.next_false_index(0), None);
  454. assert_eq!(bitmap.prev_index(31), Some(30));
  455. assert_eq!(bitmap.prev_false_index(31), None);
  456. assert_eq!(bitmap.prev_index(0), None);
  457. assert_eq!(bitmap.is_full(), true);
  458. assert_eq!(bitmap.is_empty(), false);
  459. // 反转
  460. bitmap.invert();
  461. assert_eq!(bitmap.first_index(), None);
  462. assert_eq!(bitmap.first_false_index(), Some(0));
  463. assert_eq!(bitmap.last_index(), None);
  464. assert_eq!(bitmap.last_false_index(), Some(31));
  465. assert_eq!(bitmap.next_index(0), None);
  466. assert_eq!(bitmap.next_false_index(0), Some(1));
  467. assert_eq!(bitmap.prev_index(31), None);
  468. assert_eq!(bitmap.prev_false_index(31), Some(30));
  469. assert_eq!(bitmap.prev_index(0), None);
  470. assert_eq!(bitmap.is_full(), false);
  471. assert_eq!(bitmap.is_empty(), true);
  472. }
  473. #[test]
  474. fn test_alloc_bitmap_full_64() {
  475. let mut bitmap = AllocBitmap::new(64);
  476. bitmap.set_all(true);
  477. assert_eq!(bitmap.len(), 64);
  478. assert_eq!(bitmap.size(), 8);
  479. assert_eq!(bitmap.first_index(), Some(0));
  480. assert_eq!(bitmap.first_false_index(), None);
  481. assert_eq!(bitmap.last_index(), Some(63));
  482. assert_eq!(bitmap.last_false_index(), None);
  483. assert_eq!(bitmap.next_index(0), Some(1));
  484. assert_eq!(bitmap.next_index(63), None);
  485. assert_eq!(bitmap.next_false_index(0), None);
  486. assert_eq!(bitmap.prev_index(63), Some(62));
  487. assert_eq!(bitmap.prev_false_index(63), None);
  488. assert_eq!(bitmap.prev_index(0), None);
  489. assert_eq!(bitmap.is_full(), true);
  490. assert_eq!(bitmap.is_empty(), false);
  491. // 反转
  492. bitmap.invert();
  493. assert_eq!(bitmap.first_index(), None);
  494. assert_eq!(bitmap.first_false_index(), Some(0));
  495. assert_eq!(bitmap.last_index(), None);
  496. assert_eq!(bitmap.last_false_index(), Some(63));
  497. assert_eq!(bitmap.next_index(0), None);
  498. assert_eq!(bitmap.next_false_index(0), Some(1));
  499. assert_eq!(bitmap.prev_index(63), None);
  500. assert_eq!(bitmap.prev_false_index(63), Some(62));
  501. assert_eq!(bitmap.prev_index(0), None);
  502. assert_eq!(bitmap.is_full(), false);
  503. assert_eq!(bitmap.is_empty(), true);
  504. }
  505. #[test]
  506. fn test_alloc_bitmap_full_100() {
  507. let mut bitmap = AllocBitmap::new(100);
  508. bitmap.set_all(true);
  509. assert_eq!(bitmap.len(), 100);
  510. assert_eq!(bitmap.size(), 16);
  511. assert_eq!(bitmap.first_index(), Some(0));
  512. assert_eq!(bitmap.first_false_index(), None);
  513. assert_eq!(bitmap.last_index(), Some(99));
  514. assert_eq!(bitmap.last_false_index(), None);
  515. assert_eq!(bitmap.next_index(0), Some(1));
  516. assert_eq!(bitmap.next_index(99), None);
  517. assert_eq!(bitmap.next_false_index(0), None);
  518. assert_eq!(bitmap.prev_index(99), Some(98));
  519. assert_eq!(bitmap.prev_false_index(99), None);
  520. assert_eq!(bitmap.prev_index(0), None);
  521. assert_eq!(bitmap.is_full(), true);
  522. assert_eq!(bitmap.is_empty(), false);
  523. // 反转
  524. bitmap.invert();
  525. assert_eq!(bitmap.first_index(), None);
  526. assert_eq!(bitmap.first_false_index(), Some(0));
  527. assert_eq!(bitmap.last_index(), None);
  528. assert_eq!(bitmap.last_false_index(), Some(99));
  529. assert_eq!(bitmap.next_index(0), None);
  530. assert_eq!(bitmap.next_false_index(0), Some(1));
  531. assert_eq!(bitmap.prev_index(99), None);
  532. assert_eq!(bitmap.prev_false_index(99), Some(98));
  533. assert_eq!(bitmap.prev_index(0), None);
  534. assert_eq!(bitmap.is_full(), false);
  535. assert_eq!(bitmap.is_empty(), true);
  536. }
  537. #[test]
  538. fn test_alloc_bitmap_full_128() {
  539. let mut bitmap = AllocBitmap::new(128);
  540. bitmap.set_all(true);
  541. assert_eq!(bitmap.len(), 128);
  542. assert_eq!(bitmap.size(), 16);
  543. assert_eq!(bitmap.first_index(), Some(0));
  544. assert_eq!(bitmap.first_false_index(), None);
  545. assert_eq!(bitmap.last_index(), Some(127));
  546. assert_eq!(bitmap.last_false_index(), None);
  547. assert_eq!(bitmap.next_index(0), Some(1));
  548. assert_eq!(bitmap.next_index(127), None);
  549. assert_eq!(bitmap.next_false_index(0), None);
  550. assert_eq!(bitmap.prev_index(127), Some(126));
  551. assert_eq!(bitmap.prev_false_index(127), None);
  552. assert_eq!(bitmap.prev_index(0), None);
  553. assert_eq!(bitmap.is_full(), true);
  554. assert_eq!(bitmap.is_empty(), false);
  555. // 反转
  556. bitmap.invert();
  557. assert_eq!(bitmap.first_index(), None);
  558. assert_eq!(bitmap.first_false_index(), Some(0));
  559. assert_eq!(bitmap.last_index(), None);
  560. assert_eq!(bitmap.last_false_index(), Some(127));
  561. assert_eq!(bitmap.next_index(0), None);
  562. assert_eq!(bitmap.next_false_index(0), Some(1));
  563. assert_eq!(bitmap.prev_index(127), None);
  564. assert_eq!(bitmap.prev_false_index(127), Some(126));
  565. assert_eq!(bitmap.prev_index(0), None);
  566. assert_eq!(bitmap.is_full(), false);
  567. assert_eq!(bitmap.is_empty(), true);
  568. }
  569. #[test]
  570. fn test_alloc_bitmap_bitand_128() {
  571. let mut bitmap = AllocBitmap::new(128);
  572. bitmap.set_all(true);
  573. let mut bitmap2 = AllocBitmap::new(128);
  574. bitmap2.set(0, true);
  575. bitmap2.set(1, true);
  576. bitmap2.set(67, true);
  577. let bitmap3 = bitmap & bitmap2;
  578. assert_eq!(bitmap3.len(), 128);
  579. assert_eq!(bitmap3.size(), 16);
  580. assert_eq!(bitmap3.first_index(), Some(0));
  581. assert_eq!(bitmap3.first_false_index(), Some(2));
  582. assert_eq!(bitmap3.last_index(), Some(67));
  583. }
  584. #[test]
  585. fn test_alloc_bitmap_bitand_assign_128() {
  586. let mut bitmap = AllocBitmap::new(128);
  587. bitmap.set_all(true);
  588. let mut bitmap2 = AllocBitmap::new(128);
  589. bitmap2.set(0, true);
  590. bitmap2.set(1, true);
  591. bitmap2.set(67, true);
  592. bitmap.bitand_assign(&bitmap2);
  593. assert_eq!(bitmap.len(), 128);
  594. assert_eq!(bitmap.size(), 16);
  595. assert_eq!(bitmap.first_index(), Some(0));
  596. assert_eq!(bitmap.first_false_index(), Some(2));
  597. assert_eq!(bitmap.last_index(), Some(67));
  598. }