def test_timed(self): from xoutil.bound import timed fib10ms = timed(1/100)(fibonacci) # Since the wait time below will be larger than the allowed execution # (10 ms) fib1ms will only be able to yield a single value (notice # that `timed` always allow a cycle.) res = fib10ms(wait=1/10) self.assertEquals(res, 1) # If the time boundary is too low timed will allow not allow a cycle. fib0ms = timed(0)(fibonacci) res = fib0ms() self.assertEquals(res, None)
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)