-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
422 lines (352 loc) · 12 KB
/
test.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import pathlib
import decode as d
import ascii_helpers
from ast_eval import Evaluator
def test_ascii():
assert ascii_helpers.ascii_to_index("!") == [0]
assert ascii_helpers.ascii_to_index("/") == [14]
# pylint: disable=singleton-comparison
def test_bool():
tok = d.parse_token("T")
assert isinstance(tok, d.ValueToken)
assert tok.value == True # noqa
tok = d.parse_token("F")
assert isinstance(tok, d.ValueToken)
assert tok.value == False # noqa
def test_int():
tok = d.parse_token("I/6")
assert isinstance(tok, d.ValueToken)
assert tok.value == 1337
def test_string():
tok = d.parse_token("SB%,,/}Q/2,$_")
assert isinstance(tok, d.ValueToken)
assert tok.value == "Hello World!"
def test_unary():
for op in d.UnaryOperator:
tok = d.parse_token(f"U{op.value}")
assert isinstance(tok, d.UnaryOperatorToken)
assert tok.operator == op
def test_binary():
for op in d.BinaryOperator:
tok = d.parse_token(f"B{op.value}")
assert isinstance(tok, d.BinaryOperatorToken)
assert tok.operator == op
def test_lambda():
tok = d.parse_token("L/6")
assert isinstance(tok, d.LambdaToken)
assert tok.variable == 1337
def test_variable():
tok = d.parse_token("v/6")
assert isinstance(tok, d.VariableToken)
assert tok.variable == 1337
def test_intro_msg():
msg_str = r"""SB%,,/}!.$}7%,#/-%}4/}4(%}M#(//,}/&}4(%}</5.$}P!2)!",%_~~<%&/2%}4!+).'}!}#/523%j}7%}35''%34}4(!4}9/5}(!6%}!},//+}!2/5.$l}S/5e2%}./7},//+).'}!4}4(%}u).$%8wl}N/}02!#4)#%}9/52}#/--5.)#!4)/.}3+),,3j}9/5}#!.}53%}/52}u%#(/w}3%26)#%l}@524(%2-/2%j}4/}+./7}(/7}9/5}!.$}/4(%2}345$%.43}!2%}$/).'j}9/5}#!.},//+}!4}4(%}u3#/2%"/!2$wl~~;&4%2},//+).'}!2/5.$j}9/5}-!9}"%}!$-)44%$}4/}9/52}&)234}#/523%3j}3/}-!+%}352%}4/}#(%#+}4()3}0!'%}&2/-}4)-%}4/}4)-%l}C.}4(%}-%!.4)-%j})&}9/5}7!.4}4/}02!#4)#%}-/2%}!$6!.#%$}#/--5.)#!4)/.}3+),,3j}9/5}-!9}!,3/}4!+%}/52}u,!.'5!'%y4%34wl~"""
tok = d.parse_token(msg_str)
assert isinstance(tok, d.ValueToken)
assert (
tok.value
== "Hello and welcome to the School of the Bound Variable!\n\nBefore taking a course, we suggest that you have a look around. You're now looking at the [index]. To practice your communication skills, you can use our [echo] service. Furthermore, to know how you and other students are doing, you can look at the [scoreboard].\n\nAfter looking around, you may be admitted to your first courses, so make sure to check this page from time to time. In the meantime, if you want to practice more advanced communication skills, you may also take our [language_test].\n" # noqa
)
def test_encode_decode():
msg = "Hello and welcome to the School of the Bound Variable!\n\nBefore taking a course, we suggest that you have a look around. You're now looking at the [index]. To practice your communication skills, you can use our [echo] service. Furthermore, to know how you and other students are doing, you can look at the [scoreboard].\n\nAfter looking around, you may be admitted to your first courses, so make sure to check this page from time to time. In the meantime, if you want to practice more advanced communication skills, you may also take our [language_test].\n"
encoded = "S" + ascii_helpers.encode_string(msg)
tok = d.parse_token(encoded)
assert isinstance(tok, d.ValueToken)
assert tok.value == msg
def test_eval():
assert Evaluator().eval_tokens([d.ValueToken(value=42)]) == 42
assert Evaluator().eval_tokens([d.ValueToken(value="Hello")]) == "Hello"
assert not Evaluator().eval_tokens(
[
d.UnaryOperatorToken(operator=d.UnaryOperator.NOT),
d.ValueToken(value=True),
]
)
assert (
Evaluator().eval_tokens(
[
d.UnaryOperatorToken(operator=d.UnaryOperator.NEGATE),
d.ValueToken(value=42),
]
)
== -42
)
def test_eval_binary():
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.ADD),
d.ValueToken(value=1),
d.ValueToken(value=2),
]
)
== 3
)
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.SUBTRACT),
d.ValueToken(value=1),
d.ValueToken(value=2),
]
)
== -1
)
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.MULTIPLY),
d.ValueToken(value=2),
d.ValueToken(value=3),
]
)
== 6
)
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.DIVIDE),
d.ValueToken(value=6),
d.ValueToken(value=2),
]
)
== 3
)
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.MODULO),
d.ValueToken(value=5),
d.ValueToken(value=2),
]
)
== 1
)
assert Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.EQUAL),
d.ValueToken(value=5),
d.ValueToken(value=5),
]
)
assert Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.LESS_THAN),
d.ValueToken(value=5),
d.ValueToken(value=6),
]
)
assert Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.GREATER_THAN),
d.ValueToken(value=6),
d.ValueToken(value=5),
]
)
assert Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.AND),
d.ValueToken(value=True),
d.ValueToken(value=True),
]
)
def test_eval_int_to_string():
assert (
Evaluator().eval_tokens(
[
d.UnaryOperatorToken(operator=d.UnaryOperator.STRING_TO_INT),
d.parse_token("S/6"),
]
)
== 1337
)
def test_eval_string_to_int():
assert (
Evaluator().eval_tokens(
[
d.UnaryOperatorToken(operator=d.UnaryOperator.INT_TO_STRING),
d.parse_token("IB%,,/}Q/2,$_"),
]
)
== "Hello World!"
)
def test_if():
assert (
Evaluator().eval_tokens(
[
d.IfToken(),
d.ValueToken(value=True),
d.ValueToken(value=42),
d.ValueToken(value=1337),
]
)
== 42
)
assert (
Evaluator().eval_tokens(
[
d.IfToken(),
d.ValueToken(value=False),
d.ValueToken(value=42),
d.ValueToken(value=1337),
]
)
== 1337
)
def test_if_with_eval():
assert (
Evaluator().eval_tokens(
[
d.IfToken(),
d.ValueToken(value=True),
d.BinaryOperatorToken(operator=d.BinaryOperator.ADD),
d.ValueToken(value=1),
d.ValueToken(value=2),
d.BinaryOperatorToken(operator=d.BinaryOperator.SUBTRACT),
d.ValueToken(value=1),
d.ValueToken(value=2),
]
)
== 3
)
def test_if_with_variables():
assert (
Evaluator(
context={
1: 1,
2: 2,
}
).eval_tokens(
[
d.IfToken(),
d.ValueToken(value=True),
d.VariableToken(variable=1),
d.VariableToken(variable=2),
]
)
== 1
)
assert (
Evaluator(
context={
1: 1,
2: 2,
}
).eval_tokens(
[
d.IfToken(),
d.ValueToken(value=False),
d.VariableToken(variable=1),
d.VariableToken(variable=2),
]
)
== 2
)
def test_eval_lambda():
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=1),
d.BinaryOperatorToken(operator=d.BinaryOperator.ADD),
d.VariableToken(variable=1),
d.ValueToken(value=2),
d.ValueToken(value=3),
]
)
== 5
)
def test_eval_nested_lambda():
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=1),
d.LambdaToken(variable=2),
d.BinaryOperatorToken(operator=d.BinaryOperator.ADD),
d.VariableToken(variable=1),
d.VariableToken(variable=2),
d.ValueToken(value=3),
d.ValueToken(value=4),
]
)
== 7
)
def test_comst_lambda():
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=1),
d.ValueToken(value=1),
d.ValueToken(value=1337),
]
)
== 1
)
def test_higher_order():
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=2),
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.VariableToken(variable=2),
d.ValueToken(value=1337),
d.LambdaToken(variable=1),
d.ValueToken(value=1),
d.ValueToken(value=1337),
]
)
== 1
)
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=2),
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.VariableToken(variable=2),
d.ValueToken(value=1337),
d.LambdaToken(variable=1),
d.ValueToken(value=1),
d.ValueToken(value=1337),
]
)
== 1
)
def test_variable_shadowing():
assert (
Evaluator().eval_tokens(
[
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=1),
d.BinaryOperatorToken(operator=d.BinaryOperator.ADD),
d.VariableToken(variable=1),
d.BinaryOperatorToken(operator=d.BinaryOperator.APPLY),
d.LambdaToken(variable=1),
d.BinaryOperatorToken(operator=d.BinaryOperator.ADD),
d.VariableToken(variable=1),
d.ValueToken(value=1337),
d.ValueToken(value=1),
d.ValueToken(value=7),
]
)
== 1345
)
def test_eval_example():
tokens = 'B$ L# B$ L" B+ v" v" B* I$ I# v8'.split()
tokens = [d.parse_token(tok) for tok in tokens]
expect = d.parse_token("I-")
assert isinstance(expect, d.ValueToken)
assert Evaluator().eval_tokens(tokens) == expect.value
def test_eval_lambda_example():
tokens = "B$ B$ B$ B$ L$ L$ L$ L# v$ I\" I# I$ I%".split()
tokens = [d.parse_token(tok) for tok in tokens]
assert Evaluator().eval_tokens(tokens) == 3
def test_language_test():
content = pathlib.Path("language_test.txt").read_text(encoding="utf-8")
tokens = [d.parse_token(tok) for tok in content.split()]
res = Evaluator().eval_tokens(tokens)
assert res == "Self-check OK, send `solve language_test 4w3s0m3` to claim points for it"