+#[macro_use]
+extern crate global_counter;
+
use learning::*;
#[test]
assert_eq!(longest("abc", "defg"), "defg");
}
-// FIXME: How to make this not unsafe?
-static mut CALLED: u32 = 0;
-
#[test]
fn test_cacher_int_int() {
- unsafe { CALLED = 0; }
+ global_default_counter!(CALLED, u32);
let mut cacher = Cacher::new(|x| {
- unsafe { CALLED += 1; }
+ CALLED.inc();
2 * x
});
assert_eq!(cacher.value(1), 2);
- unsafe { assert_eq!(CALLED, 1); }
+ assert_eq!(CALLED.get_cloned(), 1);
// second time cached
assert_eq!(cacher.value(1), 2);
- unsafe { assert_eq!(CALLED, 1); }
+ assert_eq!(CALLED.get_cloned(), 1);
// re-evaluated for new value
assert_eq!(cacher.value(-2), -4);
- unsafe { assert_eq!(CALLED, 2); }
+ assert_eq!(CALLED.get_cloned(), 2);
// old arg still cached
assert_eq!(cacher.value(1), 2);
- unsafe { assert_eq!(CALLED, 2); }
+ assert_eq!(CALLED.get_cloned(), 2);
}
#[test]
fn test_cacher_str_usize() {
- unsafe { CALLED = 0; }
+ global_default_counter!(CALLED, u32);
let mut cacher = Cacher::new(|x: &str| {
- unsafe { CALLED += 1; }
+ CALLED.inc();
x.len()
});
assert_eq!(cacher.value("abc"), 3);
- unsafe { assert_eq!(CALLED, 1); }
+ assert_eq!(CALLED.get_cloned(), 1);
// second time cached
assert_eq!(cacher.value("abc"), 3);
- unsafe { assert_eq!(CALLED, 1); }
+ assert_eq!(CALLED.get_cloned(), 1);
// re-evaluated for new value
assert_eq!(cacher.value("defg"), 4);
- unsafe { assert_eq!(CALLED, 2); }
+ assert_eq!(CALLED.get_cloned(), 2);
// old arg still cached
assert_eq!(cacher.value("abc"), 3);
- unsafe { assert_eq!(CALLED, 2); }
+ assert_eq!(CALLED.get_cloned(), 2);
}