Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to load ".set" files, which aggregate a set of coverage files #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions cutterdrcov_plugin/drcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,65 @@ def dead_module_elimination(modules, bbs):
for i in delete:
del bbs[i]
del modules[i]


def intersect_bbs(bbs):
result = {}
first_dict = bbs[0]
other_dicts = bbs[1:]
for bb, size in first_dict.items():
if all(bb in d for d in other_dicts):
result[bb] = size
return result


def union_bbs(bbs):
result = {}
for d in bbs:
result.update(d)
return result


def subtract_bbs(bbs):
result = {}
first_dict = bbs[0]
other_dicts = bbs[1:]
for bb, size in first_dict.items():
if all(bb not in d for d in other_dicts):
result[bb] = size
return result


def process_set_of_files(path):
supported_operations = {
'intersect': intersect_bbs,
'subtract': subtract_bbs,
'union': union_bbs
}
with open(path, "r") as f:
all_lines = [l.rstrip(' \t\n\r') for l in f.readlines()]
lines = [l for l in all_lines if len(l)]
op = supported_operations.get(lines[0])
if not op:
raise Exception('Unsupported operation')
lines = lines[1:]
if len(lines) == 1:
return load(lines[0])
files = [load(l) for l in lines]
mod_list = files[0][0]
mod_names = [ m['name'] for m in mod_list ]
if any([m['name'] for m in f[0]] != mod_names for f in files[1:]):
raise Exception('Module lists differ among coverage files')
bbs = []
for m in range(len(mod_list)):
dicts = [f[1][m] for f in files]
bbs.append(op(dicts))
return [mod_list, bbs]


def load(path):
if path.endswith(".set"):
return process_set_of_files(path)
drcov_file = open(path, "rb")
modules = read_module_list(drcov_file)
bbs = read_bb_list(drcov_file, len(modules))
Expand Down
28 changes: 28 additions & 0 deletions unittest/test_set_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest
import sys
sys.path.append(".")
from cutterdrcov_plugin import drcov

A = { 1:1, 2:2, 3:3 }
B = { 2:2, 4:4 }
C = { 1:1, 3:3, 5:5 }

class TestSetOperations(unittest.TestCase):

def test_union(self):
self.assertEqual(drcov.union_bbs([A]), A)
self.assertEqual(drcov.union_bbs([A, B]), {1:1, 2:2, 3:3, 4:4})
self.assertEqual(drcov.union_bbs([A, B, C]), {1:1, 2:2, 3:3, 4:4, 5:5})

def test_subtract(self):
self.assertEqual(drcov.subtract_bbs([A]), {1:1, 2:2, 3:3})
self.assertEqual(drcov.subtract_bbs([A, B]), {1:1, 3:3})
self.assertEqual(drcov.subtract_bbs([A, B, C]), {})

def test_intersect(self):
self.assertEqual(drcov.intersect_bbs([A]), A)
self.assertEqual(drcov.intersect_bbs([A, B]), {2:2})
self.assertEqual(drcov.intersect_bbs([A, B, C]), {})

if __name__ == '__main__':
unittest.main()