Skip to content

Commit eefa7ea

Browse files
committed
[#213] test: add test cases
* refine some comments to make them more accurate
1 parent 6f05b7b commit eefa7ea

File tree

3 files changed

+187
-6
lines changed

3 files changed

+187
-6
lines changed

iceoryx2/src/service/builder/publish_subscribe.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ impl<Payload: Debug + ?Sized, UserHeader: Debug, ServiceType: service::Service>
271271
}
272272
}
273273

274+
// triggers the underlying is_service_available method to check whether the service described in base is available.
274275
fn is_service_available(
275276
&mut self,
276277
error_msg: &str,
@@ -370,6 +371,7 @@ impl<Payload: Debug + ?Sized, UserHeader: Debug, ServiceType: service::Service>
370371
self
371372
}
372373

374+
/// Validates configuration and overrides the invalid setting with meaningful values.
373375
fn adjust_attributes_to_meaningful_values(&mut self) {
374376
let origin = format!("{:?}", self);
375377
let settings = self.base.service_config.publish_subscribe_mut();

iceoryx2/src/service/static_config/message_type_details.rs

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ use serde::{Deserialize, Serialize};
2020
#[derive(Default, Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
2121
pub enum TypeVariant {
2222
#[default]
23-
/// A fixed size type like [`u64`]
23+
/// A fixed size type which is determined during compile.
24+
/// A structure holds some pointer fields should be clarified as Dynamic.
2425
FixedSize,
25-
/// A dynamic sized type like a slice
26+
/// A dynamic sized type like a slice or it contains some pointer fields.
2627
Dynamic,
2728
}
2829

@@ -33,9 +34,9 @@ pub struct TypeDetail {
3334
pub variant: TypeVariant,
3435
/// Contains the output of [`core::any::type_name()`].
3536
pub type_name: String,
36-
/// The size of the underlying type.
37+
/// The size of the underlying type calculated by [`core::mem::size_of`].
3738
pub size: usize,
38-
/// The alignment of the underlying type.
39+
/// The ABI-required minimum alignment of the underlying type calculated by [`core::mem::align_of`].
3940
pub alignment: usize,
4041
}
4142

@@ -64,11 +65,11 @@ pub struct MessageTypeDetails {
6465
}
6566

6667
impl MessageTypeDetails {
67-
pub(crate) fn from<Header, UserHeader, Payload>(variant: TypeVariant) -> Self {
68+
pub(crate) fn from<Header, UserHeader, Payload>(payload_variant: TypeVariant) -> Self {
6869
Self {
6970
header: TypeDetail::__internal_new::<Header>(TypeVariant::FixedSize),
7071
user_header: TypeDetail::__internal_new::<UserHeader>(TypeVariant::FixedSize),
71-
payload: TypeDetail::__internal_new::<Payload>(variant),
72+
payload: TypeDetail::__internal_new::<Payload>(payload_variant),
7273
}
7374
}
7475

@@ -78,6 +79,7 @@ impl MessageTypeDetails {
7879
payload_start as *const u8
7980
}
8081

82+
/// returns pointer of user_header which is the field follows the header field in a structure.
8183
pub(crate) fn user_header_ptr_from_header(&self, header: *const u8) -> *const u8 {
8284
let header = header as usize;
8385
let user_header_start = align(header + self.header.size, self.user_header.alignment);
@@ -120,3 +122,124 @@ impl MessageTypeDetails {
120122
&& self.payload.alignment <= rhs.payload.alignment
121123
}
122124
}
125+
126+
#[cfg(test)]
127+
mod tests {
128+
use super::*;
129+
use iceoryx2_bb_testing::assert_that;
130+
131+
#[test]
132+
fn test_from(){
133+
struct MyPayload{
134+
_a: i32,
135+
_b: bool,
136+
_c: i64,
137+
}
138+
139+
let got = MessageTypeDetails::from::<i32,i64,MyPayload>(TypeVariant::FixedSize);
140+
let want = MessageTypeDetails{
141+
header: TypeDetail{
142+
variant: TypeVariant::FixedSize,
143+
type_name: "i32".to_string(),
144+
size: 4,
145+
alignment: 4,
146+
},
147+
user_header: TypeDetail{
148+
variant: TypeVariant::FixedSize,
149+
type_name: "i64".to_string(),
150+
size: 8,
151+
alignment: 8,
152+
},
153+
payload: TypeDetail{
154+
variant: TypeVariant::FixedSize,
155+
type_name: "iceoryx2::service::static_config::message_type_details::tests::test_from::MyPayload".to_string(),
156+
size: 16,
157+
alignment: 8,
158+
},
159+
};
160+
assert_that!(want, eq got);
161+
162+
let got = MessageTypeDetails::from::<i32,bool,String>(TypeVariant::Dynamic);
163+
let want = MessageTypeDetails{
164+
header: TypeDetail{
165+
variant: TypeVariant::FixedSize,
166+
type_name: "i32".to_string(),
167+
size: 4,
168+
alignment: 4,
169+
},
170+
user_header: TypeDetail{
171+
variant: TypeVariant::FixedSize,
172+
type_name: "bool".to_string(),
173+
size: 1,
174+
alignment: 1,
175+
},
176+
payload: TypeDetail{
177+
variant: TypeVariant::Dynamic,
178+
type_name: "alloc::string::String".to_string(),
179+
size: 24,
180+
alignment: 8,
181+
},
182+
};
183+
assert_that!(want, eq got);
184+
}
185+
186+
#[test]
187+
fn test_user_header_ptr_from_header(){
188+
let details = MessageTypeDetails::from::<i32,bool,String>(TypeVariant::Dynamic);
189+
struct Demo {
190+
header: i32,
191+
user_header: bool,
192+
_payload: String,
193+
}
194+
195+
let demo = Demo{
196+
header: 123,
197+
user_header: true,
198+
_payload: "test".to_string(),
199+
};
200+
201+
let ptr: *const u8 = &demo.header as *const _ as *const u8;
202+
let user_header_ptr = details.user_header_ptr_from_header(ptr);
203+
let got: *const bool = user_header_ptr as *const bool;
204+
assert_that!(unsafe { *got } , eq demo.user_header);
205+
206+
let details = MessageTypeDetails::from::<i64,i32,String>(TypeVariant::Dynamic);
207+
struct Demo2 {
208+
header: i64,
209+
user_header: i32,
210+
_payload: String,
211+
}
212+
213+
let demo = Demo2{
214+
header: 123,
215+
user_header: 999,
216+
_payload: "test".to_string(),
217+
};
218+
219+
let ptr: *const u8 = &demo.header as *const _ as *const u8;
220+
let user_header_ptr = details.user_header_ptr_from_header(ptr);
221+
let got: *const i32 = user_header_ptr as *const i32;
222+
assert_that!(unsafe { *got } , eq demo.user_header);
223+
}
224+
225+
#[test]
226+
fn test_payload_ptr_from_header(){
227+
let details = MessageTypeDetails::from::<i32,i32,i32>(TypeVariant::Dynamic);
228+
struct Demo {
229+
header: i32,
230+
_user_header: i32,
231+
payload: i32,
232+
}
233+
234+
let demo = Demo{
235+
header: 123,
236+
_user_header: 123,
237+
payload:9999,
238+
};
239+
240+
let ptr: *const u8 = &demo.header as *const _ as *const u8;
241+
let payload_ptr = details.payload_ptr_from_header(ptr) as *const i32;
242+
let got = unsafe { *payload_ptr };
243+
assert_that!(got, eq demo.payload);
244+
}
245+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
#[cfg(test)]
14+
mod service_static_config_message_type_details {
15+
use iceoryx2::service::static_config::message_type_details::{TypeDetail,TypeVariant};
16+
use iceoryx2_bb_testing::assert_that;
17+
18+
#[test]
19+
fn test_internal_new(){
20+
struct Tmp;
21+
let got = TypeDetail::__internal_new::<Tmp>(TypeVariant::FixedSize);
22+
let want = TypeDetail{
23+
variant: TypeVariant::FixedSize,
24+
type_name: "service_static_config_tests::service_static_config_message_type_details::test_internal_new::Tmp".to_string(),
25+
size: 0 ,
26+
alignment: 1,
27+
};
28+
assert_that!(want, eq got);
29+
30+
let got = TypeDetail::__internal_new::<i64>(TypeVariant::FixedSize);
31+
let want = TypeDetail{
32+
variant: TypeVariant::FixedSize,
33+
type_name: "i64".to_string(),
34+
size: 8,
35+
alignment: 8,
36+
};
37+
assert_that!(want, eq got);
38+
39+
let got = TypeDetail::__internal_new::<TypeDetail>(TypeVariant::FixedSize);
40+
let want = TypeDetail{
41+
variant: TypeVariant::FixedSize,
42+
type_name: "iceoryx2::service::static_config::message_type_details::TypeDetail".to_string(),
43+
size: 48,
44+
alignment: 8,
45+
};
46+
47+
assert_that!(want, eq got);
48+
}
49+
50+
51+
52+
#[test]
53+
fn test_payload_ptr_from_header(){
54+
55+
}
56+
}

0 commit comments

Comments
 (0)