Skip to content

Commit

Permalink
types/primitive/sector_size: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Aug 13, 2024
1 parent dcf933a commit 5d2c804
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions src/types/primitive/sector_size.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -18,7 +16,7 @@ pub fn parse() -> BinResult<u16> {

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)
Expand All @@ -28,10 +26,72 @@ pub fn parse() -> BinResult<u16> {
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(&sector_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(&sector_size, &mut cursor, BINARY_ENDIAN, ());
assert!(got.is_err());
}
}

0 comments on commit 5d2c804

Please sign in to comment.