Skip to content

Commit

Permalink
Add tests for Fn impl of Rc and Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
upsuper committed May 10, 2020
1 parent 0caccdf commit 0cb80ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/liballoc/rc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,23 @@ fn test_array_from_slice() {
let a: Result<Rc<[u32; 2]>, _> = r.clone().try_into();
assert!(a.is_err());
}

#[test]
fn test_fn() {
fn apply_fn_once<T>(v: T, f: impl FnOnce(T)) {
f(v)
}
fn apply_fn_mut<T>(v: T, mut f: impl FnMut(T)) {
f(v)
}
fn apply_fn<T>(v: T, f: impl Fn(T)) {
f(v)
}

let x = RefCell::new(0);
let f = Rc::new(|v: i32| *x.borrow_mut() += v);
apply_fn_once(1, f.clone());
apply_fn_mut(2, f.clone());
apply_fn(4, f.clone());
assert_eq!(*x.borrow(), 7);
}
20 changes: 20 additions & 0 deletions src/liballoc/sync/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,23 @@ fn test_array_from_slice() {
let a: Result<Arc<[u32; 2]>, _> = r.clone().try_into();
assert!(a.is_err());
}

#[test]
fn test_fn() {
fn apply_fn_once<T>(v: T, f: impl FnOnce(T)) {
f(v)
}
fn apply_fn_mut<T>(v: T, mut f: impl FnMut(T)) {
f(v)
}
fn apply_fn<T>(v: T, f: impl Fn(T)) {
f(v)
}

let x = Mutex::new(0);
let f = Arc::new(|v: i32| *x.lock().unwrap() += v);
apply_fn_once(1, f.clone());
apply_fn_mut(2, f.clone());
apply_fn(4, f.clone());
assert_eq!(*x.lock().unwrap(), 7);
}

0 comments on commit 0cb80ab

Please sign in to comment.