Skip to content

Commit 42b8e98

Browse files
authored
Add tests for slice_as_uninit_mut in common.rs (#432)
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<u8>` 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.
1 parent 97a5bfd commit 42b8e98

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/common.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,45 @@ macro_rules! impl_default_new {
9696
}
9797
};
9898
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
104+
#[test]
105+
fn test_bsr32() {
106+
assert_eq!(bsr32(1), 0);
107+
assert_eq!(bsr32(2), 1);
108+
assert_eq!(bsr32(3), 1);
109+
assert_eq!(bsr32(4), 2);
110+
assert_eq!(bsr32(7), 2);
111+
assert_eq!(bsr32(8), 3);
112+
assert_eq!(bsr32(15), 3);
113+
assert_eq!(bsr32(16), 4);
114+
assert_eq!(bsr32(0x80000000), 31);
115+
assert_eq!(bsr32(0xFFFFFFFF), 31);
116+
}
117+
118+
#[test]
119+
fn test_slice_as_uninit_mut() {
120+
let mut data = [1u8, 2, 3, 4, 5];
121+
let uninit_slice = slice_as_uninit_mut(&mut data);
122+
123+
assert_eq!(uninit_slice.len(), 5);
124+
125+
// Mutate through the uninit slice
126+
uninit_slice[0].write(10);
127+
uninit_slice[4].write(50);
128+
129+
assert_eq!(data[0], 10);
130+
assert_eq!(data[1], 2);
131+
assert_eq!(data[4], 50);
132+
}
133+
134+
#[test]
135+
fn test_slice_as_uninit_mut_empty() {
136+
let mut data: [u8; 0] = [];
137+
let uninit_slice = slice_as_uninit_mut(&mut data);
138+
assert_eq!(uninit_slice.len(), 0);
139+
}
140+
}

0 commit comments

Comments
 (0)