def test_evaluate_3var_expr(self): expr = 'a & b | c' variableMap = {'a' : 0, 'b' : 1 , 'c' : 0 } postfix = infixToPostfix(expr) root = postfixToExpressionTree(postfix) root.in_traversal() print('') self.assertEqual(evaluateExpr(postfix, variableMap), 0)
def test_evaluate_expr_true(self): expr = 'a | !a' variableMap = {'a' : 0} postfix = infixToPostfix(expr) root = postfixToExpressionTree(postfix) root.in_traversal() print('') self.assertEqual(evaluateExpr(postfix, variableMap), 1)
def test_evaluate_complex_expr(self): expr = '(a & (!b | b)) | (!a & (!b | b))' variableMap = {'a' : 0, 'b' : 1} postfix = infixToPostfix(expr) root = postfixToExpressionTree(postfix) root.in_traversal() print('') self.assertEqual(evaluateExpr(postfix, variableMap), 1)
def isTautology(self): ''' Determines whether a statement given is a tuatology or not Returns: This function returns whether a given statement is tautology or not ''' logging.debug (self.statement) (variableMap, uniqueVaraibles) = self._getVariableMap(self.statement) #optimization step is that if all are unique variables, it won't be tautology logging.debug (variableMap, uniqueVaraibles) if uniqueVaraibles: return False numVariables = len(variableMap) postfix = infixToPostfix(self.statement) root = postfixToExpressionTree(postfix) for i in range(pow(2, numVariables)): for e in enumerate(variableMap): offset = e[0] key = e[1] variableMap[key] = (i & (1 << offset)) >> offset if not self._evaluateExprTree(root, variableMap): return False return True
def isTautology(self): ''' Determines whether a statement given is a tuatology or not Returns: This function returns whether a given statement is tautology or not ''' logging.debug(self.statement) (variableMap, uniqueVaraibles) = self._getVariableMap(self.statement) #optimization step is that if all are unique variables, it won't be tautology logging.debug(variableMap, uniqueVaraibles) if uniqueVaraibles: return False numVariables = len(variableMap) postfix = infixToPostfix(self.statement) root = postfixToExpressionTree(postfix) for i in range(pow(2, numVariables)): for e in enumerate(variableMap): offset = e[0] key = e[1] variableMap[key] = (i & (1 << offset)) >> offset if not self._evaluateExprTree(root, variableMap): return False return True
def test_simple_expr(self): expr = 'a' self.assertEqual(infixToPostfix(expr), 'a')
def test_evaluate_expr_simple(self): expr = 'a' variableMap = {'a' : 0} postfix = infixToPostfix(expr) self.assertEqual(evaluateExpr(postfix, variableMap), 0)
def test_expr_with_not_complex(self): expr = '(a & (!b | b)) | (!a & (!b | b))' self.assertEqual(infixToPostfix(expr), 'ab!b|&a!b!b|&|')
def test_expr_with_not_same(self): expr = 'a & !a' self.assertEqual(infixToPostfix(expr), 'aa!&')
def test_expr_with_not(self): expr = '!a & !b' self.assertEqual(infixToPostfix(expr), 'a!b!&')
def test_tree_evaluate_3var_expr(self): expr = 'a & b | c' variableMap = {'a' : 0, 'b' : 1 , 'c' : 0 } postfix = infixToPostfix(expr) root = postfixToExpressionTree(postfix) self.assertEqual(evaluateExprTree(root, variableMap), 0)
def test_tree_evaluate_complex_expr(self): expr = '(a & (!b | b)) | (!a & (!b | b))' variableMap = {'a' : 0, 'b' : 1} postfix = infixToPostfix(expr) root = postfixToExpressionTree(postfix) self.assertEqual(evaluateExprTree(root, variableMap), 1)
def test_complex_expr_braces(self): expr = 'a & (b | c)' self.assertEqual(infixToPostfix(expr), 'abc|&')
def test_tree_evaluate_expr_true(self): expr = 'a | !a' variableMap = {'a' : 0} postfix = infixToPostfix(expr) root = postfixToExpressionTree(postfix) self.assertEqual(evaluateExprTree(root, variableMap), 1)
def test_simple_expr_braces(self): expr = 'a&b' self.assertEqual(infixToPostfix(expr), 'ab&')