refact: Inline new_value_function(), a test helper
(cherry picked from commit a354accb952558d30a780eb57fd3161be139db7d)
This commit is contained in:
parent
b5f9764c4c
commit
5355b663c0
1 changed files with 42 additions and 58 deletions
|
|
@ -539,7 +539,6 @@ mod tests {
|
|||
use cstr::cstr;
|
||||
use ctor::ctor;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::CStr;
|
||||
use std::fs::read_dir;
|
||||
use std::io::Write as _;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
|
@ -549,39 +548,6 @@ mod tests {
|
|||
test_init();
|
||||
}
|
||||
|
||||
/// A worse quality shortcut for calling [new_value_primop].
|
||||
pub fn new_value_function<const N: usize>(
|
||||
es: &mut EvalState,
|
||||
name: &CStr,
|
||||
f: Box<dyn Fn(&mut EvalState, &[Value; N]) -> Result<Value>>,
|
||||
) -> Result<Value> {
|
||||
if N == 0 {
|
||||
return es.new_value_thunk(
|
||||
"test_thunk",
|
||||
Box::new(move |eval_state| {
|
||||
f(eval_state, {
|
||||
let empty: &[Value] = &[];
|
||||
empty.try_into().unwrap()
|
||||
})
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
let args: [&CStr; N] = [cstr!("arg"); N];
|
||||
|
||||
let prim = primop::PrimOp::new(
|
||||
es,
|
||||
primop::PrimOpMeta {
|
||||
name,
|
||||
args,
|
||||
doc: cstr!("anonymous test function"),
|
||||
},
|
||||
f,
|
||||
)?;
|
||||
|
||||
es.new_value_primop(prim)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_state_new_and_drop() {
|
||||
gc_registering_current_thread(|| {
|
||||
|
|
@ -1360,9 +1326,14 @@ mod tests {
|
|||
let mut es = EvalState::new(store, []).unwrap();
|
||||
let bias: Arc<Mutex<Int>> = Arc::new(Mutex::new(0));
|
||||
let bias_control = bias.clone();
|
||||
let f = new_value_function(
|
||||
|
||||
let primop = primop::PrimOp::new(
|
||||
&mut es,
|
||||
cstr!("testFunction"),
|
||||
primop::PrimOpMeta {
|
||||
name: cstr!("testFunction"),
|
||||
args: [cstr!("a"), cstr!("b")],
|
||||
doc: cstr!("anonymous test function"),
|
||||
},
|
||||
Box::new(move |es, [a, b]| {
|
||||
let a = es.require_int(a)?;
|
||||
let b = es.require_int(b)?;
|
||||
|
|
@ -1371,6 +1342,9 @@ mod tests {
|
|||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let f = es.new_value_primop(primop).unwrap();
|
||||
|
||||
{
|
||||
*bias_control.lock().unwrap() = 10;
|
||||
}
|
||||
|
|
@ -1392,14 +1366,24 @@ mod tests {
|
|||
gc_registering_current_thread(|| {
|
||||
let store = Store::open("auto", []).unwrap();
|
||||
let mut es = EvalState::new(store, []).unwrap();
|
||||
let f = new_value_function(
|
||||
&mut es,
|
||||
cstr!("throwingTestFunction"),
|
||||
Box::new(move |es, [a]| {
|
||||
let a = es.require_int(a)?;
|
||||
bail!("error with arg [{}]", a);
|
||||
}),
|
||||
)
|
||||
let f = {
|
||||
let es: &mut EvalState = &mut es;
|
||||
let prim = primop::PrimOp::new(
|
||||
es,
|
||||
primop::PrimOpMeta {
|
||||
name: cstr!("throwingTestFunction"),
|
||||
args: [cstr!("arg")],
|
||||
doc: cstr!("anonymous test function"),
|
||||
},
|
||||
Box::new(move |es, [a]| {
|
||||
let a = es.require_int(a)?;
|
||||
bail!("error with arg [{}]", a);
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
es.new_value_primop(prim)
|
||||
}
|
||||
.unwrap();
|
||||
let a = es.new_value_int(2).unwrap();
|
||||
let r = es.call(f, a);
|
||||
|
|
@ -1421,12 +1405,12 @@ mod tests {
|
|||
gc_registering_current_thread(|| {
|
||||
let store = Store::open("auto", []).unwrap();
|
||||
let mut es = EvalState::new(store, []).unwrap();
|
||||
let v = new_value_function(
|
||||
&mut es,
|
||||
cstr!("zeroArgTestFunction"),
|
||||
Box::new(move |es, []| Ok(es.new_value_int(42)?)),
|
||||
)
|
||||
.unwrap();
|
||||
let v = es
|
||||
.new_value_thunk(
|
||||
"test_thunk",
|
||||
Box::new(move |es: &mut EvalState| Ok(es.new_value_int(42)?)),
|
||||
)
|
||||
.unwrap();
|
||||
es.force(&v).unwrap();
|
||||
let t = es.value_type(&v).unwrap();
|
||||
eprintln!("{:?}", t);
|
||||
|
|
@ -1442,14 +1426,14 @@ mod tests {
|
|||
gc_registering_current_thread(|| {
|
||||
let store = Store::open("auto", []).unwrap();
|
||||
let mut es = EvalState::new(store, []).unwrap();
|
||||
let v = new_value_function(
|
||||
&mut es,
|
||||
cstr!("zeroArgTestFunction"),
|
||||
Box::new(move |_es, []| {
|
||||
bail!("error message in test case eval_state_primop_anon_call_no_args_lazy")
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
let v = es
|
||||
.new_value_thunk(
|
||||
"test_thunk",
|
||||
Box::new(move |_| {
|
||||
bail!("error message in test case eval_state_primop_anon_call_no_args_lazy")
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
let r = es.force(&v);
|
||||
match r {
|
||||
Ok(_) => panic!("expected an error"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue