-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codeclone_detection: Add CountVector class
This class will be used later to count occurrences of variables under certain conditions.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class CountVector: | ||
def __init__(self, name, conditions=None, weightings=None): | ||
""" | ||
Creates a new count vector. | ||
:param name: The name of the variable in the original code. | ||
:param conditions: The counting conditions as list of function objects, | ||
each shall return true when getting data indicating | ||
that this occurrence should be counted. | ||
:param weightings: Optional factors to weight counting conditions. | ||
Defaults to 1 for all conditions. | ||
""" | ||
self.name = name | ||
self.conditions = conditions if conditions is not None else [] | ||
self.count_vector = [0 for elem in self.conditions] | ||
self.weightings = weightings | ||
if self.weightings is None: | ||
self.weightings = [1 for elem in self.conditions] | ||
|
||
assert len(self.count_vector) is len(self.weightings) | ||
|
||
def count_reference(self, *args, **kwargs): | ||
""" | ||
Counts the reference to the variable under the conditions held in this | ||
object. | ||
Any arguments or kwarguments will be passed to all conditions. | ||
""" | ||
for i in range(len(self.conditions)): | ||
if self.conditions[i](*args, **kwargs): | ||
self.count_vector[i] += self.weightings[i] | ||
|
||
def __str__(self): | ||
return self.__repr__() | ||
|
||
def __repr__(self): | ||
return str(self.count_vector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import sys | ||
|
||
sys.path.insert(0, ".") | ||
import unittest | ||
from bears.codeclone_detection.CountVector import CountVector | ||
|
||
|
||
class CountVectorTest(unittest.TestCase): | ||
def test_simple_creation(self): | ||
uut = CountVector("varname") | ||
self.assertEqual(uut.count_vector, []) | ||
uut = CountVector("varname", []) | ||
self.assertEqual(uut.count_vector, []) | ||
self.assertRaises(AssertionError, | ||
CountVector, | ||
"varname", | ||
[], | ||
[2]) | ||
|
||
def test_counting(self): | ||
uut = CountVector("varname", [lambda cursor, stack: cursor and stack]) | ||
self.assertEqual(uut.count_vector, [0]) | ||
uut.count_reference(True, True) | ||
self.assertEqual(uut.count_vector, [1]) | ||
uut.count_reference(True, False) | ||
self.assertEqual(uut.count_vector, [1]) | ||
|
||
def test_weighting(self): | ||
uut = CountVector("varname", | ||
[lambda cursor, stack: cursor and stack, | ||
lambda cursor, stack: cursor], | ||
[2, 1]) | ||
uut.count_reference(True, True) | ||
self.assertEqual(uut.count_vector, [2, 1]) | ||
uut.count_reference(True, False) | ||
self.assertEqual(uut.count_vector, [2, 2]) | ||
|
||
def test_conversion(self): | ||
uut = CountVector("varname", | ||
[lambda cursor, stack: cursor and stack], | ||
[2]) | ||
uut.count_reference(True, True) | ||
self.assertEqual(repr(uut), str(uut)) | ||
self.assertEqual(repr(uut), "[2]") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main(verbosity=2) |
3bf0e23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack