def check_counting_condition(self, conditions, function, expected): """ Checks if the given count vectors match the given expected data. :param conditions: String indicating which condition(s) to use (will be fed to ClangCountingConditions.counting_condition) :param function: String indicating which function from test file to use. (i.e. "used(int, int)") :param expected: Dict with python lists of counts for all variables. """ counter = ClangCountVectorCreator( ClangCountingConditions.counting_condition( Setting("irrelevant", conditions))) vectors = counter.get_vectors_for_file(self.testfile) actual = vectors[function] self.assertEqual(len(actual), len(expected), "Actual dict: " + str(actual)) self.assertEqual(sorted(actual.keys()), sorted(expected.keys())) for variable in actual: self.assertEqual(actual[variable].count_vector, expected[variable], "Variable '{}' doesnt match.".format(variable))
def fitness(file_dict, conditions, weightings, clones, origin, average_calculation, poly_postprocessing, exp_postprocessing, extra_include_paths): differences = ret_diffs(get_count_matrices( ClangCountVectorCreator(conditions, weightings), list(file_dict.keys()), lambda x: x, origin, extra_include_paths), average_calculation=average_calculation, poly_postprocessing=poly_postprocessing, exp_postprocessing=exp_postprocessing) clones_diffs = [0] non_clones_diffs = [1] must_have = list(file_dict.keys()) for function_1, function_2, difference in differences: if function_1[0] != function_2[0]: continue if function_1[0] in must_have: must_have.remove(function_1[0]) if re.match(clones, function_1[0]) is not None: clones_diffs.append(difference) else: non_clones_diffs.append(difference) for filename in must_have: if not re.match(clones, filename): must_have.remove(filename) # Each file must have one result yielded at least. If not some # function was ignored invalidly and that shouldn't be. return (min(non_clones_diffs) - max(clones_diffs) - len(must_have), max(clones_diffs))
def test_counting(self): expected_results = { (6, "test()"): {}, (12, "main(int, char *)"): { # Variables "i": [4, 1], "asd": [1, 0], "t": [4, 1], "args": [2, 0], # Globals "g": [3, 1], # Functions "smile": [1, 1], "printf": [1, 1], # Constants "#5": [1, 0], '#"i is %d"': [1, 1]}} self.uut = ClangCountVectorCreator([no_condition, is_call_argument]) cv_dict = self.uut.get_vectors_for_file(self.testfile) self.check_cv_dict(cv_dict, expected_results)
class ClangCountVectorCreatorTest(unittest.TestCase): functions = sorted(["main(int, char *)", "test()"]) def setUp(self): self.testfile = os.path.abspath(os.path.join( os.path.dirname(__file__), "sample.c")) def test_empty_counting(self): expected_results = { (6, "test()"): {}, (12, "main(int, char *)"): { # Variables "i": [], "asd": [], "t": [], "args": [], # Globals "g": [], # Functions "smile": [], "printf": [], # Constants "#5": [], '#"i is %d"': []}} self.uut = ClangCountVectorCreator() cv_dict = self.uut.get_vectors_for_file(self.testfile) self.check_cv_dict(cv_dict, expected_results) def check_cv_dict(self, actual, expected): self.assertEqual(len(actual), len(expected), str(actual)) self.assertEqual(sorted(actual.keys()), sorted(expected.keys())) for function in actual: self.assertEqual(len(actual[function]), len(expected[function])) self.assertEqual(sorted(actual[function].keys()), sorted(expected[function].keys())) for variable in actual[function]: self.assertEqual(actual[function][variable].count_vector, expected[function][variable]) def test_counting(self): expected_results = { (6, "test()"): {}, (12, "main(int, char *)"): { # Variables "i": [4, 1], "asd": [1, 0], "t": [4, 1], "args": [2, 0], # Globals "g": [3, 1], # Functions "smile": [1, 1], "printf": [1, 1], # Constants "#5": [1, 0], '#"i is %d"': [1, 1]}} self.uut = ClangCountVectorCreator([no_condition, is_call_argument]) cv_dict = self.uut.get_vectors_for_file(self.testfile) self.check_cv_dict(cv_dict, expected_results)