test(internal): increasing test coverage of internal/cell module

This commit is contained in:
Marius Seritan
2015-05-30 07:39:55 -07:00
parent 1e5d7d403b
commit abbc4d6f7e

View File

@@ -129,3 +129,76 @@ impl<V: ?Sized + fmt::Debug + Any + 'static> Clone for PtrMapCell<V> where Box<V
cell
}
}
#[cfg(test)]
mod test {
use std::any::TypeId;
use super::*;
#[test]
fn test_opt_cell_set() {
let one:OptCell<u32> = OptCell::new(None);
one.set(1);
assert_eq!(*one,Some(1));
}
#[test]
fn test_opt_cell_clone() {
let one:OptCell<u32> = OptCell::new(Some(3));
let stored = *one.clone();
assert_eq!(stored,Some(3));
}
#[test]
fn test_ptr_map_cell_none() {
let type_id = TypeId::of::<u32>();
let pm:PtrMapCell<u32> = PtrMapCell::new();
assert_eq!(pm.get(type_id),None);
}
#[test]
fn test_ptr_map_cell_one() {
let type_id = TypeId::of::<String>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(type_id, Box::new("a".to_string())); }
assert_eq!(pm.get(type_id), Some(&"a".to_string()));
assert_eq!(unsafe {pm.one()}, "a");
}
#[test]
fn test_ptr_map_cell_two() {
let type_id = TypeId::of::<String>();
let type_id2 = TypeId::of::<Vec<u8>>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(type_id, Box::new("a".to_string())); }
unsafe { pm.insert(type_id2, Box::new("b".to_string())); }
assert_eq!(pm.get(type_id), Some(&"a".to_string()));
assert_eq!(pm.get(type_id2), Some(&"b".to_string()));
}
#[test]
fn test_ptr_map_cell_many() {
let id1 = TypeId::of::<String>();
let id2 = TypeId::of::<Vec<u8>>();
let id3 = TypeId::of::<OptCell<String>>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(id1, Box::new("a".to_string())); }
unsafe { pm.insert(id2, Box::new("b".to_string())); }
unsafe { pm.insert(id3, Box::new("c".to_string())); }
assert_eq!(pm.get(id1), Some(&"a".to_string()));
assert_eq!(pm.get(id2), Some(&"b".to_string()));
assert_eq!(pm.get(id3), Some(&"c".to_string()));
}
#[test]
fn test_ptr_map_cell_clone() {
let type_id = TypeId::of::<String>();
let pm:PtrMapCell<String> = PtrMapCell::new();
unsafe { pm.insert(type_id, Box::new("a".to_string())); }
let cloned = pm.clone();
assert_eq!(cloned.get(type_id), Some(&"a".to_string()));
}
}