-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage.py
More file actions
executable file
·121 lines (87 loc) · 2.21 KB
/
coverage.py
File metadata and controls
executable file
·121 lines (87 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
import subprocess
import re
import os
import tempfile
import difflib
branch_re = 'branch *(\d+) taken (\d+)( \(fallthrough\))?'
branches = re.compile(branch_re)
line_taken_re = '^\s*(\d+):\s*(\d+):'
line_taken = re.compile(line_taken_re)
line_not_taken_re = '^\s*\D+:\s*(\d+):'
line_not_taken = re.compile(line_not_taken_re)
linemap = None
def run(testvec):
os.system("rm *.gcda")
os.system("./bin %s > /dev/null 2> /dev/null" % testvec)
def make_map(fn1, fn2):
global linemap
f1 = open(fn1)
l1 = f1.readlines()
f1.close()
f2 = open(fn2)
l2 = f2.readlines()
f2.close()
seq = difflib.SequenceMatcher()
seq.set_seq1(l1)
seq.set_seq2(l2)
blocks = seq.get_matching_blocks()
linemap = {}
blockidx = 0
block = blocks[blockidx]
blockoffset = 0
blockstart = block.a
blocklim = blockstart + block.size
for i in xrange(len(l1)):
if i >= blocklim:
blockoffset = 0
blockidx += 1
block = blocks[blockidx]
blockstart = block.a
blocklim = blockstart + block.size
if i < blockstart:
linemap[i] = block.b - 1
else:
linemap[i] = block.b + blockoffset
blockoffset += 1
def find_lineno(no):
global linemap
if linemap is not None:
return linemap[no]
else:
return no
def coverage(src):
os.system("gcov -bc *.gcda > /dev/null 2> /dev/null")
raw_data = open("%s.gcov" % os.path.basename(src))
ret = {}
for l in raw_data:
taken = line_taken.match(l)
if taken:
times = int(taken.group(1))
lineno = find_lineno(int(taken.group(2)))
if times > 0:
ret[lineno] = True
raw_data.close()
#retvec = tuple(ret[lineno] for lineno in sorted(ret))
ret = tuple(sorted(ret))
print ret
return ret
def compile(src):
os.system("gcc %s -fprofile-arcs -ftest-coverage -O0 -o bin > /dev/null 2> /dev/null" % src)
def all_tests(src, testfile):
ret = set([])
compile(src)
f = open(testfile)
for l in f:
run(l.strip())
ret.add(coverage(src))
#print len(ret)
if __name__ == '__main__':
import sys
src = sys.argv[1]
testfile = sys.argv[2]
if len(sys.argv) > 3:
make_map(sys.argv[1], sys.argv[3])
vecs = all_tests(src, testfile)
#for v in vecs:
# print v