Skip to content

Commit

Permalink
Add semantic test for payload size field encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
hchataing committed Dec 1, 2024
1 parent 6dd9f2c commit 7bc6880
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions pdl-tests/tests/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ packet Test {
#[cfg(test)]
mod optional_field {
#[test]
fn test_value_0() {
fn test_ok_value_0() {
let value = Test { a: Some(255), b: None };
let mut encoded_value = vec![];

Expand All @@ -44,7 +44,7 @@ mod optional_field {
}

#[test]
fn test_value_1() {
fn test_ok_value_1() {
let value = Test { a: None, b: Some(Enum16::X) };
let mut encoded_value = vec![];

Expand All @@ -54,7 +54,7 @@ mod optional_field {
}

#[test]
fn test_value_inconsistent() {
fn test_err_inconsistent_condition_value() {
// The optional fields would provide the value 1 and 0
// for the condition flag.
assert!(matches!(
Expand All @@ -70,3 +70,51 @@ mod optional_field {
));
}
}

#[pdl_inline(
r#"
little_endian_packets
struct Elem {
tag : 16,
_size_(value) : 16,
value : 8[],
}
packet Parent {
a : 8,
_size_(_payload_) : 8,
_payload_,
}
packet Child : Parent(a = 42) {
b : Elem,
c : Elem,
}
"#
)]
#[cfg(test)]
mod payload_size_field {
#[test]
fn test_ok() {
assert_eq!(Parent { a: 1, payload: vec![] }.encode_to_vec(), Ok(vec![1, 0]));
}

#[test]
fn test_err_size_overflow() {
// Attempting to encode a packet with a payload size exceeding
// the range for the size field must fail.
assert!(matches!(
Parent { a: 42, payload: vec![1; 1024] }.encode_to_vec(),
Err(EncodeError::SizeOverflow { .. })
));
assert!(matches!(
Child {
b: Elem { tag: 42, value: vec![1; 42] },
c: Elem { tag: 42, value: vec![1; 1024] }
}
.encode_to_vec(),
Err(EncodeError::SizeOverflow { .. })
));
}
}

0 comments on commit 7bc6880

Please sign in to comment.