From 8a194aa41d8d5f043b7f3357762c58a09d18ddd6 Mon Sep 17 00:00:00 2001 From: sergerad Date: Mon, 2 Sep 2024 21:11:16 +1200 Subject: [PATCH] buff unit tests --- core/src/utils/racy_lock.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/core/src/utils/racy_lock.rs b/core/src/utils/racy_lock.rs index df78874ee..600234819 100644 --- a/core/src/utils/racy_lock.rs +++ b/core/src/utils/racy_lock.rs @@ -72,17 +72,40 @@ where #[cfg(test)] mod tests { + use alloc::vec::Vec; + use super::*; #[test] - fn test_lazylock_copy() { + fn test_racy_lock_copy() { + // Lock a copy type and validate value. let lock = RacyLock::new(|| 42); assert_eq!(*lock, 42); } #[test] - fn test_lazylock_move() { - let lock = RacyLock::new(|| vec![1, 2, 3]); - assert_eq!(*lock, vec![1, 2, 3]); + fn test_racy_lock_clone() { + // Lock a no copy type. + let lock = RacyLock::new(|| Vec::from([1, 2, 3])); + + // Use the value so that the compiler forces us to clone. + let mut v = lock.clone(); + v.push(4); + + // Validate the value. + assert_eq!(v, Vec::from([1, 2, 3, 4])); + } + + #[test] + fn test_racy_lock_static() { + // Create a static lock. + static VEC: RacyLock> = RacyLock::new(|| Vec::from([1, 2, 3])); + + // Validate that the address of the value does not change. + let addr = &*VEC as *const Vec; + for _ in 0..5 { + assert_eq!(*VEC, [1, 2, 3]); + assert_eq!(addr, &(*VEC) as *const Vec) + } } }