Esempio n. 1
0
class TestNextNum(unittest2.TestCase):

    def setUp(self):
        values = [(-1, 0.01), (0, 0.3), (1, 0.58), (2, 0.1), (3, 0.01)]
        self.random_generator = RandomGen(values)

    def assertListAlmostEqual(self, a, b):
        for a, b in zip(a, b):
            self.assertAlmostEqual(a, b)

    def test_create_cdf_bad_probabilities(self):
        values = [0.2]
        with self.assertRaises(InvalidProbabilityTotalException):
            create_cdf(values)

    def test_create_cdf(self):
        values = [0.01, 0.3, 0.58, 0.1, 0.01]
        self.assertListAlmostEqual(create_cdf(values), [0.01, 0.31, 0.89, 0.99, 1])

    @mock.patch('next_num.next_num.random_point', return_value=0.2)
    def test_next_num(self, _):
        self.assertEqual(0, self.random_generator.next_num())

    @mock.patch('next_num.next_num.random_point', return_value=0.34)
    def test_next_num2(self, _):
        self.assertEqual(1, self.random_generator.next_num())

    @mock.patch('next_num.next_num.random_point', return_value=0.001)
    def test_next_num3(self, _):
        self.assertEqual(-1, self.random_generator.next_num())
Esempio n. 2
0
 def setUp(self):
     values = [(-1, 0.01), (0, 0.3), (1, 0.58), (2, 0.1), (3, 0.01)]
     self.random_generator = RandomGen(values)