//! Utilities and helper functions for integration tests. #![allow(dead_code)] use std::fmt; use weld::*; pub use weld::data::*; #[derive(Clone, Debug, PartialEq)] #[repr(C)] pub struct Pair { pub ele1: K, pub ele2: V, } impl Pair { pub fn new(a: K, b: V) -> Pair { Pair { ele1: a, ele2: b } } } impl fmt::Display for Pair where K: fmt::Display, V: fmt::Display, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "({}, {})", self.ele1, self.ele2) } } pub fn default_conf() -> WeldConf { conf(1) } pub fn many_threads_conf() -> WeldConf { conf(4) } fn conf(threads: i32) -> WeldConf { let threads = format!("{}", threads); let mut conf = WeldConf::new(); conf.set("weld.threads", threads); conf } /// Compiles and runs some code on a configuration and input data pointer. If the run is /// successful, returns the resulting value. If the run fails (via a runtime error), returns an /// error. Both the value and error must be freed by the caller. The `conf` passed to this /// function is freed. unsafe fn _compile_and_run(code: &str, conf: &WeldConf, ptr: &T) -> WeldResult { let ref input_value = WeldValue::new_from_data(ptr as *const _ as Data); let module = match WeldModule::compile(code, conf) { Ok(module) => module, Err(err) => { return Err(err); } }; let ref mut context = WeldContext::new(conf).unwrap(); module.run(context, input_value) } /// Runs `code` with the given `conf` and input data pointer `ptr`, expecting /// a succeessful result to be returned. Panics if an error is thrown by the runtime. pub fn compile_and_run(code: &str, conf: &WeldConf, ptr: &T) -> WeldValue { unsafe { _compile_and_run(code, conf, ptr).expect("Run failed!") } } /// Runs `code` with the given `conf` and input data pointer `ptr`, expecting /// a succeessful result to be returned. Panics if an error is thrown by the runtime. pub fn compile_and_run_error(code: &str, conf: &WeldConf, ptr: &T) -> WeldError { unsafe { _compile_and_run(code, conf, ptr).expect_err("Expected error!") } }