Lines
100 %
Functions
Branches
use super::ArithOp;
use super::emit::{Dim, scaling_dim};
use super::eval::{Operands, div, fold_div, fold_mul, mul};
use crate::ast::Fraction;
use crate::ast::{Expr, Number, PairElement, WasmType};
use crate::runtime::SymbolTable;
fn scalar() -> Expr {
Expr::WasmRuntime(WasmType::Ratio)
}
fn money() -> Expr {
Expr::WasmRuntime(WasmType::Commodity)
fn count() -> Expr {
Expr::WasmRuntime(WasmType::I32)
fn refusal(result: crate::error::Result<Expr>) -> String {
result.expect_err("a mix is refused").to_string()
#[test]
fn a_non_numeric_divisor_is_rejected() {
let pair = Expr::WasmRuntime(WasmType::PairRef(PairElement::AnyRef));
let mut symbols = SymbolTable::new();
assert!(div(&mut symbols, &[scalar(), pair]).is_err());
fn numeric_operands_keep_the_left_dimension() {
assert_eq!(
div(&mut symbols, &[scalar(), scalar()]).expect("scalar"),
scalar()
);
div(&mut symbols, &[money(), money()]).expect("money"),
money()
fn number(n: i64, d: i64) -> Expr {
Expr::Number(Number::Scalar(Fraction::new(n, d)))
fn index(n: i64) -> Expr {
Expr::Number(Number::Index(n))
fn an_integral_scalar_fold_stays_a_scalar() {
let folded = |operands: &[Expr]| {
let operands = Operands::of(operands, ArithOp::Mul)
.expect("one kind")
.expect("constants");
let value = fold_mul(&operands.values).expect("folds");
operands.folded(value)
};
assert_eq!(folded(&[number(1, 2), number(2, 1)]), number(1, 1));
assert_eq!(folded(&[number(2, 1), number(3, 1)]), number(6, 1));
assert_eq!(folded(&[index(2), index(3)]), index(6));
assert_eq!(folded(&[number(1, 2), number(3, 1)]), number(3, 2));
fn a_scalar_operand_divides_rationally() {
let operands = Operands::of(&[number(2, 1), number(4, 1)], ArithOp::Div)
assert!(!operands.is_index());
fold_div(&operands.values, operands.is_index()).expect("folds"),
Fraction::new(1, 2)
fn an_index_beside_a_scalar_is_a_mix() {
let mixed = Operands::of(&[index(1), number(1, 2)], ArithOp::Add);
assert!(mixed.is_err_and(|e| {
e.to_string()
.contains("+ cannot mix an index (count) with a scalar")
}));
fn the_mix_reads_in_lattice_order_whichever_operand_came_first() {
let forward = refusal(mul(&mut symbols, &[count(), scalar()]));
let backward = refusal(mul(&mut symbols, &[scalar(), count()]));
assert_eq!(forward, backward);
assert!(forward.contains("* cannot mix an index (count) with a scalar"));
fn money_scales_by_a_scalar_and_refuses_an_index() {
mul(&mut symbols, &[money(), scalar()]).expect("money"),
assert!(
refusal(mul(&mut symbols, &[money(), count()]))
.contains("* cannot mix an index (count) with a money value")
refusal(mul(&mut symbols, &[count(), money()]))
fn division_refuses_what_codegen_refuses() {
assert!(refusal(div(&mut symbols, &[scalar(), money()])).contains("pure rational"));
assert!(refusal(div(&mut symbols, &[money()])).contains("reciprocal"));
refusal(div(&mut symbols, &[money(), count()]))
.contains("/ cannot mix an index (count) with a money value")
fn a_raising_operand_has_no_dimension_in_a_scaling_chain() {
let dims = [None, Some(Dim::Money), Some(Dim::Scalar)];
assert_eq!(scaling_dim(ArithOp::Mul, &dims).expect("money"), Dim::Money);
scaling_dim(ArithOp::Mul, &[None]).expect("index"),
Dim::Index