Skip to content

Commit

Permalink
types/sonar_return_status: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Aug 16, 2024
1 parent 15a49f2 commit f4b0af4
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/types/sonar_return_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,119 @@ impl BinWrite for SonarReturnStatus {
self.write(writer)
}
}

#[cfg(test)]
mod tests {
use super::*;
use binrw::{io::Cursor, BinRead, BinWrite};

use log::info;
use test_log::test;

#[test]
fn test_default() {
let want = SonarReturnStatus {
range_error: false,
frequency_error: false,
internal_sensor_error: false,
calibration_error: false,
switches_accepted: false,
};

let got = SonarReturnStatus::default();
assert_eq!(want, got);
}

#[test]
fn test_has_error() {
let mut status = SonarReturnStatus::default();
assert!(!status.has_error());

status.range_error = true;
assert!(status.has_error());

status.range_error = false;
status.frequency_error = true;
assert!(status.has_error());

status.internal_sensor_error = true;
assert!(status.has_error());

status.calibration_error = true;
assert!(status.has_error());
}

const BINARY_CASES: [(SonarReturnStatus, u8); 5] = [
(
SonarReturnStatus {
range_error: false,
frequency_error: false,
internal_sensor_error: false,
calibration_error: false,
switches_accepted: false,
},
0b0000_0000,
),
(
SonarReturnStatus {
range_error: true,
frequency_error: false,
internal_sensor_error: false,
calibration_error: false,
switches_accepted: true,
},
0b1000_0001,
),
(
SonarReturnStatus {
range_error: true,
frequency_error: true,
internal_sensor_error: true,
calibration_error: true,
switches_accepted: true,
},
0b1111_0001,
),
(
SonarReturnStatus {
range_error: false,
frequency_error: true,
internal_sensor_error: false,
calibration_error: true,
switches_accepted: false,
},
0b0101_0000,
),
(
SonarReturnStatus {
range_error: true,
frequency_error: true,
internal_sensor_error: false,
calibration_error: false,
switches_accepted: false,
},
0b1100_0000,
),
];

#[test]
fn test_parse() {
for &(ref want, raw) in BINARY_CASES.iter() {
info!("Parsing {raw:?}, expecting {want:?}");
let mut cursor = Cursor::new(vec![raw]);
let got = SonarReturnStatus::read(&mut cursor).expect("It should not return an error");
assert_eq!(want, &got);
}
}

#[test]
fn test_write() {
for &(ref status, raw) in BINARY_CASES.iter() {
info!("Writing {status:?}, expecting {raw:?}");
let mut cursor = Cursor::new(Vec::new());
status.write(&mut cursor).expect("It should not return an error");
let written_data = cursor.into_inner();
assert_eq!(written_data, vec![raw]);
}
}
}

0 comments on commit f4b0af4

Please sign in to comment.