aboutsummaryrefslogtreecommitdiffstats
path: root/tests/canonicalize.rs
blob: b6d9c62d5978486484b14742530a5133bf710af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use casual::{CExpr, CNumber};

#[test]
fn basics() {
    assert_eq!(
        CExpr::from_str("(+ 1 2 3)").unwrap(),
        CExpr::Number(CNumber::Integer(6)),
    );
}

#[test]
fn canonicalization() {
    let cases = vec![
        ("(+ (+ a 2) (+ b 2))", "(+ 4 a b)"),
        ("(+ (+ a 2) (+ b 2))", "(+ 4 a b)"),
        ("(+ 1 2 b)", "(+ 3 b)"),
        ("(+ 1 2 3)", "6"),
        ("(* (/ 2 3) (/ 3 2))", "1"),
        ("(^ (^ a 2) 3)", "(^ a 6)"),
        ("(^ (/ 1 3) -1)", "3"),
        // TODO: ("(/ c (^ d 2))", "(* c (^ d -2))"),
        ("(/ c (^ d 2))", "(* c (^ (^ d 2) -1))"),
        ("(- a (* 2 b))", "(+ a (* -2 b))"),
        ("(- a b)", "(+ a (* -1 b))"),
        ("(* a (* b c))", "(* a b c)"),
    ];

    for (input, output) in cases.iter() {
        assert_eq!(CExpr::from_str(input).unwrap().to_string(), *output);
    }
}

// TODO: helper to read an examples file, as pairs of rows