diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib.rs | 37 | ||||
-rw-r--r-- | tests/modelica_ast.rs | 78 | ||||
-rw-r--r-- | tests/rebalance.rs | 28 |
3 files changed, 29 insertions, 114 deletions
diff --git a/tests/lib.rs b/tests/lib.rs index baddf72..bf28476 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,5 +1,6 @@ extern crate modelthing; +extern crate modelica_parser; use std::path::Path; @@ -31,39 +32,3 @@ fn test_load_model_entry() { fn test_search_models() { assert_eq!(search_models(Path::new("./examples/")).len() > 1, true); } - -#[test] -fn test_lexical() { - assert_eq!(&format!("{:?}", modelica_parser::parse_integer("+123").unwrap()), - "123"); - assert_eq!(&format!("{:?}", modelica_parser::parse_integer("-9").unwrap()), - "-9"); - assert_eq!(&format!("{:?}", modelica_parser::parse_float("-1.0e0").unwrap()), - "-1"); - assert_eq!(&format!("{:?}", modelica_parser::parse_float("123.456").unwrap()), - "123.456"); -} - -#[test] -fn test_parse() { - let example1 = -r#"model MinimalModel - Real x; -equation - x = 1; -end MinimalModel; -"#; - assert_eq!(&format!("{:?}", modelica_parser::parse_model(example1).unwrap()), example1); - - let example2 = -r#"model MinimalModel - parameter Real a; - Real b; -equation - connect(a, b); - a = 1; - b = ((abs(a) + 2) / 4); -end MinimalModel; -"#; - assert_eq!(&format!("{:?}", modelica_parser::parse_model(example2).unwrap()), example2); -} diff --git a/tests/modelica_ast.rs b/tests/modelica_ast.rs deleted file mode 100644 index d39615e..0000000 --- a/tests/modelica_ast.rs +++ /dev/null @@ -1,78 +0,0 @@ - -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()); - -} diff --git a/tests/rebalance.rs b/tests/rebalance.rs new file mode 100644 index 0000000..f365e84 --- /dev/null +++ b/tests/rebalance.rs @@ -0,0 +1,28 @@ + +extern crate modelica_parser; +extern crate modelthing; + +use modelthing::modelica_model::SimpleEquationExt; +use modelica_parser::ast::*; + +#[test] +fn test_rebalance_for() { + use modelica_parser::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()); + +} |