Browse Source

Some tests

pull/10/head
Dennis Schwerdel 5 years ago
parent
commit
f78de42980
  1. 90
      src/util/bitmap.rs
  2. 99
      src/util/chunk.rs

90
src/util/bitmap.rs

@ -1,10 +1,12 @@
use std::ops::Deref;
#[derive(Clone)]
pub struct Bitmap {
bytes: Vec<u8>
}
impl Bitmap {
/// Creates a new bitmap
pub fn new(len: usize) -> Self {
let len = (len+7)/8;
let mut bytes = Vec::with_capacity(len);
@ -12,11 +14,13 @@ impl Bitmap {
Self { bytes: bytes }
}
/// Returns the number of bits in the bitmap
#[inline]
pub fn len(&self) -> usize {
self.bytes.len() * 8
}
/// Returns whether the bitmap is empty, i.e. contains no bits
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
@ -75,3 +79,89 @@ impl Deref for Bitmap {
&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());
}
}

99
src/util/chunk.rs

@ -128,3 +128,102 @@ impl<'a> Deserialize<'a> for ChunkList {
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);
}
}

Loading…
Cancel
Save