Some tests

This commit is contained in:
Dennis Schwerdel 2017-06-20 15:36:00 +02:00
parent 709e5d1624
commit f78de42980
2 changed files with 189 additions and 0 deletions

View File

@ -1,10 +1,12 @@
use std::ops::Deref; use std::ops::Deref;
#[derive(Clone)]
pub struct Bitmap { pub struct Bitmap {
bytes: Vec<u8> bytes: Vec<u8>
} }
impl Bitmap { impl Bitmap {
/// Creates a new bitmap
pub fn new(len: usize) -> Self { pub fn new(len: usize) -> Self {
let len = (len+7)/8; let len = (len+7)/8;
let mut bytes = Vec::with_capacity(len); let mut bytes = Vec::with_capacity(len);
@ -12,11 +14,13 @@ impl Bitmap {
Self { bytes: bytes } Self { bytes: bytes }
} }
/// Returns the number of bits in the bitmap
#[inline] #[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.bytes.len() * 8 self.bytes.len() * 8
} }
/// Returns whether the bitmap is empty, i.e. contains no bits
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.len() == 0 self.len() == 0
@ -75,3 +79,89 @@ impl Deref for Bitmap {
&self.bytes &self.bytes
} }
} }
mod tests {
#[allow(unused_imports)]
use super::Bitmap;
#[test]
fn test_new() {
Bitmap::new(1024);
}
#[test]
fn test_len() {
assert_eq!(Bitmap::new(1024).len(), 1024);
}
#[test]
fn test_is_empty() {
assert!(!Bitmap::new(1024).is_empty());
assert!(Bitmap::new(0).is_empty());
}
#[test]
fn test_set() {
let mut bitmap = Bitmap::new(1024);
assert!(!bitmap.get(5));
assert!(!bitmap.get(154));
bitmap.set(5);
assert!(bitmap.get(5));
assert!(!bitmap.get(154));
bitmap.set(154);
assert!(bitmap.get(5));
assert!(bitmap.get(154));
}
#[test]
fn test_unset() {
let mut bitmap = Bitmap::new(1024);
assert!(!bitmap.get(5));
bitmap.set(5);
assert!(bitmap.get(5));
bitmap.unset(5);
assert!(!bitmap.get(5));
assert!(!bitmap.get(154));
bitmap.unset(154);
assert!(!bitmap.get(154));
}
#[test]
fn test_flip() {
let mut bitmap = Bitmap::new(1024);
assert!(!bitmap.get(5));
bitmap.flip(5);
assert!(bitmap.get(5));
bitmap.set(154);
assert!(bitmap.get(154));
bitmap.flip(154);
assert!(!bitmap.get(154));
}
#[test]
fn test_as_bytes() {
let mut bitmap = Bitmap::new(16);
assert_eq!(bitmap.as_bytes(), &[0, 0]);
bitmap.set(0);
assert_eq!(bitmap.as_bytes(), &[1, 0]);
bitmap.set(8);
bitmap.set(9);
assert_eq!(bitmap.as_bytes(), &[1, 3]);
}
#[test]
fn test_into_bytes() {
let mut bitmap = Bitmap::new(16);
bitmap.set(0);
bitmap.set(8);
bitmap.set(9);
assert_eq!(bitmap.as_bytes(), &bitmap.clone().into_bytes() as &[u8]);
}
#[test]
fn test_from_bytes() {
assert_eq!(&[1, 3], Bitmap::from_bytes(vec![1, 3]).as_bytes());
}
}

View File

@ -128,3 +128,102 @@ impl<'a> Deserialize<'a> for ChunkList {
Ok(ChunkList::read_n_from(data.len()/20, &mut Cursor::new(data)).unwrap()) Ok(ChunkList::read_n_from(data.len()/20, &mut Cursor::new(data)).unwrap())
} }
} }
mod tests {
#[allow(unused_imports)]
use super::ChunkList;
#[allow(unused_imports)]
use super::super::Hash;
#[allow(unused_imports)]
use super::super::msgpack;
#[test]
fn test_new() {
ChunkList::new();
}
#[test]
fn test_with_capacity() {
ChunkList::with_capacity(0);
ChunkList::with_capacity(1024);
}
#[test]
fn test_push() {
let mut list = ChunkList::new();
assert!(list.is_empty());
assert_eq!(list.len(), 0);
list.push((Hash::default(), 0));
assert!(!list.is_empty());
assert_eq!(list.len(), 1);
list.push((Hash::default(), 1));
assert!(!list.is_empty());
assert_eq!(list.len(), 2);
}
#[test]
fn test_into_inner() {
let mut list = ChunkList::new();
list.push((Hash::default(), 0));
list.push((Hash::default(), 1));
assert_eq!(list.into_inner(), vec![(Hash::default(), 0), (Hash::default(), 1)]);
}
#[test]
fn test_write_to() {
let mut list = ChunkList::new();
list.push((Hash::default(), 0));
list.push((Hash::default(), 1));
let mut buf = Vec::new();
assert!(list.write_to(&mut buf).is_ok());
assert_eq!(buf.len(), 40);
assert_eq!(&buf[16..20], &[0,0,0,0]);
assert_eq!(&buf[36..40], &[1,0,0,0]);
}
#[test]
fn test_encoded_size() {
let mut list = ChunkList::new();
list.push((Hash::default(), 0));
list.push((Hash::default(), 1));
assert_eq!(list.encoded_size(), 40);
}
#[test]
fn test_read_from() {
let data = vec![0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0];
let list = ChunkList::read_from(&data);
assert_eq!(list.len(), 2);
assert_eq!(list[0], (Hash::default(), 0));
assert_eq!(list[1], (Hash::default(), 1));
}
#[test]
fn test_serialize() {
let mut list = ChunkList::new();
list.push((Hash::default(), 0));
list.push((Hash::default(), 1));
let mut buf = Vec::new();
assert!(list.write_to(&mut buf).is_ok());
let encoded = msgpack::encode(&list).unwrap();
assert_eq!(buf, &encoded[2..]);
assert_eq!(&[196,40], &encoded[..2]);
}
#[test]
fn test_deserialize() {
let mut list = ChunkList::new();
list.push((Hash::default(), 0));
list.push((Hash::default(), 1));
let mut buf = vec![196,40];
assert!(list.write_to(&mut buf).is_ok());
assert!(msgpack::decode::<ChunkList>(&buf).is_ok());
assert_eq!(msgpack::decode::<ChunkList>(&buf).unwrap(), list);
}
}