-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
207 lines (170 loc) · 7.2 KB
/
__main__.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from pyfuncpatmatch import (
pm,
__,
_eq,
_lt,
_lte,
_gt,
_gte,
_lambda,
_and,
_extract,
_empty,
_or,
pm_raise,
)
if __name__ == "__main__":
import sys
nb_errors = 0
def check_exact(check_name, func_call, args, kwargs, expected):
global nb_errors
try:
actual = func_call(*args, **kwargs)
except Exception as esc:
nb_errors += 1
print(
f"ERR: {check_name}: "
f"[expected]`{expected}` | got exception: {esc}",
file=sys.stderr,
)
return
if actual != expected:
nb_errors += 1
print(
f"ERR: {check_name}: "
f"[expected]`{expected}` | [actual]`{actual}`",
file=sys.stderr,
)
else:
print(f"ERR: {check_name}: OK", file=sys.stderr)
def check_error_exact(check_name, func_call, args, kwargs, expected):
global nb_errors
try:
func_call(*args, **kwargs)
except Exception as esc:
if not isinstance(esc, expected):
nb_errors += 1
print(
f"ERR: {check_name}: "
f"[expected]`{expected}` | [actual]`{esc}`",
file=sys.stderr,
)
else:
print(f"ERR: {check_name}: OK", file=sys.stderr)
else:
nb_errors += 1
print(
f"ERR: {check_name}: "
f"[expected]`{expected}` | actual: not an exception",
file=sys.stderr,
)
def check_raise():
global nb_errors
if nb_errors != 0:
print(f"ERR: {nb_errors}", file=sys.stderr)
sys.exit(1)
else:
print("OK", file=sys.stderr)
sys.exit(0)
# ------------------------------------------------------------------------
@pm(lambda _: 0, _eq(0))
@pm(lambda _: 1, _eq(1))
def fib_rec(n: int):
return fib_rec(n - 1) + fib_rec(n - 2)
check_exact("fib_rec(0)", fib_rec, (0,), {}, 0)
check_exact("fib_rec(1)", fib_rec, (1,), {}, 1)
check_exact("fib_rec(2)", fib_rec, (2,), {}, 1)
check_exact("fib_rec(6)", fib_rec, (6,), {}, 8)
check_exact("fib_rec(n=0)", fib_rec, (), {"n": 0}, 0)
check_exact("fib_rec(n=1)", fib_rec, (), {"n": 1}, 1)
check_exact("fib_rec(n=2)", fib_rec, (), {"n": 2}, 1)
check_exact("fib_rec(n=6)", fib_rec, (), {"n": 6}, 8)
@pm(lambda _: 0, n=_eq(0))
@pm(lambda _: 1, n=_eq(1))
def fib_rec_kwargs(n: int):
return fib_rec(n - 1) + fib_rec(n - 2)
check_exact("fib_rec_kwargs(0)", fib_rec_kwargs, (0,), {}, 0)
check_exact("fib_rec_kwargs(1)", fib_rec_kwargs, (1,), {}, 1)
check_exact("fib_rec_kwargs(2)", fib_rec_kwargs, (2,), {}, 1)
check_exact("fib_rec_kwargs(6)", fib_rec_kwargs, (6,), {}, 8)
check_exact("fib_rec_kwargs(n=0)", fib_rec_kwargs, (), {"n": 0}, 0)
check_exact("fib_rec_kwargs(n=1)", fib_rec_kwargs, (), {"n": 1}, 1)
check_exact("fib_rec_kwargs(n=2)", fib_rec_kwargs, (), {"n": 2}, 1)
check_exact("fib_rec_kwargs(n=6)", fib_rec_kwargs, (), {"n": 6}, 8)
# ------------------------------------------------------------------------
@pm(lambda x: x, __())
def wtf_not_called(_):
return 0
check_exact("wtf_not_called(1)", wtf_not_called, (1,), {}, 1)
check_exact("wtf_not_called('abc')", wtf_not_called, ("abc",), {}, "abc")
# ------------------------------------------------------------------------
@pm(lambda x: f"You are not an adult ({x})", _lt(18))
def is_adult(x):
return f"You are an adult ({x})"
check_exact("is_adult(18)", is_adult, (18,), {}, "You are an adult (18)")
check_exact(
"is_adult(17)", is_adult, (17,), {}, "You are not an adult (17)"
)
check_exact("is_adult(0)", is_adult, (0,), {}, "You are not an adult (0)")
# ------------------------------------------------------------------------
@pm(lambda x: f"You are not a teen ({x})", _gte(18))
@pm(lambda x: f"You are not a teen ({x})", _lte(11))
def is_teen(x):
return f"You are a teen ({x})"
check_exact("is_teen(18)", is_teen, (11,), {}, "You are not a teen (11)")
check_exact("is_teen(17)", is_teen, (18,), {}, "You are not a teen (18)")
check_exact("is_teen(17)", is_teen, (19,), {}, "You are not a teen (19)")
check_exact("is_teen(10)", is_teen, (10,), {}, "You are not a teen (10)")
check_exact("is_teen(15)", is_teen, (15,), {}, "You are a teen (15)")
# ------------------------------------------------------------------------
@pm(lambda x: f"You are not a child ({x})", _gt(11))
def is_child(x):
return f"You are a child ({x})"
check_exact("is_child(10)", is_child, (10,), {}, "You are a child (10)")
check_exact("is_child(11)", is_child, (11,), {}, "You are a child (11)")
check_exact(
"is_child(12)", is_child, (12,), {}, "You are not a child (12)"
)
# ------------------------------------------------------------------------
@pm(
lambda _: "FizzBuzz",
_and(_lambda(lambda x: x % 3 == 0), _lambda(lambda x: x % 5 == 0)),
)
@pm(lambda _: "Fizz", _lambda(lambda x: x % 3 == 0))
@pm(lambda _: "Buzz", _lambda(lambda x: x % 5 == 0))
def fizzbuzz(x):
return f"{x}"
check_exact("fizzbuzz(1)", fizzbuzz, (1,), {}, "1")
check_exact("fizzbuzz(2)", fizzbuzz, (2,), {}, "2")
check_exact("fizzbuzz(3)", fizzbuzz, (3,), {}, "Fizz")
check_exact("fizzbuzz(5)", fizzbuzz, (5,), {}, "Buzz")
check_exact("fizzbuzz(15)", fizzbuzz, (15,), {}, "FizzBuzz")
# ------------------------------------------------------------------------
@pm(lambda _: pm_raise(ValueError("empty iterable")), _empty())
@pm(lambda splited: splited[0], _extract(__(), __()))
def head(x):
raise SyntaxError(f"{x} is not iterable")
check_error_exact("head('')", head, ("",), {}, ValueError)
check_error_exact("head(1)", head, (1,), {}, SyntaxError)
check_exact("head([1])", head, ([1],), {}, 1)
check_exact("head('a')", head, ("a",), {}, "a")
check_exact("head('abc')", head, ("abc",), {}, "a")
# ------------------------------------------------------------------------
@pm(lambda splited: splited[1], _extract(__(), __()))
def tail(x):
raise ValueError(f"{x} is not iterable")
check_error_exact("tail('')", tail, ("",), {}, ValueError)
check_exact("tail([1])", tail, ([1],), {}, [])
check_exact("tail('a')", tail, ("a",), {}, "")
check_exact("tail('abc')", tail, ("abc",), {}, "bc")
# ------------------------------------------------------------------------
@pm(lambda x, z: (x, z), x=__(), z=_or(_eq(2), _eq(5)))
@pm(lambda x, z: (x, z), __(), z=_eq(10))
def select(x, y, z):
return (x, y, z)
check_exact("select(1, 2, 3)", select, (1, 2), {"z": 3}, (1, 2, 3))
check_exact("select(1, 3, 2)", select, (0, 1, 2), {}, (0, 2))
check_exact("select(1, 3, 10)", select, (1, 3, 10), {}, (1, 10))
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
check_raise()