Skip to content

Commit

Permalink
buff unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Sep 2, 2024
1 parent 1453507 commit 8a194aa
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions core/src/utils/racy_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>> = RacyLock::new(|| Vec::from([1, 2, 3]));

// Validate that the address of the value does not change.
let addr = &*VEC as *const Vec<i32>;
for _ in 0..5 {
assert_eq!(*VEC, [1, 2, 3]);
assert_eq!(addr, &(*VEC) as *const Vec<i32>)
}
}
}

0 comments on commit 8a194aa

Please sign in to comment.