Example #1
0
    def test_times(self):
        from xoutil.bound import times
        fib8 = times(8)(fibonacci)
        # Fibonacci numbers are yielded:
        # 1 1 2 3 5 8 13 21
        self.assertEquals(fib8(), 21)

        fib8 = times(8)(fibonacci)

        fib8gen = fib8.generate()  # exposed bounded generator
        self.assertEquals(tuple(fib8gen), (1, 1, 2, 3, 5, 8, 13, 21))
Example #2
0
    def test_accumulated(self):
        from xoutil.bound import until
        from xoutil.bound import accumulated, timed, times
        # 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144 = 376
        # ^   ^        ...                                  ^
        # |   |        ...                                  |
        # 1   2   3   4   5   6    7    8    9   10   11    12    13
        # |   |        ...                                  |     |
        # V   V        ...                                  V     V
        # 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144 + 233 = 609
        fib500 = accumulated(500)(fibonacci)
        self.assertEqual(fib500(), 233)
        fib500 = until(accumulate=500)(fibonacci)
        self.assertEqual(fib500(), 233)

        fib500timed = whenall(accumulated(500), timed(0))(fibonacci)
        self.assertEqual(fib500timed(), 233)

        self.assertEqual(
            tuple(fib500.generate()),
            (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233)
        )

        # With .generate()  you may count
        self.assertEqual(len(tuple(fib500.generate())), 13)  # the 13th

        # Since 500 is reached at the 13th fib number, looping up to the 20th
        # number must be bigger.
        fib500at20 = whenall(accumulated(500), times(20))(fibonacci)
        self.assertGreater(fib500at20(), 233)
Example #3
0
    def test_atmost_unnamed(self):
        from xoutil.bound import times

        fib8 = times(8)(fibonacci)
        # Fibonacci numbers are yielded:
        # 1 1 2 3 5 8 13 21
        self.assertEquals(fib8(), 21)
Example #4
0
    def test_whenall_with_invalid(self):
        from xoutil.bound import times

        @boundary
        def invalid():
            yield

        fibinv = whenall(invalid, times(10))(fibonacci)
        with self.assertRaises(RuntimeError):
            fibinv()
Example #5
0
 def test_plain_generator(self):
     from xoutil.bound import times
     fibseq = fibonacci()
     limited = times(5)(fibseq)
     self.assertEqual(limited(), 5)