From 2b4eb1c8b6e3df26335d48ba30a6deaf411b7811 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 14 Jun 2022 17:55:59 +0200 Subject: [PATCH] more tests --- Cargo.lock | 18 ++++++++++------ src/sp/mod.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05a1a74..fd75af0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -471,11 +471,11 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" +checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] @@ -631,13 +631,13 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.94" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a07e33e919ebcd69113d5be0e4d70c5707004ff45188910106854f38b960df4a" +checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] @@ -687,6 +687,12 @@ dependencies = [ "serde", ] +[[package]] +name = "unicode-ident" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" + [[package]] name = "unicode-xid" version = "0.2.3" diff --git a/src/sp/mod.rs b/src/sp/mod.rs index 8d4a8e8..d7b534b 100644 --- a/src/sp/mod.rs +++ b/src/sp/mod.rs @@ -51,10 +51,33 @@ impl TryFrom for SequenceFlags { pub struct PacketId { pub ptype: PacketType, pub sec_header_flag: bool, - pub apid: u16, + apid: u16, } impl PacketId { + pub fn new(ptype: PacketType, sec_header_flag: bool, apid: u16) -> Option { + 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 { ((self.ptype as u16) << 12) | ((self.sec_header_flag as u16) << 11) | self.apid } @@ -73,13 +96,33 @@ impl From for PacketId { #[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)] pub struct PacketSequenceCtrl { pub seq_flags: SequenceFlags, - pub ssc: u16, + ssc: u16, } impl PacketSequenceCtrl { + pub fn new(seq_flags: SequenceFlags, ssc: u16) -> Option { + if ssc > num::pow(2, 14) - 1 { + return None + } + Some(PacketSequenceCtrl { + seq_flags, + ssc + }) + } pub fn raw(&self) -> u16 { ((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 for PacketSequenceCtrl { @@ -355,6 +398,10 @@ mod tests { let packet_id_from_raw = PacketId::from(packet_id.raw()); 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 { seq_flags: SequenceFlags::ContinuationSegment, ssc: 77, @@ -362,12 +409,18 @@ mod tests { assert_eq!(psc.raw(), 77); let psc_from_raw = PacketSequenceCtrl::from(psc.raw()); 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] fn test_deser_internally() { let sp_header = SpHeader::tc(0x42, 12).expect("Error creating SP header"); assert_eq!(sp_header.version(), 0b000); + assert!(sp_header.is_tc()); assert_eq!(sp_header.sec_header_flag(), true); assert_eq!(sp_header.ptype(), PacketType::Tc); 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"); sp_header.data_len = 36; assert_eq!(sp_header.version(), 0b000); + assert!(sp_header.is_tm()); assert_eq!(sp_header.sec_header_flag(), true); assert_eq!(sp_header.ptype(), PacketType::Tm); 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"); 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 slice = sp_header_zc.as_bytes(); assert_eq!(slice.len(), 6);