From 00541a9c9e07e8f672912d6c0f3d4501b1aba8ed Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 27 Aug 2024 19:45:07 +0200 Subject: test: add unit test for get function --- src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src') 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::::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::() * 3]; + multi_vec.length = 3; + multi_vec.capacity = 3; + + assert_eq!( + multi_vec.get::(0).copied(), + Some(u32::MAX - 3000) + ); + assert_eq!(multi_vec.get::(0).copied(), Some(20210)); + + assert_eq!(multi_vec.get::(1).copied(), Some(901)); + assert_eq!(multi_vec.get::(1).copied(), Some(7120)); + + assert_eq!(multi_vec.get::(2).copied(), Some(5560000)); + assert_eq!(multi_vec.get::(2).copied(), Some(1010)); + } } -- cgit v1.2.3-18-g5258