memscope_rs/core/
test_optimized_locks.rs

1//! Tests for optimized lock implementations
2
3#[cfg(test)]
4mod tests {
5    use crate::core::scope_tracker::{get_global_scope_tracker, ScopeGuard};
6
7    #[test]
8    fn test_optimized_scope_tracker_basic() {
9        let tracker = get_global_scope_tracker();
10
11        // Test basic scope operations
12        let scope_id = tracker
13            .enter_scope("test_scope".to_string())
14            .expect("Failed to enter scope");
15
16        // Associate a variable
17        tracker
18            .associate_variable("test_var".to_string(), 64)
19            .expect("Failed to track allocation");
20
21        // Exit scope
22        tracker.exit_scope(scope_id).expect("Failed to exit scope");
23
24        // Get analysis
25        let analysis = tracker
26            .get_scope_analysis()
27            .expect("Failed to get scope analysis");
28        assert!(analysis.total_scopes > 0);
29    }
30
31    #[test]
32    fn test_scope_guard_raii() {
33        // Test basic RAII functionality - scope guard should not panic
34        let result = std::panic::catch_unwind(|| {
35            let _guard = ScopeGuard::enter("raii_test_scope").expect("Failed to enter RAII scope");
36            // If we get here, the guard was created successfully
37            true
38        });
39
40        assert!(
41            result.is_ok(),
42            "ScopeGuard creation and drop should not panic"
43        );
44
45        // Test that we can create multiple guards without issues
46        let result2 = std::panic::catch_unwind(|| {
47            let _guard1 =
48                ScopeGuard::enter("raii_test_scope_1").expect("Failed to enter RAII scope 1");
49            let _guard2 =
50                ScopeGuard::enter("raii_test_scope_2").expect("Failed to enter RAII scope 2");
51            // Both guards should be dropped cleanly
52            true
53        });
54
55        assert!(
56            result2.is_ok(),
57            "Multiple ScopeGuards should work correctly"
58        );
59    }
60}