From 77846511b285e61d505f7337042709f377f8ae4f Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 25 Apr 2023 00:07:14 +0000 Subject: [PATCH] boolean tests --- src/std/atomic/boolean.rs | 33 ++++++++++++++++++++++++++++++++- src/std/atomic/plain.rs | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/std/atomic/boolean.rs b/src/std/atomic/boolean.rs index a380478..019e01e 100644 --- a/src/std/atomic/boolean.rs +++ b/src/std/atomic/boolean.rs @@ -11,7 +11,7 @@ impl Serializable for bool { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum BooleanParseError { OutOfBounds(u8), Eof, @@ -63,3 +63,34 @@ impl Atomic for bool { impl ConstSizeAtomic for bool { const SIZE: usize = 1; } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_parse_false() { + assert_eq!(bool::parse_slice(&[0]).unwrap(), false) + } + + #[test] + fn can_parse_true() { + assert_eq!(bool::parse_slice(&[1]).unwrap(), true) + } + + #[test] + fn cannot_parse_out_of_bound_value() { + assert_eq!(bool::parse_slice(&[2]).unwrap_err(), BooleanParseError::OutOfBounds(2)); + assert_eq!(bool::parse_slice(&[2, 0]).unwrap_err(), BooleanParseError::OutOfBounds(2)); + } + + #[test] + fn cannot_parse_empty_slice() { + assert_eq!(bool::parse_slice(&[]).unwrap_err(), BooleanParseError::Eof); + } + + #[test] + fn cannot_parse_extra_tail() { + assert_eq!(bool::parse_slice(&[0, 10, 20, 30]).unwrap_err(), BooleanParseError::ExtraData(3)); + } +} diff --git a/src/std/atomic/plain.rs b/src/std/atomic/plain.rs index 86c184a..0c9a408 100644 --- a/src/std/atomic/plain.rs +++ b/src/std/atomic/plain.rs @@ -61,7 +61,7 @@ mod tests { use crate::std::*; #[test] - fn test_plain() -> Result<(), PlainParseError> { + fn can_parse_plain_slice() -> Result<(), PlainParseError> { let plain = Plain::parse_slice(b"slice")?; assert_eq!(plain.bytes().as_slice(), b"slice"); Ok(())