def generic_test_type_inference(self, expression, given_env, expected_env, expected_expression_type): """ Args: expression: given_env: expected_env: """ parser = boogie_parsing.get_parser_instance() tree = parser.parse(expression) t = boogie_parsing.infer_variable_types(tree, given_env) t, type_env = t.derive_type() for var, derived_type in type_env.items(): self.assertEqual( expected_env[var], derived_type, msg= "Error deriving `{}` type for variable `{}`. Derived `{}` instead." .format(expected_env[var], var, derived_type)) self.assertEqual( expected_expression_type, t, msg="Error deriving expression type `{}`. Got `{}` instead.". format(expected_expression_type, t))
def test_reconstruction_real_numbers(self): parser = boogie_parsing.get_parser_instance() expressions = [ '1.1<0.0', '(1.1<0.0)&&(1.1<0.0)&&(1.1<0.0)', '((1.1<0.0)&&(1.1<0.0))||(1.1<0.0)', '((-1.1<-0.0)&&(-1.1<-0.0))||(-1.1<-0.0)', '((-1.2341<-0.2340)&&(-1.1<-0.000023498))||(-1234.1<-23.0)', '((-1.2341<-0.2340)&&(BAR<-0.000023498))==>(-1234.1<FOO)', '((-1.2341<-0.2340)&&(BAR<-0.000023498))<==>(-1234.1<FOO)', '((-1.2341>-0.2340)&&(BAR>-0.000023498))==>(1234.1>FOO)', '((-1.2341==-0.2340)==(BAR<-0.000023498))==>(-1234.1==FOO)', '((-1.2341<-0.2340)&&(BAR<-0.000023498))==>(-1234.1<FOO)', '((-1.2341<-0.2340)&&(BAR+-2.000023498))==>(-1234.1<FOO)', ] for index, expression in enumerate(expressions): try: tree = parser.parse(expression) reconstructed_expression = boogie_parsing.Reconstructor( parser).reconstruct(tree) except UnexpectedInput as e: print('Error in expression:\n{}\n{}^'.format( expression, ' ' * (e.column - 1))) raise e self.assertEqual(expression, reconstructed_expression, 'Reconstructed expression should match origin.')
def test_parse_real_numbers(self): parser = boogie_parsing.get_parser_instance() expression = '1.1 < 0.0' parseable = True try: tree = parser.parse(expression) except: parseable = False self.assertEqual(parseable, True)
def test_true_false_should_be_terminals(self): expressions = [ 'false', 'true', 'true ', 'false ', 'true == false', ] for expr in expressions: parser = boogie_parsing.get_parser_instance() tree = parser.parse(expr) used_variables = set(boogie_parsing.get_variables_list(tree)) print('For {} I found {} variables'.format(expr, used_variables)) self.assertEqual(set(), used_variables)
def test_if_else_expressions(self): expressions = [ 'if foo==bar then bar==true else spam==false', 'if(if foo==bar then bar==true else spam==false)then bar==foo else spam==false', 'if(if foo==bar then bar==true else spam==false)then(if foo==bar then bar==foo else spam==false)else ' 'spam==false', ] for expr in expressions: parser = boogie_parsing.get_parser_instance() tree = parser.parse(expr) used_variables = set(boogie_parsing.get_variables_list(tree)) print('For {} I found {} variables'.format(expr, used_variables)) self.assertEqual({'foo', 'bar', 'spam'}, used_variables) reconstructed_expression = boogie_parsing.Reconstructor( parser).reconstruct(tree) self.assertEqual(expr, reconstructed_expression)
def test_parse_expressions(self): parser = boogie_parsing.get_parser_instance() expressions = [('abs(-10)', True), ('abs(10)', True), ('abs(10 + 12)', True), ('abs(foo)', True), ('abs(foo + 42)', True), ('abs(42 - foo)', True), ('abs(+-foo)', True), ('abs(42 > foo)', False), ('abs(42 < foo)', False), ('abs()', False), ('abs(foo + bar) > 10', True), ('abs(bar + foo) == spam', True)] for expression, should_be_parseable in expressions: is_parseable = True try: tree = parser.parse(expression) # pydot__tree_to_png(tree, "parse_tree.png") except Exception as e: is_parseable = False self.assertEqual(is_parseable, should_be_parseable, msg='Error parsing ' + expression)
def parse(self, code: str) -> Tree: parser = boogie_parsing.get_parser_instance() return parser.parse(code)
def test_get_var_list(self): tree = boogie_parsing.get_parser_instance().parse( '(Hello <==> Spam) && (((((b + a) + d) * 23) < 4) && x ==> y )') self.assertEqual(set(boogie_parsing.get_variables_list(tree)), {'Hello', 'Spam', 'b', 'a', 'd', 'x', 'y'})