Example #1
0
    def test_add_choice(self):
        """ test that add_choice() works correctly
        """

        # check the cumulative sum while adding in new values
        choices = WeightedChoice()
        choices.add_choice(1, 1)
        self.assertEqual(choices.get_summed_rate(), 1)
        choices.add_choice(2, 5)
        self.assertEqual(choices.get_summed_rate(), 6)
        choices.add_choice(3, 10)
        self.assertEqual(choices.get_summed_rate(), 16)

        # check that it works for unsorted probabilities
        choices = WeightedChoice()
        choices.add_choice(1, 1)
        choices.add_choice(2, 10)
        choices.add_choice(3, 5)
        self.assertEqual(choices.get_summed_rate(), 16)

        # check for very low values, with very high precision (but not
        # necessarily exactly equal)
        choices = WeightedChoice()
        choices.add_choice(1, 5e-9)
        choices.add_choice(2, 1e-8)
        choices.add_choice(3, 1.000000000000005e-10)
        self.assertAlmostEqual(choices.get_summed_rate(),
                               1.51000000000000005e-8,
                               places=23)
Example #2
0
 def test_add_choice(self):
     """ test that add_choice() works correctly
     """
     
     # check the cumulative sum while adding in new values
     choices = WeightedChoice()
     choices.add_choice(1, 1)
     self.assertEqual(choices.get_summed_rate(), 1)
     choices.add_choice(2, 5)
     self.assertEqual(choices.get_summed_rate(), 6)
     choices.add_choice(3, 10)
     self.assertEqual(choices.get_summed_rate(), 16)
     
     # check that it works for unsorted probabilities
     choices = WeightedChoice()
     choices.add_choice(1, 1)
     choices.add_choice(2, 10)
     choices.add_choice(3, 5)
     self.assertEqual(choices.get_summed_rate(), 16)
     
     # check for very low values, with very high precision (but not
     # necessarily exactly equal)
     choices = WeightedChoice()
     choices.add_choice(1, 5e-9)
     choices.add_choice(2, 1e-8)
     choices.add_choice(3, 1.000000000000005e-10)
     self.assertAlmostEqual(choices.get_summed_rate(), 1.51000000000000005e-8, places=23)
Example #3
0
    def test_append(self):
        """ test that append() works correctly
        """

        # construct two objects
        a = WeightedChoice()
        a.add_choice(1, 0.5)

        b = WeightedChoice()
        b.add_choice(2, 1)

        # add one object to the other
        a.append(b)

        # check that the first object has changed correctly, but the other
        # remains unchanged
        self.assertEqual(a.get_summed_rate(), 1.5)
        self.assertEqual(b.get_summed_rate(), 1.0)
 def test_append(self):
     """ test that append() works correctly
     """
     
     # construct two objects
     a = WeightedChoice()
     a.add_choice(1, 0.5)
     
     b = WeightedChoice()
     b.add_choice(2, 1)
     
     # add one object to the other
     a.append(b)
     
     # check that the first object has changed correctly, but the other
     # remains unchanged
     self.assertEqual(a.get_summed_rate(), 1.5)
     self.assertEqual(b.get_summed_rate(), 1.0)
Example #5
0
    def test___init__(self):
        """ check that __init__() initiates the object correctly
        """

        choices = WeightedChoice()

        # check that an object without any possible choices has a cumulative
        # sum of 0, but returns a choice of -1
        self.assertEqual(choices.get_summed_rate(), 0)
        self.assertEqual(choices.choice(), -1)

        # check that the type is set correctly
        self.assertEqual(type(choices), WeightedChoice)
Example #6
0
 def test___init__(self):
     """ check that __init__() initiates the object correctly
     """
     
     choices = WeightedChoice()
     
     # check that an object without any possible choices has a cumulative
     # sum of 0, but returns a choice of -1
     self.assertEqual(choices.get_summed_rate(), 0)
     self.assertEqual(choices.choice(), -1)
     
     # check that the type is set correctly
     self.assertEqual(type(choices), WeightedChoice)