more tests

This commit is contained in:
Robin Müller 2022-06-14 17:55:59 +02:00
parent 933d946f68
commit 2b4eb1c8b6
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 69 additions and 8 deletions

18
Cargo.lock generated
View File

@ -471,11 +471,11 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.38" version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
dependencies = [ dependencies = [
"unicode-xid", "unicode-ident",
] ]
[[package]] [[package]]
@ -631,13 +631,13 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.94" version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a07e33e919ebcd69113d5be0e4d70c5707004ff45188910106854f38b960df4a" checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"unicode-xid", "unicode-ident",
] ]
[[package]] [[package]]
@ -687,6 +687,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "unicode-ident"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.3" version = "0.2.3"

View File

@ -51,10 +51,33 @@ impl TryFrom<u8> for SequenceFlags {
pub struct PacketId { pub struct PacketId {
pub ptype: PacketType, pub ptype: PacketType,
pub sec_header_flag: bool, pub sec_header_flag: bool,
pub apid: u16, apid: u16,
} }
impl PacketId { impl PacketId {
pub fn new(ptype: PacketType, sec_header_flag: bool, apid: u16) -> Option<PacketId> {
if apid > num::pow(2, 11) - 1 {
return None
}
Some(PacketId{
ptype,
sec_header_flag,
apid
})
}
pub fn set_apid(&mut self, apid: u16) -> bool {
if apid > num::pow(2, 11) {
return false;
}
self.apid = apid;
true
}
pub fn apid(&self) -> u16 {
self.apid
}
pub fn raw(&self) -> u16 { pub fn raw(&self) -> u16 {
((self.ptype as u16) << 12) | ((self.sec_header_flag as u16) << 11) | self.apid ((self.ptype as u16) << 12) | ((self.sec_header_flag as u16) << 11) | self.apid
} }
@ -73,13 +96,33 @@ impl From<u16> for PacketId {
#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)]
pub struct PacketSequenceCtrl { pub struct PacketSequenceCtrl {
pub seq_flags: SequenceFlags, pub seq_flags: SequenceFlags,
pub ssc: u16, ssc: u16,
} }
impl PacketSequenceCtrl { impl PacketSequenceCtrl {
pub fn new(seq_flags: SequenceFlags, ssc: u16) -> Option<PacketSequenceCtrl> {
if ssc > num::pow(2, 14) - 1 {
return None
}
Some(PacketSequenceCtrl {
seq_flags,
ssc
})
}
pub fn raw(&self) -> u16 { pub fn raw(&self) -> u16 {
((self.seq_flags as u16) << 14) | self.ssc ((self.seq_flags as u16) << 14) | self.ssc
} }
pub fn set_ssc(&mut self, ssc: u16) -> bool {
if ssc > num::pow(2, 14) - 1 {
return false
}
self.ssc = ssc;
true
}
pub fn ssc(&self) -> u16 {
self.ssc
}
} }
impl From<u16> for PacketSequenceCtrl { impl From<u16> for PacketSequenceCtrl {
@ -355,6 +398,10 @@ mod tests {
let packet_id_from_raw = PacketId::from(packet_id.raw()); let packet_id_from_raw = PacketId::from(packet_id.raw());
assert_eq!(packet_id_from_raw, packet_id); assert_eq!(packet_id_from_raw, packet_id);
let packet_id_invalid = PacketId::new(PacketType::Tc, true, 0xFFFF);
assert!(packet_id_invalid.is_none());
let packet_id_from_new = PacketId::new(PacketType::Tm, false, 0x42).unwrap();
assert_eq!(packet_id_from_new, packet_id);
let psc = PacketSequenceCtrl { let psc = PacketSequenceCtrl {
seq_flags: SequenceFlags::ContinuationSegment, seq_flags: SequenceFlags::ContinuationSegment,
ssc: 77, ssc: 77,
@ -362,12 +409,18 @@ mod tests {
assert_eq!(psc.raw(), 77); assert_eq!(psc.raw(), 77);
let psc_from_raw = PacketSequenceCtrl::from(psc.raw()); let psc_from_raw = PacketSequenceCtrl::from(psc.raw());
assert_eq!(psc_from_raw, psc); assert_eq!(psc_from_raw, psc);
let psc_invalid = PacketSequenceCtrl::new(SequenceFlags::FirstSegment, 0xFFFF);
assert!(psc_invalid.is_none());
let psc_from_new = PacketSequenceCtrl::new(SequenceFlags::ContinuationSegment, 77).unwrap();
assert_eq!(psc_from_new, psc);
} }
#[test] #[test]
fn test_deser_internally() { fn test_deser_internally() {
let sp_header = SpHeader::tc(0x42, 12).expect("Error creating SP header"); let sp_header = SpHeader::tc(0x42, 12).expect("Error creating SP header");
assert_eq!(sp_header.version(), 0b000); assert_eq!(sp_header.version(), 0b000);
assert!(sp_header.is_tc());
assert_eq!(sp_header.sec_header_flag(), true); assert_eq!(sp_header.sec_header_flag(), true);
assert_eq!(sp_header.ptype(), PacketType::Tc); assert_eq!(sp_header.ptype(), PacketType::Tc);
assert_eq!(sp_header.ssc(), 12); assert_eq!(sp_header.ssc(), 12);
@ -390,6 +443,7 @@ mod tests {
let mut sp_header = SpHeader::tm(0x7, 22).expect("Error creating SP header"); let mut sp_header = SpHeader::tm(0x7, 22).expect("Error creating SP header");
sp_header.data_len = 36; sp_header.data_len = 36;
assert_eq!(sp_header.version(), 0b000); assert_eq!(sp_header.version(), 0b000);
assert!(sp_header.is_tm());
assert_eq!(sp_header.sec_header_flag(), true); assert_eq!(sp_header.sec_header_flag(), true);
assert_eq!(sp_header.ptype(), PacketType::Tm); assert_eq!(sp_header.ptype(), PacketType::Tm);
assert_eq!(sp_header.ssc(), 22); assert_eq!(sp_header.ssc(), 22);
@ -407,6 +461,7 @@ mod tests {
let sp_header = SpHeader::tc(0x7FF, num::pow(2, 14) - 1).expect("Error creating SP header"); let sp_header = SpHeader::tc(0x7FF, num::pow(2, 14) - 1).expect("Error creating SP header");
assert_eq!(sp_header.packet_id.ptype, PacketType::Tc); assert_eq!(sp_header.packet_id.ptype, PacketType::Tc);
assert!(sp_header.is_tc());
let sp_header_zc = sp::zc::SpHeader::from(sp_header); let sp_header_zc = sp::zc::SpHeader::from(sp_header);
let slice = sp_header_zc.as_bytes(); let slice = sp_header_zc.as_bytes();
assert_eq!(slice.len(), 6); assert_eq!(slice.len(), 6);