summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-08-27 19:45:07 +0200
committerHampusM <hampus@hampusmat.com>2024-08-27 19:45:07 +0200
commit00541a9c9e07e8f672912d6c0f3d4501b1aba8ed (patch)
tree6a8c63c21349e0f708e3ee1bbb14eac6f66c2cda /src
parent9d6a63f7dba402a95243d9d0c4b20b45becfdaed (diff)
test: add unit test for get function
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index b90baa5..0a8333a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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));
+ }
}