#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
use proptest::prelude::*;
proptest! {
#[test]
fn basic_property_stability(_input in ".*") {
prop_assert!(true);
}
#[test]
fn module_consistency_check(_x in 0u32..1000) {
prop_assert!(_x < 1001);
}
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod coverage_tests {
use super::*;
fn minimal_wasm_module() -> Vec<u8> {
vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, ]
}
fn mixed_instructions_wasm() -> Vec<u8> {
vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x02, 0x0a, 0x11, 0x01, 0x0f, 0x00, 0x02, 0x7f, 0x41, 0x00, 0x28, 0x02, 0x00, 0x41, 0x01, 0x6a, 0x0c, 0x00, 0x0b, 0x0b, ]
}
fn memory_wasm() -> Vec<u8> {
vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x04, 0x01, 0x01, 0x02, 0x10, ]
}
fn function_call_wasm() -> Vec<u8> {
vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, 0x03, 0x03, 0x02, 0x00, 0x00, 0x0a, 0x09, 0x02, 0x04, 0x00, 0x10, 0x01, 0x0b, 0x02, 0x00, 0x0b, ]
}
#[test]
fn test_async_profiler_new() {
let profiler = AsyncProfiler::new();
assert_eq!(profiler.sample_interval, Duration::from_millis(10));
}
#[test]
fn test_async_profiler_default() {
let profiler = AsyncProfiler::default();
assert_eq!(profiler.sample_interval, Duration::from_millis(10));
}
#[tokio::test]
async fn test_profile_minimal_module() {
let profiler = AsyncProfiler::new();
let result = profiler.profile_module(&minimal_wasm_module()).await;
assert!(result.is_ok());
let report = result.unwrap();
assert_eq!(report.instruction_mix.total_instructions, 0);
}
#[tokio::test]
async fn test_profile_mixed_instructions() {
let profiler = AsyncProfiler::new();
let result = profiler.profile_module(&mixed_instructions_wasm()).await;
assert!(result.is_ok());
let report = result.unwrap();
assert!(report.instruction_mix.total_instructions > 0);
assert!(report.instruction_mix.control_flow > 0);
assert!(report.instruction_mix.memory_ops > 0);
assert!(report.instruction_mix.arithmetic > 0);
}
#[tokio::test]
async fn test_profile_memory_section() {
let profiler = AsyncProfiler::new();
let result = profiler.profile_module(&memory_wasm()).await;
assert!(result.is_ok());
let report = result.unwrap();
assert_eq!(report.memory_usage.initial_pages, 2);
assert_eq!(report.memory_usage.max_pages, Some(16));
}
#[tokio::test]
async fn test_profile_function_calls() {
let profiler = AsyncProfiler::new();
let result = profiler.profile_module(&function_call_wasm()).await;
assert!(result.is_ok());
let report = result.unwrap();
assert!(report.instruction_mix.calls > 0);
}
#[tokio::test]
async fn test_profile_invalid_wasm() {
let profiler = AsyncProfiler::new();
let result = profiler.profile_module(&[0x00, 0x01, 0x02]).await;
assert!(result.is_err());
}
#[test]
fn test_analyze_instruction_mix() {
let profiler = AsyncProfiler::new();
let result = profiler.analyze_instruction_mix(&mixed_instructions_wasm());
assert!(result.is_ok());
let mix = result.unwrap();
assert!(mix.total_instructions > 0);
}
#[test]
fn test_identify_hot_functions() {
let profiler = AsyncProfiler::new();
let result = profiler.identify_hot_functions(&mixed_instructions_wasm());
assert!(result.is_ok());
let hot_functions = result.unwrap();
assert!(!hot_functions.is_empty());
assert!(hot_functions.iter().all(|f| f.percentage > 5.0));
}
#[test]
fn test_analyze_memory_usage_with_memory() {
let profiler = AsyncProfiler::new();
let result = profiler.analyze_memory_usage(&memory_wasm());
assert!(result.is_ok());
let memory = result.unwrap();
assert_eq!(memory.initial_pages, 2);
assert_eq!(memory.max_pages, Some(16));
}
#[test]
fn test_analyze_memory_usage_no_memory() {
let profiler = AsyncProfiler::new();
let result = profiler.analyze_memory_usage(&minimal_wasm_module());
assert!(result.is_ok());
let memory = result.unwrap();
assert_eq!(memory.initial_pages, 1);
assert_eq!(memory.max_pages, Some(256));
}
}