From 220b2b6b8063ba05bb49809c59f3cff94b01769e Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Wed, 5 Jun 2024 19:46:18 +0200 Subject: [PATCH] [#224] Add tests for placement_new; remove PlacementNew for semantic string since it breaks the contract --- iceoryx2-bb/container/src/semantic_string.rs | 7 ----- .../container/tests/byte_string_tests.rs | 20 +++++++++++++ iceoryx2-bb/container/tests/queue_tests.rs | 30 ++++++++++++++++++- iceoryx2-bb/container/tests/vec_tests.rs | 25 ++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/iceoryx2-bb/container/src/semantic_string.rs b/iceoryx2-bb/container/src/semantic_string.rs index f0a521bb3..87df5a91d 100644 --- a/iceoryx2-bb/container/src/semantic_string.rs +++ b/iceoryx2-bb/container/src/semantic_string.rs @@ -379,13 +379,6 @@ macro_rules! semantic_string { value: iceoryx2_bb_container::byte_string::FixedSizeByteString<$capacity> } - impl iceoryx2_bb_elementary::placement_default::PlacementDefault for $string_name { - unsafe fn placement_default(ptr: *mut Self) { - let ptr = core::ptr::addr_of_mut!((&mut *ptr).value); - iceoryx2_bb_elementary::placement_default::PlacementDefault::placement_default(ptr) - } - } - impl iceoryx2_bb_container::semantic_string::SemanticString<$capacity> for $string_name { fn as_string(&self) -> &iceoryx2_bb_container::byte_string::FixedSizeByteString<$capacity> { &self.value diff --git a/iceoryx2-bb/container/tests/byte_string_tests.rs b/iceoryx2-bb/container/tests/byte_string_tests.rs index 12329f747..82c78fb77 100644 --- a/iceoryx2-bb/container/tests/byte_string_tests.rs +++ b/iceoryx2-bb/container/tests/byte_string_tests.rs @@ -12,11 +12,14 @@ mod fixed_size_byte_string { use std::{ + alloc::{alloc, dealloc, Layout}, hash::{Hash, Hasher}, + mem::size_of, ops::DerefMut, }; use iceoryx2_bb_container::byte_string::*; + use iceoryx2_bb_elementary::placement_default::PlacementDefault; use iceoryx2_bb_testing::assert_that; use std::collections::hash_map::DefaultHasher; @@ -500,4 +503,21 @@ mod fixed_size_byte_string { let mut sut = Sut::from_bytes_truncated(b"Who did eat the last unicorn?"); sut.remove_range(48, 12); } + + #[test] + fn placement_default_works() { + let layout = Layout::new::(); + let memory = unsafe { alloc(layout) } as *mut Sut; + for i in 0..size_of::() { + unsafe { (memory as *mut u8).add(i).write(0xff) }; + } + unsafe { Sut::placement_default(memory) }; + let sut = unsafe { &mut (*memory) }; + assert_that!(sut, len 0); + + assert_that!(sut.push_bytes(b"hello"), is_ok); + assert_that!(sut.as_bytes(), eq b"hello"); + + unsafe { dealloc(memory.cast(), layout) }; + } } diff --git a/iceoryx2-bb/container/tests/queue_tests.rs b/iceoryx2-bb/container/tests/queue_tests.rs index 2c12a15d7..00843c9dd 100644 --- a/iceoryx2-bb/container/tests/queue_tests.rs +++ b/iceoryx2-bb/container/tests/queue_tests.rs @@ -11,8 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT mod queue { + use std::{ + alloc::{alloc, dealloc, Layout}, + mem::size_of, + }; + use iceoryx2_bb_container::queue::*; - use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; + use iceoryx2_bb_elementary::{ + bump_allocator::BumpAllocator, placement_default::PlacementDefault, + }; use iceoryx2_bb_testing::{assert_that, lifetime_tracker::LifetimeTracker}; const SUT_CAPACITY: usize = 128; @@ -291,4 +298,25 @@ mod queue { assert_that!(unsafe { sut.get_unchecked(i) }, eq i * 3 + 2); } } + + #[test] + fn placement_default_works() { + type Sut = FixedSizeQueue; + let layout = Layout::new::(); + let memory = unsafe { alloc(layout) } as *mut Sut; + for i in 0..size_of::() { + unsafe { (memory as *mut u8).add(i).write(0xff) }; + } + unsafe { Sut::placement_default(memory) }; + + let sut = unsafe { &mut (*memory) }; + assert_that!(sut, len 0); + assert_that!(sut.push(123), eq true); + assert_that!(sut.push(456), eq true); + + assert_that!(sut.pop(), eq Some(123)); + assert_that!(sut.pop(), eq Some(456)); + + unsafe { dealloc(memory.cast(), layout) }; + } } diff --git a/iceoryx2-bb/container/tests/vec_tests.rs b/iceoryx2-bb/container/tests/vec_tests.rs index c0324d231..0a86db7f5 100644 --- a/iceoryx2-bb/container/tests/vec_tests.rs +++ b/iceoryx2-bb/container/tests/vec_tests.rs @@ -10,8 +10,12 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT +use std::alloc::{alloc, dealloc, Layout}; +use std::mem::size_of; + use iceoryx2_bb_container::vec::*; use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; +use iceoryx2_bb_elementary::placement_default::PlacementDefault; use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer; use iceoryx2_bb_testing::assert_that; use iceoryx2_bb_testing::lifetime_tracker::LifetimeTracker; @@ -243,3 +247,24 @@ fn fixed_size_vec_pop_releases_ownership() { assert_that!(LifetimeTracker::number_of_living_instances(), eq i); } } + +#[test] +fn placement_default_works() { + type Sut = FixedSizeVec; + let layout = Layout::new::(); + let memory = unsafe { alloc(layout) } as *mut Sut; + for i in 0..size_of::() { + unsafe { (memory as *mut u8).add(i).write(0xff) }; + } + unsafe { Sut::placement_default(memory) }; + + let sut = unsafe { &mut (*memory) }; + assert_that!(sut, len 0); + assert_that!(sut.push(123), eq true); + assert_that!(sut.push(456), eq true); + + assert_that!(sut.pop(), eq Some(456)); + assert_that!(sut.pop(), eq Some(123)); + + unsafe { dealloc(memory.cast(), layout) }; +}