diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-27 19:45:07 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-27 19:45:07 +0200 |
commit | 00541a9c9e07e8f672912d6c0f3d4501b1aba8ed (patch) | |
tree | 6a8c63c21349e0f708e3ee1bbb14eac6f66c2cda | |
parent | 9d6a63f7dba402a95243d9d0c4b20b45becfdaed (diff) |
test: add unit test for get function
-rw-r--r-- | src/lib.rs | 38 |
1 files changed, 37 insertions, 1 deletions
@@ -446,7 +446,7 @@ struct CoolLayoutError; mod tests { use std::mem::offset_of; - use std::ptr::drop_in_place; + use std::ptr::{drop_in_place, NonNull}; use crate::{Item, ItemFieldMetadata, ItemFieldSelection, MultiVec}; @@ -609,4 +609,40 @@ mod tests [654, u16::MAX / 7] ); } + + #[test] + fn get_works() + { + let mut multi_vec = MultiVec::<Foo>::new(); + + #[repr(packed)] + #[allow(dead_code)] + struct Data + { + num_a: [u32; 3], + num_b: [u16; 3], + } + + let data = Data { + num_a: [u32::MAX - 3000, 901, 5560000], + num_b: [20210, 7120, 1010], + }; + + multi_vec.ptr = NonNull::from(&data).cast(); + multi_vec.field_arr_byte_offsets = vec![0, size_of::<u32>() * 3]; + multi_vec.length = 3; + multi_vec.capacity = 3; + + assert_eq!( + multi_vec.get::<FooFieldNumA>(0).copied(), + Some(u32::MAX - 3000) + ); + assert_eq!(multi_vec.get::<FooFieldNumB>(0).copied(), Some(20210)); + + assert_eq!(multi_vec.get::<FooFieldNumA>(1).copied(), Some(901)); + assert_eq!(multi_vec.get::<FooFieldNumB>(1).copied(), Some(7120)); + + assert_eq!(multi_vec.get::<FooFieldNumA>(2).copied(), Some(5560000)); + assert_eq!(multi_vec.get::<FooFieldNumB>(2).copied(), Some(1010)); + } } |