use super::*;
#[test]
fn skip_advances_and_rejects_truncation_without_moving() {
let mut r = ByteReader::new(&[1, 2, 3, 4, 5]);
r.skip(0).unwrap();
r.skip(2).unwrap();
assert_eq!(r.pos(), 2);
assert_eq!(r.skip(4), Err(CodecError::Truncated { needed: 4, remaining: 3 }));
assert_eq!(r.pos(), 2, "failed skip must not advance");
r.skip(3).unwrap();
assert_eq!(r.remaining(), 0);
r.finish().unwrap();
}
#[test]
fn read_u32_words_into_appends_le_words_and_checks_bounds() {
let bytes = [0x01, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F];
let mut r = ByteReader::new(&bytes);
let mut out = vec![9];
r.read_u32_words_into(3, &mut out).unwrap();
assert_eq!(out, [9, 1, 0xDEAD_BEEF, u32::MAX], "appends after existing words");
assert_eq!(r.remaining(), 1);
r.read_u32_words_into(0, &mut out).unwrap();
assert_eq!(out.len(), 4);
assert_eq!(r.remaining(), 1);
assert_eq!(
r.read_u32_words_into(1, &mut out),
Err(CodecError::Truncated { needed: 4, remaining: 1 })
);
assert_eq!(out.len(), 4);
assert_eq!(r.remaining(), 1);
let mut empty = Vec::new();
assert!(matches!(
ByteReader::new(&[]).read_u32_words_into(usize::MAX, &mut empty),
Err(CodecError::LenOverflow { len: usize::MAX, .. })
));
assert_eq!(empty.capacity(), 0);
}
#[test]
fn for_each_padded_le_word_zero_pads_every_tail_length() {
let collect = |bytes: &[u8]| {
let mut words = Vec::new();
for_each_padded_le_word(bytes, |w| words.push(w));
words
};
assert!(collect(&[]).is_empty());
assert_eq!(collect(&[0xAA]), [0x0000_00AA]);
assert_eq!(collect(&[0xAA, 0xBB]), [0x0000_BBAA]);
assert_eq!(collect(&[0xAA, 0xBB, 0xCC]), [0x00CC_BBAA]);
assert_eq!(collect(&[1, 2, 3, 4]), [0x0403_0201]);
assert_eq!(collect(&[1, 2, 3, 4, 5]), [0x0403_0201, 0x0000_0005]);
for len in 0..=9usize {
let bytes: Vec<u8> = (1..=len as u8).collect();
let mut block = Vec::new();
assert!(write_flat_op_bytes_words(&mut block, &bytes, 64));
assert_eq!(block[0] as usize, len);
assert_eq!(block[1..], collect(&bytes)[..], "len {len}");
assert_eq!(block.len(), 1 + flat_op_str_word_len(len));
}
}
#[test]
fn read_flat_op_padded_bytes_validates_framing_and_leaves_pos_on_error() {
let mut words = Vec::new();
assert!(write_flat_op_bytes_words(&mut words, &[0xFF, 0x00, 0x80], 16));
let bytes: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
assert_eq!(bytes.len(), 8);
let mut pos = 0;
assert_eq!(read_flat_op_padded_bytes(&bytes, &mut pos, 16), Ok(&[0xFF, 0x00, 0x80][..]));
assert_eq!(pos, 8);
let mut pos = 0;
assert_eq!(read_flat_op_str_bytes(&bytes, &mut pos, 16), Err(FlatOpStrError::InvalidUtf8));
assert_eq!(pos, 0);
let mut pos = 0;
assert_eq!(
read_flat_op_padded_bytes(&bytes, &mut pos, 2),
Err(FlatOpStrError::ExceedsMaxBytes { len: 3, max_bytes: 2 })
);
let mut dirty = bytes.clone();
dirty[7] = 1;
assert_eq!(read_flat_op_padded_bytes(&dirty, &mut pos, 16), Err(FlatOpStrError::NonZeroPadding));
assert_eq!(read_flat_op_padded_bytes(&bytes[..7], &mut pos, 16), Err(FlatOpStrError::Truncated));
assert_eq!(read_flat_op_padded_bytes(&bytes[..3], &mut pos, 16), Err(FlatOpStrError::Truncated));
let mut far = usize::MAX - 1;
assert_eq!(read_flat_op_padded_bytes(&bytes, &mut far, 16), Err(FlatOpStrError::Truncated));
assert_eq!(pos, 0);
let mut pos = 0;
assert_eq!(read_flat_op_padded_bytes(&[0; 4], &mut pos, 0), Ok(&[][..]));
assert_eq!(pos, 4);
}