From c9b01ab365159e9625722fb66648f1ec2eb2e435 Mon Sep 17 00:00:00 2001 From: microproofs Date: Fri, 1 Sep 2023 22:31:40 -0400 Subject: [PATCH] chore: fill in cost model test: Add case and constr eval tests --- crates/uplc/src/machine.rs | 63 +++++++++++++++++++++++++++ crates/uplc/src/machine/cost_model.rs | 16 +++---- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 8433463d..95600519 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -480,4 +480,67 @@ mod tests { ); } } + + #[test] + fn case_constr_case_0() { + let make_program = + |fun: DefaultFunction, tag: usize, n: i32, m: i32| Program:: { + version: (0, 0, 0), + term: Term::Case { + constr: Term::Constr { + tag, + fields: vec![ + Term::Constant(Constant::Integer(n.into()).into()), + Term::Constant(Constant::Integer(m.into()).into()), + ], + } + .into(), + branches: vec![Term::Builtin(fun), Term::sub_integer()], + }, + }; + + let test_data = vec![ + (DefaultFunction::AddInteger, 0, 8, 3, 11), + (DefaultFunction::AddInteger, 1, 8, 3, 5), + ]; + + for (fun, tag, n, m, result) in test_data { + let eval_result = make_program(fun, tag, n, m).eval(ExBudget::default()); + + assert_eq!( + eval_result.result().unwrap(), + Term::Constant(Constant::Integer(result.into()).into()) + ); + } + } + + #[test] + fn case_constr_case_1() { + let make_program = |tag: usize| Program:: { + version: (0, 0, 0), + term: Term::Case { + constr: Term::Constr { + tag, + fields: vec![], + } + .into(), + branches: vec![ + Term::integer(5.into()), + Term::integer(10.into()), + Term::integer(15.into()), + ], + }, + }; + + let test_data = vec![(0, 5), (1, 10), (2, 15)]; + + for (tag, result) in test_data { + let eval_result = make_program(tag).eval(ExBudget::default()); + + assert_eq!( + eval_result.result().unwrap(), + Term::Constant(Constant::Integer(result.into()).into()) + ); + } + } } diff --git a/crates/uplc/src/machine/cost_model.rs b/crates/uplc/src/machine/cost_model.rs index 8d6ab4ae..02f7bdde 100644 --- a/crates/uplc/src/machine/cost_model.rs +++ b/crates/uplc/src/machine/cost_model.rs @@ -147,12 +147,12 @@ impl MachineCosts { }, // Placeholder values constr: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, case: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, } } @@ -193,12 +193,12 @@ impl Default for MachineCosts { }, // Placeholder values constr: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, case: ExBudget { - mem: 30000000000, - cpu: 30000000000, + mem: 100, + cpu: 23000, }, } }