tests done

This commit is contained in:
Robin Müller 2023-05-15 00:11:41 +02:00
parent 8bb4d6d32e
commit 074882c160
Signed by: muellerr
GPG Key ID: A649FB78196E3849

View File

@ -129,8 +129,8 @@ impl UnsignedByteField {
pub fn new_from_be_bytes(width: usize, buf: &[u8]) -> Result<Self, UnsignedByteFieldError> { pub fn new_from_be_bytes(width: usize, buf: &[u8]) -> Result<Self, UnsignedByteFieldError> {
if width > buf.len() { if width > buf.len() {
return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch { return Err(ByteConversionError::FromSliceTooSmall(SizeMissmatch {
found: buf.len(),
expected: width, expected: width,
found: buf.len(),
}) })
.into()); .into());
} }
@ -301,6 +301,7 @@ pub mod tests {
UnsignedByteField, UnsignedByteFieldError, UnsignedEnum, UnsignedU16, UnsignedU32, UnsignedByteField, UnsignedByteFieldError, UnsignedEnum, UnsignedU16, UnsignedU32,
UnsignedU64, UnsignedU8, UnsignedU64, UnsignedU8,
}; };
use crate::ByteConversionError;
use std::format; use std::format;
#[test] #[test]
@ -553,4 +554,70 @@ pub mod tests {
assert_eq!(u64.width, 8); assert_eq!(u64.width, 8);
assert_eq!(u64.value, 0x0102030405060708); assert_eq!(u64.value, 0x0102030405060708);
} }
#[test]
fn type_u16_target_buf_too_small() {
let u16 = UnsignedU16::new(500);
let mut buf: [u8; 1] = [0; 1];
let res = u16.write_to_be_bytes(&mut buf);
assert!(res.is_err());
let err = res.unwrap_err();
match err {
ByteConversionError::ToSliceTooSmall(missmatch) => {
assert_eq!(missmatch.found, 1);
assert_eq!(missmatch.expected, 2);
}
_ => {
panic!("invalid exception")
}
}
}
#[test]
fn type_erased_u16_target_buf_too_small() {
let u16 = UnsignedByteField::new(2, 500);
let mut buf: [u8; 1] = [0; 1];
let res = u16.write_to_be_bytes(&mut buf);
assert!(res.is_err());
let err = res.unwrap_err();
match err {
ByteConversionError::ToSliceTooSmall(missmatch) => {
assert_eq!(missmatch.found, 1);
assert_eq!(missmatch.expected, 2);
}
_ => {
panic!("invalid exception {}", err)
}
}
let u16 = UnsignedByteField::new_from_be_bytes(2, &buf);
assert!(u16.is_err());
let err = u16.unwrap_err();
if let UnsignedByteFieldError::ByteConversionError(
ByteConversionError::FromSliceTooSmall(missmatch),
) = err
{
assert_eq!(missmatch.expected, 2);
assert_eq!(missmatch.found, 1);
} else {
panic!("unexpected exception {}", err);
}
}
#[test]
fn type_u32_target_buf_too_small() {
let u16 = UnsignedU32::new(500);
let mut buf: [u8; 3] = [0; 3];
let res = u16.write_to_be_bytes(&mut buf);
assert!(res.is_err());
let err = res.unwrap_err();
match err {
ByteConversionError::ToSliceTooSmall(missmatch) => {
assert_eq!(missmatch.found, 3);
assert_eq!(missmatch.expected, 4);
}
_ => {
panic!("invalid exception")
}
}
}
} }