11import unittest
22import MLC .Log .log as lg
3+ from MLC import config as config_path
34from MLC .Log .log import set_logger
45from MLC .mlc_parameters .mlc_parameters import Config
6+ from MLC .Common .LispTreeExpr .LispTreeExpr import ExprException
57from MLC .Common .LispTreeExpr .LispTreeExpr import LispTreeExpr
6- from MLC import config as config_path
8+ from MLC .Common .LispTreeExpr .LispTreeExpr import NotBalancedParanthesisException
9+ from MLC .Common .LispTreeExpr .LispTreeExpr import OperationArgumentsAmountException
10+ from MLC .Common .LispTreeExpr .LispTreeExpr import OperationNotFoundException
11+ from MLC .Common .LispTreeExpr .LispTreeExpr import RootNotFoundExprException
712
813import os
914
15+
1016class ExpressionTreeTest (unittest .TestCase ):
1117
1218 @classmethod
@@ -18,6 +24,74 @@ def setUpClass(cls):
1824 def setUp (self ):
1925 pass
2026
27+ def test_check_expression_root_not_found (self ):
28+ expression = '123'
29+ self .assert_check_expression_with_exception (expression , RootNotFoundExprException )
30+
31+ def test_check_expression_misplaced_parenthesis_1 (self ):
32+ expression = '(root )* 2 3))'
33+ self .assert_check_expression_with_exception (expression , NotBalancedParanthesisException )
34+
35+ def test_check_expression_misplaced_parenthesis_2 (self ):
36+ expression = '(root (* 2 3()'
37+ self .assert_check_expression_with_exception (expression , NotBalancedParanthesisException )
38+
39+ def test_check_expression_misplaced_parenthesis_3 (self ):
40+ expression = '(root (* 2 3)'
41+ self .assert_check_expression_with_exception (expression , NotBalancedParanthesisException )
42+
43+ def test_check_expression_misplaced_parenthesis_4 (self ):
44+ expression = '(root (* 2 3)))'
45+ self .assert_check_expression_with_exception (expression , NotBalancedParanthesisException )
46+
47+ def test_check_expression_operation_not_found (self ):
48+ expression = '(root (y 2 3))'
49+ self .assert_check_expression_with_exception (expression , OperationNotFoundException )
50+
51+ def test_check_expression_incorrect_multiply_arguments_amount (self ):
52+ expression = '(root (* 2 3 3))'
53+ self .assert_check_expression_with_exception (expression , OperationArgumentsAmountException )
54+
55+ def test_check_expression_incorrect_tanh_arguments_amount (self ):
56+ expression = '(root (tanh 2 3))'
57+ self .assert_check_expression_with_exception (expression , OperationArgumentsAmountException )
58+
59+ def test_check_expression_complex_incorrect_tanh_arguments_amount (self ):
60+ expression = '(root (tanh 2 (* 3 (/ 2 5))))'
61+ self .assert_check_expression_with_exception (expression , OperationArgumentsAmountException )
62+
63+ def test_check_expression_complex_incorrect_plus_arguments_amount (self ):
64+ expression = '(root (+ (* 2 3 5) 8))'
65+ self .assert_check_expression_with_exception (expression , OperationArgumentsAmountException )
66+
67+ def test_check_expression_simple_correct_plus_arguments_amount (self ):
68+ expression = '(root (+ 2 3))'
69+ self .assert_check_expression_without_exception (expression )
70+
71+ def test_check_expression_simple_correct_tanh_arguments_amount (self ):
72+ expression = '(root (tanh 3))'
73+ self .assert_check_expression_without_exception (expression )
74+
75+ def test_check_expression_complex_correct_plus_arguments_amount_1 (self ):
76+ expression = '(root (+ 2 (* 3 (/ 2 5))))'
77+ self .assert_check_expression_without_exception (expression )
78+
79+ def test_check_expression_complex_correct_plus_arguments_amount_2 (self ):
80+ expression = '(root (+ 2 (* (/ 2 5) 3)))'
81+ self .assert_check_expression_without_exception (expression )
82+
83+ def test_check_expression_complex_correct_plus_arguments_amount_3 (self ):
84+ expression = '(root (+ (* 2 3) (/ 4 5)))'
85+ self .assert_check_expression_without_exception (expression )
86+
87+ def test_check_expression_complex_correct_plus_arguments_amount_4 (self ):
88+ expression = '(root (+ 8 (/ 4 5)))'
89+ self .assert_check_expression_without_exception (expression )
90+
91+ def test_check_expression_complex_correct_plus_arguments_amount_5 (self ):
92+ expression = '(root (+ (* 2 3) 8))'
93+ self .assert_check_expression_without_exception (expression )
94+
2195 def test_tree_depth_root (self ):
2296 expression = '(root S0)'
2397 tree = LispTreeExpr (expression )
@@ -72,3 +146,17 @@ def assertNode(self, node, depth, childs, expr_index):
72146
73147 if not node .is_leaf ():
74148 self .assertEquals (len (node ._nodes ), childs )
149+
150+ def assert_check_expression_with_exception (self , expression , exception_class ):
151+ try :
152+ LispTreeExpr .check_expression (expression )
153+ self .assertTrue (True , False )
154+ except exception_class :
155+ self .assertTrue (True , True )
156+
157+ def assert_check_expression_without_exception (self , expression ):
158+ try :
159+ LispTreeExpr .check_expression (expression )
160+ self .assertTrue (True , True )
161+ except ExprException :
162+ self .assertTrue (True , False )
0 commit comments