Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions stdlib/src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading