-#[macro_use]
-extern crate global_counter;
+use std::cell::RefCell;
use learning::*;
#[test]
fn test_cacher_int_int() {
- global_default_counter!(CALLED, u32);
+ let called = RefCell::new(0);
let mut cacher = Cacher::new(|x| {
- CALLED.inc();
+ *(called.borrow_mut()) += 1;
2 * x
});
assert_eq!(cacher.value(1), 2);
- assert_eq!(CALLED.get_cloned(), 1);
+ assert_eq!(*called.borrow(), 1);
// second time cached
assert_eq!(cacher.value(1), 2);
- assert_eq!(CALLED.get_cloned(), 1);
+ assert_eq!(*called.borrow(), 1);
// re-evaluated for new value
assert_eq!(cacher.value(-2), -4);
- assert_eq!(CALLED.get_cloned(), 2);
+ assert_eq!(*called.borrow(), 2);
// old arg still cached
assert_eq!(cacher.value(1), 2);
- assert_eq!(CALLED.get_cloned(), 2);
+ assert_eq!(*called.borrow(), 2);
}
#[test]
fn test_cacher_str_usize() {
- global_default_counter!(CALLED, u32);
+ let called = RefCell::new(0);
let mut cacher = Cacher::new(|x: &str| {
- CALLED.inc();
+ *(called.borrow_mut()) += 1;
x.len()
});
assert_eq!(cacher.value("abc"), 3);
- assert_eq!(CALLED.get_cloned(), 1);
+ assert_eq!(*called.borrow(), 1);
// second time cached
assert_eq!(cacher.value("abc"), 3);
- assert_eq!(CALLED.get_cloned(), 1);
+ assert_eq!(*called.borrow(), 1);
// re-evaluated for new value
assert_eq!(cacher.value("defg"), 4);
- assert_eq!(CALLED.get_cloned(), 2);
+ assert_eq!(*called.borrow(), 2);
// old arg still cached
assert_eq!(cacher.value("abc"), 3);
- assert_eq!(CALLED.get_cloned(), 2);
+ assert_eq!(*called.borrow(), 2);
}