|
1
|
|
|
import os |
|
2
|
|
|
import sys |
|
3
|
|
|
import unittest |
|
4
|
|
|
|
|
5
|
|
|
sys.path.insert(0, ".") |
|
6
|
|
|
from bears.tests.c_languages import skip_if_no_clang |
|
7
|
|
|
from bears.c_languages.codeclone_detection.ClangCountVectorCreator import ( |
|
8
|
|
|
ClangCountVectorCreator) |
|
9
|
|
|
from clang.cindex import CursorKind |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def no_condition(stack): |
|
13
|
|
|
return True |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def is_call_argument(stack): |
|
17
|
|
|
for elem, child_num in stack: |
|
18
|
|
|
if elem.kind == CursorKind.CALL_EXPR: |
|
19
|
|
|
return True |
|
20
|
|
|
|
|
21
|
|
|
return False |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
@skip_if_no_clang() |
|
25
|
|
|
class ClangCountVectorCreatorTest(unittest.TestCase): |
|
26
|
|
|
functions = sorted(["main(int, char *)", "test()"]) |
|
27
|
|
|
|
|
28
|
|
|
def setUp(self): |
|
29
|
|
|
self.testfile = os.path.abspath(os.path.join( |
|
30
|
|
|
os.path.dirname(__file__), |
|
31
|
|
|
"sample.c")) |
|
32
|
|
|
|
|
33
|
|
|
def test_empty_counting(self): |
|
34
|
|
|
expected_results = { |
|
35
|
|
|
(6, "test()"): {}, |
|
36
|
|
|
(12, "main(int, char *)"): { |
|
37
|
|
|
# Variables |
|
38
|
|
|
"i": [], |
|
39
|
|
|
"asd": [], |
|
40
|
|
|
"t": [], |
|
41
|
|
|
"args": [], |
|
42
|
|
|
# Globals |
|
43
|
|
|
"g": [], |
|
44
|
|
|
# Functions |
|
45
|
|
|
"smile": [], |
|
46
|
|
|
"printf": [], |
|
47
|
|
|
# Constants |
|
48
|
|
|
"#5": [], |
|
49
|
|
|
'#"i is %d"': []}} |
|
50
|
|
|
|
|
51
|
|
|
self.uut = ClangCountVectorCreator() |
|
52
|
|
|
cv_dict = self.uut.get_vectors_for_file(self.testfile) |
|
53
|
|
|
|
|
54
|
|
|
self.check_cv_dict(cv_dict, expected_results) |
|
55
|
|
|
|
|
56
|
|
|
def check_cv_dict(self, actual, expected): |
|
57
|
|
|
self.assertEqual(len(actual), len(expected), str(actual)) |
|
58
|
|
|
self.assertEqual(sorted(actual.keys()), sorted(expected.keys())) |
|
59
|
|
|
|
|
60
|
|
|
for function in actual: |
|
61
|
|
|
self.assertEqual(len(actual[function]), len(expected[function])) |
|
62
|
|
|
self.assertEqual(sorted(actual[function].keys()), |
|
63
|
|
|
sorted(expected[function].keys())) |
|
64
|
|
|
for variable in actual[function]: |
|
65
|
|
|
self.assertEqual(actual[function][variable].count_vector, |
|
66
|
|
|
expected[function][variable]) |
|
67
|
|
|
|
|
68
|
|
|
def test_counting(self): |
|
69
|
|
|
expected_results = { |
|
70
|
|
|
(6, "test()"): {}, |
|
71
|
|
|
(12, "main(int, char *)"): { |
|
72
|
|
|
# Variables |
|
73
|
|
|
"i": [4, 1], |
|
74
|
|
|
"asd": [1, 0], |
|
75
|
|
|
"t": [4, 1], |
|
76
|
|
|
"args": [2, 0], |
|
77
|
|
|
# Globals |
|
78
|
|
|
"g": [3, 1], |
|
79
|
|
|
# Functions |
|
80
|
|
|
"smile": [1, 1], |
|
81
|
|
|
"printf": [1, 1], |
|
82
|
|
|
# Constants |
|
83
|
|
|
"#5": [1, 0], |
|
84
|
|
|
'#"i is %d"': [1, 1]}} |
|
85
|
|
|
|
|
86
|
|
|
self.uut = ClangCountVectorCreator([no_condition, is_call_argument]) |
|
87
|
|
|
cv_dict = self.uut.get_vectors_for_file(self.testfile) |
|
88
|
|
|
|
|
89
|
|
|
self.check_cv_dict(cv_dict, expected_results) |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
if __name__ == '__main__': |
|
93
|
|
|
unittest.main(verbosity=2) |
|
94
|
|
|
|