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)
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()
def test_close_is_always_called(self): @boundary def bailout(): yield try: yield False yield True except GeneratorExit: pass else: raise AssertionError('close() must have been called') fibnone = whenall(whenany(bailout))(fibonacci) self.assertEqual(fibnone(), 1)