#[cfg(CONFIG_RUST_BITMAP_KUNIT_TEST)]#[macros::kunit_tests(rust_kernel_bitmap)]mod tests { use super::*; use kernel::alloc::flags::GFP_KERNEL; #[test] fn bitmap_borrow() { let fake_bitmap: [usize; 2] = [0, 0]; let fake_bitmap_len = 2 * usize::BITS as usize; // SAFETY: `fake_c_bitmap` is an array of expected length. let b = unsafe { Bitmap::from_raw(fake_bitmap.as_ptr(), fake_bitmap_len) }; assert_eq!(fake_bitmap_len, b.len()); assert_eq!(None, b.next_bit(0)); } #[test] fn bitmap_copy() { let fake_bitmap: usize = 0xFF; // SAFETY: `fake_c_bitmap` can be used as one-element array of expected length. let b = unsafe { Bitmap::from_raw(core::ptr::addr_of!(fake_bitmap), 8) }; assert_eq!(8, b.len()); assert_eq!(None, b.next_zero_bit(0)); } #[test] fn bitmap_vec_new() -> Result<(), AllocError> { let b = BitmapVec::new(0, GFP_KERNEL)?; assert_eq!(0, b.len()); let b = BitmapVec::new(3, GFP_KERNEL)?; assert_eq!(3, b.len()); let b = BitmapVec::new(1024, GFP_KERNEL)?; assert_eq!(1024, b.len()); // Requesting too large values results in [`AllocError`]. let res = BitmapVec::new(1 << 31, GFP_KERNEL); assert!(res.is_err()); Ok(()) } #[test] fn bitmap_set_clear_find() -> Result<(), AllocError> { let mut b = BitmapVec::new(128, GFP_KERNEL)?; // Zero-initialized assert_eq!(None, b.next_bit(0)); assert_eq!(Some(0), b.next_zero_bit(0)); assert_eq!(None, b.last_bit()); b.set_bit(17); assert_eq!(Some(17), b.next_bit(0)); assert_eq!(Some(17), b.next_bit(17)); assert_eq!(None, b.next_bit(18)); assert_eq!(Some(17), b.last_bit()); b.set_bit(107); assert_eq!(Some(17), b.next_bit(0)); assert_eq!(Some(17), b.next_bit(17)); assert_eq!(Some(107), b.next_bit(18)); assert_eq!(Some(107), b.last_bit()); b.clear_bit(17); assert_eq!(Some(107), b.next_bit(0)); assert_eq!(Some(107), b.last_bit()); Ok(()) } #[test] fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> { // TODO: Kunit #[test]s do not support `cfg` yet, // so we add it here in the body. #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))] { let mut b = BitmapVec::new(128, GFP_KERNEL)?; b.set_bit(2048); b.set_bit_atomic(2048); b.clear_bit(2048); b.clear_bit_atomic(2048); assert_eq!(None, b.next_bit(2048)); assert_eq!(None, b.next_zero_bit(2048)); assert_eq!(None, b.last_bit()); } Ok(()) } // TODO: uncomment once kunit supports [should_panic] and `cfg`. // #[cfg(CONFIG_RUST_BITMAP_HARDENED)] // #[test] // #[should_panic] // fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> { // let mut b = BitmapVec::new(128, GFP_KERNEL)?; // // b.set_bit(2048); // } #[test] fn bitmap_copy_and_extend() -> Result<(), AllocError> { let mut long_bitmap = BitmapVec::new(256, GFP_KERNEL)?; long_bitmap.set_bit(3); long_bitmap.set_bit(200); let mut short_bitmap = BitmapVec::new(32, GFP_KERNEL)?; short_bitmap.set_bit(17); long_bitmap.copy_and_extend(&short_bitmap); // Previous bits have been cleared. assert_eq!(Some(17), long_bitmap.next_bit(0)); assert_eq!(Some(17), long_bitmap.last_bit()); Ok(()) }}Lines 501–620 of 620. Previous lines
#[cfg(CONFIG_RUST_BITMAP_KUNIT_TEST)] #[macros::kunit_tests(rust_kernel_bitmap)] mod tests { use super::*; use kernel::alloc::flags::GFP_KERNEL; #[test] fn bitmap_borrow() { let fake_bitmap: [usize; 2] = [0, 0]; let fake_bitmap_len = 2 * usize::BITS as usize; // SAFETY: `fake_c_bitmap` is an array of expected length. let b = unsafe { Bitmap::from_raw(fake_bitmap.as_ptr(), fake_bitmap_len) }; assert_eq!(fake_bitmap_len, b.len()); assert_eq!(None, b.next_bit(0)); } #[test] fn bitmap_copy() { let fake_bitmap: usize = 0xFF; // SAFETY: `fake_c_bitmap` can be used as one-element array of expected length. let b = unsafe { Bitmap::from_raw(core::ptr::addr_of!(fake_bitmap), 8) }; assert_eq!(8, b.len()); assert_eq!(None, b.next_zero_bit(0)); } #[test] fn bitmap_vec_new() -> Result<(), AllocError> { let b = BitmapVec::new(0, GFP_KERNEL)?; assert_eq!(0, b.len()); let b = BitmapVec::new(3, GFP_KERNEL)?; assert_eq!(3, b.len()); let b = BitmapVec::new(1024, GFP_KERNEL)?; assert_eq!(1024, b.len()); // Requesting too large values results in [`AllocError`]. let res = BitmapVec::new(1 << 31, GFP_KERNEL); assert!(res.is_err()); Ok(()) } #[test] fn bitmap_set_clear_find() -> Result<(), AllocError> { let mut b = BitmapVec::new(128, GFP_KERNEL)?; // Zero-initialized assert_eq!(None, b.next_bit(0)); assert_eq!(Some(0), b.next_zero_bit(0)); assert_eq!(None, b.last_bit()); b.set_bit(17); assert_eq!(Some(17), b.next_bit(0)); assert_eq!(Some(17), b.next_bit(17)); assert_eq!(None, b.next_bit(18)); assert_eq!(Some(17), b.last_bit()); b.set_bit(107); assert_eq!(Some(17), b.next_bit(0)); assert_eq!(Some(17), b.next_bit(17)); assert_eq!(Some(107), b.next_bit(18)); assert_eq!(Some(107), b.last_bit()); b.clear_bit(17); assert_eq!(Some(107), b.next_bit(0)); assert_eq!(Some(107), b.last_bit()); Ok(()) } #[test] fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> { // TODO: Kunit #[test]s do not support `cfg` yet, // so we add it here in the body. #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))] { let mut b = BitmapVec::new(128, GFP_KERNEL)?; b.set_bit(2048); b.set_bit_atomic(2048); b.clear_bit(2048); b.clear_bit_atomic(2048); assert_eq!(None, b.next_bit(2048)); assert_eq!(None, b.next_zero_bit(2048)); assert_eq!(None, b.last_bit()); } Ok(()) } // TODO: uncomment once kunit supports [should_panic] and `cfg`. // #[cfg(CONFIG_RUST_BITMAP_HARDENED)] // #[test] // #[should_panic] // fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> { // let mut b = BitmapVec::new(128, GFP_KERNEL)?; // // b.set_bit(2048); // } #[test] fn bitmap_copy_and_extend() -> Result<(), AllocError> { let mut long_bitmap = BitmapVec::new(256, GFP_KERNEL)?; long_bitmap.set_bit(3); long_bitmap.set_bit(200); let mut short_bitmap = BitmapVec::new(32, GFP_KERNEL)?; short_bitmap.set_bit(17); long_bitmap.copy_and_extend(&short_bitmap); // Previous bits have been cleared. assert_eq!(Some(17), long_bitmap.next_bit(0)); assert_eq!(Some(17), long_bitmap.last_bit()); Ok(()) } }