def test_local_binding(): fl = fluid() assert not boundp(fl) with fluids((fl, 2)): assert boundp(fl) assert fl() == 2 with fluids((fl, 3)): assert fl() == 3 assert fl() == 2 @raises(Unbound) def unbound(): return fl() unbound()
def test_unhashable(): @memoizable def f(a): return a with fluids((memos, Memos())): f([])
def test_global_utf_binding(): assert utf() == 1 utf(2) assert utf() == 2 with fluids((utf, 3)): assert utf() == 3 utf(4) assert utf() == 4 assert utf() == 2
def test_fib70_memoized(): prev_handler = signal(SIGALRM, timeout) try: with fluids((memos, Memos())): alarm(120) # this is absurdly too long assert fib(70) == f70 finally: alarm(0) signal(SIGALRM, prev_handler)
def test_key_extraction(): @memoizable(key=lambda a: a[0]) def silly_fact(a): return 1 if a[0] == 1 else a[0] * silly_fact([a[0] - 1]) fact10 = 3628800 assert silly_fact([10]) == fact10 with fluids((memos, Memos())): assert silly_fact([10]) == fact10
def test_simple_memoized_spread(): @memoizable(spread=True) def addup(x, y): return x + y assert addup(1, 2) == 3 assert addup(2, 2) == 4 with fluids((memos, Memos())): assert addup(1, 2) == 3 assert addup(2, 3) == 5
def test_thr_binding(): def set_tf_3(): assert tf() == 1 tf(3) assert tf() == 3 set_tf_3() assert tf() == 3 with fluids((tf, 12)): thr = Thread(target=set_tf_3) thr.start() thr.join() assert tf() == 12 assert tf() == 3
def test_fib30_memoized(): with fluids((memos, Memos())): assert fib(30) == f30
def test_fiblike30_memoized(): with fluids((memos, Memos())): assert fiblike(30, 1, 1) == f30