-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_basics.py
61 lines (42 loc) · 1.36 KB
/
test_basics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import endless
endless.MAX_DEPTH = 100000
def test_factorial():
@endless.make
def fac(n):
return 1 if n == 1 else n * (yield {'n': n-1})
assert fac(n=10) == 3628800
assert fac(10000) == fac(n=9999) * 10000
def test_palindrome():
# very inefficient recursive algorithm
@endless.make
def is_palindrome(word):
return (
len(word) in [0, 1] or word[0] == word[-1] and
(yield {'word': word[1:-1]})
)
assert not is_palindrome(word='some arbitary text')
assert is_palindrome('abcddcba')
# https://en.wikipedia.org/wiki/McCarthy_91_function
def test_maccarthy():
@endless.make
def maccarthy(value):
if value > 100:
return value - 10
else:
return (yield {'value': (yield {'value': value+11})})
for value in range(-100, 100):
assert maccarthy(value=value) == 91
assert maccarthy(-1000000) == 91
# https://en.wikipedia.org/wiki/Ackermann_function
def test_ackermann():
@endless.make
def ackermann(m, n):
if m == 0:
return n + 1
elif m > 0 and n == 0:
return (yield {'m': m - 1, 'n': 1})
elif m > 0 and n > 0:
return (yield {'m': m - 1, 'n': (yield {'m': m, 'n': n-1})})
assert ackermann(n=1, m=1) == 3
assert ackermann(2, 2) == 7
assert ackermann(3, n=3) == 61