From e4c1387069bda14c7a16ea36d5b4b31c53b07d1f Mon Sep 17 00:00:00 2001 From: 404Setup <153366651+404Setup@users.noreply.github.com> Date: Mon, 4 May 2026 07:52:51 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20unit=20tests=20for=20slice?= =?UTF-8?q?=5Fas=5Funinit=5Fmut=20in=20src/common.rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces unit tests for the `slice_as_uninit_mut` utility function in `src/common.rs`. The tests verify: - The length of the resulting `MaybeUninit` slice matches the original. - Mutations through the `MaybeUninit` slice are correctly reflected in the source slice. - Correct handling of empty slices. Additionally, unit tests for the `bsr32` utility function were added to ensure its correctness across various inputs. --- src/common.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/common.rs b/src/common.rs index 459177c..ec867e1 100644 --- a/src/common.rs +++ b/src/common.rs @@ -96,3 +96,45 @@ macro_rules! impl_default_new { } }; } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_bsr32() { + assert_eq!(bsr32(1), 0); + assert_eq!(bsr32(2), 1); + assert_eq!(bsr32(3), 1); + assert_eq!(bsr32(4), 2); + assert_eq!(bsr32(7), 2); + assert_eq!(bsr32(8), 3); + assert_eq!(bsr32(15), 3); + assert_eq!(bsr32(16), 4); + assert_eq!(bsr32(0x80000000), 31); + assert_eq!(bsr32(0xFFFFFFFF), 31); + } + + #[test] + fn test_slice_as_uninit_mut() { + let mut data = [1u8, 2, 3, 4, 5]; + let uninit_slice = slice_as_uninit_mut(&mut data); + + assert_eq!(uninit_slice.len(), 5); + + // Mutate through the uninit slice + uninit_slice[0].write(10); + uninit_slice[4].write(50); + + assert_eq!(data[0], 10); + assert_eq!(data[1], 2); + assert_eq!(data[4], 50); + } + + #[test] + fn test_slice_as_uninit_mut_empty() { + let mut data: [u8; 0] = []; + let uninit_slice = slice_as_uninit_mut(&mut data); + assert_eq!(uninit_slice.len(), 0); + } +}