class TestFormulaParser(unittest.TestCase): def setUp(self): self.sa = MockSequenceAnalyzer() self.f_parser = FormulaParser() #self.f_parser.enable_debug(verbose=False) def testParseFormula_eq(self): expr = self.f_parser.parseFormula(self.sa, 'EAX <= EBX') self.assertTrue(isinstance(expr, MockLeExpr)) self.assertTrue(isinstance(expr.lhs, MockConstExpr)) self.assertEqual(expr.lhs.val, 0x0) self.assertTrue(isinstance(expr.rhs, MockConstExpr)) self.assertEqual(expr.rhs.val, 0x1) self.assertEqual(len(self.f_parser.expr_stack), 0) self.assertEqual(len(self.f_parser.connective_stack), 0) self.assertEqual(len(self.f_parser.eq_symbol_stack), 0) def testParseFormula_connective(self): expr = self.f_parser.parseFormula(self.sa, 'EAX = ECX ^ ECX != 0x10') self.assertTrue(isinstance(expr, MockBoolAndExpr)) self.assertTrue(isinstance(expr.lhs, MockEqExpr)) self.assertTrue(isinstance(expr.rhs, MockNeExpr)) eq_expr = expr.lhs self.assertTrue(isinstance(eq_expr, MockEqExpr)) self.assertTrue(isinstance(eq_expr.lhs, MockConstExpr)) self.assertEqual(eq_expr.lhs.val, 0x0) self.assertTrue(isinstance(eq_expr.rhs, MockConstExpr)) self.assertEqual(eq_expr.rhs.val, 0x2) ne_expr = expr.rhs self.assertTrue(isinstance(ne_expr, MockNeExpr)) self.assertTrue(isinstance(ne_expr.lhs, MockConstExpr)) self.assertEqual(ne_expr.lhs.val, 0x2) self.assertTrue(isinstance(ne_expr.rhs, MockConstExpr)) self.assertEqual(ne_expr.rhs.val, 0x10) self.assertEqual(len(self.f_parser.expr_stack), 0) self.assertEqual(len(self.f_parser.connective_stack), 0) self.assertEqual(len(self.f_parser.eq_symbol_stack), 0) def testCheckSyntax(self): valid = 'EAX = EDX' self.assertTrue(self.f_parser.check_syntax(valid)[0]) valid = 'EAX = EDX ^ EDX != 0x10' self.assertTrue(self.f_parser.check_syntax(valid)[0]) invalid = 'EAX = EC' self.assertFalse(self.f_parser.check_syntax(invalid)[0]) invalid = 'EAX = EDX <= EDX != 0x10' self.assertFalse(self.f_parser.check_syntax(invalid)[0])
def setUp(self): self.sa = MockSequenceAnalyzer() self.f_parser = FormulaParser()