diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-27 19:24:43 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-27 19:24:43 +0200 |
commit | 13d4ff167880c5c46f9373c7e75b69cb68da42d8 (patch) | |
tree | e0eae70baca346ce58811f442c6c80f28363f531 | |
parent | edb9ff116d35927330977588606edff9ce6642bb (diff) |
test: add push function unit tests
-rw-r--r-- | src/lib.rs | 87 |
1 files changed, 83 insertions, 4 deletions
@@ -518,16 +518,95 @@ mod tests } #[test] - fn works() + fn single_push_works() { let mut multi_vec = MultiVec::<Foo>::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::<FooFieldNumA>(1).unwrap(); + assert_eq!(multi_vec.field_arr_byte_offsets, [0, size_of::<u32>()]); - println!("yay: {}", *item); + assert_eq!( + unsafe { + std::slice::from_raw_parts::<u32>(multi_vec.ptr.as_ptr().cast(), 1) + }, + [123] + ); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::<u16>( + multi_vec.ptr.as_ptr().byte_add(size_of::<u32>()).cast(), + 1, + ) + }, + [654] + ); + } + + #[test] + fn multiple_pushes_works() + { + let mut multi_vec = MultiVec::<Foo>::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::<u32>() * 4]); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::<u32>(multi_vec.ptr.as_ptr().cast(), 3) + }, + [u32::MAX / 2, 765, u32::MAX / 5] + ); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::<u16>( + multi_vec.ptr.as_ptr().byte_add(size_of::<u32>() * 4).cast(), + 3, + ) + }, + [654, u16::MAX / 3, 337] + ); + } + + #[test] + fn multiple_pushes_in_preallocated_works() + { + let mut multi_vec = MultiVec::<Foo>::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::<u32>() * 2]); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::<u32>(multi_vec.ptr.as_ptr().cast(), 2) + }, + [83710000, 765] + ); + + assert_eq!( + unsafe { + std::slice::from_raw_parts::<u16>( + multi_vec.ptr.as_ptr().byte_add(size_of::<u32>() * 2).cast(), + 2, + ) + }, + [654, u16::MAX / 7] + ); } } |