From 0cb80ab265f389f054f1f3ee08026596cfd9e6c4 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Sun, 10 May 2020 10:18:32 +1000 Subject: [PATCH] Add tests for Fn impl of Rc and Arc --- src/liballoc/rc/tests.rs | 20 ++++++++++++++++++++ src/liballoc/sync/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/liballoc/rc/tests.rs b/src/liballoc/rc/tests.rs index 56788bb56d550..d847948559082 100644 --- a/src/liballoc/rc/tests.rs +++ b/src/liballoc/rc/tests.rs @@ -434,3 +434,23 @@ fn test_array_from_slice() { let a: Result, _> = r.clone().try_into(); assert!(a.is_err()); } + +#[test] +fn test_fn() { + fn apply_fn_once(v: T, f: impl FnOnce(T)) { + f(v) + } + fn apply_fn_mut(v: T, mut f: impl FnMut(T)) { + f(v) + } + fn apply_fn(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); +} diff --git a/src/liballoc/sync/tests.rs b/src/liballoc/sync/tests.rs index edc2820ee22f1..f4319dabf7ef5 100644 --- a/src/liballoc/sync/tests.rs +++ b/src/liballoc/sync/tests.rs @@ -490,3 +490,23 @@ fn test_array_from_slice() { let a: Result, _> = r.clone().try_into(); assert!(a.is_err()); } + +#[test] +fn test_fn() { + fn apply_fn_once(v: T, f: impl FnOnce(T)) { + f(v) + } + fn apply_fn_mut(v: T, mut f: impl FnMut(T)) { + f(v) + } + fn apply_fn(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); +}