From 5d2c80439350a0b201cd3fbb19c13645247f41c7 Mon Sep 17 00:00:00 2001 From: John Lettman Date: Tue, 13 Aug 2024 14:41:27 -0400 Subject: [PATCH] types/primitive/sector_size: add tests --- src/types/primitive/sector_size.rs | 72 +++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/src/types/primitive/sector_size.rs b/src/types/primitive/sector_size.rs index af3bba5..0b9c20e 100644 --- a/src/types/primitive/sector_size.rs +++ b/src/types/primitive/sector_size.rs @@ -1,14 +1,12 @@ use binrw::{parser, writer, BinRead, BinResult, BinWrite, Error}; use const_format::concatcp; -pub const MIN: u16 = 0; pub const MAX: u16 = 360; - -const ERR_MESSAGE_RANGE: &'static str = concatcp!("sector size exceeds maximum of ", MAX); +const ERR_RANGE: &'static str = concatcp!("sector size exceeds maximum of ", MAX); #[inline] pub fn valid(sector_size: u16) -> bool { - MIN <= sector_size && sector_size <= MAX + sector_size <= MAX } #[parser(reader)] @@ -18,7 +16,7 @@ pub fn parse() -> BinResult { if !valid(sector_size) { let pos = reader.stream_position()?; - return Err(Error::AssertFail { pos, message: ERR_MESSAGE_RANGE.to_string() }); + return Err(Error::AssertFail { pos, message: ERR_RANGE.to_string() }); } Ok(sector_size) @@ -28,10 +26,72 @@ pub fn parse() -> BinResult { pub fn write(sector_size: &u16) -> BinResult<()> { if !valid(*sector_size) { let pos = writer.stream_position()?; - return Err(Error::AssertFail { pos, message: ERR_MESSAGE_RANGE.to_string() }); + return Err(Error::AssertFail { pos, message: ERR_RANGE.to_string() }); } let raw = (*sector_size / 3) as u8; raw.write(writer)?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use binrw::Endian; + use std::io::Cursor; + + use log::info; + use test_log::test; + + #[test] + fn test_valid() { + let cases = vec![(MAX + 1, false), (MAX - 1, true), (MAX, true)]; + + for (sector_size, want) in cases { + info!("Checking validity of {sector_size:?}, want {want:?}"); + let got = valid(sector_size); + assert_eq!(want, got); + } + } + + const BINARY_ENDIAN: Endian = Endian::NATIVE; + const BINARY_CASES: [(u16, [u8; 1]); 4] = [(360, [120]), (60, [20]), (9, [3]), (15, [5])]; + + #[test] + fn test_parse() { + for (want, bytes) in BINARY_CASES { + info!("Parsing {bytes:?}, want {want:?}"); + let mut cursor = Cursor::new(bytes); + let got = parse(&mut cursor, BINARY_ENDIAN, ()).expect("It should not return an error"); + assert_eq!(want, got); + } + } + + #[test] + fn test_parse_invalid() { + let mut cursor = Cursor::new([122u8]); + let got = parse(&mut cursor, BINARY_ENDIAN, ()); + assert!(got.is_err()); + } + + #[test] + fn test_write() { + for (sector_size, want) in BINARY_CASES { + info!("Writing {sector_size:?}, want {want:?}"); + let mut cursor = Cursor::new(Vec::new()); + write(§or_size, &mut cursor, BINARY_ENDIAN, ()) + .expect("It should not return an error"); + let inner = cursor.into_inner(); + let got = inner.as_slice(); + assert_eq!(want, got); + } + } + + #[test] + fn test_write_invalid() { + let sector_size = MAX + 1; + let mut cursor = Cursor::new(Vec::new()); + let got = write(§or_size, &mut cursor, BINARY_ENDIAN, ()); + assert!(got.is_err()); + } +}