Skip to content

Commit

Permalink
Merge pull request eclipse-iceoryx#455 from xieyuschen/iox2-213-test-…
Browse files Browse the repository at this point in the history
…cases-for-static-config

 [eclipse-iceoryx#213]: tests methods of MessageTypeDetails
  • Loading branch information
elfenpiff authored Oct 10, 2024
2 parents ca22976 + 88aba3d commit a684249
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 8 deletions.
162 changes: 162 additions & 0 deletions iceoryx2/src/service/static_config/message_type_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,166 @@ mod tests {
let sut = unsafe { *payload_ptr };
assert_that!(sut, eq demo.payload);
}

#[test]
fn test_payload_layout() {
let details = MessageTypeDetails::from::<i64, i64, i64>(TypeVariant::FixedSize);
let sut = details.payload_layout(0);
assert_that!(sut.size(), eq 0);
let sut = details.payload_layout(5);
assert_that!(sut.size(), eq 40);

#[repr(C)]
struct Demo {
_b: bool,
_i16: i16,
_i64: i64,
}

let details = MessageTypeDetails::from::<i64, i64, Demo>(TypeVariant::FixedSize);
let sut = details.payload_layout(1);
#[cfg(target_pointer_width = "32")]
let expected = 12;
#[cfg(target_pointer_width = "64")]
let expected = 16;
assert_that!(sut.size(), eq expected);

#[cfg(target_pointer_width = "32")]
let expected = 36;
#[cfg(target_pointer_width = "64")]
let expected = 48;
let sut = details.payload_layout(3);
assert_that!(sut.size(), eq expected);
}

#[test]
// test_sample_layout tests the sample layout for combinations of different types.
fn test_sample_layout() {
let details = MessageTypeDetails::from::<i64, i64, i64>(TypeVariant::FixedSize);
let sut = details.sample_layout(0);
#[cfg(target_pointer_width = "32")]
let expected = 24;
#[cfg(target_pointer_width = "64")]
let expected = 32;
assert_that!(sut.size(), eq expected);

let details = MessageTypeDetails::from::<i64, i64, i64>(TypeVariant::FixedSize);
let sut = details.sample_layout(2);
#[cfg(target_pointer_width = "32")]
let expected = 40;
#[cfg(target_pointer_width = "64")]
let expected = 48;
assert_that!(sut.size(), eq expected);

let details = MessageTypeDetails::from::<i64, i64, bool>(TypeVariant::FixedSize);
let sut = details.sample_layout(3);
#[cfg(target_pointer_width = "32")]
let expected = 24;
#[cfg(target_pointer_width = "64")]
let expected = 32;
assert_that!(sut.size(), eq expected);

let details = MessageTypeDetails::from::<i64, i32, bool>(TypeVariant::FixedSize);
let sut = details.sample_layout(11);

#[cfg(target_pointer_width = "32")]
let expected = 28;
#[cfg(target_pointer_width = "64")]
let expected = 32;
assert_that!(sut.size(), eq expected);

#[repr(C)]
struct Demo {
_b: bool,
_i16: i16,
_i64: i64,
}

let details = MessageTypeDetails::from::<i64, i64, Demo>(TypeVariant::FixedSize);
let sut = details.sample_layout(2);
#[cfg(target_pointer_width = "32")]
let expected = 48;
#[cfg(target_pointer_width = "64")]
let expected = 64;
assert_that!(sut.size(), eq expected);
}

#[test]
fn test_is_compatible_to_failed_when_types_differ() {
let left = MessageTypeDetails::from::<i64, i64, i8>(TypeVariant::FixedSize);
let right = MessageTypeDetails::from::<i64, i64, u8>(TypeVariant::FixedSize);
let sut = left.is_compatible_to(&right);
assert_that!(sut, eq false);

let left = MessageTypeDetails::from::<i64, i64, i64>(TypeVariant::FixedSize);
let right = MessageTypeDetails::from::<i64, i64, i32>(TypeVariant::FixedSize);
let sut = left.is_compatible_to(&right);
assert_that!(sut, eq false);
}

#[test]
fn test_is_compatible_to_succeed_when_rhs_aligment_is_bigger() {
let left = MessageTypeDetails::from::<i64, i64, i64>(TypeVariant::FixedSize);

// right may have a different alignment from left.
// but note that the header alignment must be the same
let right = MessageTypeDetails {
header: TypeDetail {
variant: TypeVariant::FixedSize,
type_name: "i64".to_string(),
size: 8,
alignment: ALIGNMENT,
},
user_header: TypeDetail {
variant: TypeVariant::FixedSize,
type_name: "i64".to_string(),
size: 8,
alignment: 2 * ALIGNMENT,
},
payload: TypeDetail {
variant: TypeVariant::FixedSize,
type_name: "i64".to_string(),
size: 8,
alignment: 2 * ALIGNMENT,
},
};
// smaller to bigger is allowed.
let sut = left.is_compatible_to(&right);
assert_that!(sut, eq true);

// bigger to smaller is invalid.
let sut = right.is_compatible_to(&left);
assert_that!(sut, eq false);
}

#[test]
fn test_is_compatible_to_fail_when_rhs_aligment_is_smaller() {
let left = MessageTypeDetails::from::<i64, i64, i64>(TypeVariant::FixedSize);

// right may have a different alignment from left.
// but note that the header alignment must be the same
let right = MessageTypeDetails {
header: TypeDetail {
variant: TypeVariant::FixedSize,
type_name: "i64".to_string(),
size: 8,
alignment: ALIGNMENT,
},
user_header: TypeDetail {
variant: TypeVariant::FixedSize,
type_name: "i64".to_string(),
size: 8,
alignment: 2 * ALIGNMENT,
},
payload: TypeDetail {
variant: TypeVariant::FixedSize,
type_name: "i64".to_string(),
size: 8,
alignment: 2 * ALIGNMENT,
},
};
// bigger to smaller is invalid.
let sut = right.is_compatible_to(&left);
assert_that!(sut, eq false);
}
}
73 changes: 65 additions & 8 deletions iceoryx2/src/service/static_config/messaging_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ impl Display for MessagingPattern {
}

