aboutsummaryrefslogtreecommitdiffstats
path: root/tests/modelica_ast.rs
blob: d39615e065f8cadc835cf3119f1a6daea652df9b (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

extern crate modelthing;

use std::collections::HashSet;
use std::iter::FromIterator;
use modelthing::modelica_ast::*;

fn set_eq(a: Vec<String>, b: Vec<String>) -> bool {
    let set_a: HashSet<String> = HashSet::from_iter(a);
    let set_b: HashSet<String> = HashSet::from_iter(b);
    return set_a == set_b;
}

#[test]
fn test_expr_identifiers() {
    use modelthing::modelica_ast::Expr::*;

    assert!(set_eq(
        vec![],
        Integer(0).identifiers()));
    assert!(set_eq(
        vec!["x".to_string()],
        Ident("x".to_string()).identifiers()));
    assert!(set_eq(
        vec!["x".to_string(), "y".to_string(), "z".to_string()],
        BinExpr(BinOperator::Add,
            Box::new(Abs(Box::new(Ident("z".to_string())))),
            Box::new(BinExpr(BinOperator::Add,
                Box::new(Abs(Box::new(Ident("x".to_string())))),
                Box::new(Abs(Box::new(Ident("y".to_string()))))))).identifiers()));
    assert!(set_eq(
        vec!["z".to_string()],
        BinExpr(BinOperator::Add,
            Box::new(Ident("z".to_string())),
            Box::new(Ident("z".to_string()))).identifiers()));
}

#[test]
fn test_eqn_identifiers() {
    use modelthing::modelica_ast::Expr::*;

    assert!(set_eq(
        vec![],
        SimpleEquation{
            lhs: Integer(0),
            rhs: Integer(0),
        }.identifiers()));
    assert!(set_eq(
        vec!["z".to_string()],
        SimpleEquation{
            lhs: Ident("z".to_string()),
            rhs: BinExpr(BinOperator::Add,
                Box::new(Ident("z".to_string())),
                Box::new(Ident("z".to_string()))),
        }.identifiers()));
}

#[test]
fn test_rebalance_for() {
    use modelthing::modelica_ast::Expr::*;

    // z = a - 1.2345
    // a = z + 1.2345
    let done = SimpleEquation{
        lhs: Ident("z".to_string()),
        rhs: BinExpr(BinOperator::Subtract,
            Box::new(Ident("a".to_string())),
            Box::new(Float(1.2345)))};
    assert_eq!(done,
        done.rebalance_for("z".to_string()).unwrap());
    assert_eq!(SimpleEquation{
        lhs: Ident("a".to_string()),
        rhs: BinExpr(BinOperator::Add,
            Box::new(Ident("z".to_string())),
            Box::new(Float(1.2345)))},
        done.rebalance_for("a".to_string()).unwrap());

}