Skip to content

Commit

Permalink
fix tests to be platform-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowhatter committed Jul 24, 2024
1 parent 4c8575d commit 1cbf809
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
22 changes: 11 additions & 11 deletions commons/zenoh-shm/tests/posix_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,43 @@ impl TestElem {
}

fn validate_array<ElemIndex>(
array1: &mut ArrayInSHM<TestSegmentID, TestElem, ElemIndex>,
array2: &ArrayInSHM<TestSegmentID, TestElem, ElemIndex>,
created_array: &mut ArrayInSHM<TestSegmentID, TestElem, ElemIndex>,
opened_array: &ArrayInSHM<TestSegmentID, TestElem, ElemIndex>,
expected_elem_count: usize,
) where
ElemIndex: Unsigned + PrimInt + 'static + AsPrimitive<usize>,
isize: AsPrimitive<ElemIndex>,
usize: AsPrimitive<ElemIndex>,
{
assert!(array1.elem_count() == expected_elem_count);
assert!(array2.elem_count() == expected_elem_count);
assert!(created_array.elem_count() == expected_elem_count);
assert!(opened_array.elem_count() >= expected_elem_count);

let mut fill_ctr = 0;
let mut validate_ctr = 0;

// first of all, fill and validate elements sequentially
for i in 0..array1.elem_count() {
for i in 0..expected_elem_count {
unsafe {
let elem1 = &mut *array1.elem_mut(i.as_());
let elem2 = &*array2.elem(i.as_());
let elem1 = &mut *created_array.elem_mut(i.as_());
let elem2 = &*opened_array.elem(i.as_());

elem1.fill(&mut fill_ctr);
elem2.validate(&mut validate_ctr);
}
}

// then fill all the elements...
for i in 0..array1.elem_count() {
for i in 0..expected_elem_count {
unsafe {
let elem1 = &mut *array1.elem_mut(i.as_());
let elem1 = &mut *created_array.elem_mut(i.as_());
elem1.fill(&mut fill_ctr);
}
}

// ...and validate all the elements
for i in 0..array2.elem_count() {
for i in 0..expected_elem_count {
unsafe {
let elem2 = &*array2.elem(i.as_());
let elem2 = &*opened_array.elem(i.as_());
elem2.validate(&mut validate_ctr);
}
}
Expand Down
40 changes: 22 additions & 18 deletions commons/zenoh-shm/tests/posix_segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ use zenoh_shm::posix_shm::segment::Segment;
pub mod common;
use common::{validate_memory, TEST_SEGMENT_PREFIX};

fn validate_segment<ID>(segment1: &Segment<ID>, segment2: &Segment<ID>)
where
fn validate_segment<ID>(
created_segment: &Segment<ID>,
opened_segment: &Segment<ID>,
expected_elem_count: usize,
) where
rand::distributions::Standard: rand::distributions::Distribution<ID>,
ID: Clone + Display,
{
assert!(segment1.len() == segment2.len());
assert!(created_segment.len() == expected_elem_count);
assert!(opened_segment.len() >= expected_elem_count);

let ptr1 = segment1.as_ptr();
let ptr2 = segment2.as_ptr();
let ptr1 = created_segment.as_ptr();
let ptr2 = opened_segment.as_ptr();

let slice1 = unsafe { slice::from_raw_parts_mut(ptr1, segment1.len()) };
let slice2 = unsafe { slice::from_raw_parts(ptr2, segment2.len()) };
let slice1 = unsafe { slice::from_raw_parts_mut(ptr1, expected_elem_count) };
let slice2 = unsafe { slice::from_raw_parts(ptr2, expected_elem_count) };

validate_memory(slice1, slice2);
}
Expand All @@ -40,22 +44,22 @@ where
rand::distributions::Standard: rand::distributions::Distribution<ID>,
ID: Copy + Clone + Display,
{
let new_segment: Segment<ID> =
let created_segment: Segment<ID> =
Segment::create(900, TEST_SEGMENT_PREFIX).expect("error creating new segment");

let opened_segment_instance_1 = Segment::open(new_segment.id(), TEST_SEGMENT_PREFIX)
let opened_segment_instance_1 = Segment::open(created_segment.id(), TEST_SEGMENT_PREFIX)
.expect("error opening existing segment!");

validate_segment(&new_segment, &opened_segment_instance_1);
validate_segment(&created_segment, &opened_segment_instance_1);

let opened_segment_instance_2 = Segment::open(new_segment.id(), TEST_SEGMENT_PREFIX)
let opened_segment_instance_2 = Segment::open(created_segment.id(), TEST_SEGMENT_PREFIX)
.expect("error opening existing segment!");

validate_segment(&new_segment, &opened_segment_instance_1);
validate_segment(&new_segment, &opened_segment_instance_2);
validate_segment(&created_segment, &opened_segment_instance_1);
validate_segment(&created_segment, &opened_segment_instance_2);

drop(opened_segment_instance_1);
validate_segment(&new_segment, &opened_segment_instance_2);
validate_segment(&created_segment, &opened_segment_instance_2);
}

/// UNSIGNED ///
Expand Down Expand Up @@ -116,19 +120,19 @@ fn segment_i128_id() {
#[test]
fn segment_open() {
let new_segment: Segment<u8> =
let created_segment: Segment<u8> =
Segment::create(900, TEST_SEGMENT_PREFIX).expect("error creating new segment");

let _opened_segment = Segment::open(new_segment.id(), TEST_SEGMENT_PREFIX)
let _opened_segment = Segment::open(created_segment.id(), TEST_SEGMENT_PREFIX)
.expect("error opening existing segment!");
}

#[test]
fn segment_open_error() {
let id = {
let new_segment: Segment<u8> =
let created_segment: Segment<u8> =
Segment::create(900, TEST_SEGMENT_PREFIX).expect("error creating new segment");
new_segment.id()
created_segment.id()
};

let _opened_segment = Segment::open(id, TEST_SEGMENT_PREFIX)
Expand Down

0 comments on commit 1cbf809

Please sign in to comment.