diff --git a/stdlib/src/parallel.rs b/stdlib/src/parallel.rs index e5f4eaeb..c4d9784c 100644 --- a/stdlib/src/parallel.rs +++ b/stdlib/src/parallel.rs @@ -62,3 +62,79 @@ impl StdlibRegistry { ); } } + +#[cfg(test)] +mod tests { + use super::*; + use techscript_runtime::context::{RuntimeConfig, RuntimeContext}; + + #[test] + fn test_register_parallel() { + let mut registry = StdlibRegistry { + modules: HashMap::new(), + }; + registry.register_parallel(); + + assert!(registry.has_module("std.parallel")); + let module = registry.get_module("std.parallel").unwrap(); + assert_eq!(module.name, "std.parallel"); + assert_eq!(module.version, "1.0.0"); + assert!(module.exports.contains_key("run")); + assert!(module.exports.contains_key("sleep")); + assert!(module.exports.contains_key("num_cpus")); + } + + #[test] + fn test_parallel_num_cpus() { + let mut registry = StdlibRegistry { + modules: HashMap::new(), + }; + registry.register_parallel(); + + let module = registry.get_module("std.parallel").unwrap(); + let num_cpus_fn = module.exports.get("num_cpus").unwrap(); + + let mut ctx = RuntimeContext::new(RuntimeConfig::default()); + let result = num_cpus_fn.call(&mut ctx, vec![]).unwrap(); + + if let RuntimeValue::Int(cpus) = result { + assert!(cpus >= 1); + } else { + panic!("Expected RuntimeValue::Int for num_cpus"); + } + } + + #[test] + fn test_parallel_run() { + let mut registry = StdlibRegistry { + modules: HashMap::new(), + }; + registry.register_parallel(); + + let module = registry.get_module("std.parallel").unwrap(); + let run_fn = module.exports.get("run").unwrap(); + + let mut ctx = RuntimeContext::new(RuntimeConfig::default()); + let result = run_fn + .call(&mut ctx, vec![RuntimeValue::Str("task1".to_string())]) + .unwrap(); + + assert_eq!(result, RuntimeValue::Null); + } + + #[test] + fn test_parallel_sleep() { + let mut registry = StdlibRegistry { + modules: HashMap::new(), + }; + registry.register_parallel(); + + let module = registry.get_module("std.parallel").unwrap(); + let sleep_fn = module.exports.get("sleep").unwrap(); + + let mut ctx = RuntimeContext::new(RuntimeConfig::default()); + let result = sleep_fn.call(&mut ctx, vec![RuntimeValue::Int(0)]).unwrap(); + + assert_eq!(result, RuntimeValue::Null); + } +}