Skip to content

Commit

Permalink
Add some unit tests for split_init. (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Oct 17, 2024
1 parent 8c6c871 commit 4739956
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,52 @@ pub(super) unsafe fn split_init(
let uninit = slice::from_raw_parts_mut(buf_ptr.add(init_len), uninit_len);
(init, uninit)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_split_init() {
let mut input_array = [
MaybeUninit::new(0_u8),
MaybeUninit::new(1_u8),
MaybeUninit::new(2_u8),
MaybeUninit::new(3_u8),
];
let input_array_clone = input_array.clone();
let input_array_ptr = input_array.as_ptr();
let output_array = [0_u8, 1_u8, 2_u8, 3_u8];

unsafe {
let (init, uninit) = split_init(&mut input_array, 0);
assert_eq!(init, &[]);
assert_eq!(uninit.len(), input_array_clone.len());
assert_eq!(uninit.as_ptr(), input_array_ptr);

let (init, uninit) = split_init(&mut input_array, input_array_clone.len());
assert_eq!(init, &output_array[..]);
assert_eq!(init.as_ptr(), input_array_ptr.cast());
assert_eq!(uninit.len(), 0);
assert_eq!(
uninit.as_ptr(),
input_array_ptr.add(input_array_clone.len())
);

let (init, uninit) = split_init(&mut input_array, 2);
assert_eq!(init, &output_array[..2]);
assert_eq!(init.as_ptr(), input_array_ptr.cast());
assert_eq!(uninit.len(), 2);
assert_eq!(uninit.as_ptr(), input_array_ptr.add(2));
}
}

#[test]
fn test_split_init_empty() {
unsafe {
let (init, uninit) = split_init(&mut [], 0);
assert!(init.is_empty());
assert!(uninit.is_empty());
}
}
}

0 comments on commit 4739956

Please sign in to comment.