Skip to content

Commit

Permalink
[eclipse-iceoryx#224] Add tests for placement_new; remove PlacementNe…
Browse files Browse the repository at this point in the history
…w for semantic string since it breaks the contract
  • Loading branch information
elfenpiff committed Jun 5, 2024
1 parent f7441c0 commit 220b2b6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
7 changes: 0 additions & 7 deletions iceoryx2-bb/container/src/semantic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions iceoryx2-bb/container/tests/byte_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<Sut>();
let memory = unsafe { alloc(layout) } as *mut Sut;
for i in 0..size_of::<Sut>() {
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) };
}
}
30 changes: 29 additions & 1 deletion iceoryx2-bb/container/tests/queue_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<usize, SUT_CAPACITY>;
let layout = Layout::new::<Sut>();
let memory = unsafe { alloc(layout) } as *mut Sut;
for i in 0..size_of::<Sut>() {
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) };
}
}
25 changes: 25 additions & 0 deletions iceoryx2-bb/container/tests/vec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<usize, SUT_CAPACITY>;
let layout = Layout::new::<Sut>();
let memory = unsafe { alloc(layout) } as *mut Sut;
for i in 0..size_of::<Sut>() {
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) };
}

0 comments on commit 220b2b6

Please sign in to comment.