impl MessagingPattern {
/// checks whether the 2 MessagingPatterns are the same regardless the values inside them.
pub(crate) fn is_same_pattern(&self, rhs: &MessagingPattern) -> bool {
match self {
MessagingPattern::PublishSubscribe(_) => {
matches!(rhs, MessagingPattern::PublishSubscribe(_))
}
MessagingPattern::Event(_) => {
matches!(rhs, MessagingPattern::Event(_))
}
}
std::mem::discriminant(self) == std::mem::discriminant(rhs)
}

pub(crate) fn required_amount_of_samples_per_data_segment(
Expand All @@ -68,3 +62,66 @@ impl MessagingPattern {
}
}
}

#[cfg(test)]
mod tests {
use iceoryx2_bb_testing::assert_that;

use super::*;
use crate::service::config;
use crate::service::static_config::event;
use crate::service::static_config::publish_subscribe;

#[test]
fn test_is_same_pattern() {
let cfg = config::Config::default();
let p1 = MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(&cfg));
let p2 = MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(&cfg));
assert_that!(p1.is_same_pattern(&p2), eq true);
assert_that!(p2.is_same_pattern(&p1), eq true);

let mut new_defaults = config::Defaults {
publish_subscribe: cfg.defaults.publish_subscribe.clone(),
event: cfg.defaults.event.clone(),
};
new_defaults.event.event_id_max_value -= 1;
new_defaults.publish_subscribe.max_nodes -= 1;

let cfg2 = config::Config {
defaults: new_defaults,
global: cfg.global.clone(),
};

// ensure the cfg and cfg2 are not equal
assert_that!(cfg, ne cfg2);
let p3 = MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(&cfg2));
assert_that!(p1.is_same_pattern(&p3), eq true);
assert_that!(p3.is_same_pattern(&p1), eq true);

let e1 = MessagingPattern::Event(event::StaticConfig::new(&cfg));
let e2 = MessagingPattern::Event(event::StaticConfig::new(&cfg));
assert_that!(e1.is_same_pattern(&e2), eq true);
assert_that!(e2.is_same_pattern(&e1), eq true);

let e3 = MessagingPattern::Event(event::StaticConfig::new(&cfg2));
assert_that!(e1.is_same_pattern(&e3), eq true);
assert_that!(e2.is_same_pattern(&e3), eq true);

assert_that!(p1.is_same_pattern(&e1), eq false);
assert_that!(p3.is_same_pattern(&e3), eq false);
}

#[test]
fn test_required_amount_of_samples_per_data_segment() {
let cfg = config::Config::default();
let p1 = MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(&cfg));
let sut = p1.required_amount_of_samples_per_data_segment(0);
assert_that!(sut, eq 33);
let sut = p1.required_amount_of_samples_per_data_segment(1);
assert_that!(sut, eq 34);

let e1 = MessagingPattern::Event(event::StaticConfig::new(&cfg));
let sut = e1.required_amount_of_samples_per_data_segment(1);
assert_that!(sut, eq 0);
}
}

0 comments on commit a684249

Please sign in to comment.