From 04f65ae7748ad73a0daa180c1398fdd2a0b1d4fc Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 17 May 2024 17:38:51 +0200 Subject: [PATCH] unit tests for BitVec --- servicepoint2/src/bit_vec.rs | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/servicepoint2/src/bit_vec.rs b/servicepoint2/src/bit_vec.rs index e1c9c6a..4313287 100644 --- a/servicepoint2/src/bit_vec.rs +++ b/servicepoint2/src/bit_vec.rs @@ -217,3 +217,72 @@ pub mod c_api { } } } + +#[cfg(test)] +mod tests { + use crate::BitVec; + + #[test] + fn fill() { + let mut vec = BitVec::new(8 * 3); + assert_eq!(vec.data, [0x00, 0x00, 0x00]); + + vec.fill(true); + assert_eq!(vec.data, [0xFF, 0xFF, 0xFF]); + + vec.fill(false); + assert_eq!(vec.data, [0x00, 0x00, 0x00]); + } + + #[test] + fn get_set() { + let mut vec = BitVec::new(8 * 3); + assert_eq!(vec.get(1), false); + assert_eq!(vec.get(11), false); + + vec.set(1, true); + vec.set(11, true); + assert_eq!(vec.data, [0x40, 0x10, 0x00]); + assert_eq!(vec.get(0), false); + assert_eq!(vec.get(1), true); + assert_eq!(vec.get(11), true); + } + + #[test] + fn load() { + let mut vec = BitVec::new(8 * 3); + vec.set(6, true); + vec.set(7, true); + vec.set(8, true); + vec.set(9, true); + vec.set(10, true); + vec.set(vec.len() - 1, true); + + assert_eq!(vec.data, [0x03, 0xE0, 0x01]); + + let data: Vec = vec.into(); + + let vec = BitVec::from(&*data); + assert_eq!(vec.data, [0x03, 0xE0, 0x01]); + } + + #[test] + fn mut_data_ref(){ + let mut vec = BitVec::new(8 * 3); + + let data_ref = vec.mut_data_ref(); + data_ref.copy_from_slice(&[0x40, 0x10, 0x00]); + + assert_eq!(vec.data, [0x40, 0x10, 0x00]); + assert_eq!(vec.get(1), true); + } + + #[test] + fn is_empty() { + let vec = BitVec::new(8 * 3); + assert_eq!(vec.is_empty(), false); + + let vec = BitVec::new(0); + assert_eq!(vec.is_empty(), true); + } +} \ No newline at end of file