From 59713a9d88217119acc378f9f9dd3a194ddafa49 Mon Sep 17 00:00:00 2001 From: Tin Svagelj Date: Sun, 17 Sep 2023 16:14:56 +0200 Subject: [PATCH] Add resize_automatically test Signed-off-by: Tin Svagelj --- src/lib.rs | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index eb38a82..fe235fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -551,6 +551,8 @@ pub type UnsafeContiguousMemory = ContiguousMemoryStorage; #[cfg(all(test, not(feature = "no_std")))] mod test { + use core::mem::align_of; + use super::*; #[derive(Debug, Clone, PartialEq, Eq)] @@ -652,26 +654,28 @@ mod test { #[test] fn resize_automatically() { - let mut memory = ContiguousMemory::new(144); - - memory.push(Person { - name: "Jacky".to_string(), - last_name: "Larsson".to_string(), - }); - memory.push(Person { - name: "Jacky".to_string(), - last_name: "Larsson".to_string(), - }); - memory.push(Person { - name: "Jacky".to_string(), - last_name: "Larsson".to_string(), - }); - assert_eq!(memory.can_push::().unwrap(), false); - - assert_eq!(memory.get_capacity(), 288); + let mut memory = ContiguousMemory::new_aligned(12, align_of::()).unwrap(); + + { + let _a = memory.push(1u32); + let _b = memory.push(2u32); + let _c = memory.push(3u32); + assert_eq!(memory.can_push::().unwrap(), false); + let _d = memory.push(4u32); + assert_eq!(memory.get_capacity(), 24); + } - // 144 - //192 + memory.resize(4).expect("can't shrink empty storage"); + { + let _a = memory.push(1u16); + let _b = memory.push(2u16); + assert_eq!(memory.can_push::().unwrap(), false); + let _c = memory.push(3u64); + // expecting 12, but due to alignment we're skipping two u16 slots + // and then double the size as remaining (aligned) 4 bytes aren't + // enough for u64 + assert_eq!(memory.get_capacity(), 24); + } } #[test]