From 13d4ff167880c5c46f9373c7e75b69cb68da42d8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 27 Aug 2024 19:24:43 +0200 Subject: test: add push function unit tests --- src/lib.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 228b7e1..8bfcd29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -518,16 +518,95 @@ mod tests } #[test] - fn works() + fn single_push_works() { let mut multi_vec = MultiVec::::new(); multi_vec.push(Foo { num_a: 123, num_b: 654 }); - multi_vec.push(Foo { num_a: 12338, num_b: 191 }); + assert_eq!(multi_vec.capacity, 1); + assert_eq!(multi_vec.length, 1); - let item = multi_vec.get::(1).unwrap(); + assert_eq!(multi_vec.field_arr_byte_offsets, [0, size_of::()]); - println!("yay: {}", *item); + assert_eq!( + unsafe { + std::slice::from_raw_parts::(multi_vec.ptr.as_ptr().cast(), 1) + }, + [123] + ); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::( + multi_vec.ptr.as_ptr().byte_add(size_of::()).cast(), + 1, + ) + }, + [654] + ); + } + + #[test] + fn multiple_pushes_works() + { + let mut multi_vec = MultiVec::::new(); + + multi_vec.push(Foo { num_a: u32::MAX / 2, num_b: 654 }); + multi_vec.push(Foo { num_a: 765, num_b: u16::MAX / 3 }); + multi_vec.push(Foo { num_a: u32::MAX / 5, num_b: 337 }); + + assert_eq!(multi_vec.capacity, 4); + assert_eq!(multi_vec.length, 3); + + assert_eq!(multi_vec.field_arr_byte_offsets, [0, size_of::() * 4]); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::(multi_vec.ptr.as_ptr().cast(), 3) + }, + [u32::MAX / 2, 765, u32::MAX / 5] + ); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::( + multi_vec.ptr.as_ptr().byte_add(size_of::() * 4).cast(), + 3, + ) + }, + [654, u16::MAX / 3, 337] + ); + } + + #[test] + fn multiple_pushes_in_preallocated_works() + { + let mut multi_vec = MultiVec::::with_capacity(2); + + multi_vec.push(Foo { num_a: 83710000, num_b: 654 }); + multi_vec.push(Foo { num_a: 765, num_b: u16::MAX / 7 }); + + assert_eq!(multi_vec.capacity, 2); + assert_eq!(multi_vec.length, 2); + + assert_eq!(multi_vec.field_arr_byte_offsets, [0, size_of::() * 2]); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::(multi_vec.ptr.as_ptr().cast(), 2) + }, + [83710000, 765] + ); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::( + multi_vec.ptr.as_ptr().byte_add(size_of::() * 2).cast(), + 2, + ) + }, + [654, u16::MAX / 7] + ); } } -- cgit v1.2.3-18-g5258