use super::*;
use crate::performance_optimization::PerformanceMetrics;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::thread;
impl Default for FormalVerificationEngine {
fn default() -> Self {
Self::new()
}
}
impl FormalVerificationEngine {
pub fn new() -> Self {
Self {
verification_tasks: Arc::new(Mutex::new(HashMap::new())),
results_cache: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn verify_contract(&self, contract: &ApiContract) -> CoreResult<()> {
let taskid = format!("{}-{}", contract.module, contract.apiname);
let properties = self.extract_verification_properties(contract);
let task = VerificationTask {
apiname: contract.apiname.clone(),
module: contract.module.clone(),
properties,
status: VerificationStatus::InProgress,
started_at: Instant::now(),
};
{
let mut tasks = self.verification_tasks.lock().expect("Operation failed");
tasks.insert(taskid.clone(), task);
}
let tasks_clone = Arc::clone(&self.verification_tasks);
let results_clone = Arc::clone(&self.results_cache);
thread::spawn(move || {
let result = Self::perform_verification(&taskid, &tasks_clone);
{
let mut results = results_clone.write().expect("Operation failed");
results.insert(taskid.clone(), result);
}
{
let mut tasks = tasks_clone.lock().expect("Operation failed");
if let Some(task) = tasks.get_mut(&taskid) {
task.status = VerificationStatus::Verified;
}
}
});
Ok(())
}
fn extract_verification_properties(&self, contract: &ApiContract) -> Vec<VerificationProperty> {
let mut properties = Vec::new();
properties.push(VerificationProperty {
name: "performance_bound".to_string(),
specification: format!(
"execution_time <= {:?}",
contract
.performance
.maxexecution_time
.unwrap_or(Duration::from_secs(1))
),
property_type: PropertyType::Safety,
});
if let Some(max_memory) = contract.memory.max_memory {
properties.push(VerificationProperty {
name: "memory_bound".to_string(),
specification: format!("memory_usage <= {max_memory}"),
property_type: PropertyType::Safety,
});
}
if contract.concurrency.thread_safety == ThreadSafety::ThreadSafe {
properties.push(VerificationProperty {
name: "thread_safety".to_string(),
specification: "no_race_conditions AND no_deadlocks".to_string(),
property_type: PropertyType::Safety,
});
}
properties
}
fn verify_task(
taskid: &str,
tasks: &Arc<Mutex<HashMap<String, VerificationTask>>>,
) -> VerificationResult {
let start_time = Instant::now();
thread::sleep(Duration::from_millis(100));
let task = {
let tasks_guard = tasks.lock().expect("Operation failed");
tasks_guard.get(taskid).cloned()
};
if let Some(task) = task {
VerificationResult {
verified: true, verification_time: start_time.elapsed(),
checked_properties: task.properties.iter().map(|p| p.name.clone()).collect(),
counterexample: None,
method: VerificationMethod::StaticAnalysis,
}
} else {
VerificationResult {
verified: false,
verification_time: start_time.elapsed(),
checked_properties: vec![],
counterexample: Some("Task not found".to_string()),
method: VerificationMethod::StaticAnalysis,
}
}
}
pub fn get_verification_status(&self, apiname: &str, module: &str) -> VerificationStatus {
let taskid = format!("{module}-{apiname}");
if let Ok(tasks) = self.verification_tasks.lock() {
if let Some(task) = tasks.get(&taskid) {
return task.status;
}
}
VerificationStatus::NotVerified
}
pub fn get_all_results(&self) -> HashMap<String, VerificationResult> {
if let Ok(results) = self.results_cache.read() {
results.clone()
} else {
HashMap::new()
}
}
pub fn is_verification_complete(&self, apiname: &str, module: &str) -> bool {
matches!(
self.get_verification_status(apiname, module),
VerificationStatus::Verified | VerificationStatus::Failed
)
}
pub fn get_verification_coverage(&self) -> f64 {
if let Ok(tasks) = self.verification_tasks.lock() {
if tasks.is_empty() {
return 0.0;
}
let verified_count = tasks
.values()
.filter(|task| task.status == VerificationStatus::Verified)
.count();
(verified_count as f64 / tasks.len() as f64) * 100.0
} else {
0.0
}
}
fn perform_verification(
_taskid: &str,
_tasks: &Arc<Mutex<HashMap<String, VerificationTask>>>,
) -> VerificationResult {
VerificationResult {
verified: true,
verification_time: Duration::from_millis(100),
checked_properties: vec!["safety_check".to_string()],
counterexample: None,
method: VerificationMethod::StaticAnalysis,
}
}
}
impl RuntimeContractValidator {
pub fn new() -> (Self, Receiver<MonitoringEvent>) {
let (sender, receiver) = mpsc::channel();
let validator = Self {
contracts: Arc::new(RwLock::new(HashMap::new())),
event_sender: sender,
stats: Arc::new(Mutex::new(ValidationStatistics {
total_validations: 0,
violations_detected: 0,
avg_validation_time: Duration::from_nanos(0),
success_rate: 1.0,
})),
chaos_controller: Arc::new(Mutex::new(ChaosEngineeringController {
enabled: false,
faultprobability: 0.01,
active_faults: Vec::new(),
fault_history: Vec::new(),
})),
};
(validator, receiver)
}
pub fn register_contract(&self, contract: ApiContract) {
let key = format!("{}-{}", contract.module, contract.apiname);
if let Ok(mut contracts) = self.contracts.write() {
contracts.insert(key, contract);
}
}
pub fn validate_api_call(
&self,
apiname: &str,
module: &str,
context: &ApiCallContext,
) -> CoreResult<()> {
let start_time = Instant::now();
let key = format!("{module}-{apiname}");
{
if let Ok(mut stats) = self.stats.lock() {
stats.total_validations += 1;
}
}
self.maybe_inject_fault(apiname, module)?;
let contract = {
if let Ok(contracts) = self.contracts.read() {
contracts.get(&key).cloned()
} else {
return Err(CoreError::ValidationError(ErrorContext::new(
"Cannot access contracts for validation".to_string(),
)));
}
};
let contract = contract.ok_or_else(|| {
CoreError::ValidationError(ErrorContext::new(format!(
"No contract found for {module}::{apiname}"
)))
})?;
if let Some(max_time) = contract.performance.maxexecution_time {
if context.execution_time > max_time {
self.report_violation(
apiname,
module,
ContractViolation {
violation_type: ViolationType::Performance,
expected: format!("{max_time:?}"),
actual: format!("{:?}", context.execution_time),
severity: ViolationSeverity::High,
},
)?;
}
}
if let Some(max_memory) = contract.memory.max_memory {
if context.memory_usage > max_memory {
self.report_violation(
apiname,
module,
ContractViolation {
violation_type: ViolationType::Memory,
expected: format!("{max_memory}"),
actual: context.memory_usage.to_string(),
severity: ViolationSeverity::Medium,
},
)?;
}
}
let validation_time = start_time.elapsed();
{
if let Ok(mut stats) = self.stats.lock() {
let total = stats.total_validations as f64;
let prev_avg = stats.avg_validation_time.as_nanos() as f64;
let new_avg =
(prev_avg * (total - 1.0) + validation_time.as_nanos() as f64) / total;
stats.avg_validation_time = Duration::from_nanos(new_avg as u64);
stats.success_rate = (total - stats.violations_detected as f64) / total;
}
}
Ok(())
}
pub fn enable_chaos_engineering(&self, faultprobability: f64) {
if let Ok(mut controller) = self.chaos_controller.lock() {
controller.enabled = true;
controller.faultprobability = faultprobability.clamp(0.0, 1.0);
}
}
fn maybe_inject_fault(&self, apiname: &str, module: &str) -> CoreResult<()> {
if let Ok(mut controller) = self.chaos_controller.lock() {
if !controller.enabled {
return Ok(());
}
let mut hasher = DefaultHasher::new();
apiname.hash(&mut hasher);
module.hash(&mut hasher);
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
.hash(&mut hasher);
let rand_val = (hasher.finish() % 10000) as f64 / 10000.0;
if rand_val < controller.faultprobability {
let fault = match rand_val * 4.0 {
x if x < 1.0 => ChaosFault::LatencyInjection(Duration::from_millis(100)),
x if x < 2.0 => ChaosFault::MemoryPressure(1024 * 1024), x if x < 3.0 => ChaosFault::CpuThrottling(0.5),
_ => ChaosFault::RandomFailure(0.1),
};
controller.active_faults.push(fault.clone());
controller
.fault_history
.push((Instant::now(), fault.clone()));
let event = MonitoringEvent {
timestamp: Instant::now(),
apiname: apiname.to_string(),
module: module.to_string(),
event_type: MonitoringEventType::ChaosEngineeringFault(fault.clone()),
performance_metrics: RuntimePerformanceMetrics {
execution_time: Duration::from_nanos(0),
memory_usage: 0,
cpu_usage: 0.0,
cache_hit_rate: 0.0,
thread_count: 1,
},
thread_id: format!("{:?}", thread::current().id()),
};
let _ = self.event_sender.send(event);
match fault {
ChaosFault::LatencyInjection(delay) => {
thread::sleep(delay);
}
ChaosFault::RandomFailure(prob) if rand_val < prob => {
return Err(CoreError::ValidationError(ErrorContext::new(
"Chaos engineering: Random failure injected".to_string(),
)));
}
_ => {} }
}
}
Ok(())
}
fn report_violation(
&self,
apiname: &str,
module: &str,
violation: ContractViolation,
) -> CoreResult<()> {
{
if let Ok(mut stats) = self.stats.lock() {
stats.violations_detected += 1;
let total = stats.total_validations as f64;
stats.success_rate = (total - stats.violations_detected as f64) / total;
}
}
let event = MonitoringEvent {
timestamp: Instant::now(),
apiname: apiname.to_string(),
module: module.to_string(),
event_type: MonitoringEventType::ContractViolation(violation.clone()),
performance_metrics: RuntimePerformanceMetrics {
execution_time: Duration::from_nanos(0),
memory_usage: 0,
cpu_usage: 0.0,
cache_hit_rate: 0.0,
thread_count: 1,
},
thread_id: format!("{:?}", thread::current().id()),
};
let _ = self.event_sender.send(event);
if violation.severity >= ViolationSeverity::High {
return Err(CoreError::ValidationError(ErrorContext::new(format!(
"Critical contract violation in {}::{}: {} (expected: {}, actual: {})",
module,
apiname,
match violation.violation_type {
ViolationType::Performance => "Performance",
ViolationType::Memory => "Memory",
ViolationType::Numerical => "Numerical",
ViolationType::Concurrency => "Concurrency",
ViolationType::Behavioral => "Behavioral",
},
violation.expected,
violation.actual
))));
}
Ok(())
}
pub fn get_statistics(&self) -> Option<ValidationStatistics> {
self.stats.lock().ok().map(|stats| stats.clone())
}
pub fn get_chaos_status(&self) -> Option<(bool, f64, usize)> {
if let Ok(controller) = self.chaos_controller.lock() {
Some((
controller.enabled,
controller.faultprobability,
controller.fault_history.len(),
))
} else {
None
}
}
pub fn disable_chaos_engineering(&self) {
if let Ok(mut controller) = self.chaos_controller.lock() {
controller.enabled = false;
controller.active_faults.clear();
}
}
}
#[derive(Debug, Clone)]
pub struct ApiCallContext {
pub execution_time: Duration,
pub memory_usage: usize,
pub input_hash: String,
pub output_hash: String,
pub thread_id: String,
}
impl Default for AdvancedPerformanceModeler {
fn default() -> Self {
Self::new()
}
}
impl AdvancedPerformanceModeler {
pub fn new() -> Self {
Self {
performance_history: Arc::new(RwLock::new(Vec::new())),
prediction_models: Arc::new(RwLock::new(HashMap::new())),
training_status: Arc::new(Mutex::new(HashMap::new())),
}
}
pub fn record_measurement(
&self,
apiname: &str,
input_characteristics: InputCharacteristics,
performance: PerformanceMetrics,
system_state: SystemState,
) {
let runtime_performance = RuntimePerformanceMetrics {
execution_time: Duration::from_secs_f64(
performance.operation_times.values().sum::<f64>()
/ performance.operation_times.len().max(1) as f64,
),
memory_usage: 0, cpu_usage: 0.0, cache_hit_rate: performance.cache_hit_rate,
thread_count: 1, };
let data_point = PerformanceDataPoint {
timestamp: Instant::now(),
apiname: apiname.to_string(),
input_characteristics,
performance: runtime_performance,
system_state,
};
if let Ok(mut history) = self.performance_history.write() {
history.push(data_point);
if history.len() > 10000 {
history.remove(0);
}
}
self.maybe_retrain_model(apiname);
}
pub fn predict_performance(
&self,
apiname: &str,
input_characteristics: InputCharacteristics,
system_state: &SystemState,
) -> Option<RuntimePerformanceMetrics> {
if let Ok(models) = self.prediction_models.read() {
if let Some(model) = models.get(apiname) {
let base_time = Duration::from_nanos(1000);
let size_factor = match model.model_type {
ModelType::LinearRegression => {
if model.parameters.len() >= 2 {
model.parameters[0] * input_characteristics.size as f64
+ model.parameters[1]
} else {
(input_characteristics.size as f64).sqrt()
}
}
ModelType::PolynomialRegression => (input_characteristics.size as f64).sqrt(),
_ => (input_characteristics.size as f64).sqrt(),
};
let scaled_time = Duration::from_nanos(
(base_time.as_nanos() as f64 * size_factor.max(1.0)) as u64,
);
return Some(RuntimePerformanceMetrics {
execution_time: scaled_time,
memory_usage: input_characteristics.size * 8, cpu_usage: system_state.cpu_utilization * 1.1, cache_hit_rate: 0.8, thread_count: 1,
});
}
}
None
}
fn maybe_retrain_model(&self, apiname: &str) {
let should_retrain = {
if let Ok(history) = self.performance_history.read() {
let api_data_points = history.iter().filter(|dp| dp.apiname == apiname).count();
api_data_points > 100 && (api_data_points % 50 == 0)
} else {
false
}
};
if should_retrain {
self.train_model(apiname);
}
}
fn train_model(&self, apiname: &str) {
{
if let Ok(mut status) = self.training_status.lock() {
status.insert(apiname.to_string(), TrainingStatus::InProgress);
}
}
let apiname = apiname.to_string();
let history_clone = Arc::clone(&self.performance_history);
let models_clone = Arc::clone(&self.prediction_models);
let status_clone = Arc::clone(&self.training_status);
thread::spawn(move || {
let training_data = {
if let Ok(history) = history_clone.read() {
history
.iter()
.filter(|dp| dp.apiname == apiname)
.cloned()
.collect::<Vec<_>>()
} else {
Vec::new()
}
};
if training_data.len() < 10 {
if let Ok(mut status) = status_clone.lock() {
status.insert(apiname.clone(), TrainingStatus::Failed);
}
return;
}
let mut sum_x = 0.0;
let mut sum_y = 0.0;
let mut sum_xy = 0.0;
let mut sum_x2 = 0.0;
let n = training_data.len() as f64;
for dp in &training_data {
let x = dp.input_characteristics.size as f64;
let y = dp.performance.execution_time.as_nanos() as f64;
sum_x += x;
sum_y += y;
sum_xy += x * y;
sum_x2 += x * x;
}
let slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
let intercept = (sum_y - slope * sum_x) / n;
let y_mean = sum_y / n;
let mut ss_tot = 0.0;
let mut ss_res = 0.0;
for dp in &training_data {
let x = dp.input_characteristics.size as f64;
let y = dp.performance.execution_time.as_nanos() as f64;
let y_pred = slope * x + intercept;
ss_tot += (y - y_mean).powi(2);
ss_res += (y - y_pred).powi(2);
}
let r_squared = if ss_tot > 0.0 {
1.0 - (ss_res / ss_tot)
} else {
0.0
};
let model = PerformancePredictionModel {
model_type: ModelType::LinearRegression,
parameters: vec![slope, intercept],
accuracy: r_squared.clamp(0.0, 1.0),
training_data_size: training_data.len(),
last_updated: Instant::now(),
};
{
if let Ok(mut models) = models_clone.write() {
models.insert(apiname.clone(), model);
}
}
{
if let Ok(mut status) = status_clone.lock() {
status.insert(apiname, TrainingStatus::Completed);
}
}
});
}
pub fn get_training_status(&self, apiname: &str) -> TrainingStatus {
if let Ok(status) = self.training_status.lock() {
status
.get(apiname)
.copied()
.unwrap_or(TrainingStatus::NotStarted)
} else {
TrainingStatus::NotStarted
}
}
pub fn get_model_accuracy(&self, apiname: &str) -> Option<f64> {
if let Ok(models) = self.prediction_models.read() {
models.get(apiname).map(|model| model.accuracy)
} else {
None
}
}
pub fn get_data_point_count(&self, apiname: &str) -> usize {
if let Ok(history) = self.performance_history.read() {
history.iter().filter(|dp| dp.apiname == apiname).count()
} else {
0
}
}
}
impl Default for ImmutableAuditTrail {
fn default() -> Self {
Self::new()
}
}
impl ImmutableAuditTrail {
pub fn new() -> Self {
Self {
audit_chain: Arc::new(RwLock::new(Vec::new())),
current_hash: Arc::new(RwLock::new(0.to_string())),
}
}
pub fn add_record(&self, data: AuditData) -> CoreResult<()> {
let timestamp = SystemTime::now();
let previous_hash = {
if let Ok(hash) = self.current_hash.read() {
hash.clone()
} else {
return Err(CoreError::ValidationError(ErrorContext::new(
"Cannot access current hash".to_string(),
)));
}
};
let mut record = AuditRecord {
timestamp,
previous_hash: previous_hash.clone(),
data,
signature: String::new(), record_hash: String::new(),
};
record.record_hash = self.calculate_record_hash(&record);
record.signature = record.record_hash.to_string();
{
if let Ok(mut chain) = self.audit_chain.write() {
chain.push(record.clone());
} else {
return Err(CoreError::ValidationError(ErrorContext::new(
"Cannot access audit chain".to_string(),
)));
}
}
{
if let Ok(mut hash) = self.current_hash.write() {
*hash = record.record_hash;
}
}
Ok(())
}
fn calculate_record_hash(&self, record: &AuditRecord) -> String {
let mut hasher = DefaultHasher::new();
record
.timestamp
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos()
.hash(&mut hasher);
record.previous_hash.hash(&mut hasher);
match &record.data {
AuditData::ContractRegistration(name) => name.hash(&mut hasher),
AuditData::ContractValidation {
apiname,
module,
result,
} => {
apiname.hash(&mut hasher);
module.hash(&mut hasher);
result.hash(&mut hasher);
}
AuditData::PerformanceMeasurement {
apiname,
module,
metrics,
} => {
apiname.hash(&mut hasher);
module.hash(&mut hasher);
metrics.hash(&mut hasher);
}
AuditData::ViolationDetection {
apiname,
module,
violation,
} => {
apiname.hash(&mut hasher);
module.hash(&mut hasher);
violation.hash(&mut hasher);
}
}
format!("{:x}", hasher.finish())
}
pub fn verify_integrity(&self) -> bool {
if let Ok(chain) = self.audit_chain.read() {
if chain.is_empty() {
return true;
}
for (i, record) in chain.iter().enumerate() {
let expected_hash = self.calculate_record_hash(record);
if record.record_hash != expected_hash {
return false;
}
if i > 0 {
let prev_record = &chain[i.saturating_sub(1)];
if record.previous_hash != prev_record.record_hash {
return false;
}
}
}
true
} else {
false
}
}
pub fn len(&self) -> usize {
if let Ok(chain) = self.audit_chain.read() {
chain.len()
} else {
0
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn get_recent_records(&self, count: usize) -> Vec<AuditRecord> {
if let Ok(chain) = self.audit_chain.read() {
let start = chain.len().saturating_sub(count);
chain[start..].to_vec()
} else {
Vec::new()
}
}
#[cfg(feature = "serialization")]
pub fn export_trail(&self) -> CoreResult<String> {
if let Ok(chain) = self.audit_chain.read() {
serde_json::to_string_pretty(&*chain).map_err(|e| {
CoreError::ValidationError(ErrorContext::new(format!(
"Failed to serialize audit trail: {e}"
)))
})
} else {
Err(CoreError::ValidationError(ErrorContext::new(
"Cannot access audit chain for export".to_string(),
)))
}
}
#[cfg(not(feature = "serialization"))]
pub fn export_trail(&self) -> CoreResult<String> {
Err(CoreError::ValidationError(ErrorContext::new(
"Audit trail export requires serialization feature".to_string(),
)))
}
}
impl InputCharacteristics {
pub fn new(size: usize, datatype: String) -> Self {
Self {
size,
datatype,
memory_layout: "contiguous".to_string(),
access_pattern: "sequential".to_string(),
}
}
pub fn matrix(rows: usize, cols: usize) -> Self {
Self {
size: rows * cols,
datatype: "f64".to_string(),
memory_layout: "row_major".to_string(),
access_pattern: "matrix".to_string(),
}
}
pub fn vector(length: usize) -> Self {
Self {
size: length,
datatype: "f64".to_string(),
memory_layout: "contiguous".to_string(),
access_pattern: "sequential".to_string(),
}
}
}
impl SystemState {
pub fn new() -> Self {
Self {
cpu_utilization: 0.5, memory_utilization: 0.6, io_load: 0.1, network_load: 0.05, temperature: 65.0, }
}
pub fn current() -> Self {
Self::new()
}
}
impl Default for InputCharacteristics {
fn default() -> Self {
Self::new(1000, "f64".to_string())
}
}
impl Default for SystemState {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_formal_verification_engine() {
let engine = FormalVerificationEngine::new();
assert_eq!(engine.get_verification_coverage(), 0.0);
let contract = ApiContract {
apiname: "test_api".to_string(),
module: "test_module".to_string(),
contract_hash: "test_hash".to_string(),
created_at: SystemTime::now(),
verification_status: VerificationStatus::NotVerified,
stability: StabilityLevel::Stable,
since_version: Version::new(1, 0, 0),
performance: PerformanceContract {
time_complexity: ComplexityBound::Linear,
space_complexity: ComplexityBound::Constant,
maxexecution_time: Some(Duration::from_millis(100)),
min_throughput: None,
memorybandwidth: None,
},
numerical: NumericalContract {
precision: PrecisionGuarantee::MachinePrecision,
stability: NumericalStability::Stable,
input_domain: InputDomain {
ranges: vec![],
exclusions: vec![],
special_values: SpecialValueHandling::Propagate,
},
output_range: OutputRange {
bounds: None,
monotonic: None,
continuous: true,
},
},
concurrency: ConcurrencyContract {
thread_safety: ThreadSafety::ThreadSafe,
atomicity: AtomicityGuarantee::OperationAtomic,
lock_free: false,
wait_free: false,
memory_ordering: MemoryOrdering::AcquireRelease,
},
memory: MemoryContract {
allocation_pattern: AllocationPattern::SingleAllocation,
max_memory: Some(1024),
alignment: None,
locality: LocalityGuarantee::GoodSpatial,
gc_behavior: GcBehavior::MinimalGc,
},
deprecation: None,
};
engine.verify_contract(&contract).expect("Operation failed");
let status = engine.get_verification_status("test_api", "test_module");
assert_ne!(status, VerificationStatus::NotVerified);
}
#[test]
fn test_runtime_contract_validator() {
let (validator, receiver) = RuntimeContractValidator::new();
let stats = validator.get_statistics().expect("Operation failed");
assert_eq!(stats.total_validations, 0);
assert_eq!(stats.violations_detected, 0);
assert_eq!(stats.success_rate, 1.0);
}
#[test]
fn test_performance_modeler() {
let modeler = AdvancedPerformanceModeler::new();
let input_chars = InputCharacteristics::new(1000, "f64".to_string());
let system_state = SystemState::new();
let performance = PerformanceMetrics {
operation_times: std::collections::HashMap::new(),
strategy_success_rates: std::collections::HashMap::new(),
memorybandwidth_utilization: 0.8,
cache_hit_rate: 0.8,
parallel_efficiency: 0.9,
};
modeler.record_measurement(
"test_api",
input_chars.clone(),
performance,
system_state.clone(),
);
assert_eq!(modeler.get_data_point_count("test_api"), 1);
assert_eq!(
modeler.get_training_status("test_api"),
TrainingStatus::NotStarted
);
}
#[test]
fn test_audit_trail() {
let trail = ImmutableAuditTrail::new();
assert!(trail.is_empty());
assert!(trail.verify_integrity());
let data = AuditData::ContractRegistration("test::api".to_string());
trail.add_record(data).expect("Operation failed");
assert_eq!(trail.len(), 1);
assert!(trail.verify_integrity());
}
#[test]
fn test_input_characteristics() {
let chars = InputCharacteristics::matrix(10, 10);
assert_eq!(chars.size, 100);
assert_eq!(chars.memory_layout, "row_major");
let vector_chars = InputCharacteristics::vector(50);
assert_eq!(vector_chars.size, 50);
assert_eq!(vector_chars.access_pattern, "sequential");
}
#[test]
fn test_system_state() {
let state = SystemState::current();
assert!(state.cpu_utilization >= 0.0 && state.cpu_utilization <= 1.0);
assert!(state.memory_utilization >= 0.0 && state.memory_utilization <= 1.0);
assert!(state.temperature > 0.0);
}
}