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