use std::{fs, path::PathBuf};
fn fixture_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.join("m6-single.ts")
}
#[test]
fn identity_passthrough_byte_identical() {
let input = fs::read(fixture_path()).expect("fixture m6-single.ts not found");
assert!(!input.is_empty(), "fixture is empty");
assert_eq!(
input.len() % 188,
0,
"fixture length {} is not a multiple of 188",
input.len()
);
let expected_packet_count = input.len() / 188;
let mut engine = ts_fix::TsFix::builder()
.build()
.expect("identity build should not fail");
let mut output: Vec<u8> = Vec::with_capacity(input.len());
let mut emitted_count: usize = 0;
for chunk in input.chunks(188) {
engine
.push(chunk, |pkt| {
output.extend_from_slice(pkt);
emitted_count += 1;
})
.expect("valid 188-byte packet from fixture");
}
engine.finish(|pkt| {
output.extend_from_slice(pkt);
emitted_count += 1;
});
assert_eq!(
emitted_count, expected_packet_count,
"emitted {emitted_count} packets, expected {expected_packet_count}"
);
assert_eq!(output, input, "identity engine output differs from input");
}
#[test]
fn identity_rejects_short_packet() {
let mut engine = ts_fix::TsFix::builder().build().unwrap();
let short = [0x47u8; 100]; let result = engine.push(&short, |_| {});
assert!(result.is_err(), "engine should reject a short packet");
match result.unwrap_err() {
ts_fix::Error::ShortPacket { len } => assert_eq!(len, 100),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn identity_rejects_bad_sync_byte() {
let mut engine = ts_fix::TsFix::builder().build().unwrap();
let mut pkt = [0u8; 188];
pkt[0] = 0x00; let result = engine.push(&pkt, |_| {});
assert!(
result.is_err(),
"engine should reject a packet with bad sync byte"
);
match result.unwrap_err() {
ts_fix::Error::NoSyncByte { found } => assert_eq!(found, 0x00),
other => panic!("unexpected error: {other:?}"),
}
}
#[test]
fn error_is_non_exhaustive() {
let err = ts_fix::Error::ShortPacket { len: 42 };
let _label = match err {
ts_fix::Error::ShortPacket { len } => alloc::format!("short:{len}"),
ts_fix::Error::NoSyncByte { found } => alloc::format!("sync:{found:#04x}"),
_ => "unknown".to_string(),
};
}
extern crate alloc;