memscope_rs/cli/commands/
test.rs

1//! Test command implementation
2//!
3//! This module provides the test subcommand functionality.
4
5use crate::core::tracker::get_global_tracker;
6use crate::track_var;
7use clap::ArgMatches;
8use std::error::Error;
9
10/// Run the test command
11pub fn run_test(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
12    let output_path = matches
13        .get_one::<String>("output")
14        .map(|s| s.as_str())
15        .unwrap_or("enhanced_memory_test");
16
17    println!("🧪 Running enhanced memory tests...");
18    println!("Output path: {}", output_path);
19
20    // Initialize memory tracking
21    crate::init();
22
23    // Run the test
24    run_enhanced_memory_test()?;
25
26    Ok(())
27}
28
29fn run_enhanced_memory_test() -> Result<(), Box<dyn Error>> {
30    use crate::core::tracker::get_global_tracker;
31
32    println!("Creating test allocations...");
33
34    // Create some test allocations
35    let vec1 = vec![1, 2, 3, 4, 5];
36    let vec2 = vec![6, 7, 8, 9, 10];
37    let string1 = String::from("Hello, World!");
38    let boxed1 = Box::new(42);
39
40    track_var!(vec1);
41    track_var!(vec2);
42    track_var!(string1);
43    track_var!(boxed1);
44
45    // Create some temporary objects
46    for i in 0..10 {
47        let temp = vec![i; i];
48        track_var!(temp);
49    }
50
51    // Get all allocations
52    let tracker = get_global_tracker();
53    let _allocations = match tracker.get_active_allocations() {
54        Ok(allocs) => allocs,
55        Err(e) => {
56            println!("Error getting allocations: {}", e);
57            Vec::new()
58        }
59    };
60
61    println!("Enhanced Memory Analysis Summary:");
62    println!("--------------------------------");
63    println!("Total active allocations: {}", _allocations.len());
64
65    // Keep variables alive until the end
66    println!("Vec1 length: {}", vec1.len());
67    println!("Vec2 length: {}", vec2.len());
68    println!("String1 length: {}", string1.len());
69    println!("Boxed1 value: {}", *boxed1);
70
71    Ok(())
72}
73
74fn _test_enhanced_memory_analysis() {
75    // Create some test allocations
76    let vec1 = vec![1, 2, 3, 4, 5];
77    let vec2 = vec![6, 7, 8, 9, 10];
78    let string1 = String::from("Hello, World!");
79    let boxed1 = Box::new(42);
80
81    track_var!(vec1);
82    track_var!(vec2);
83    track_var!(string1);
84    track_var!(boxed1);
85
86    // Create some temporary objects
87    for i in 0..10 {
88        let temp = vec![i; i];
89        track_var!(temp);
90    }
91
92    // Get all allocations
93    let tracker = get_global_tracker();
94    let _allocations = match tracker.get_active_allocations() {
95        Ok(allocs) => allocs,
96        Err(e) => {
97            println!("Error getting allocations: {}", e);
98            Vec::new()
99        }
100    };
101
102    // Run enhanced analysis
103    let report = crate::analysis::analyze_memory_with_enhanced_features()
104        .unwrap_or_else(|e| format!("Error: {}", e));
105
106    // Print summary
107    println!("Enhanced Memory Analysis Summary:");
108    println!("--------------------------------");
109    println!("Report: {}", report);
110
111    // Keep variables alive until the end
112    println!("Vec1 length: {}", vec1.len());
113    println!("Vec2 length: {}", vec2.len());
114    println!("String1 length: {}", string1.len());
115    println!("Boxed1 value: {}", *boxed1);
116}