def test_conversions(self):
     uut = CountVector("varname",
                       [lambda cursor, stack: cursor and stack],
                       [2])
     uut.count_reference(True, True)
     self.assertEqual(str(uut), "[2]")
     self.assertEqual(list(uut), [2])
    def check_difference(self,
                         cv1,
                         cv2,
                         expected_difference,
                         diff_function):
        """
        Checks the difference between the given count vectors through a
        given difference function.

        :param cv1:                 List of counts to put in the first CV.
        :param cv2:                 List of counts to put in the second CV.
        :param expected_difference: The expected difference value.
        :param diff_function:       The name of the member function to check
                                    (string).
        """
        # Create empty CountVector objects
        count_vector1 = CountVector("", [lambda: False for i in cv1])
        count_vector2 = CountVector("", [lambda: False for i in cv2])
        # Manually hack in the test values
        count_vector1.count_vector = cv1
        count_vector2.count_vector = cv2

        self.assertEqual(getattr(count_vector1, diff_function)(count_vector2),
                         expected_difference,
                         "Difference value for vectors {} and {} doesnt match"
                         ".".format(cv1, cv2))
        self.assertEqual(getattr(count_vector2, diff_function)(count_vector1),
                         expected_difference,
                         "The difference operation is not symmetric.")
 def test_conversions(self):
     uut = CountVector('varname',
                       conditions=[lambda cursor, stack: cursor and stack],
                       weightings=[2])
     uut.count_reference(True, True)
     self.assertEqual(str(uut), '[2]')
     self.assertEqual(list(uut), [2])
 def test_conversions(self):
     uut = CountVector("varname",
                       conditions=[lambda cursor, stack: cursor and stack],
                       weightings=[2])
     uut.count_reference(True, True)
     self.assertEqual(str(uut), "[2]")
     self.assertEqual(list(uut), [2])
 def test_abs(self):
     uut = CountVector("varname",
                       [lambda x: True, lambda x: x])
     self.assertEqual(abs(uut), 0)
     uut.count_reference(True)
     self.assertEqual(abs(uut), sqrt(2))
     uut.count_reference(False)
     self.assertEqual(abs(uut), sqrt(5))
 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])
     self.assertEqual(uut.unweighted, [1])
Exemple #7
0
 def test_cloning(self):
     uut = CountVector("varname", [lambda cursor, stack: cursor and stack],
                       [2])
     uut.count_reference(True, True)
     clone = uut.create_null_vector("test")
     self.assertEqual(clone.name, "test")
     self.assertEqual(clone.weightings, uut.weightings)
     self.assertEqual(clone.conditions, uut.conditions)
     self.assertEqual(clone.count_vector, [0])
 def test_cloning(self):
     uut = CountVector("varname",
                       [lambda cursor, stack: cursor and stack],
                       [2])
     uut.count_reference(True, True)
     clone = uut.create_null_vector("test")
     self.assertEqual(clone.name, "test")
     self.assertEqual(clone.weightings, uut.weightings)
     self.assertEqual(clone.conditions, uut.conditions)
     self.assertEqual(clone.count_vector, [0])
 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",
                       conditions=[],
                       weightings=[2])
 def test_weighting(self):
     uut = CountVector('varname',
                       conditions=[lambda cursor, stack: cursor and stack,
                                   lambda cursor, stack: cursor],
                       weightings=[2, 1])
     uut.count_reference(True, True)
     self.assertEqual(uut.count_vector, [2, 1])
     self.assertEqual(uut.unweighted, [1, 1])
     uut.count_reference(True, False)
     self.assertEqual(uut.count_vector, [2, 2])
     self.assertEqual(uut.unweighted, [1, 2])
Exemple #11
0
    def check_difference(self, cv1, cv2, expected_difference, diff_function):
        """
        Checks the difference between the given count vectors through a
        given difference function.

        :param cv1:                 List of counts to put in the first CV.
        :param cv2:                 List of counts to put in the second CV.
        :param expected_difference: The expected difference value.
        :param diff_function:       The name of the member function to check
                                    (string).
        """
        # Create empty CountVector objects
        count_vector1 = CountVector("", [lambda: False for i in cv1])
        count_vector2 = CountVector("", [lambda: False for i in cv2])
        # Manually hack in the test values
        count_vector1.count_vector = cv1
        count_vector2.count_vector = cv2

        self.assertEqual(
            getattr(count_vector1,
                    diff_function)(count_vector2), expected_difference,
            "Difference value for vectors {} and {} doesnt match"
            ".".format(cv1, cv2))
        self.assertEqual(
            getattr(count_vector2, diff_function)(count_vector1),
            expected_difference, "The difference operation is not symmetric.")
Exemple #12
0
 def test_abs(self):
     uut = CountVector("varname", [lambda x: True, lambda x: x])
     self.assertEqual(abs(uut), 0)
     uut.count_reference(True)
     self.assertEqual(abs(uut), sqrt(2))
     uut.count_reference(False)
     self.assertEqual(abs(uut), sqrt(5))
Exemple #13
0
 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])
     self.assertEqual(uut.unweighted, [1])
 def test_weighting(self):
     uut = CountVector("varname",
                       conditions=[
                           lambda cursor, stack: cursor and stack,
                           lambda cursor, stack: cursor
                       ],
                       weightings=[2, 1])
     uut.count_reference(True, True)
     self.assertEqual(uut.count_vector, [2, 1])
     self.assertEqual(uut.unweighted, [1, 1])
     uut.count_reference(True, False)
     self.assertEqual(uut.count_vector, [2, 2])
     self.assertEqual(uut.unweighted, [1, 2])
Exemple #15
0
    def count_identifier(self, identifier, category):
        if identifier not in self.count_vectors:
            self.count_vectors[identifier] = CountVector(
                identifier, category, self.conditions, self.weightings)

        self.count_vectors[identifier].count_reference(self.stack)
Exemple #16
0
    def test_len(self):
        uut = CountVector("varname")
        self.assertEqual(len(uut), 0)

        uut = CountVector("varname", [lambda x: x])
        self.assertEqual(len(uut), 1)