use super::types::Query;
pub struct QueryPresets;
impl QueryPresets {
pub fn top_allocations_by_size(limit: usize) -> Query {
Query::TopAllocationsBySize { limit }
}
pub fn top_allocations_by_count(limit: usize) -> Query {
Query::TopAllocationsByCount { limit }
}
pub fn memory_leaks(min_age_ms: u64) -> Query {
Query::MemoryLeaks { min_age_ms }
}
pub fn large_allocations(min_size: usize) -> Query {
Query::LargeAllocations { min_size }
}
pub fn thread_memory_stats(thread_id: u64) -> Query {
Query::ThreadMemoryStats { thread_id }
}
pub fn scope_memory_stats(scope_name: String) -> Query {
Query::ScopeMemoryStats { scope_name }
}
pub fn type_memory_stats(type_name: String) -> Query {
Query::TypeMemoryStats { type_name }
}
pub fn system_summary() -> Query {
Query::Summary
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_top_allocations_by_size() {
let query = QueryPresets::top_allocations_by_size(10);
assert!(
matches!(query, Query::TopAllocationsBySize { limit: 10 }),
"Query should be TopAllocationsBySize with limit 10"
);
}
#[test]
fn test_top_allocations_by_count() {
let query = QueryPresets::top_allocations_by_count(25);
assert!(
matches!(query, Query::TopAllocationsByCount { limit: 25 }),
"Query should be TopAllocationsByCount with limit 25"
);
}
#[test]
fn test_memory_leaks() {
let query = QueryPresets::memory_leaks(1000);
assert!(
matches!(query, Query::MemoryLeaks { min_age_ms: 1000 }),
"Query should be MemoryLeaks with min_age_ms 1000"
);
}
#[test]
fn test_large_allocations() {
let query = QueryPresets::large_allocations(1024);
assert!(
matches!(query, Query::LargeAllocations { min_size: 1024 }),
"Query should be LargeAllocations with min_size 1024"
);
}
#[test]
fn test_thread_memory_stats() {
let query = QueryPresets::thread_memory_stats(42);
assert!(
matches!(query, Query::ThreadMemoryStats { thread_id: 42 }),
"Query should be ThreadMemoryStats with thread_id 42"
);
}
#[test]
fn test_scope_memory_stats() {
let query = QueryPresets::scope_memory_stats("main".to_string());
match query {
Query::ScopeMemoryStats { scope_name } => {
assert_eq!(scope_name, "main", "Scope name should be 'main'");
}
_ => panic!("Expected ScopeMemoryStats query"),
}
}
#[test]
fn test_type_memory_stats() {
let query = QueryPresets::type_memory_stats("Vec<u8>".to_string());
match query {
Query::TypeMemoryStats { type_name } => {
assert_eq!(type_name, "Vec<u8>", "Type name should be 'Vec<u8>'");
}
_ => panic!("Expected TypeMemoryStats query"),
}
}
#[test]
fn test_system_summary() {
let query = QueryPresets::system_summary();
assert!(
matches!(query, Query::Summary),
"Query should be Summary variant"
);
}
#[test]
fn test_top_allocations_zero_limit() {
let query = QueryPresets::top_allocations_by_size(0);
assert!(
matches!(query, Query::TopAllocationsBySize { limit: 0 }),
"Query should accept zero limit"
);
}
#[test]
fn test_thread_memory_stats_large_id() {
let large_id = u64::MAX;
let query = QueryPresets::thread_memory_stats(large_id);
assert!(
matches!(query, Query::ThreadMemoryStats { thread_id } if thread_id == large_id),
"Query should handle max u64 thread ID"
);
}
}