Skip to content

Commit

Permalink
codeclone_detection: Add CountVector class
Browse files Browse the repository at this point in the history
This class will be used later to count occurrences of variables under
certain conditions.
  • Loading branch information
sils committed Apr 18, 2015
1 parent 1a16329 commit 3bf0e23
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bears/codeclone_detection/CountVector.py
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)
48 changes: 48 additions & 0 deletions bears/tests/codeclone_detection/CountVectorTest.py
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)

1 comment on commit 3bf0e23

@Udayan12167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

Please sign in to comment.