1
use super::ArithOp;
2
use super::emit::{Dim, scaling_dim};
3
use super::eval::{Operands, div, fold_div, fold_mul, mul};
4
use crate::ast::Fraction;
5
use crate::ast::{Expr, Number, PairElement, WasmType};
6
use crate::runtime::SymbolTable;
7

            
8
8
fn scalar() -> Expr {
9
8
    Expr::WasmRuntime(WasmType::Ratio)
10
8
}
11

            
12
10
fn money() -> Expr {
13
10
    Expr::WasmRuntime(WasmType::Commodity)
14
10
}
15

            
16
5
fn count() -> Expr {
17
5
    Expr::WasmRuntime(WasmType::I32)
18
5
}
19

            
20
7
fn refusal(result: crate::error::Result<Expr>) -> String {
21
7
    result.expect_err("a mix is refused").to_string()
22
7
}
23

            
24
#[test]
25
1
fn a_non_numeric_divisor_is_rejected() {
26
1
    let pair = Expr::WasmRuntime(WasmType::PairRef(PairElement::AnyRef));
27
1
    let mut symbols = SymbolTable::new();
28
1
    assert!(div(&mut symbols, &[scalar(), pair]).is_err());
29
1
}
30

            
31
#[test]
32
1
fn numeric_operands_keep_the_left_dimension() {
33
1
    let mut symbols = SymbolTable::new();
34
1
    assert_eq!(
35
1
        div(&mut symbols, &[scalar(), scalar()]).expect("scalar"),
36
1
        scalar()
37
    );
38
1
    assert_eq!(
39
1
        div(&mut symbols, &[money(), money()]).expect("money"),
40
1
        money()
41
    );
42
1
}
43

            
44
12
fn number(n: i64, d: i64) -> Expr {
45
12
    Expr::Number(Number::Scalar(Fraction::new(n, d)))
46
12
}
47

            
48
4
fn index(n: i64) -> Expr {
49
4
    Expr::Number(Number::Index(n))
50
4
}
51

            
52
#[test]
53
1
fn an_integral_scalar_fold_stays_a_scalar() {
54
4
    let folded = |operands: &[Expr]| {
55
4
        let operands = Operands::of(operands, ArithOp::Mul)
56
4
            .expect("one kind")
57
4
            .expect("constants");
58
4
        let value = fold_mul(&operands.values).expect("folds");
59
4
        operands.folded(value)
60
4
    };
61
1
    assert_eq!(folded(&[number(1, 2), number(2, 1)]), number(1, 1));
62
1
    assert_eq!(folded(&[number(2, 1), number(3, 1)]), number(6, 1));
63
1
    assert_eq!(folded(&[index(2), index(3)]), index(6));
64
1
    assert_eq!(folded(&[number(1, 2), number(3, 1)]), number(3, 2));
65
1
}
66

            
67
#[test]
68
1
fn a_scalar_operand_divides_rationally() {
69
1
    let operands = Operands::of(&[number(2, 1), number(4, 1)], ArithOp::Div)
70
1
        .expect("one kind")
71
1
        .expect("constants");
72
1
    assert!(!operands.is_index());
73
1
    assert_eq!(
74
1
        fold_div(&operands.values, operands.is_index()).expect("folds"),
75
1
        Fraction::new(1, 2)
76
    );
77
1
}
78

            
79
#[test]
80
1
fn an_index_beside_a_scalar_is_a_mix() {
81
1
    let mixed = Operands::of(&[index(1), number(1, 2)], ArithOp::Add);
82
1
    assert!(mixed.is_err_and(|e| {
83
1
        e.to_string()
84
1
            .contains("+ cannot mix an index (count) with a scalar")
85
1
    }));
86
1
}
87

            
88
#[test]
89
1
fn the_mix_reads_in_lattice_order_whichever_operand_came_first() {
90
1
    let mut symbols = SymbolTable::new();
91
1
    let forward = refusal(mul(&mut symbols, &[count(), scalar()]));
92
1
    let backward = refusal(mul(&mut symbols, &[scalar(), count()]));
93
1
    assert_eq!(forward, backward);
94
1
    assert!(forward.contains("* cannot mix an index (count) with a scalar"));
95
1
}
96

            
97
#[test]
98
1
fn money_scales_by_a_scalar_and_refuses_an_index() {
99
1
    let mut symbols = SymbolTable::new();
100
1
    assert_eq!(
101
1
        mul(&mut symbols, &[money(), scalar()]).expect("money"),
102
1
        money()
103
    );
104
1
    assert!(
105
1
        refusal(mul(&mut symbols, &[money(), count()]))
106
1
            .contains("* cannot mix an index (count) with a money value")
107
    );
108
1
    assert!(
109
1
        refusal(mul(&mut symbols, &[count(), money()]))
110
1
            .contains("* cannot mix an index (count) with a money value")
111
    );
112
1
}
113

            
114
#[test]
115
1
fn division_refuses_what_codegen_refuses() {
116
1
    let mut symbols = SymbolTable::new();
117
1
    assert!(refusal(div(&mut symbols, &[scalar(), money()])).contains("pure rational"));
118
1
    assert!(refusal(div(&mut symbols, &[money()])).contains("reciprocal"));
119
1
    assert!(
120
1
        refusal(div(&mut symbols, &[money(), count()]))
121
1
            .contains("/ cannot mix an index (count) with a money value")
122
    );
123
1
}
124

            
125
#[test]
126
1
fn a_raising_operand_has_no_dimension_in_a_scaling_chain() {
127
1
    let dims = [None, Some(Dim::Money), Some(Dim::Scalar)];
128
1
    assert_eq!(scaling_dim(ArithOp::Mul, &dims).expect("money"), Dim::Money);
129
1
    assert_eq!(
130
1
        scaling_dim(ArithOp::Mul, &[None]).expect("index"),
131
1
        Dim::Index
132
    );
133
1
}