Commit 1900c7c1 authored by Sergio Costas's avatar Sergio Costas

Added extra tests

parent 93266b75
......@@ -209,6 +209,7 @@ class crusty(object):
for var_group in var_container:
if var_name in var_group:
return var_group[var_name]
#added
self._add_error(self.MSG_CRITICAL,"Unknown variable '{:s}' at line {:d}",str(var_name),line_number)
raise self.VarNotFoundException()
......@@ -220,6 +221,7 @@ class crusty(object):
if (not force) and ((variable["properties"] & self.PROPERTY_CRUST_VARIABLE) != 0):
status = variable["status"]
if (status == crusty.STATUS_NOT_NULL) or (status == crusty.STATUS_NOT_NULL_OR_NULL):
#added
self._add_error(self.MSG_ERROR,"Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}",var_name,line_number,variable["init_line"])
variable["init_line"] = line_number
if force_init_line:
......@@ -280,10 +282,12 @@ class crusty(object):
def _eval_function_call(self,node,var_container,function_container):
if node.name not in function_container:
#added
self._add_error(self.MSG_CRITICAL,"Calling function '{:s}' at line {:d}, but it is not declared or defined",node.name,node.line)
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container)) ]
function_data = function_container[node.name]
if (len(function_data) - 1) != len(node.function_params):
#added
self._add_error(self.MSG_CRITICAL,"Calling function '{:s}' at line {:d} with an incorrect number of parameters",node.name,node.line)
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container)) ]
error_found = False
......@@ -293,6 +297,7 @@ class crusty(object):
variable = node.function_params[pos]
if variable.type != "IDENTIFIER":
if ((function_data[pos+1] & self.PROPERTY_CRUST_VARIABLE) != 0) and (not variable.t_null):
#added
self._add_error(self.MSG_CRITICAL,"Expected a __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}",pos+1,node.name,node.line)
error_found = True
continue
......@@ -302,20 +307,24 @@ class crusty(object):
param_is_borrowed = (function_data[pos+1] & self.PROPERTY_BORROWED) != 0
param_is_recycled = (function_data[pos+1] & self.PROPERTY_RECYCLED) != 0
if (not var_is_crusty) and param_is_crusty:
#added
self._add_error(self.MSG_CRITICAL,"Expected a __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable",pos+1,node.name,node.line)
error_found = True
continue
if var_is_crusty and (not param_is_crusty):
#added
self._add_error(self.MSG_CRITICAL,"Expected a non __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}, but passed a __crust_t__ variable",pos+1,node.name,node.line)
error_found = True
continue
if not param_is_crusty:
continue
if node_var["status"] == self.STATUS_UNINITIALIZED:
#added
self._add_error(self.MSG_ERROR,"Parameter {:d} when calling function '{:s}' at line {:d} isn't initialized",pos+1,node.name,node.line)
error_found = True
continue
if node_var["status"] == self.STATUS_FREED:
#added
self._add_error(self.MSG_ERROR,"Parameter {:d} when calling function '{:s}' at line {:d} was freed at line {:d}",pos+1,node.name,node.line,node_var["init_line"])
error_found = True
continue
......@@ -396,6 +405,7 @@ class crusty(object):
if node.pointer == 0:
if is_parameter:
if node.name is None:
#added
self._add_error(self.MSG_CRITICAL,"A parameter is defined as __crust_t__ type at line {:d}, but it is not a pointer",node.line)
else:
self._add_error(self.MSG_CRITICAL,"Parameter '{:s}' defined as __crust_t__ type at line {:d}, but it is not a pointer",node.name,node.line)
......
......@@ -11,12 +11,26 @@ from crusty import crusty
class Test(unittest.TestCase):
def _test_msg(self,error_element,*params):
def _test_msg(self,lib,error_element,*params):
self.assertEqual(len(error_element),len(params))
for index in range(len(error_element)):
self.assertEqual(error_element[index],params[index])
def _generic_test(self,filename,show,*params):
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file(filename,False)
self.assertIsNot(tree,None)
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
if show:
lib.print_errors()
self.assertEqual(len(lib.errors),1)
error_element = lib.errors[0]
self._test_msg(lib,error_element,*params)
def testAllFine(self):
# This test just must work
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file("unitest/test1.c",False)
self.assertIsNot(tree,None)
......@@ -25,6 +39,7 @@ class Test(unittest.TestCase):
self.assertEqual(len(lib.errors),0)
def testUnknownVariable(self):
# This test must fail with an Unknown variable exception and the corresponding error message
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file("unitest/test2.c",False)
self.assertIsNot(tree,None)
......@@ -32,34 +47,37 @@ class Test(unittest.TestCase):
with self.assertRaises(crusty.crusty.VarNotFoundException):
lib.process_functions(tree,variables,functions)
self.assertEqual(len(lib.errors),1)
self._test_msg(lib.errors[0],crusty.crusty.MSG_CRITICAL,"Unknown variable '{:s}' at line {:d}", "var", 4)
self._test_msg(lib,lib.errors[0],crusty.crusty.MSG_CRITICAL,"Unknown variable '{:s}' at line {:d}", "var", 4)
def testOverwrite(self):
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file("unitest/test3.c",False)
self.assertIsNot(tree,None)
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
self.assertEqual(len(lib.errors),1)
self._test_msg(lib.errors[0],crusty.crusty.MSG_ERROR,"Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param3", 5, 4)
self._generic_test("unitest/test3.c",False,crusty.crusty.MSG_ERROR,"Assignment to '{:s}' at line {:d}, which was already assigned at line {:d}", "param3", 5, 4)
def testUseUninitialized(self):
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file("unitest/test4.c",False)
self.assertIsNot(tree,None)
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
self.assertEqual(len(lib.errors),1)
self._test_msg(lib.errors[0],crusty.crusty.MSG_ERROR,"Parameter {:d} when calling function '{:s}' at line {:d} isn't initialized",1,"function",5)
self._generic_test("unitest/test4.c",False,crusty.crusty.MSG_ERROR,"Parameter {:d} when calling function '{:s}' at line {:d} isn't initialized",1,"function",5)
def testUseFreed(self):
lib = crusty.crusty("lexyacclib/crusty.h","lexyacclib/crusty.so")
tree = lib.read_file("unitest/test5.c",False)
self.assertIsNot(tree,None)
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
self.assertEqual(len(lib.errors),1)
self._test_msg(lib.errors[0],crusty.crusty.MSG_ERROR,"Parameter {:d} when calling function '{:s}' at line {:d} was freed at line {:d}",1,"function",6,5)
self._generic_test("unitest/test5.c",False,crusty.crusty.MSG_ERROR,"Parameter {:d} when calling function '{:s}' at line {:d} was freed at line {:d}",1,"function",6,5)
def testPassedNonCrustyToCrusty(self):
self._generic_test("unitest/test6.c",False,crusty.crusty.MSG_CRITICAL,"Expected a __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable",1,"function",6)
def testPassedCrustyToNonCrusty(self):
self._generic_test("unitest/test7.c",False,crusty.crusty.MSG_CRITICAL,"Expected a non __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}, but passed a __crust_t__ variable",1,"function",6)
def testPassedOtherToCrusty(self):
self._generic_test("unitest/test8.c",False,crusty.crusty.MSG_CRITICAL,"Expected a __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}",1,"function",5)
def testPassedIncorrectNumberOfParameters(self):
self._generic_test("unitest/test9.c",False,crusty.crusty.MSG_CRITICAL,"Calling function '{:s}' at line {:d} with an incorrect number of parameters","function",5)
def testNonExistingFunction(self):
self._generic_test("unitest/test10.c",False,crusty.crusty.MSG_CRITICAL,"Calling function '{:s}' at line {:d}, but it is not declared or defined","function3",5)
def testCrustParameterNotPointer(self):
self._generic_test("unitest/test11.c",False,crusty.crusty.MSG_CRITICAL,"A parameter is defined as __crust_t__ type at line {:d}, but it is not a pointer",1)
def testCrustNamedParameterNotPointer(self):
self._generic_test("unitest/test12.c",False,crusty.crusty.MSG_CRITICAL,"Parameter '{:s}' defined as __crust_t__ type at line {:d}, but it is not a pointer","parameter",1)
if __name__ == '__main__':
unittest.main()
# 1 "test11.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "test11.c"
typedef __crust_t__ unsigned char crust_t;
typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t retval = function3(3,5);
}
void function(__crust_t__ int);
void function(__crust_t__ int parameter);
typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int *param = 1;
crust_t retval = function(param);
}
typedef __crust_t__ unsigned char *crust_t;
int function(int param3) {
crust_t *param = 1;
crust_t retval = function(param);
}
typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t retval = function(2);
}
typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t retval = function(3,5);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment