use auto_allocator::get_allocator_info;
#[test]
fn test_allocator_selection_consistency() {
let info1 = get_allocator_info();
let info2 = get_allocator_info();
assert_eq!(info1.allocator_type, info2.allocator_type);
assert_eq!(info1.reason, info2.reason);
}
#[test]
fn test_platform_specific_allocator_selection() {
let info = get_allocator_info();
#[cfg(target_arch = "wasm32")]
{
assert_eq!(info.allocator_type, auto_allocator::AllocatorType::System);
assert!(info.reason.contains("WASM") || info.reason.contains("compatibility"));
}
#[cfg(debug_assertions)]
{
assert_eq!(info.allocator_type, auto_allocator::AllocatorType::System);
assert!(info.reason.contains("Debug") || info.reason.contains("debug"));
}
#[cfg(all(
not(debug_assertions),
not(target_arch = "wasm32"),
target_os = "none"
))]
{
assert_eq!(info.allocator_type, auto_allocator::AllocatorType::EmbeddedHeap);
assert!(info.reason.contains("embedded") || info.reason.contains("Embedded"));
}
#[cfg(all(
not(debug_assertions),
not(target_arch = "wasm32"),
target_env = "msvc",
not(target_os = "none")
))]
{
println!("Windows MSVC allocator: {:?}", info.allocator_type);
}
#[cfg(all(
not(debug_assertions),
not(target_arch = "wasm32"),
any(
target_os = "windows",
target_os = "macos",
all(target_os = "linux", target_env = "gnu")
),
not(target_os = "none")
))]
{
assert_eq!(info.allocator_type, auto_allocator::AllocatorType::Mimalloc);
assert!(info.reason.contains("performance") || info.reason.contains("optimal") || info.reason.contains("mimalloc"));
}
}
#[test]
fn test_memory_allocation_basic() {
let data: Vec<u8> = vec![0; 1024];
assert_eq!(data.len(), 1024);
let string_data = String::from("Hello, auto-allocator!");
assert_eq!(string_data, "Hello, auto-allocator!");
}
#[test]
fn test_memory_allocation_stress() {
let mut allocations = Vec::new();
for size in [16, 64, 256, 1024, 4096] {
let data: Vec<u8> = vec![42; size];
assert_eq!(data.len(), size);
assert!(data.iter().all(|&x| x == 42));
allocations.push(data);
}
for (i, alloc) in allocations.iter().enumerate() {
let expected_size = [16, 64, 256, 1024, 4096][i];
assert_eq!(alloc.len(), expected_size);
assert!(alloc.iter().all(|&x| x == 42));
}
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn test_system_info_detection() {
let info = get_allocator_info();
assert!(info.system_info.cpu_cores >= 1);
assert!(info.system_info.cpu_cores <= 1024);
assert!(info.system_info.total_memory_bytes > 0);
#[cfg(target_os = "windows")]
assert_eq!(info.system_info.os_type, "windows");
#[cfg(target_os = "macos")]
assert_eq!(info.system_info.os_type, "macos");
#[cfg(target_os = "linux")]
assert_eq!(info.system_info.os_type, "linux");
#[cfg(debug_assertions)]
assert!(info.system_info.is_debug);
#[cfg(not(debug_assertions))]
assert!(!info.system_info.is_debug);
assert_eq!(info.system_info.is_wasm, cfg!(target_arch = "wasm32"));
}
#[test]
fn test_allocator_optimization_check() {
let (is_optimal, suggestion) = auto_allocator::check_allocator_optimization();
#[cfg(debug_assertions)]
{
assert!(is_optimal);
assert!(suggestion.is_none());
}
#[cfg(target_arch = "wasm32")]
{
assert!(is_optimal);
assert!(suggestion.is_none());
}
#[cfg(all(
not(debug_assertions),
not(target_arch = "wasm32"),
any(
target_os = "windows",
target_os = "macos",
all(target_os = "linux", target_env = "gnu")
),
not(target_os = "none")
))]
{
assert!(is_optimal);
assert!(suggestion.is_none());
}
if let Some(msg) = suggestion {
assert!(!msg.is_empty());
assert!(msg.len() > 10); }
}
#[test]
fn test_concurrent_access() {
use std::thread;
let handles: Vec<_> = (0..4)
.map(|i| {
thread::spawn(move || {
let info = get_allocator_info();
let data: Vec<u8> = vec![i as u8; 1000];
assert_eq!(data.len(), 1000);
assert!(data.iter().all(|&x| x == i as u8));
info.allocator_type
})
})
.collect();
let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
let first_allocator = results[0];
assert!(results
.iter()
.all(|&allocator| allocator == first_allocator));
}