Skip to content

Commit

Permalink
test: add tests for SDR decode
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstephens committed Jan 16, 2024
1 parent 02fff97 commit 5d7da22
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,63 @@ where
Some(next_record.record)
}
}

#[cfg(test)]
mod tests {
use crate::sensor_event::{RawSensorReading, ThresholdReading};
use crate::storage::sdr::record::SensorId;
use crate::storage::sdr::Record;
use crate::SensorRecord;

const FAN_2A_SDR: [u8; 55] = [
0x0E, 0x00, 0x0D, 0x00, 0x51, 0x01, 0x30, 0x20, 0x00, 0x32, 0x07, 0x01, 0x7F, 0xD4, 0x04,
0x01, 0x05, 0x30, 0x05, 0x00, 0x03, 0x00, 0x00, 0x12, 0x00, 0x00, 0x78, 0x02, 0x00, 0x02,
0x30, 0x00, 0x07, 0x54, 0xC5, 0x8B, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x05, 0x07, 0x01,
0x01, 0x00, 0x00, 0x00, 0xC5, 0x46, 0x61, 0x6E, 0x32, 0x41,
];
const FAN_2A_READING: [u8; 3] = [0x2c, 0xc0, 0xc0];
#[test]
fn test_decode_fan_rpm() {
let sensor = Record::parse(&FAN_2A_SDR[2..]).unwrap();
assert_eq!(
SensorId::Ascii8BAndLatin1("Fan2A".to_string()),
sensor.common_data().unwrap().sensor_id
);
let raw_reading = RawSensorReading::parse(&FAN_2A_READING).unwrap();
let reading: ThresholdReading = (&raw_reading).into();
assert_eq!(
"5280.00 rpm",
sensor
.full_sensor()
.unwrap()
.display_reading(reading.reading.unwrap())
.unwrap()
);
}

const INLET_TEMP_SDR: [u8; 60] = [
0x13, 0x00, 0x12, 0x00, 0x51, 0x01, 0x35, 0x20, 0x00, 0x04, 0x07, 0x01, 0x7F, 0x68, 0x01,
0x01, 0x85, 0x32, 0x85, 0x32, 0x1B, 0x09, 0x00, 0x01, 0x00, 0x00, 0x01, 0x02, 0x80, 0xC2,
0x30, 0x00, 0x07, 0x97, 0xC5, 0x8B, 0xFF, 0x00, 0xFF, 0xAF, 0xAA, 0x00, 0x79, 0x83, 0x01,
0x01, 0x00, 0x00, 0x00, 0xCA, 0x49, 0x6E, 0x6C, 0x65, 0x74, 0x20, 0x54, 0x65, 0x6D, 0x70,
];
const INLET_TEMP_READING: [u8; 3] = [0x99, 0xc0, 0xc0];
#[test]
fn test_decode_inlet_temp() {
let sensor = Record::parse(&INLET_TEMP_SDR[2..]).unwrap();
assert_eq!(
SensorId::Ascii8BAndLatin1("Inlet Temp".to_string()),
sensor.common_data().unwrap().sensor_id
);
let raw_reading = RawSensorReading::parse(&INLET_TEMP_READING).unwrap();
let reading: ThresholdReading = (&raw_reading).into();
assert_eq!(
"25.00 °C",
sensor
.full_sensor()
.unwrap()
.display_reading(reading.reading.unwrap())
.unwrap()
);
}
}
2 changes: 1 addition & 1 deletion src/sensor_event/sensor_reading/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use super::RawSensorReading;

impl RawSensorReading {
fn parse(data: &[u8]) -> Option<Self> {
pub(crate) fn parse(data: &[u8]) -> Option<Self> {
if data.len() < 2 {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/sdr/record/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl<'a> From<TypeLengthRaw<'a>> for SensorId {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum SensorId {
Unicode(String),
BCDPlus(Vec<u8>),
Expand Down

0 comments on commit 5d7da22

Please sign in to comment.