Lines
99.37 %
Functions
100 %
Branches
//! A BLOCK or IF of integer literals is an index, so beside a fractional
//! literal or a runtime scalar arithmetic refuses it; an operand that always
//! raises has no dimension.
use super::ExpectingForm;
use crate::ast::{Expr, WasmType};
use crate::compiler::CompileMode;
use crate::compiler::behaviour_oracle::{Outcome, observe};
use crate::compiler::expr::eval_value;
use crate::compiler::stats::CompileStats;
use crate::error::Result;
use crate::host_fn::HostFnSpec;
use crate::reader::Reader;
use crate::runtime::SymbolTable;
const TEST: &str = "(> (rpc-protocol-version) 0)";
const MACRO: &str = "(defmacro m (x) (list 'progn x))";
const REFUSED: &str = "+ cannot mix an index (count) with a scalar";
fn hosts() -> [HostFnSpec; 1] {
[
HostFnSpec::new("rpc-protocol-version", "nomi", "rpc_protocol_version")
.returns(WasmType::I32),
]
}
/// The outcome in the empty-batch world (protocol 1) and the ledger world
/// (protocol 0), and the BLOCK pass counters.
fn run(source: &str) -> (Vec<String>, (u64, u64)) {
let (outcomes, stats) = run_with_stats(source);
(outcomes, (stats.block_passes_max, stats.block_passes_total))
fn run_with_stats(source: &str) -> (Vec<String>, CompileStats) {
let (behaviours, stats) = observe(source, CompileMode::Eval, &hosts());
let outcomes = behaviours
.into_iter()
.map(|b| match b.outcome {
Outcome::Value(v) => v.replace("Struct(2), ", ""),
Outcome::CompileError(e) => e,
other => format!("{other:?}"),
})
.collect();
(outcomes, stats)
fn ratio(numer: i64, denom: i64) -> String {
format!("Some(Ratio) [I64({numer}), I64({denom})]")
fn refused(outcomes: &[String]) -> bool {
outcomes.iter().all(|outcome| outcome.contains(REFUSED))
#[test]
fn a_block_of_literals_beside_a_fraction_is_refused() {
let (outcomes, _) = run(&format!("{MACRO} (+ 1/2 (block b (m 2) 1))"));
assert!(refused(&outcomes), "{outcomes:?}");
fn a_block_with_a_literal_exit_beside_a_fraction_is_refused() {
let (outcomes, _) = run(&format!(
"(+ 1/2 (block b (if {TEST} (return-from b 1)) 2))"
));
fn an_if_of_literals_beside_a_fraction_is_refused() {
let (outcomes, _) = run(&format!("(+ 1/2 (if {TEST} 1 2))"));
fn a_constant_test_if_beside_a_fraction_is_refused() {
for form in [
"(+ 1/2 (if t (block b (m 2) 1) 2))",
"(+ 1/2 (if nil 2 (block b (m 2) 1)))",
] {
let (outcomes, _) = run(&format!("{MACRO} {form}"));
assert!(refused(&outcomes), "{form}: {outcomes:?}");
fn nested_forms_of_literals_beside_a_fraction_are_refused() {
format!("(+ 1/2 (block b (m 2) (if {TEST} 1 2)))"),
format!("(+ 1/2 (block b (if {TEST} (return-from b (if {TEST} 1 3))) 2))"),
"(+ 1/2 (block a (block b (m 2) 1)))".to_string(),
format!(
"(+ 1/2 (block a (if {TEST} (return-from a (block b (m 2) 1))) (block c (m 3) 2)))"
),
fn an_if_joining_an_index_arm_with_a_fraction_arm_is_refused() {
let (outcomes, _) = run(&format!("(+ (if {TEST} (if {TEST} 1 2) 1/2) 1/2)"));
assert!(
outcomes
.iter()
.all(|outcome| outcome.contains("expects numeric arguments")),
"{outcomes:?}"
);
fn a_block_whose_every_arm_raises_has_no_dimension() {
let (outcomes, _) = run("(handler-case (+ 1/2 (block b (error 'boom \"x\"))) (boom (e) 1/4))");
assert_eq!(outcomes, [ratio(1, 4), ratio(1, 4)]);
fn every_arithmetic_operator_refuses_a_block_of_literals_beside_a_fraction() {
for op in ["+", "-", "*", "/"] {
let (outcomes, _) = run(&format!("{MACRO} ({op} (block b (m 2) 1) 1/2)"));
let text = format!("{op} cannot mix an index (count) with a scalar");
outcomes.iter().all(|outcome| outcome.contains(&text)),
"{op}: {outcomes:?}"
fn integer_literals_expect_nothing() {
let (outcomes, _) = run(&format!("{MACRO} (+ 1 (block b (m 2) 1))"));
assert_eq!(
outcomes,
["Some(I32) [I64(2), I64(1)]", "Some(I32) [I64(2), I64(1)]"]
fn a_runtime_index_beside_a_fraction_is_refused() {
let (outcomes, _) = run("(+ 1/2 (rpc-protocol-version))");
fn a_block_with_a_computed_index_exit_is_refused() {
"(+ 1/2 (block b (if {TEST} (return-from b (rpc-protocol-version))) 1))"
fn an_if_with_a_computed_index_arm_is_refused() {
let (outcomes, _) = run(&format!("(+ 1/2 (if {TEST} (rpc-protocol-version) 2))"));
fn a_closure_body_refuses_the_mix_on_the_eval_surface() {
"(mapcar (lambda (x) (+ 1/2 (if {TEST} 1 2))) (list 1 2))"
fn try_eval(source: &str) -> Result<Expr> {
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.register_host_fns(&hosts());
symbols.register_host_fns(&[
HostFnSpec::new("money-source", "nomi", "money_source").returns(WasmType::Commodity)
]);
let program = Reader::parse(source).expect("parse");
eval_value(&mut symbols, &program.exprs[0])
fn eval(source: &str) -> Expr {
try_eval(source).expect("eval")
fn the_eval_surface_refuses_a_runtime_index_beside_a_fraction() {
let err = try_eval("(+ 1/2 (rpc-protocol-version))").expect_err("refused");
assert!(err.to_string().contains("cannot mix an index"), "{err}");
fn the_eval_surface_refuses_a_block_or_if_operand_beside_a_fraction() {
for operand in [
format!("(if {TEST} 1 2)"),
format!("(block b (if {TEST} (return-from b 1)) 2)"),
let err = try_eval(&format!("(+ 1/2 {operand})")).expect_err("refused");
assert!(err.to_string().contains(REFUSED), "{operand}: {err}");
eval(&format!("(+ 1 {operand})")),
Expr::WasmRuntime(WasmType::I32),
"{operand}"
fn only_a_block_or_an_if_takes_an_expected_type() {
let forms = [
"(block b 1)",
"(if t 1 2)",
"(progn 1)",
"(foo 1)",
"1",
"()",
];
let taken: Vec<bool> = forms
.map(|source| {
ExpectingForm::of(&program.exprs[0]).is_some()
assert_eq!(taken, [true, true, false, false, false, false]);
fn the_eval_surface_keeps_a_money_operand_beside_a_fraction() {
format!("(if {TEST} (money-source) (money-source))"),
format!("(block b (if {TEST} (return-from b (money-source))) (money-source))"),
eval(&format!("(* 1/2 {operand})")),
Expr::WasmRuntime(WasmType::Commodity),